diff --git a/apps/grow_bitcoin/sources/grow_bitcoin.move b/apps/grow_bitcoin/sources/grow_bitcoin.move index c8f415d31c..13d6de6a55 100644 --- a/apps/grow_bitcoin/sources/grow_bitcoin.move +++ b/apps/grow_bitcoin/sources/grow_bitcoin.move @@ -26,7 +26,7 @@ module grow_bitcoin::grow_bitcoin { use rooch_framework::account_coin_store; use rooch_framework::coin; use rooch_framework::coin::{CoinInfo, Coin}; - + use bitcoin_move::utxo::{Self, UTXO, value, TempStateDropEvent}; #[test_only] use bitcoin_move::bitcoin::add_latest_block; @@ -274,16 +274,31 @@ module grow_bitcoin::grow_bitcoin { signer: &signer, assets: vector, ) { + let stake_table = obain_stake_table(signer); let len = vector::length(&assets); let i = 0; while (i < len) { let asset_id = *vector::borrow(&assets, i); - let asset = object::borrow_mut_object(signer, asset_id); - stake(signer, asset); + if (!table::contains(&stake_table.stake, asset_id)) { + let asset = object::borrow_mut_object(signer, asset_id); + stake(signer, asset); + }; i = i + 1; } } + fun obain_stake_table(signer: &signer): &UserStake { + process_expired_state(); + let account = signer::address_of(signer); + // Initialize the stake table if it doesn't exist + if (!account::exists_resource(account)) { + account::move_resource_to(signer, UserStake { + stake: table::new() + }); + }; + account::borrow_resource(account) + } + /// Call by stake user, staking amount of asset in order to get yield farming token public entry fun stake( signer: &signer, @@ -301,12 +316,15 @@ module grow_bitcoin::grow_bitcoin { signer: &signer, assets: vector, ) { + let stake_table = obain_stake_table(signer); let len = vector::length(&assets); let i = 0; while (i < len) { let asset_id = *vector::borrow(&assets, i); - let asset = object::borrow_mut_object(signer, asset_id); - stake_bbn(signer, asset); + if (!table::contains(&stake_table.stake, asset_id)) { + let asset = object::borrow_mut_object(signer, asset_id); + stake_bbn(signer, asset); + }; i = i + 1; } } @@ -399,15 +417,20 @@ module grow_bitcoin::grow_bitcoin { signer: &signer, assets: vector, ) { + process_expired_state(); + let farming_asset = account::borrow_resource(DEPLOYER); + let len = vector::length(&assets); let i = 0; let total_coin = coin::zero(); while (i < len) { let asset_id = *vector::borrow(&assets, i); - let asset = object::borrow_mut_object(signer, asset_id); - let coin = do_unstake(signer, object::id(asset)); - utxo::remove_temp_state(asset); - coin::merge(&mut total_coin, coin); + if (table::contains(&farming_asset.stake_table, asset_id)) { + let asset = object::borrow_mut_object(signer, asset_id); + let coin = do_unstake(signer, object::id(asset)); + utxo::remove_temp_state(asset); + coin::merge(&mut total_coin, coin); + }; i = i + 1; }; account_coin_store::deposit(signer::address_of(signer), total_coin); @@ -422,15 +445,20 @@ module grow_bitcoin::grow_bitcoin { /// Supports batch invocation by stake users, allowing multiple assets to be unstaked to receive yield farming tokens public entry fun batch_unstake_bbn(signer: &signer, assets: vector) { + process_expired_state(); + let farming_asset = account::borrow_resource(DEPLOYER); + let len = vector::length(&assets); let i = 0; let total_coin = coin::zero(); while (i < len) { let asset_id = *vector::borrow(&assets, i); - let asset = object::borrow_mut_object(signer, asset_id); - let coin = do_unstake(signer, object::id(asset)); - bbn::remove_temp_state(asset); - coin::merge(&mut total_coin, coin); + if (table::contains(&farming_asset.stake_table, asset_id)) { + let asset = object::borrow_mut_object(signer, asset_id); + let coin = do_unstake(signer, object::id(asset)); + bbn::remove_temp_state(asset); + coin::merge(&mut total_coin, coin); + }; i = i + 1; }; account_coin_store::deposit(signer::address_of(signer), total_coin); @@ -483,15 +511,20 @@ module grow_bitcoin::grow_bitcoin { /// Harvest yield farming token from stake public entry fun batch_harvest(signer:&signer, assets: vector) { + process_expired_state(); + let account = signer::address_of(signer); + let stake_table = account::borrow_resource(account); + let len = vector::length(&assets); let i = 0; let total_coin = coin::zero(); while (i < len) { let asset_id = *vector::borrow(&assets, i); - let utxo_obj = object::borrow_mut_object(signer, asset_id); - let coin = do_harvest(signer, object::id(utxo_obj)); - utxo::remove_temp_state(utxo_obj); - coin::merge(&mut total_coin, coin); + if (table::contains(&stake_table.stake, asset_id)) { + let utxo_obj = object::borrow_mut_object(signer, asset_id); + let coin = do_harvest(signer, object::id(utxo_obj)); + coin::merge(&mut total_coin, coin); + }; i = i + 1; }; account_coin_store::deposit(signer::address_of(signer), total_coin); @@ -505,15 +538,20 @@ module grow_bitcoin::grow_bitcoin { /// Harvest yield farming token from bbn stake public entry fun batch_harvest_bbn(signer:&signer, assets: vector) { + process_expired_state(); + let account = signer::address_of(signer); + let stake_table = account::borrow_resource(account); + let len = vector::length(&assets); let i = 0; let total_coin = coin::zero(); while (i < len) { let asset_id = *vector::borrow(&assets, i); - let bbn_obj = object::borrow_mut_object(signer, asset_id); - let coin = do_harvest(signer, object::id(bbn_obj)); - bbn::remove_temp_state(bbn_obj); - coin::merge(&mut total_coin, coin); + if (table::contains(&stake_table.stake, asset_id)) { + let bbn_obj = object::borrow_mut_object(signer, asset_id); + let coin = do_harvest(signer, object::id(bbn_obj)); + coin::merge(&mut total_coin, coin); + }; i = i + 1; }; account_coin_store::deposit(signer::address_of(signer), total_coin); @@ -754,7 +792,7 @@ module grow_bitcoin::grow_bitcoin { let (height,_hash) = types::unpack_block_height_hash(option::destroy_some(height_hash)); height } - + public entry fun remove_expired_stake(asset_id: ObjectID) { let (is_staked, _) = check_asset_is_staked(asset_id); if (!is_staked || object::exists_object_with_type(asset_id) || object::exists_object_with_type(asset_id)){ @@ -858,10 +896,18 @@ module grow_bitcoin::grow_bitcoin { assert!(amount == 2025450, 1); assert!(amount2 == 675150, 2); + let amount = account_coin_store::balance(owner_addr); + assert!(amount == 0, 3); + + timestamp::fast_forward_seconds_for_test(seconds); + batch_harvest(&sender, vector[utxo_id, utxo_id2]); + let amount = account_coin_store::balance(owner_addr); + assert!(amount == 4050900, 4); + batch_unstake(&sender, vector[utxo_id, utxo_id2]); let amount = query_gov_token_amount(utxo_id); - assert!(amount == 0, 3); + assert!(amount == 0, 5); let amount2 = query_gov_token_amount(utxo_id2); - assert!(amount2 == 0, 4); + assert!(amount2 == 0, 6); } -} \ No newline at end of file +}