Skip to content

Commit

Permalink
tests: add more assert upon propose transfer and vesting
Browse files Browse the repository at this point in the history
  • Loading branch information
thounyy committed Nov 1, 2024
1 parent 1c5e3c5 commit 12ba504
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 9 deletions.
8 changes: 8 additions & 0 deletions packages/actions/sources/currency.move
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,11 @@ public fun propose_transfer<Config, Outcome, CoinType>(
assert!(has_lock<Config, Outcome, CoinType>(account), ENoLock);
assert!(amounts.length() == recipients.length(), EAmountsRecipentsNotSameLength);

let lock: &CurrencyLock<CoinType> = borrow_lock(account);
assert!(lock.can_mint, EMintDisabled);
let sum = amounts.fold!(0, |sum, amount| sum + amount);
if (lock.max_supply.is_some()) assert!(sum <= *lock.max_supply.borrow(), EMaxSupply);

let mut proposal = account.create_proposal(
auth,
outcome,
Expand Down Expand Up @@ -507,6 +512,9 @@ public fun propose_vesting<Config, Outcome, CoinType>(
ctx: &mut TxContext
) {
assert!(has_lock<Config, Outcome, CoinType>(account), ENoLock);
let lock: &CurrencyLock<CoinType> = borrow_lock(account);
assert!(lock.can_mint, EMintDisabled);
if (lock.max_supply.is_some()) assert!(total_amount <= *lock.max_supply.borrow(), EMaxSupply);

let mut proposal = account.create_proposal(
auth,
Expand Down
104 changes: 104 additions & 0 deletions packages/actions/tests/currency_tests.move
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,56 @@ fun test_error_propose_transfer_not_same_length() {
end(scenario, extensions, account, clock, metadata);
}

#[test, expected_failure(abort_code = currency::EMintDisabled)]
fun test_error_propose_transfer_mint_disabled() {
let (mut scenario, extensions, mut account, clock, cap, metadata) = start();
let auth = multisig::authenticate(&extensions, &account, b"".to_string(), scenario.ctx());
currency::lock_cap(auth, &mut account, cap, option::none());

currency::toggle_can_mint<Multisig, Approvals, CURRENCY_TESTS>(&mut account);

let auth = multisig::authenticate(&extensions, &account, b"".to_string(), scenario.ctx());
let outcome = multisig::new_outcome(&account, scenario.ctx());
currency::propose_transfer<Multisig, Approvals, CURRENCY_TESTS>(
auth,
&mut account,
outcome,
b"dummy".to_string(),
b"".to_string(),
0,
1,
vector[1],
vector[@0x1],
scenario.ctx()
);

end(scenario, extensions, account, clock, metadata);
}

#[test, expected_failure(abort_code = currency::EMaxSupply)]
fun test_error_propose_transfer_mint_too_many() {
let (mut scenario, extensions, mut account, clock, cap, metadata) = start();
let auth = multisig::authenticate(&extensions, &account, b"".to_string(), scenario.ctx());
currency::lock_cap(auth, &mut account, cap, option::some(4));

let auth = multisig::authenticate(&extensions, &account, b"".to_string(), scenario.ctx());
let outcome = multisig::new_outcome(&account, scenario.ctx());
currency::propose_transfer<Multisig, Approvals, CURRENCY_TESTS>(
auth,
&mut account,
outcome,
b"dummy".to_string(),
b"".to_string(),
0,
1,
vector[1, 2, 3],
vector[@0x1, @0x2, @0x3],
scenario.ctx()
);

end(scenario, extensions, account, clock, metadata);
}

#[test, expected_failure(abort_code = currency::ENoLock)]
fun test_error_propose_vesting_no_lock() {
let (mut scenario, extensions, mut account, clock, cap, metadata) = start();
Expand All @@ -1031,6 +1081,60 @@ fun test_error_propose_vesting_no_lock() {
end(scenario, extensions, account, clock, metadata);
}

#[test, expected_failure(abort_code = currency::EMintDisabled)]
fun test_error_propose_vesting_mint_disabled() {
let (mut scenario, extensions, mut account, clock, cap, metadata) = start();
let auth = multisig::authenticate(&extensions, &account, b"".to_string(), scenario.ctx());
currency::lock_cap(auth, &mut account, cap, option::none());

currency::toggle_can_mint<Multisig, Approvals, CURRENCY_TESTS>(&mut account);

let auth = multisig::authenticate(&extensions, &account, b"".to_string(), scenario.ctx());
let outcome = multisig::new_outcome(&account, scenario.ctx());
currency::propose_vesting<Multisig, Approvals, CURRENCY_TESTS>(
auth,
&mut account,
outcome,
b"dummy".to_string(),
b"".to_string(),
0,
1,
5,
1,
2,
@0x1,
scenario.ctx()
);

end(scenario, extensions, account, clock, metadata);
}

#[test, expected_failure(abort_code = currency::EMaxSupply)]
fun test_error_propose_vesting_mint_too_many() {
let (mut scenario, extensions, mut account, clock, cap, metadata) = start();
let auth = multisig::authenticate(&extensions, &account, b"".to_string(), scenario.ctx());
currency::lock_cap(auth, &mut account, cap, option::some(4));

let auth = multisig::authenticate(&extensions, &account, b"".to_string(), scenario.ctx());
let outcome = multisig::new_outcome(&account, scenario.ctx());
currency::propose_vesting<Multisig, Approvals, CURRENCY_TESTS>(
auth,
&mut account,
outcome,
b"dummy".to_string(),
b"".to_string(),
0,
1,
5,
1,
2,
@0x1,
scenario.ctx()
);

end(scenario, extensions, account, clock, metadata);
}

#[test, expected_failure(abort_code = currency::ENoChange)]
fun test_error_disable_nothing() {
let (mut scenario, extensions, mut account, clock, cap, metadata) = start();
Expand Down
18 changes: 9 additions & 9 deletions packages/actions/tests/owned_tests.move
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fun wrong_version(): TypeName {
type_name::get<Extensions>()
}

fun keep_coin(addr: address, amount: u64, scenario: &mut Scenario): ID {
fun send_coin(addr: address, amount: u64, scenario: &mut Scenario): ID {
let coin = coin::mint_for_testing<SUI>(amount, scenario.ctx());
let id = object::id(&coin);
transfer::public_transfer(coin, addr);
Expand Down Expand Up @@ -104,8 +104,8 @@ fun test_propose_execute_transfer() {
let (mut scenario, extensions, mut account, clock) = start();
let key = b"dummy".to_string();

let id1 = keep_coin(account.addr(), 1, &mut scenario);
let id2 = keep_coin(account.addr(), 2, &mut scenario);
let id1 = send_coin(account.addr(), 1, &mut scenario);
let id2 = send_coin(account.addr(), 2, &mut scenario);

let auth = multisig::authenticate(&extensions, &account, b"".to_string(), scenario.ctx());
let outcome = multisig::new_outcome(&account, scenario.ctx());
Expand Down Expand Up @@ -145,7 +145,7 @@ fun test_propose_execute_vesting() {
let (mut scenario, extensions, mut account, clock) = start();
let key = b"dummy".to_string();

let id = keep_coin(account.addr(), 5, &mut scenario);
let id = send_coin(account.addr(), 5, &mut scenario);

let auth = multisig::authenticate(&extensions, &account, b"".to_string(), scenario.ctx());
let outcome = multisig::new_outcome(&account, scenario.ctx());
Expand Down Expand Up @@ -185,7 +185,7 @@ fun test_withdraw_flow() {
let (mut scenario, extensions, mut account, clock) = start();
let key = b"dummy".to_string();

let id = keep_coin(account.addr(), 5, &mut scenario);
let id = send_coin(account.addr(), 5, &mut scenario);

let mut proposal = create_dummy_proposal(&mut scenario, &mut account, &extensions);
owned::new_withdraw(&mut proposal, id, DummyProposal());
Expand Down Expand Up @@ -213,7 +213,7 @@ fun test_withdraw_expired() {
clock.increment_for_testing(1);
let key = b"dummy".to_string();

let id = keep_coin(account.addr(), 5, &mut scenario);
let id = send_coin(account.addr(), 5, &mut scenario);

let mut proposal = create_dummy_proposal(&mut scenario, &mut account, &extensions);
owned::new_withdraw(&mut proposal, id, DummyProposal());
Expand Down Expand Up @@ -256,7 +256,7 @@ fun test_error_do_withdraw_from_wrong_account() {
let mut account2 = multisig::new_account(&extensions, b"Main".to_string(), scenario.ctx());
let key = b"dummy".to_string();

let id = keep_coin(account.addr(), 5, &mut scenario);
let id = send_coin(account.addr(), 5, &mut scenario);

// proposal is submitted to other account
let mut proposal = create_dummy_proposal(&mut scenario, &mut account2, &extensions);
Expand Down Expand Up @@ -285,7 +285,7 @@ fun test_error_do_withdraw_from_wrong_constructor_witness() {
let (mut scenario, extensions, mut account, clock) = start();
let key = b"dummy".to_string();

let id = keep_coin(account.addr(), 5, &mut scenario);
let id = send_coin(account.addr(), 5, &mut scenario);

let mut proposal = create_dummy_proposal(&mut scenario, &mut account, &extensions);
owned::new_withdraw(&mut proposal, id, DummyProposal());
Expand All @@ -312,7 +312,7 @@ fun test_error_do_withdraw_from_not_dep() {
let (mut scenario, extensions, mut account, clock) = start();
let key = b"dummy".to_string();

let id = keep_coin(account.addr(), 5, &mut scenario);
let id = send_coin(account.addr(), 5, &mut scenario);

let mut proposal = create_dummy_proposal(&mut scenario, &mut account, &extensions);
owned::new_withdraw(&mut proposal, id, DummyProposal());
Expand Down

0 comments on commit 12ba504

Please sign in to comment.