经过之前的学习,我准备进行实战开发一个简单的dao项目demo,实现一个去中心化自治组织,用于管理共享资金、社区任务、提案和投票等功能,本篇文章分享了初始化模块,权限和社区任务模块。
经过之前的学习,我准备进行实战开发一个简单的dao项目demo,实现一个去中心化自治组织,用于管理共享资金、社区任务、提案和投票等功能,本篇文章分享了初始化模块,权限和社区任务模块。
fun init(witness: DAO, ctx: &mut TxContext) {
//1. create dao token and mint supply
let (treasury_cap,metadata) = coin::create_currency<DAO>(witness,18,b"DAO",b"dao",b"Dao token.",option::none(),ctx);
transfer::public_freeze_object(metadata);
let total_balance = coin::mint_balance<DAO>(&mut treasury_cap, TOTAL_SUPPLY);
//2. move supply to treasury and share treasury
let treasury = Treasury<DAO> {
id: object::new(ctx),
supply: total_balance,
};
transfer::share_object(treasury);
//3.create dao metadata and share metadata
let total_supply = coin::treasury_into_supply<DAO>(treasury_cap);
let dao = Dao{
id: object::new(ctx),
total_members: 1,
total_supply: total_supply,
};
transfer::share_object(dao);
//4. mint Cap to msg.sender
let msg_sender = tx_context::sender(ctx);
let init_core_cap = InitCoreCap{
id: object::new(ctx),
role_address: msg_sender,
};
let core_cap = CoreCap{
id: object::new(ctx),
role_address: msg_sender,
};
let member_cap = MemberCap{
id: object::new(ctx),
role_address: msg_sender,
};
transfer::transfer(member_cap,msg_sender);
transfer::transfer(core_cap, msg_sender);
transfer::transfer(init_core_cap, msg_sender);
}
dao组织初始化有以下几个步骤:
public fun distribute_corecap(init_core_cap:& InitCoreCap, receiver:address, ctx:&mut TxContext){
check_init_corecap_role(init_core_cap,ctx);
let core_cap = CoreCap{
id: object::new(ctx),
role_address: receiver,
};
transfer::transfer(core_cap,receiver);
}
public fun distribute_init_corecap(init_core_cap:& InitCoreCap, receiver:address, ctx:&mut TxContext){
check_init_corecap_role(init_core_cap,ctx);
let init_core_cap = InitCoreCap{
id: object::new(ctx),
role_address: receiver,
};
transfer::transfer(init_core_cap,receiver);
}
初始核心成员可以向任意地址颁发初始核心成员和核心成员的权限凭证。
public fun distribute_membercap(core_cap:& CoreCap, receiver:address, dao:&mut Dao<DAO>, ctx:&mut TxContext){
check_corecap_role(core_cap,ctx);
let total_members =& dao.total_members;
dao.total_members = *total_members + 1;
let member_cap = MemberCap{
id: object::new(ctx),
role_address: receiver,
};
transfer::transfer(member_cap,receiver);
}
核心成员可以颁发普通成员权限凭证,可以发布社区任务,改变提案等级,关闭提案。
成员可以发布提案,获取提案奖励,进行投票,收回票据。
public fun set_community_task(core_cap:& CoreCap, describe:String, reward_amount:u64, ctx:&mut TxContext){
check_corecap_role(core_cap,ctx);
let new_task = CommunityTask{
id: object::new(ctx),
describe:describe,
reward_amount:reward_amount,
distribute_ended: false,
};
transfer::share_object(new_task);
}
public fun distribute_task_rewards(core_cap:& CoreCap, task:& CommunityTask, receiver:address, ctx:&mut TxContext,){
check_corecap_role(core_cap,ctx);
assert!(!task.distribute_ended, ETaskDistributeEnded);
let reward_amount = task.reward_amount;
let reward_cap = TaskRewardCap{
id: object::new(ctx),
reward_amount:reward_amount,
owner:receiver,
};
transfer::transfer(reward_cap,receiver);
}
dao
代币, 交易中奖励凭证将会被解构,防止被二次使用
public fun claim_reward(reward_cap:TaskRewardCap, treasury:&mut Treasury<DAO>, ctx:&mut TxContext){
let TaskRewardCap {id, reward_amount, owner} = reward_cap;
assert!(owner == tx_context::sender(ctx), ENotTaskCapOwner);
object::delete(id);
let reward_coin = take_coin_from_treasury(treasury, reward_amount,ctx);
transfer::public_transfer(reward_coin,owner);
}
核心成员可以关闭任务奖励分发(此项操作不可逆),删除社区任务
public fun close_task(core_cap:&mut CoreCap, task:&mut CommunityTask,ctx:&mut TxContext){
check_corecap_role(core_cap,ctx);
task.distribute_ended = true;
}
public fun delete_task(core_cap:&mut CoreCap, task:CommunityTask,ctx:&mut TxContext){
check_corecap_role(core_cap,ctx);
let CommunityTask{ id,describe,reward_amount,distribute_ended} = task;
object::delete(id);
}
我们已经实现了初始化,成员权限,社区任务模块的功能,接下来,我们将会继续实现剩余部分的设计,通过动手实践学习sui编程。
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!