Skip to content

Commit

Permalink
fix: weighted liveliness score
Browse files Browse the repository at this point in the history
  • Loading branch information
bucurdavid committed Oct 25, 2024
1 parent b03cdf4 commit e1a93bf
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 66 deletions.
2 changes: 1 addition & 1 deletion programs/core-sol-bond-stake-sc/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
46 changes: 22 additions & 24 deletions programs/core-sol-bond-stake-sc/src/instructions/bond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()?;

Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;

Expand Down
9 changes: 4 additions & 5 deletions programs/core-sol-bond-stake-sc/src/instructions/renew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,16 @@ pub fn renew(ctx: Context<Renew>) -> 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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down
12 changes: 6 additions & 6 deletions programs/core-sol-bond-stake-sc/src/instructions/topup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 5 additions & 9 deletions programs/core-sol-bond-stake-sc/src/instructions/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion programs/core-sol-bond-stake-sc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 4 additions & 5 deletions programs/core-sol-bond-stake-sc/src/libraries/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion tests/core-sol-bond-stake-sc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e1a93bf

Please sign in to comment.