Skip to content

Commit

Permalink
update shared_account defi examples
Browse files Browse the repository at this point in the history
  • Loading branch information
nkysg committed Jan 22, 2025
1 parent f90d87d commit 5afa35a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 49 deletions.
87 changes: 39 additions & 48 deletions vm/move-examples/defi/sources/locked_coins.move
Original file line number Diff line number Diff line change
Expand Up @@ -265,34 +265,25 @@ module defi::locked_coins {
});
}

#[test_only]
use std::string;
#[test_only]
use starcoin_framework::account;
#[test_only]
use starcoin_framework::coin::BurnCapability;
#[test_only]
use starcoin_framework::starcoin_coin::STC;
use starcoin_framework::starcoin_coin::{Self,STC};
#[test_only]
use starcoin_framework::starcoin_account;

#[test_only]
fun setup(starcoin_framework: &signer, sponsor: &signer): BurnCapability<STC> {
timestamp::set_time_has_started_for_testing(starcoin_framework);

let (burn_cap, freeze_cap, mint_cap) = coin::initialize<STC>(
starcoin_framework,
string::utf8(b"TC"),
string::utf8(b"TC"),
8,
false,
);
let (burn_cap, mint_cap) = starcoin_coin::initialize_for_test(starcoin_framework);
account::create_account_for_test(signer::address_of(sponsor));
coin::register<AptosCoin>(sponsor);
coin::register<STC>(sponsor);
let coins = coin::mint<STC>(2000, &mint_cap);
coin::deposit(signer::address_of(sponsor), coins);
coin::destroy_mint_cap(mint_cap);
coin::destroy_freeze_cap(freeze_cap);

burn_cap
}
Expand All @@ -308,9 +299,9 @@ module defi::locked_coins {
add_locked_coins<STC>(sponsor, recipient_addr, 1000, 1000);
assert!(total_locks<STC>(sponsor_address) == 1, 0);
timestamp::fast_forward_seconds(1000);
claim<AptosCoin>(recipient, sponsor_address);
assert!(total_locks<AptosCoin>(sponsor_address) == 0, 1);
assert!(coin::balance<AptosCoin>(recipient_addr) == 1000, 0);
claim<STC>(recipient, sponsor_address);
assert!(total_locks<STC>(sponsor_address) == 0, 1);
assert!(coin::balance<STC>(recipient_addr) == 1000, 0);
coin::destroy_burn_cap(burn_cap);
}

