引言我将前文实现的简单的dao参与了https://dacade.org/communities/sui的挑战,得到了反馈,改进了我代码中出现的不足,让我收获不少。
我将前文实现的简单的dao参与了https://dacade.org/communities/sui的挑战,得到了反馈,改进了我代码中出现的不足,让我收获不少。
首先,反馈修正了我一些代码中的风格问题。填补了未出现的空格,删除了多余的空行。
举例:
修改前
const ETaskDistributeEnded:u64 = 0;
const ENotTaskCapOwner:u64 = 1;
const EProposalClosed:u64 = 2;
const EVoteSelf:u64 = 3;
const EInvailVotes:u64 = 4;
const EProposalCheck:u64 = 5;
const EProposalNotClosed:u64 = 6;
const ERoleCheck:u64 = 7;
const EProposalNotPassed:u64 = 8;
const EAlreadyClaimed:u64 = 9;
const EInsufficientTreasurySupply:u64 = 10;
const TOTAL_SUPPLY:u64 = 100_000_000_000_000_000;
const PROPOSAL_FEE:u64 = 5;
const LEVEL0_REWARD:u64 = 0;
const LEVEL1_REWARD:u64 = 10;
const LEVEL2_REWARD:u64 = 15;
const LEVEL3_REWARD:u64 = 30;
struct DAO has drop{}
struct Dao<phantom T> has key{
修改后
const ETaskDistributeEnded: u64 = 0;
const ENotTaskCapOwner: u64 = 1;
const EProposalClosed: u64 = 2;
const EVoteSelf: u64 = 3;
const EInvailVotes: u64 = 4;
const EProposalCheck: u64 = 5;
const EProposalNotClosed: u64 = 6;
const ERoleCheck: u64 = 7;
const EProposalNotPassed: u64 = 8;
const EAlreadyClaimed: u64 = 9;
const EInsufficientTreasurySupply: u64 = 10;
const MAX_VOTES_ONE_TIME: u64 = 10;
const TOTAL_SUPPLY: u64 = 100_000_000_000_000_000;
const PROPOSAL_FEE: u64 = 5;
const LEVEL0_REWARD: u64 = 0;
const LEVEL1_REWARD: u64 = 10;
const LEVEL2_REWARD: u64 = 15;
const LEVEL3_REWARD: u64 = 30;
struct DAO has drop {}
struct Dao<phantom T> has key {
修改前
let (treasury_cap,metadata) = coin::create_currency<DAO>(witness,18,b"DAO",b"dao",b"Dao token.",option::none(),ctx);
修改后
let (treasury_cap, metadata) = coin::create_currency<DAO>(
witness,
18,
b"DAO",
b"dao",
b"Dao token.",
option::none(),
ctx,
);
修改前
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 set_community_task(
core_cap: &CoreCap,
describe: String,
reward_amount: u64,
ctx: &mut TxContext,
) {
check_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);
}
此类修改还有很多,由于篇幅问题不在一一展示。
良好的代码风格不仅可以提高代码规范性,而且可以提升代码可读性,在今后的编程中我会注意代码风格问题
由于我此前学习solidity,更加习惯使用if/else语句进行条件判断。但是rust和move中有一种更加优雅的模式匹配方式那就是match
修改前:
//2.reward calculation
let level = proposal.level;
let reward_amount:u64;
if(level == 1){
reward_amount = LEVEL1_REWARD;
}
else if(level == 2){
reward_amount = LEVEL2_REWARD;
}
else{
reward_amount = LEVEL3_REWARD;
};
修改后:
let reward_amount: u64 = match proposal.level {
1 => LEVEL1_REWARD,
2 => LEVEL2_REWARD,
3 => LEVEL3_REWARD,
_ => LEVEL0_REWARD,
我实现的dao模块有三个成员凭证,分别是普通成员,核心成员,和初始核心成员凭证,凭证与地址绑定,由于它们的角色验证会经常在函数中被使用,所以我抽象出了三个函数来分别验证这些成员
fun check_init_corecap_role(init_core_cap:& InitCoreCap,ctx: &mut TxContext){
assert!(init_core_cap.role_address== tx_context::sender(ctx), ERoleCheck);
}
fun check_corecap_role(core_cap:& CoreCap,ctx: &mut TxContext){
assert!(core_cap.role_address== tx_context::sender(ctx), ERoleCheck);
}
fun check_membercap_role(member_cap:& MemberCap,ctx: &mut TxContext){
assert!(member_cap.role_address == tx_context::sender(ctx), ERoleCheck);
}
非常感谢反馈者提供的修改建议,让我更加深刻理解到了rust中特征的魅力
trait MyTrait {
fn role_address(&self) -> address;
}
分别为三个成员凭证实现此特征
impl MyTrait for InitCoreCap {
fn role_address(&self) -> address {
self.role_address
}
}
impl MyTrait for CoreCap {
fn role_address(&self) -> address {
self.role_address
}
}
impl MyTrait for MemberCap {
fn role_address(&self) -> address {
self.role_address
}
}
fun check_role(role_cap: &dyn MyTrait, ctx: &mut TxContext) {
assert!(role_cap.role_address() == tx_context::sender(ctx), ERoleCheck);
}
// Check for overflow
assert!(TOTAL_SUPPLY >= balance::value<DAO>(&total_balance), EInsufficientTreasurySupply);
assert!(
balance::value(coin::balance<DAO>(&coin)) >= votes,
EInsufficientTreasurySupply
);
此次反馈的push链接https://github.com/shaflow01/dao-demo-on-sui/pull/1/files,其中的一些改进让我收获不少,我会总结其中可取的地方在今后的编程开发中注意并提高。
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!