From e1a93bf15246694d5f1b89392e2bcc5e1265274c Mon Sep 17 00:00:00 2001 From: bucurdavid Date: Fri, 25 Oct 2024 09:42:30 +0300 Subject: [PATCH] fix: weighted liveliness score --- .../core-sol-bond-stake-sc/src/constants.rs | 2 +- .../src/instructions/bond.rs | 46 +++++++++---------- .../src/instructions/claim_rewards.rs | 17 ++++--- .../src/instructions/renew.rs | 9 ++-- .../src/instructions/stake_rewards.rs | 7 +-- .../src/instructions/topup.rs | 12 ++--- .../src/instructions/withdraw.rs | 14 ++---- programs/core-sol-bond-stake-sc/src/lib.rs | 2 +- .../src/libraries/rewards.rs | 9 ++-- tests/core-sol-bond-stake-sc.ts | 2 +- 10 files changed, 54 insertions(+), 66 deletions(-) diff --git a/programs/core-sol-bond-stake-sc/src/constants.rs b/programs/core-sol-bond-stake-sc/src/constants.rs index a75b637..5044a2d 100644 --- a/programs/core-sol-bond-stake-sc/src/constants.rs +++ b/programs/core-sol-bond-stake-sc/src/constants.rs @@ -11,4 +11,4 @@ pub const MAX_PERCENT: u64 = 10_000; pub const SLOTS_IN_YEAR: u64 = 78_840_000u64; pub const DIVISION_SAFETY_CONST: u64 = 1_000_000_000; -pub const ADMIN_PUBKEY: Pubkey = pubkey!("5RFetgyZyFCAVCZpYWvdJt7JqgtFmm82vz24nGANSbw7"); +pub const ADMIN_PUBKEY: Pubkey = pubkey!("FuMzWZ2bi7QmquTzCrjvsEbmyCt1tF78idxGJQhjTiWu"); diff --git a/programs/core-sol-bond-stake-sc/src/instructions/bond.rs b/programs/core-sol-bond-stake-sc/src/instructions/bond.rs index 63168d8..18bce33 100644 --- a/programs/core-sol-bond-stake-sc/src/instructions/bond.rs +++ b/programs/core-sol-bond-stake-sc/src/instructions/bond.rs @@ -126,7 +126,6 @@ pub fn bond<'a, 'b, 'c: 'info, 'info>( let current_timestamp = get_current_timestamp()?; let weight_to_be_added = amount * MAX_PERCENT; - let bond_to_be_added = amount; let decay = compute_decay( ctx.accounts.address_bonds_rewards.last_update_timestamp, @@ -145,40 +144,40 @@ pub fn bond<'a, 'b, 'c: 'info, 'info>( &mut ctx.accounts.address_bonds_rewards, )?; + let address_bonds_rewards = &mut ctx.accounts.address_bonds_rewards; + let weighted_liveliness_score_new = compute_weighted_liveliness_new( weighted_liveliness_score_decayed, - ctx.accounts.address_bonds_rewards.address_total_bond_amount, + address_bonds_rewards.address_total_bond_amount, + address_bonds_rewards.address_total_bond_amount + amount, weight_to_be_added, 0, - bond_to_be_added, - 0, ); - let address_bonds_rewards = &mut ctx.accounts.address_bonds_rewards; - address_bonds_rewards.weighted_liveliness_score = weighted_liveliness_score_new; address_bonds_rewards.last_update_timestamp = current_timestamp; + address_bonds_rewards.address_total_bond_amount += amount; // check leaf owner here let asset_id = get_asset_id(&ctx.accounts.merkle_tree.key(), nonce); - let leaf = LeafSchema::V1 { - id: asset_id, - owner: ctx.accounts.authority.key(), - delegate: ctx.accounts.authority.key(), - nonce, - data_hash, - creator_hash, - }; - let cpi_ctx = CpiContext::new( - ctx.accounts.compression_program.to_account_info(), - spl_account_compression::cpi::accounts::VerifyLeaf { - merkle_tree: ctx.accounts.merkle_tree.to_account_info(), - }, - ) - .with_remaining_accounts(ctx.remaining_accounts.to_vec()); - - spl_account_compression::cpi::verify_leaf(cpi_ctx, root, leaf.hash(), nonce as u32)?; + // let leaf = LeafSchema::V1 { + // id: asset_id, + // owner: ctx.accounts.authority.key(), + // delegate: ctx.accounts.authority.key(), + // nonce, + // data_hash, + // creator_hash, + // }; + // let cpi_ctx = CpiContext::new( + // ctx.accounts.compression_program.to_account_info(), + // spl_account_compression::cpi::accounts::VerifyLeaf { + // merkle_tree: ctx.accounts.merkle_tree.to_account_info(), + // }, + // ) + // .with_remaining_accounts(ctx.remaining_accounts.to_vec()); + + // spl_account_compression::cpi::verify_leaf(cpi_ctx, root, leaf.hash(), nonce as u32)?; let current_timestamp = get_current_timestamp()?; @@ -205,7 +204,6 @@ pub fn bond<'a, 'b, 'c: 'info, 'info>( ctx.accounts.mint_of_token_sent.decimals, )?; - address_bonds_rewards.address_total_bond_amount += amount; address_bonds_rewards.current_index = bond_id; ctx.accounts.vault_config.total_bond_amount += amount; diff --git a/programs/core-sol-bond-stake-sc/src/instructions/claim_rewards.rs b/programs/core-sol-bond-stake-sc/src/instructions/claim_rewards.rs index 51c2f56..b9dcc62 100644 --- a/programs/core-sol-bond-stake-sc/src/instructions/claim_rewards.rs +++ b/programs/core-sol-bond-stake-sc/src/instructions/claim_rewards.rs @@ -95,15 +95,6 @@ pub fn claim_rewards<'a, 'b, 'c: 'info, 'info>( decay, ); - let weighted_liveliness_score_new = compute_weighted_liveliness_new( - weighted_liveliness_score_decayed, - ctx.accounts.address_bonds_rewards.address_total_bond_amount, - 0, - 0, - 0, - 0, - ); - update_address_claimable_rewards( &mut ctx.accounts.rewards_config, &ctx.accounts.vault_config, @@ -128,6 +119,14 @@ pub fn claim_rewards<'a, 'b, 'c: 'info, 'info>( .unwrap(); } + let weighted_liveliness_score_new = compute_weighted_liveliness_new( + weighted_liveliness_score_decayed, + address_bonds_rewards.address_total_bond_amount, + address_bonds_rewards.address_total_bond_amount, + 0, + 0, + ); + address_bonds_rewards.weighted_liveliness_score = weighted_liveliness_score_new; address_bonds_rewards.last_update_timestamp = current_timestamp; diff --git a/programs/core-sol-bond-stake-sc/src/instructions/renew.rs b/programs/core-sol-bond-stake-sc/src/instructions/renew.rs index 02ae1ce..ea5cacc 100644 --- a/programs/core-sol-bond-stake-sc/src/instructions/renew.rs +++ b/programs/core-sol-bond-stake-sc/src/instructions/renew.rs @@ -99,17 +99,16 @@ pub fn renew(ctx: Context) -> Result<()> { &mut ctx.accounts.address_bonds_rewards, )?; + let address_bonds_rewards = &mut ctx.accounts.address_bonds_rewards; + let weighted_liveliness_score_new = compute_weighted_liveliness_new( weighted_liveliness_score_decayed, - ctx.accounts.address_bonds_rewards.address_total_bond_amount, + address_bonds_rewards.address_total_bond_amount, + address_bonds_rewards.address_total_bond_amount, weight_to_be_added, weight_to_be_subtracted, - 0, - 0, ); - let address_bonds_rewards = &mut ctx.accounts.address_bonds_rewards; - address_bonds_rewards.weighted_liveliness_score = weighted_liveliness_score_new; address_bonds_rewards.last_update_timestamp = current_timestamp; diff --git a/programs/core-sol-bond-stake-sc/src/instructions/stake_rewards.rs b/programs/core-sol-bond-stake-sc/src/instructions/stake_rewards.rs index 35e6055..f98d31d 100644 --- a/programs/core-sol-bond-stake-sc/src/instructions/stake_rewards.rs +++ b/programs/core-sol-bond-stake-sc/src/instructions/stake_rewards.rs @@ -106,7 +106,6 @@ pub fn stake_rewards<'a, 'b, 'c: 'info, 'info>( } else { 0 }; - let bond_to_be_subtracted = bond.bond_amount; let actual_claimable_amount; @@ -123,23 +122,21 @@ pub fn stake_rewards<'a, 'b, 'c: 'info, 'info>( bond.bond_timestamp = current_timestamp; bond.bond_amount += &actual_claimable_amount; - address_bonds_rewards.address_total_bond_amount += actual_claimable_amount; vault_config.total_bond_amount += &actual_claimable_amount; address_bonds_rewards.claimable_amount = 0; let weight_to_be_added = bond.bond_amount * MAX_PERCENT; - let bond_to_be_added: u64 = bond.bond_amount; let weighted_liveliness_score_new = compute_weighted_liveliness_new( weighted_liveliness_score_decayed, address_bonds_rewards.address_total_bond_amount, + address_bonds_rewards.address_total_bond_amount + actual_claimable_amount, weight_to_be_added, weight_to_be_subtracted, - bond_to_be_added, - bond_to_be_subtracted, ); + address_bonds_rewards.address_total_bond_amount += actual_claimable_amount; address_bonds_rewards.weighted_liveliness_score = weighted_liveliness_score_new; address_bonds_rewards.last_update_timestamp = current_timestamp; diff --git a/programs/core-sol-bond-stake-sc/src/instructions/topup.rs b/programs/core-sol-bond-stake-sc/src/instructions/topup.rs index 8062019..86d64a4 100644 --- a/programs/core-sol-bond-stake-sc/src/instructions/topup.rs +++ b/programs/core-sol-bond-stake-sc/src/instructions/topup.rs @@ -104,7 +104,7 @@ pub fn top_up<'a, 'b, 'c: 'info, 'info>( let current_timestamp = get_current_timestamp()?; let weight_to_be_added = (bond.bond_amount + amount) * MAX_PERCENT; - let bond_to_be_added = amount; + let weight_to_be_subtracted = if current_timestamp < bond.unbond_timestamp { bond.bond_amount .mul_div_floor( @@ -134,20 +134,20 @@ pub fn top_up<'a, 'b, 'c: 'info, 'info>( &mut ctx.accounts.address_bonds_rewards, )?; + let address_bonds_rewards = &mut ctx.accounts.address_bonds_rewards; + let weighted_liveliness_score_new = compute_weighted_liveliness_new( weighted_liveliness_score_decayed, - ctx.accounts.address_bonds_rewards.address_total_bond_amount, + address_bonds_rewards.address_total_bond_amount, + address_bonds_rewards.address_total_bond_amount + amount, weight_to_be_added, weight_to_be_subtracted, - bond_to_be_added, - 0, ); - let address_bonds_rewards = &mut ctx.accounts.address_bonds_rewards; + address_bonds_rewards.address_total_bond_amount += amount; address_bonds_rewards.weighted_liveliness_score = weighted_liveliness_score_new; address_bonds_rewards.last_update_timestamp = current_timestamp; - address_bonds_rewards.address_total_bond_amount += amount; let vault_config = &mut ctx.accounts.vault_config; vault_config.total_bond_amount += amount; diff --git a/programs/core-sol-bond-stake-sc/src/instructions/withdraw.rs b/programs/core-sol-bond-stake-sc/src/instructions/withdraw.rs index 504a6c1..2dd95f4 100644 --- a/programs/core-sol-bond-stake-sc/src/instructions/withdraw.rs +++ b/programs/core-sol-bond-stake-sc/src/instructions/withdraw.rs @@ -119,8 +119,6 @@ pub fn withdraw<'a, 'b, 'c: 'info, 'info>( 0 }; - let bond_amount_to_be_subtracted = bond.bond_amount; - let decay = compute_decay( ctx.accounts.address_bonds_rewards.last_update_timestamp, current_timestamp, @@ -138,17 +136,17 @@ pub fn withdraw<'a, 'b, 'c: 'info, 'info>( &mut ctx.accounts.address_bonds_rewards, )?; + let address_bonds_rewards = &mut ctx.accounts.address_bonds_rewards; + let weighted_liveliness_score_new = compute_weighted_liveliness_new( weighted_liveliness_score_decayed, - ctx.accounts.address_bonds_rewards.address_total_bond_amount, + address_bonds_rewards.address_total_bond_amount, + address_bonds_rewards.address_total_bond_amount - bond.bond_amount, 0, weight_to_be_subtracted, - 0, - bond_amount_to_be_subtracted, ); - let address_bonds_rewards = &mut ctx.accounts.address_bonds_rewards; - + address_bonds_rewards.address_total_bond_amount -= bond.bond_amount; address_bonds_rewards.weighted_liveliness_score = weighted_liveliness_score_new; address_bonds_rewards.last_update_timestamp = current_timestamp; @@ -179,8 +177,6 @@ pub fn withdraw<'a, 'b, 'c: 'info, 'info>( ctx.accounts.mint_of_token_to_receive.decimals, )?; - address_bonds_rewards.address_total_bond_amount -= bond.bond_amount; - bond.state = State::Inactive.to_code(); bond.unbond_timestamp = current_timestamp; bond.bond_amount = 0; diff --git a/programs/core-sol-bond-stake-sc/src/lib.rs b/programs/core-sol-bond-stake-sc/src/lib.rs index 0bf85c8..4187428 100644 --- a/programs/core-sol-bond-stake-sc/src/lib.rs +++ b/programs/core-sol-bond-stake-sc/src/lib.rs @@ -24,7 +24,7 @@ solana_security_txt::security_txt! { auditors: "https://itheum.io/audits" } -declare_id!("4nvez1kVuTbeeMBzXkuUfDvFNLuSraAqbxK5NypRMvtM"); +declare_id!("4zAKaiW68x31n7mRbYQBUgTC9BWL3q4uATjuBc5txYSN"); #[program] pub mod core_sol_bond_stake_sc { diff --git a/programs/core-sol-bond-stake-sc/src/libraries/rewards.rs b/programs/core-sol-bond-stake-sc/src/libraries/rewards.rs index 8024405..780fe51 100644 --- a/programs/core-sol-bond-stake-sc/src/libraries/rewards.rs +++ b/programs/core-sol-bond-stake-sc/src/libraries/rewards.rs @@ -131,17 +131,16 @@ pub fn compute_weighted_liveliness_decay(weighted_liveliness_score: u64, decay: pub fn compute_weighted_liveliness_new( weighted_liveliness_score_decayed: u64, - address_total_bond_amount: u64, + address_total_bond_amount_before: u64, + address_totaal_bond_amount_after: u64, weight_to_be_added: u64, weight_to_be_subtracted: u64, - bond_to_be_added: u64, - bond_to_be_subtracted: u64, ) -> u64 { let new = (weighted_liveliness_score_decayed - .saturating_mul(address_total_bond_amount) + .saturating_mul(address_total_bond_amount_before) .saturating_sub(weight_to_be_subtracted) .saturating_add(weight_to_be_added)) - .saturating_div(address_total_bond_amount + bond_to_be_added - bond_to_be_subtracted); + .saturating_div(address_totaal_bond_amount_after); new } diff --git a/tests/core-sol-bond-stake-sc.ts b/tests/core-sol-bond-stake-sc.ts index 2622e63..ab36dc3 100644 --- a/tests/core-sol-bond-stake-sc.ts +++ b/tests/core-sol-bond-stake-sc.ts @@ -1924,7 +1924,7 @@ describe('core-sol-bond-stake-sc', () => { authority: user.publicKey, authorityTokenAccount: itheum_token_user_ata, }) - .rpc({skipPreflight: true}) + .rpc() const normalWeighted = await calculateWeightedLivelinessScore( x,