引言经过之前的学习,准备进行实战开发一个简单的dao项目,实现一个去中心化自治组织,用于管理共享资金、社区任务、提案和投票等功能,本篇文章分享了功能交互测试。测试接下来我将会对之前完成的功能进行测试,并且模拟交互流程。初始化函数首先实现一个函数,用来调用init函数模拟合约部署时da
经过之前的学习,准备进行实战开发一个简单的dao项目,实现一个去中心化自治组织,用于管理共享资金、社区任务、提案和投票等功能,本篇文章分享了功能交互测试。
接下来 我将会对之前完成的功能进行测试,并且模拟交互流程。
首先实现一个函数,用来调用init函数 模拟合约部署时dao的初始化
//test
#[test_only]
/// Wrapper of module initializer for testing
public fun test_init(ctx: &mut TxContext) {
init(DAO {}, ctx)
}
#[test]
public fun test() {
use sui::test_scenario;
use std::string::{Self};
// Initialize a mock sender address
let addr1 = @0xA;
let addr2 = @0xB;
let addr3 = @0xC;
}
导入需要用的包
设置了add1,addr2,addr3三个地址
其中add1将会成为init core member
addr2与addr3将会是member
设置交易为addr1发出,调用初始化函数,模拟合约部署
// Begins a multi-transaction scenario with addr1 as the sender
let scenario = test_scenario::begin(addr1);
//1. dao deploy
test_init(test_scenario::ctx(&mut scenario));
//2. set community task
{
test_scenario::next_tx(&mut scenario, addr1);
let core_cap = test_scenario::take_from_sender<CoreCap>(& scenario);
set_community_task(&core_cap, string::utf8(b"The first task"), 5, test_scenario::ctx(&mut scenario));
test_scenario::return_to_sender(& scenario, core_cap);
};
模拟addr1调用set_community_task发布社区任务
取出addr1的core_cap,调用之后必须还回
//3. distribute_task_rewards
{
test_scenario::next_tx(&mut scenario, addr1);
let core_cap = test_scenario::take_from_sender<CoreCap>(& scenario);
let community_task: CommunityTask = test_scenario::take_shared<CommunityTask>(& scenario);
distribute_task_rewards(&core_cap, &community_task, addr2, test_scenario::ctx(&mut scenario));
distribute_task_rewards(&core_cap, &community_task, addr3, test_scenario::ctx(&mut scenario));
test_scenario::return_to_sender(& scenario, core_cap);
test_scenario::return_shared(community_task);
};
模拟addr1调用distribute_task_rewards为addr2,addr3发布任务奖励的凭证
同样取出addr1的core_cap,并且还取出了shareobj中的金库,调用之后必须还回
//4. claim reward
{
test_scenario::next_tx(&mut scenario, addr2);
let reward_cap = test_scenario::take_from_sender<TaskRewardCap>(& scenario);
let treasury = test_scenario::take_shared<Treasury<DAO>>(& scenario);
claim_reward(reward_cap, &mut treasury, test_scenario::ctx(&mut scenario));
test_scenario::return_shared(treasury);
};
{
test_scenario::next_tx(&mut scenario, addr3);
let reward_cap = test_scenario::take_from_sender<TaskRewardCap>(& scenario);
let treasury = test_scenario::take_shared<Treasury<DAO>>(& scenario);
claim_reward(reward_cap, &mut treasury, test_scenario::ctx(&mut scenario));
test_scenario::return_shared(treasury);
};
模拟addr2 addr3销毁任务奖励凭证取出奖励
可以看到取出了reward_cap但是没有还回去,说明在claim_reward中reward_cap会被销毁
//5. distribute membercap
{
test_scenario::next_tx(&mut scenario, addr1);
let core_cap = test_scenario::take_from_sender<CoreCap>(& scenario);
let dao = test_scenario::take_shared<Dao<DAO>>(& scenario);
assert!(dao.total_members == 1, 0);
distribute_membercap(&core_cap, addr2 ,&mut dao, test_scenario::ctx(&mut scenario));
distribute_membercap(&core_cap, addr3 ,&mut dao, test_scenario::ctx(&mut scenario));
assert!(dao.total_members == 3, 0);
test_scenario::return_to_sender(& scenario, core_cap);
test_scenario::return_shared(dao);
};
模拟了addr2和addr3贡献后向addr1提出加入dao的申请后 addr1为其颁发成员凭证
{
test_scenario::next_tx(&mut scenario, addr2);
let member_cap = test_scenario::take_from_sender(& scenario);
let coin = test_scenario::take_from_sender(& scenario);
let treasury = test_scenario::take_shared<Treasury<DAO>>(& scenario);
submit_proposal(&member_cap, string::utf8(b"The first community proposal"), string::utf8(b"....."), 1, &mut coin,&mut treasury, test_scenario::ctx(&mut scenario));
coin::destroy_zero<DAO>(coin);
test_scenario::return_to_sender(& scenario, member_cap);
test_scenario::return_shared(treasury);
};
模拟了addr2成为dao成员后开启了提案
{
test_scenario::next_tx(&mut scenario, addr3);
let member_cap = test_scenario::take_from_sender(& scenario);
let coin = test_scenario::take_from_sender<Coin<DAO>>(& scenario);
let treasury = test_scenario::take_shared<Treasury<DAO>>(& scenario);
let proposal = test_scenario::take_shared<Proposal>(& scenario);
assert!(balance::value(coin::balance<DAO>(&coin)) == 5, 0);
assert!(proposal.support == 0, 0);
assert!(proposal.proposer == addr2, 0);
assert!(proposal.lock_balance == 0, 0);
vote(&member_cap,&mut proposal,5, true,&mut coin,&mut treasury, test_scenario::ctx(&mut scenario));
assert!(proposal.support == 5, 0);
assert!(proposal.lock_balance == 5, 0);
coin::destroy_zero<DAO>(coin);
test_scenario::return_to_sender(& scenario, member_cap);
test_scenario::return_shared(treasury);
test_scenario::return_shared(proposal);
};
模拟了addr3为addr2的提案进行投票 投票数为5赞成
///8. Close proposal
{
test_scenario::next_tx(&mut scenario, addr1);
let proposal = test_scenario::take_shared<Proposal>(& scenario);
let core_cap = test_scenario::take_from_sender<CoreCap>(& scenario);
assert!(proposal.is_closed == false, 0);
assert!(proposal.is_passed == false, 0);
close_proposal(&core_cap,&mut proposal, test_scenario::ctx(&mut scenario));
assert!(proposal.is_closed == true, 0);
assert!(proposal.is_passed == true, 0);
test_scenario::return_to_sender(& scenario, core_cap);
test_scenario::return_shared(proposal);
};
在一段时间后,core_member可以停止提案,并计算提案是否通过
//9. claim votes
{
test_scenario::next_tx(&mut scenario, addr3);
let proposal = test_scenario::take_shared<Proposal>(& scenario);
let member_cap = test_scenario::take_from_sender<MemberCap>(& scenario);
let vote_cap = test_scenario::take_from_sender<VoteCap>(& scenario);
let treasury = test_scenario::take_shared<Treasury<DAO>>(& scenario);
claim_proposal_vote(&member_cap,&mut proposal, vote_cap,&mut treasury, test_scenario::ctx(&mut scenario));
test_scenario::return_to_sender(& scenario, member_cap);
test_scenario::return_shared(proposal);
test_scenario::return_shared(treasury);
};
// check claim
{
test_scenario::next_tx(&mut scenario, addr3);
let coin = test_scenario::take_from_sender<Coin<DAO>>(& scenario);
assert!(balance::value(coin::balance<DAO>(&coin)) == 5, 0);
test_scenario::return_to_sender(& scenario, coin);
};
模拟了在交易结束后,获取质押选票的过程
//10. claim proposal rewards
{
test_scenario::next_tx(&mut scenario, addr2);
let proposal = test_scenario::take_shared<Proposal>(& scenario);
let member_cap = test_scenario::take_from_sender<MemberCap>(& scenario);
let treasury = test_scenario::take_shared<Treasury<DAO>>(& scenario);
claim_proposal_reward(&member_cap, &mut proposal, &mut treasury ,test_scenario::ctx(&mut scenario));
test_scenario::return_to_sender(& scenario, member_cap);
test_scenario::return_shared(proposal);
test_scenario::return_shared(treasury);
};
// check claim
{
test_scenario::next_tx(&mut scenario, addr2);
let coin = test_scenario::take_from_sender<Coin<DAO>>(&scenario);
assert!(balance::value(coin::balance<DAO>(&coin)) == 10, 0);
test_scenario::return_to_sender(& scenario, coin);
};
模拟了提案者是否领到dao token
到这里,dao项目差不多结束了,我已经大致完成了我的初步设想,接下来,我会用它来参加一个挑战,将会收到几个反馈,后续我会更改并分享我的所学。
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!