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

[LATEST V1 CANDIDATE] feat: support vault bond & remove combined liveliness #12

Merged
merged 2 commits into from
Nov 18, 2024
Merged
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
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");
2 changes: 2 additions & 0 deletions programs/core-sol-bond-stake-sc/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ pub enum Errors {
NotCreator,
#[msg("Asset Id mismatch")]
AssetIdMismatch,
#[msg("Vault bond id mismatch")]
VaultBondIdMismatch,
}
34 changes: 3 additions & 31 deletions programs/core-sol-bond-stake-sc/src/instructions/bond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ use mpl_bubblegum::{types::LeafSchema, utils::get_asset_id};
use spl_account_compression::program::SplAccountCompression;

use crate::{
compute_decay, compute_weighted_liveliness_decay, compute_weighted_liveliness_new,
get_current_timestamp, update_address_claimable_rewards, AddressBondsRewards, AssetUsage, Bond,
BondConfig, Errors, RewardsConfig, State, VaultConfig, ADDRESS_BONDS_REWARDS_SEED,
BOND_CONFIG_SEED, BOND_SEED, MAX_PERCENT, REWARDS_CONFIG_SEED, VAULT_CONFIG_SEED,
BOND_CONFIG_SEED, BOND_SEED, REWARDS_CONFIG_SEED, VAULT_CONFIG_SEED,
};

#[derive(Accounts)]
#[instruction(bond_config_index: u8, bond_id:u8, amount: u64,nonce: u64)]
#[instruction(bond_config_index: u8, bond_id:u16, amount: u64,nonce: u64)]
pub struct BondContext<'info> {
#[account(
mut,
Expand Down Expand Up @@ -110,10 +109,9 @@ pub struct BondContext<'info> {

pub fn bond<'a, 'b, 'c: 'info, 'info>(
ctx: Context<'a, 'b, 'c, 'info, BondContext<'info>>,
bond_id: u8,
bond_id: u16,
amount: u64,
nonce: u64,
is_vault: bool,
root: [u8; 32],
data_hash: [u8; 32],
creator_hash: [u8; 32],
Expand All @@ -125,22 +123,6 @@ pub fn bond<'a, 'b, 'c: 'info, 'info>(

let current_timestamp = get_current_timestamp()?;

let weight_to_be_added = amount * MAX_PERCENT;
msg!("weight_to_be_added: {}", weight_to_be_added);
msg!("weight_to_be_added amount: {}", amount);
msg!("weight_to_be_added percent: {}", MAX_PERCENT);

let decay = compute_decay(
ctx.accounts.address_bonds_rewards.last_update_timestamp,
current_timestamp,
ctx.accounts.bond_config.lock_period,
);

let weighted_liveliness_score_decayed = compute_weighted_liveliness_decay(
ctx.accounts.address_bonds_rewards.weighted_liveliness_score,
decay,
);

update_address_claimable_rewards(
&mut ctx.accounts.rewards_config,
&mut ctx.accounts.vault_config,
Expand All @@ -149,15 +131,6 @@ pub fn bond<'a, 'b, 'c: 'info, 'info>(

let address_bonds_rewards = &mut ctx.accounts.address_bonds_rewards;

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 + amount,
weight_to_be_added,
0,
);

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;