Expand Down Expand Up @@ -352,17 +343,17 @@ module defi::locked_coins {
let recipient_addr = signer::address_of(recipient);
starcoin_account::create_account(recipient_addr);
let sponsor_address = signer::address_of(sponsor);
initialize_sponsor<AptosCoin>(sponsor, sponsor_address);
add_locked_coins<AptosCoin>(sponsor, recipient_addr, 1000, 1000);
assert!(total_locks<AptosCoin>(sponsor_address) == 1, 0);
assert!(claim_time_secs<AptosCoin>(sponsor_address, recipient_addr) == 1000, 0);
initialize_sponsor<STC>(sponsor, sponsor_address);
add_locked_coins<STC>(sponsor, recipient_addr, 1000, 1000);
assert!(total_locks<STC>(sponsor_address) == 1, 0);
assert!(claim_time_secs<STC>(sponsor_address, recipient_addr) == 1000, 0);
// Extend lockup.
update_lockup<AptosCoin>(sponsor, recipient_addr, 2000);
assert!(claim_time_secs<AptosCoin>(sponsor_address, recipient_addr) == 2000, 1);
update_lockup<STC>(sponsor, recipient_addr, 2000);
assert!(claim_time_secs<STC>(sponsor_address, recipient_addr) == 2000, 1);
// Reduce lockup.
update_lockup<AptosCoin>(sponsor, recipient_addr, 1500);
assert!(claim_time_secs<AptosCoin>(sponsor_address, recipient_addr) == 1500, 2);
assert!(total_locks<AptosCoin>(sponsor_address) == 1, 1);
update_lockup<STC>(sponsor, recipient_addr, 1500);
assert!(claim_time_secs<STC>(sponsor_address, recipient_addr) == 1500, 2);
assert!(total_locks<STC>(sponsor_address) == 1, 1);

coin::destroy_burn_cap(burn_cap);
}
Expand All @@ -377,8 +368,8 @@ module defi::locked_coins {
starcoin_account::create_account(recipient_1_addr);
starcoin_account::create_account(recipient_2_addr);
let sponsor_address = signer::address_of(sponsor);
initialize_sponsor<AptosCoin>(sponsor, sponsor_address);
batch_add_locked_coins<AptosCoin>(
initialize_sponsor<STC>(sponsor, sponsor_address);
batch_add_locked_coins<STC>(
sponsor,
vector[recipient_1_addr, recipient_2_addr],
vector[1000, 1000],
Expand All @@ -387,13 +378,13 @@ module defi::locked_coins {
assert!(claim_time_secs<STC>(sponsor_addr, recipient_1_addr) == 1000, 0);
assert!(claim_time_secs<STC>(sponsor_addr, recipient_2_addr) == 1000, 0);
// Extend lockup.
batch_update_lockup<AptosCoin>(sponsor, vector[recipient_1_addr, recipient_2_addr], 2000);
assert!(claim_time_secs<AptosCoin>(sponsor_addr, recipient_1_addr) == 2000, 1);
assert!(claim_time_secs<AptosCoin>(sponsor_addr, recipient_2_addr) == 2000, 1);
batch_update_lockup<STC>(sponsor, vector[recipient_1_addr, recipient_2_addr], 2000);
assert!(claim_time_secs<STC>(sponsor_addr, recipient_1_addr) == 2000, 1);
assert!(claim_time_secs<STC>(sponsor_addr, recipient_2_addr) == 2000, 1);
// Reduce lockup.
batch_update_lockup<AptosCoin>(sponsor, vector[recipient_1_addr, recipient_2_addr], 1500);
assert!(claim_time_secs<AptosCoin>(sponsor_addr, recipient_1_addr) == 1500, 2);
assert!(claim_time_secs<AptosCoin>(sponsor_addr, recipient_2_addr) == 1500, 2);
batch_update_lockup<STC>(sponsor, vector[recipient_1_addr, recipient_2_addr], 1500);
assert!(claim_time_secs<STC>(sponsor_addr, recipient_1_addr) == 1500, 2);
assert!(claim_time_secs<STC>(sponsor_addr, recipient_2_addr) == 1500, 2);

coin::destroy_burn_cap(burn_cap);
}
Expand All @@ -407,17 +398,17 @@ module defi::locked_coins {
starcoin_account::create_account(withdrawal_addr);
starcoin_account::create_account(recipient_addr);
let sponsor_address = signer::address_of(sponsor);
initialize_sponsor<AptosCoin>(sponsor, withdrawal_addr);
add_locked_coins<AptosCoin>(sponsor, recipient_addr, 1000, 1000);
assert!(total_locks<AptosCoin>(sponsor_address) == 1, 0);
assert!(coin::balance<AptosCoin>(withdrawal_addr) == 0, 0);
cancel_lockup<AptosCoin>(sponsor, recipient_addr);
assert!(total_locks<AptosCoin>(sponsor_address) == 0, 0);
let locks = borrow_global_mut<Locks<AptosCoin>>(sponsor_address);
initialize_sponsor<STC>(sponsor, withdrawal_addr);
add_locked_coins<STC>(sponsor, recipient_addr, 1000, 1000);
assert!(total_locks<STC>(sponsor_address) == 1, 0);
assert!(coin::balance<STC>(withdrawal_addr) == 0, 0);
cancel_lockup<STC>(sponsor, recipient_addr);
assert!(total_locks<STC>(sponsor_address) == 0, 0);
let locks = borrow_global_mut<Locks<STC>>(sponsor_address);
assert!(!table::contains(&locks.locks, recipient_addr), 0);

// Funds from canceled locks should be sent to the withdrawal address.
assert!(coin::balance<AptosCoin>(withdrawal_addr) == 1000, 0);
assert!(coin::balance<STC>(withdrawal_addr) == 1000, 0);

coin::destroy_burn_cap(burn_cap);
}
Expand All @@ -438,19 +429,19 @@ module defi::locked_coins {
starcoin_account::create_account(recipient_2_addr);
starcoin_account::create_account(withdrawal_addr);
let sponsor_address = signer::address_of(sponsor);
initialize_sponsor<AptosCoin>(sponsor, withdrawal_addr);
batch_add_locked_coins<AptosCoin>(
initialize_sponsor<STC>(sponsor, withdrawal_addr);
batch_add_locked_coins<STC>(
sponsor,
vector[recipient_1_addr, recipient_2_addr],
vector[1000, 1000],
1000
);
batch_cancel_lockup<STC>(sponsor, vector[recipient_1_addr, recipient_2_addr]);
let locks = borrow_global_mut<Locks<AptosCoin>>(sponsor_address);
let locks = borrow_global_mut<Locks<STC>>(sponsor_address);
assert!(!table::contains(&locks.locks, recipient_1_addr), 0);
assert!(!table::contains(&locks.locks, recipient_2_addr), 0);
// Funds from canceled locks should be sent to the withdrawal address.
assert!(coin::balance<AptosCoin>(withdrawal_addr) == 2000, 0);
assert!(coin::balance<STC>(withdrawal_addr) == 2000, 0);
coin::destroy_burn_cap(burn_cap);
}

Expand All @@ -468,9 +459,9 @@ module defi::locked_coins {
starcoin_account::create_account(recipient_addr);
starcoin_account::create_account(withdrawal_addr);
let sponsor_address = signer::address_of(sponsor);
initialize_sponsor<AptosCoin>(sponsor, withdrawal_addr);
add_locked_coins<AptosCoin>(sponsor, recipient_addr, 1000, 1000);
update_withdrawal_address<AptosCoin>(sponsor, sponsor_address);
initialize_sponsor<STC>(sponsor, withdrawal_addr);
add_locked_coins<STC>(sponsor, recipient_addr, 1000, 1000);
update_withdrawal_address<STC>(sponsor, sponsor_address);
coin::destroy_burn_cap(burn_cap);
}

Expand All @@ -488,7 +479,7 @@ module defi::locked_coins {
starcoin_account::create_account(withdrawal_addr);
let sponsor_address = signer::address_of(sponsor);
initialize_sponsor<STC>(sponsor, withdrawal_addr);
assert!(withdrawal_address<AptosCoin>(sponsor_address) == withdrawal_addr, 0);
assert!(withdrawal_address<STC>(sponsor_address) == withdrawal_addr, 0);
add_locked_coins<STC>(sponsor, recipient_addr, 1000, 1000);
cancel_lockup<STC>(sponsor, recipient_addr);
update_withdrawal_address<STC>(sponsor, sponsor_address);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ module shared_account::SharedAccount {
use starcoin_framework::starcoin_coin::{Self, STC};
let user_addr1 = signer::address_of(&test_user1);
let user_addr2 = signer::address_of(&test_user2);
let (burn_cap, mint_cap) = STC::initialize_for_test(&core_framework);
let (burn_cap, mint_cap) = starcoin_coin::initialize_for_test(&core_framework);
let resource_addr = set_up(user, test_user1, test_user2);

let shared_account = borrow_global<SharedAccount>(resource_addr);
Expand Down

0 comments on commit 5afa35a

Please sign in to comment.