Skip to content

Commit

Permalink
feat: check exist before batch handle (#3072)
Browse files Browse the repository at this point in the history
* feat: check exist before batch handle

* feat: check exists befor batch_harvest/batch_harvest_bbn

* fix remove_temp_state of batch_harvest/batch_harvest_bbn issue

* fix wrong check

* add ut for batch_harvest
  • Loading branch information
lispking authored Dec 20, 2024
1 parent 50f6615 commit e81a723
Showing 1 changed file with 71 additions and 25 deletions.
96 changes: 71 additions & 25 deletions apps/grow_bitcoin/sources/grow_bitcoin.move
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -274,16 +274,31 @@ module grow_bitcoin::grow_bitcoin {
signer: &signer,
assets: vector<ObjectID>,
) {
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<UTXO>(signer, asset_id);
stake(signer, asset);
if (!table::contains(&stake_table.stake, asset_id)) {
let asset = object::borrow_mut_object<UTXO>(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<UserStake>(account)) {
account::move_resource_to(signer, UserStake {
stake: table::new()
});
};
account::borrow_resource<UserStake>(account)
}

/// Call by stake user, staking amount of asset in order to get yield farming token
public entry fun stake(
signer: &signer,
Expand All @@ -301,12 +316,15 @@ module grow_bitcoin::grow_bitcoin {
signer: &signer,
assets: vector<ObjectID>,
) {
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<BBNStakeSeal>(signer, asset_id);
stake_bbn(signer, asset);
if (!table::contains(&stake_table.stake, asset_id)) {
let asset = object::borrow_mut_object<BBNStakeSeal>(signer, asset_id);
stake_bbn(signer, asset);
};
i = i + 1;
}
}
Expand Down Expand Up @@ -399,15 +417,20 @@ module grow_bitcoin::grow_bitcoin {
signer: &signer,
assets: vector<ObjectID>,
) {
process_expired_state();
let farming_asset = account::borrow_resource<FarmingAsset>(DEPLOYER);

let len = vector::length(&assets);
let i = 0;
let total_coin = coin::zero<GROW>();
while (i < len) {
let asset_id = *vector::borrow(&assets, i);
let asset = object::borrow_mut_object<UTXO>(signer, asset_id);
let coin = do_unstake(signer, object::id(asset));
utxo::remove_temp_state<StakeInfo>(asset);
coin::merge(&mut total_coin, coin);
if (table::contains(&farming_asset.stake_table, asset_id)) {
let asset = object::borrow_mut_object<UTXO>(signer, asset_id);
let coin = do_unstake(signer, object::id(asset));
utxo::remove_temp_state<StakeInfo>(asset);
coin::merge(&mut total_coin, coin);
};
i = i + 1;
};
account_coin_store::deposit(signer::address_of(signer), total_coin);
Expand All @@ -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<ObjectID>) {
process_expired_state();
let farming_asset = account::borrow_resource<FarmingAsset>(DEPLOYER);

let len = vector::length(&assets);
let i = 0;
let total_coin = coin::zero<GROW>();
while (i < len) {
let asset_id = *vector::borrow(&assets, i);
let asset = object::borrow_mut_object<BBNStakeSeal>(signer, asset_id);
let coin = do_unstake(signer, object::id(asset));
bbn::remove_temp_state<StakeInfo>(asset);
coin::merge(&mut total_coin, coin);
if (table::contains(&farming_asset.stake_table, asset_id)) {
let asset = object::borrow_mut_object<BBNStakeSeal>(signer, asset_id);
let coin = do_unstake(signer, object::id(asset));
bbn::remove_temp_state<StakeInfo>(asset);
coin::merge(&mut total_coin, coin);
};
i = i + 1;
};
account_coin_store::deposit(signer::address_of(signer), total_coin);
Expand Down Expand Up @@ -483,15 +511,20 @@ module grow_bitcoin::grow_bitcoin {

/// Harvest yield farming token from stake
public entry fun batch_harvest(signer:&signer, assets: vector<ObjectID>) {
process_expired_state();
let account = signer::address_of(signer);
let stake_table = account::borrow_resource<UserStake>(account);

let len = vector::length(&assets);
let i = 0;
let total_coin = coin::zero<GROW>();
while (i < len) {
let asset_id = *vector::borrow(&assets, i);
let utxo_obj = object::borrow_mut_object<UTXO>(signer, asset_id);
let coin = do_harvest(signer, object::id(utxo_obj));
utxo::remove_temp_state<StakeInfo>(utxo_obj);
coin::merge(&mut total_coin, coin);
if (table::contains(&stake_table.stake, asset_id)) {
let utxo_obj = object::borrow_mut_object<UTXO>(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);
Expand All @@ -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<ObjectID>) {
process_expired_state();
let account = signer::address_of(signer);
let stake_table = account::borrow_resource<UserStake>(account);

let len = vector::length(&assets);
let i = 0;
let total_coin = coin::zero<GROW>();
while (i < len) {
let asset_id = *vector::borrow(&assets, i);
let bbn_obj = object::borrow_mut_object<BBNStakeSeal>(signer, asset_id);
let coin = do_harvest(signer, object::id(bbn_obj));
bbn::remove_temp_state<StakeInfo>(bbn_obj);
coin::merge(&mut total_coin, coin);
if (table::contains(&stake_table.stake, asset_id)) {
let bbn_obj = object::borrow_mut_object<BBNStakeSeal>(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);
Expand Down Expand Up @@ -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<UTXO>(asset_id) || object::exists_object_with_type<BBNStakeSeal>(asset_id)){
Expand Down Expand Up @@ -858,10 +896,18 @@ module grow_bitcoin::grow_bitcoin {
assert!(amount == 2025450, 1);
assert!(amount2 == 675150, 2);

let amount = account_coin_store::balance<GROW>(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<GROW>(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);
}
}
}

0 comments on commit e81a723

Please sign in to comment.