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

[DONT MERGE - V2 Candidate] feat: support range bond child for vault bond #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 4 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,8 @@ pub enum Errors {
NotCreator,
#[msg("Asset Id mismatch")]
AssetIdMismatch,
#[msg("ParentBondIsNotVault")]
ParentBondIsNotVault,
#[msg("ParentBondIsNotActive")]
ParentBondIsNotActive,
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn create_bond_config(
bond_config.lock_period = lock_period;
bond_config.bond_amount = bond_amount;
bond_config.withdraw_penalty = withdraw_penalty;
// bond_config.padding = [0; 32];
bond_config.padding = [0; 32];

Ok(())
}
5 changes: 4 additions & 1 deletion programs/core-sol-bond-stake-sc/src/instructions/bond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,10 @@ pub fn bond<'a, 'b, 'c: 'info, 'info>(
bond_amount: amount,
asset_id: asset_id.key(),
owner: ctx.accounts.authority.key(),
padding: [0; 64],
parent_bond: Pubkey::default(),
start_nonce: 0u64,
end_nonce: 0u64,
padding: [0; 16],
});

Ok(())
Expand Down
99 changes: 99 additions & 0 deletions programs/core-sol-bond-stake-sc/src/instructions/bond_range.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use anchor_lang::prelude::*;

use crate::{
AddressBondsRewards, Bond, BondConfig, Errors, State, ADDRESS_BONDS_REWARDS_SEED,
BOND_CONFIG_SEED, BOND_SEED,
};

#[derive(Accounts)]
#[instruction(bond_config_index:u8, bond_id:u8, parent_bond_id:u8, start_nonce: u64, end_nonce: u64)]

pub struct BondRange<'info> {
#[account(
mut,
seeds=[ADDRESS_BONDS_REWARDS_SEED.as_bytes(), authority.key().as_ref()],
bump=address_bonds_rewards.bump,
)]
pub address_bonds_rewards: Box<Account<'info, AddressBondsRewards>>,

// a way to limit the usage of the range again
// To be added
#[account(
seeds=[BOND_SEED.as_bytes(),authority.key().as_ref(),&parent_bond_id.to_le_bytes()],
bump,
)]
pub parent_bond: Box<Account<'info, Bond>>,

#[account(
init,
payer = authority,
constraint=address_bonds_rewards.current_index + 1 == bond_id @ Errors::WrongBondId,
seeds = [
BOND_SEED.as_bytes(),
authority.key().as_ref(),
&bond_id.to_le_bytes()
],
bump,
space = Bond::INIT_SPACE
)]
pub bond: Box<Account<'info, Bond>>,

#[account(
seeds=[BOND_CONFIG_SEED.as_bytes(),&bond_config_index.to_be_bytes()],
bump=bond_config.bump,
)]
pub bond_config: Box<Account<'info, BondConfig>>,

/// CHECK: unsafe
#[account(
constraint= merkle_tree.key() == bond_config.merkle_tree.key() @ Errors::MerkleTreeMismatch,
)]
pub merkle_tree: UncheckedAccount<'info>,

#[account(
mut,
constraint=address_bonds_rewards.address == authority.key() @ Errors::OwnerMismatch,
)]
pub authority: Signer<'info>,

pub system_program: Program<'info, System>,
}

pub fn bond_range<'info>(
ctx: Context<BondRange>,
_bond_config_index: u8,
bond_id: u8,
_parent_bond_id: u8,
start_nonce: u64,
end_nonce: u64,
) -> Result<()> {
require!(
ctx.accounts.parent_bond.is_vault,
Errors::ParentBondIsNotVault
);
require!(
ctx.accounts.parent_bond.state == State::Active.to_code(),
Errors::ParentBondIsNotActive
);

let address_bonds_rewards = &mut ctx.accounts.address_bonds_rewards;

address_bonds_rewards.current_index = bond_id;

ctx.accounts.bond.set_inner(Bond {
bump: ctx.bumps.bond,
state: State::Child.to_code(),
is_vault: false,
unbond_timestamp: 0u64,
bond_timestamp: 0u64,
bond_amount: 0u64,
asset_id: Pubkey::default(),
owner: ctx.accounts.authority.key(),
parent_bond: ctx.accounts.parent_bond.key(),
start_nonce,
end_nonce,
padding: [0; 16],
});

Ok(())
}
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 bond_range;
pub use bond_range::*;
22 changes: 22 additions & 0 deletions programs/core-sol-bond-stake-sc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,28 @@ pub mod core_sol_bond_stake_sc {
)
}

pub fn bond_range<'info>(
ctx: Context<BondRange>,
bond_config_index: u8,
bond_id: u8,
parent_bond_id: u8,
start_nonce: u64,
end_nonce: u64,
) -> Result<()> {
require!(
ctx.accounts.bond_config.bond_state == State::Active.to_code(),
Errors::ProgramIsPaused
);
instructions::bond_range(
ctx,
bond_config_index,
bond_id,
parent_bond_id,
start_nonce,
end_nonce,
)
}

pub fn renew(ctx: Context<Renew>, _bond_config_index: u8, _bond_id: u8) -> Result<()> {
require!(
ctx.accounts.bond_config.bond_state == State::Active.to_code(),
Expand Down
6 changes: 5 additions & 1 deletion programs/core-sol-bond-stake-sc/src/states/bond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ pub struct Bond {
pub bond_amount: u64,
pub asset_id: Pubkey,
pub owner: Pubkey,
pub padding: [u8; 64],
// Range bond for cNFTs
pub parent_bond: Pubkey,
pub start_nonce: u64,
pub end_nonce: u64,
pub padding: [u8; 16],
}
impl Space for Bond {
const INIT_SPACE: usize = 8 + 1 + 1 + 1 + 8 + 8 + 8 + 32 + 32 + 64;
Expand Down
2 changes: 2 additions & 0 deletions programs/core-sol-bond-stake-sc/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ use solana_program::clock;
pub enum State {
Inactive = 0,
Active = 1,
Child = 2,
}
impl State {
pub fn to_code(&self) -> u8 {
match self {
State::Inactive => 0,
State::Active => 1,
State::Child => 2,
}
}
}
Expand Down