Expand Down Expand Up @@ -213,7 +186,6 @@ pub fn bond<'a, 'b, 'c: 'info, 'info>(
ctx.accounts.bond.set_inner(Bond {
bump: ctx.bumps.bond,
state: State::Active.to_code(),
is_vault,
unbond_timestamp: current_timestamp.add(ctx.accounts.bond_config.lock_period),
bond_timestamp: current_timestamp,
bond_amount: amount,
Expand Down
61 changes: 35 additions & 26 deletions programs/core-sol-bond-stake-sc/src/instructions/claim_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ use anchor_spl::{
};

use crate::{
compute_decay, compute_weighted_liveliness_decay, compute_weighted_liveliness_new,
full_math::MulDiv, get_current_timestamp, update_address_claimable_rewards,
AddressBondsRewards, BondConfig, Errors, RewardsConfig, VaultConfig,
ADDRESS_BONDS_REWARDS_SEED, BOND_CONFIG_SEED, MAX_PERCENT, REWARDS_CONFIG_SEED,
compute_bond_score, full_math::MulDiv, get_current_timestamp, update_address_claimable_rewards,
AddressBondsRewards, Bond, BondConfig, Errors, RewardsConfig, State, VaultConfig,
ADDRESS_BONDS_REWARDS_SEED, BOND_CONFIG_SEED, BOND_SEED, MAX_PERCENT, REWARDS_CONFIG_SEED,
VAULT_CONFIG_SEED,
};

#[derive(Accounts)]
#[instruction(bond_config_index:u8)]
#[instruction(bond_config_index:u8,bond_id:u16)]
pub struct ClaimRewards<'info> {
#[account(
mut,
Expand All @@ -28,6 +27,18 @@ pub struct ClaimRewards<'info> {
)]
pub bond_config: Account<'info, BondConfig>,

#[account(
mut,
seeds = [
BOND_SEED.as_bytes(),
authority.key().as_ref(),
&bond_id.to_le_bytes()
],
bump=bond.bump,
)]
pub bond: Account<'info, Bond>,

#[account(
mut,
seeds=[REWARDS_CONFIG_SEED.as_bytes()],
Expand Down Expand Up @@ -76,25 +87,26 @@ pub struct ClaimRewards<'info> {

