Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: stable release #3

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
223 changes: 140 additions & 83 deletions interactions/index.ts

Large diffs are not rendered by default.

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
13 changes: 12 additions & 1 deletion programs/core-sol-bond-stake-sc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,18 @@ use constants::*;
mod errors;
use errors::*;

declare_id!("4nvez1kVuTbeeMBzXkuUfDvFNLuSraAqbxK5NypRMvtM");
#[cfg(not(feature = "no-entrypoint"))]
solana_security_txt::security_txt! {
name: "itheum-bonding-staking-program",
project_url: "https://www.itheum.io/",
contacts: "https://itheum.io/bug-bounty",
policy: "https://itheum.io/bug-bounty",
source_code: "https://github.com/Itheum/core-sol-bonding-staking-sc",
preferred_languages: "en",
auditors: "https://itheum.io/audits"
}

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