pub fn claim_rewards<'a, 'b, 'c: 'info, 'info>(
ctx: Context<'a, 'b, 'c, 'info, ClaimRewards<'info>>,
bond_id: u16,
) -> Result<()> {
let signer_seeds: [&[&[u8]]; 1] = [&[
VAULT_CONFIG_SEED.as_bytes(),
&[ctx.accounts.vault_config.bump],
]];

let current_timestamp = get_current_timestamp()?;

let decay = compute_decay(
ctx.accounts.address_bonds_rewards.last_update_timestamp,
current_timestamp,
ctx.accounts.bond_config.lock_period,
require!(
ctx.accounts.address_bonds_rewards.vault_bond_id == bond_id
&& ctx.accounts.address_bonds_rewards.vault_bond_id != 0,
Errors::VaultBondIdMismatch
);

let weighted_liveliness_score_decayed = compute_weighted_liveliness_decay(
ctx.accounts.address_bonds_rewards.weighted_liveliness_score,
decay,
require!(
ctx.accounts.bond.state == State::Active.to_code(),
Errors::BondIsInactive
);

let current_timestamp = get_current_timestamp()?;

update_address_claimable_rewards(
&mut ctx.accounts.rewards_config,
&ctx.accounts.vault_config,
Expand All @@ -110,24 +122,21 @@ pub fn claim_rewards<'a, 'b, 'c: 'info, 'info>(

let actual_claimable_amount;

if weighted_liveliness_score_decayed >= 95_00u64 {
let actual_vault_liveliness_score = compute_bond_score(
ctx.accounts.bond_config.lock_period,
current_timestamp,
ctx.accounts.bond.unbond_timestamp,
);

if actual_vault_liveliness_score >= 95_00u64 {
actual_claimable_amount = address_bonds_rewards.claimable_amount;
} else {
actual_claimable_amount = address_bonds_rewards
.claimable_amount
.mul_div_floor(weighted_liveliness_score_decayed, MAX_PERCENT)
.mul_div_floor(actual_vault_liveliness_score, MAX_PERCENT)
.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;

let cpi_accounts = TransferChecked {
Expand All @@ -149,4 +158,4 @@ pub fn claim_rewards<'a, 'b, 'c: 'info, 'info>(
address_bonds_rewards.claimable_amount = 0;

Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ pub fn initialize_address<'info>(ctx: Context<InitializeAddress<'info>>) -> Resu
address: ctx.accounts.authority.key(),
address_total_bond_amount: 0,
current_index: 0,
weighted_liveliness_score: 0,
last_update_timestamp: get_current_timestamp()?,
address_rewards_per_share: ctx.accounts.rewards_config.rewards_per_share,
claimable_amount: 0,
vault_bond_id: 0,
padding: [0; 16],
});

Expand Down
2 changes: 2 additions & 0 deletions programs/core-sol-bond-stake-sc/src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ pub mod claim_rewards;
pub use claim_rewards::*;
pub mod initialize_address;
pub use initialize_address::*;
pub mod vault_address_update;
pub use vault_address_update::*;
58 changes: 4 additions & 54 deletions programs/core-sol-bond-stake-sc/src/instructions/renew.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use anchor_lang::prelude::*;

use crate::{
compute_decay, compute_weighted_liveliness_decay, compute_weighted_liveliness_new,
full_math::MulDiv, get_current_timestamp, update_address_claimable_rewards,
AddressBondsRewards, Bond, BondConfig, Errors, RewardsConfig, State, VaultConfig,
ADDRESS_BONDS_REWARDS_SEED, BOND_CONFIG_SEED, BOND_SEED, MAX_PERCENT, REWARDS_CONFIG_SEED,
VAULT_CONFIG_SEED,
get_current_timestamp, update_address_claimable_rewards, AddressBondsRewards, Bond, BondConfig,
Errors, RewardsConfig, State, VaultConfig, ADDRESS_BONDS_REWARDS_SEED, BOND_CONFIG_SEED,
BOND_SEED, REWARDS_CONFIG_SEED, VAULT_CONFIG_SEED,
};

#[derive(Accounts)]
#[instruction(bond_config_index:u8,bond_id:u8)]
#[instruction(bond_config_index:u8,bond_id:u16)]
pub struct Renew<'info> {
#[account(
seeds=[BOND_CONFIG_SEED.as_bytes(),&bond_config_index.to_be_bytes()],
Expand Down Expand Up @@ -69,45 +67,6 @@ pub fn renew(ctx: Context<Renew>) -> Result<()> {
Errors::BondIsInactive
);

let weight_to_be_added = bond.bond_amount * MAX_PERCENT;
msg!("weight_to_be_added: {}", weight_to_be_added);
msg!("weight_to_be_added amount: {}", bond.bond_amount);
msg!("weight_to_be_added percent: {}", MAX_PERCENT);

let weight_to_be_subtracted = if current_timestamp < bond.unbond_timestamp {
bond.bond_amount
.mul_div_floor(
bond.unbond_timestamp - current_timestamp,
ctx.accounts.bond_config.lock_period,
)
.unwrap()
* MAX_PERCENT
} else {
0
};
msg!("weight_to_be_subtracted: {}", weight_to_be_subtracted);
msg!("weight_to_be_subtracted amount: {}", bond.bond_amount);
if current_timestamp < bond.unbond_timestamp {
msg!(
"weight_to_be_subtracted percent: {}",
(bond.unbond_timestamp - current_timestamp) * MAX_PERCENT
/ ctx.accounts.bond_config.lock_period
);
} else {
msg!("weight_to_be_subtracted percent: 0");
}

let decay = compute_decay(
ctx.accounts.address_bonds_rewards.last_update_timestamp,
current_timestamp,
ctx.accounts.bond_config.lock_period,
);

let weighted_liveliness_score_decayed = compute_weighted_liveliness_decay(
ctx.accounts.address_bonds_rewards.weighted_liveliness_score,
decay,
);

update_address_claimable_rewards(
&mut ctx.accounts.rewards_config,
&ctx.accounts.vault_config,
Expand All @@ -116,15 +75,6 @@ pub fn renew(ctx: Context<Renew>) -> Result<()> {

let address_bonds_rewards = &mut ctx.accounts.address_bonds_rewards;

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,
weight_to_be_added,
weight_to_be_subtracted,
);

address_bonds_rewards.weighted_liveliness_score = weighted_liveliness_score_new;
address_bonds_rewards.last_update_timestamp = current_timestamp;

bond.unbond_timestamp = current_timestamp + ctx.accounts.bond_config.lock_period;
Expand Down
Loading