Skip to content
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
359 changes: 194 additions & 165 deletions Cargo.lock

Large diffs are not rendered by default.

25 changes: 13 additions & 12 deletions programs/cp-swap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ enable-log = []
devnet = []
client = []
anchor-debug = []
idl-build = ["anchor-lang/idl-build", "light-sdk/idl-build", "light-token/idl-build", "light-anchor-spl/idl-build"]
idl-build = ["anchor-lang/idl-build", "light-sdk/idl-build", "light-token/idl-build", "light-anchor-spl/idl-build", "light-anchor-spl/token_2022"]
test-sbf = []

[dependencies]
anchor-lang = { version = "=0.31.1", features = ["init-if-needed", "idl-build"] }
anchor-lang = { version = "=0.31.1", features = ["init-if-needed"] }
spl-token-2022 = { version = "7.0.0", features = ["no-entrypoint"] }
spl-math = { version = "0.3", features = ["no-entrypoint"] }
uint = "0.10.0"
Expand All @@ -32,13 +32,14 @@ bytemuck = { version = "1.4.0", features = ["derive", "min_const_generics"] }
arrayref = { version = "0.3.6" }
blake3 = { workspace = true }

light-sdk = { version = "0.18.0", features = ["anchor", "anchor-discriminator", "idl-build", "cpi-context"] }
light-token = { version = "0.3.0", features = ["anchor", "idl-build"] }
light-hasher = "5"
light-anchor-spl = { version = "0.31.1", features = ["idl-build", "memo"] }
solana-account-info = "2.3"
solana-program = "2.2"
solana-pubkey = "2.2"
light-sdk = { git = "https://github.com/Lightprotocol/light-protocol", rev = "9876c8558b66753f29c58c0a995a88b2a70d9bda", features = ["anchor", "anchor-discriminator", "cpi-context"] }
light-token = { git = "https://github.com/Lightprotocol/light-protocol", rev = "9876c8558b66753f29c58c0a995a88b2a70d9bda", features = ["anchor"] }
light-hasher = { git = "https://github.com/Lightprotocol/light-protocol", rev = "9876c8558b66753f29c58c0a995a88b2a70d9bda" }
light-account = { git = "https://github.com/Lightprotocol/light-protocol", rev = "9876c8558b66753f29c58c0a995a88b2a70d9bda", features = ["token", "anchor", "sha256"] }
light-anchor-spl = { version = "0.31.1", features = ["memo", "token_2022"] }
solana-account-info = "2.2"
solana-program = "2.3"
solana-pubkey = "2.4"
solana-program-error = "2.2"
solana-cpi = { version = "2.2" }
solana-msg = "2.2"
Expand All @@ -48,13 +49,13 @@ quickcheck = "1.0.3"
proptest = "1.0"
rand = "0.9.0"

light-program-test = { version = "0.18.0" }
light-client = { version = "0.18.0" }
light-program-test = { git = "https://github.com/Lightprotocol/light-protocol", rev = "9876c8558b66753f29c58c0a995a88b2a70d9bda" }
light-client = { git = "https://github.com/Lightprotocol/light-protocol", rev = "9876c8558b66753f29c58c0a995a88b2a70d9bda" }
tokio = { version = "1", features = ["full"] }
spl-token = "7.0.0"
solana-keypair = { version = "2.2" }
solana-signer = { version = "2.2" }
solana-instruction = { version = "2.2" }
solana-instruction = { version = "2.3" }
solana-sdk = { version = "2.3" }
bincode = "1.3"

Expand Down
35 changes: 23 additions & 12 deletions programs/cp-swap/src/instructions/admin/collect_fund_fee.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::error::ErrorCode;
use crate::states::*;
use crate::utils::token::*;
use anchor_lang::prelude::*;
use anchor_lang::{accounts::account_loader::AccountLoader, prelude::*};
use light_anchor_spl::token::Token;
use light_anchor_spl::token_interface::Mint;
use light_anchor_spl::token_interface::Token2022;
Expand All @@ -23,24 +23,17 @@ pub struct CollectFundFee<'info> {

/// Pool state stores accumulated protocol fee amount
#[account(mut)]
pub pool_state: Account<'info, PoolState>,
pub pool_state: AccountLoader<'info, PoolState>,

/// Amm config account stores fund_owner
#[account(address = pool_state.amm_config)]
pub amm_config: Account<'info, AmmConfig>,

/// The address that holds pool tokens for token_0
#[account(
mut,
constraint = token_0_vault.key() == pool_state.token_0_vault
)]
#[account(mut)]
pub token_0_vault: Box<InterfaceAccount<'info, TokenAccount>>,

/// The address that holds pool tokens for token_1
#[account(
mut,
constraint = token_1_vault.key() == pool_state.token_1_vault
)]
#[account(mut)]
pub token_1_vault: Box<InterfaceAccount<'info, TokenAccount>>,

/// The mint of token_0 vault
Expand Down Expand Up @@ -84,7 +77,25 @@ pub fn collect_fund_fee(
let amount_1: u64;
let auth_bump: u8;
{
let pool_state = &mut ctx.accounts.pool_state;
let pool_state = &mut ctx.accounts.pool_state.load_mut()?;

// Validate addresses
require_keys_eq!(
ctx.accounts.amm_config.key(),
pool_state.amm_config,
ErrorCode::InvalidOwner
);
require_keys_eq!(
ctx.accounts.token_0_vault.key(),
pool_state.token_0_vault,
ErrorCode::InvalidVault
);
require_keys_eq!(
ctx.accounts.token_1_vault.key(),
pool_state.token_1_vault,
ErrorCode::InvalidVault
);

amount_0 = amount_0_requested.min(pool_state.fund_fees_token_0);
amount_1 = amount_1_requested.min(pool_state.fund_fees_token_1);

Expand Down
34 changes: 22 additions & 12 deletions programs/cp-swap/src/instructions/admin/collect_protocol_fee.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::error::ErrorCode;
use crate::states::*;
use crate::utils::*;
use anchor_lang::prelude::*;
use anchor_lang::{accounts::account_loader::AccountLoader, prelude::*};
use light_anchor_spl::token::Token;
use light_anchor_spl::token_interface::Mint;
use light_anchor_spl::token_interface::Token2022;
Expand All @@ -24,24 +24,17 @@ pub struct CollectProtocolFee<'info> {

/// Pool state stores accumulated protocol fee amount
#[account(mut)]
pub pool_state: Account<'info, PoolState>,
pub pool_state: AccountLoader<'info, PoolState>,

/// Amm config account stores owner
#[account(address = pool_state.amm_config)]
pub amm_config: Account<'info, AmmConfig>,

/// The address that holds pool tokens for token_0
#[account(
mut,
constraint = token_0_vault.key() == pool_state.token_0_vault
)]
#[account(mut)]
pub token_0_vault: Box<InterfaceAccount<'info, TokenAccount>>,

/// The address that holds pool tokens for token_1
#[account(
mut,
constraint = token_1_vault.key() == pool_state.token_1_vault
)]
#[account(mut)]
pub token_1_vault: Box<InterfaceAccount<'info, TokenAccount>>,

/// The mint of token_0 vault
Expand Down Expand Up @@ -85,7 +78,24 @@ pub fn collect_protocol_fee(
let amount_1: u64;
let auth_bump: u8;
{
let pool_state = &mut ctx.accounts.pool_state;
let pool_state = &mut ctx.accounts.pool_state.load_mut()?;

// Validate addresses
require_keys_eq!(
ctx.accounts.amm_config.key(),
pool_state.amm_config,
ErrorCode::InvalidOwner
);
require_keys_eq!(
ctx.accounts.token_0_vault.key(),
pool_state.token_0_vault,
ErrorCode::InvalidVault
);
require_keys_eq!(
ctx.accounts.token_1_vault.key(),
pool_state.token_1_vault,
ErrorCode::InvalidVault
);

amount_0 = amount_0_requested.min(pool_state.protocol_fees_token_0);
amount_1 = amount_1_requested.min(pool_state.protocol_fees_token_1);
Expand Down
6 changes: 3 additions & 3 deletions programs/cp-swap/src/instructions/admin/update_pool_status.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::states::*;
use anchor_lang::prelude::*;
use anchor_lang::{accounts::account_loader::AccountLoader, prelude::*};

#[derive(Accounts)]
pub struct UpdatePoolStatus<'info> {
Expand All @@ -9,12 +9,12 @@ pub struct UpdatePoolStatus<'info> {
pub authority: Signer<'info>,

#[account(mut)]
pub pool_state: Account<'info, PoolState>,
pub pool_state: AccountLoader<'info, PoolState>,
}

pub fn update_pool_status(ctx: Context<UpdatePoolStatus>, status: u8) -> Result<()> {
require_gte!(255, status);
let pool_state = &mut ctx.accounts.pool_state;
let pool_state = &mut ctx.accounts.pool_state.load_mut()?;
pool_state.set_status(status);
pool_state.recent_epoch = Clock::get()?.epoch;
Ok(())
Expand Down
40 changes: 25 additions & 15 deletions programs/cp-swap/src/instructions/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::curve::RoundDirection;
use crate::error::ErrorCode;
use crate::states::*;
use crate::utils::token::*;
use anchor_lang::prelude::*;
use anchor_lang::{accounts::account_loader::AccountLoader, prelude::*};
use light_anchor_spl::token::Token;
use light_anchor_spl::token_interface::Token2022;
use light_token::instruction::MintToCpi;
Expand All @@ -25,7 +25,7 @@ pub struct Deposit<'info> {
pub authority: UncheckedAccount<'info>,

#[account(mut)]
pub pool_state: Account<'info, PoolState>,
pub pool_state: AccountLoader<'info, PoolState>,

/// Owner lp token account
#[account(mut, token::authority = owner)]
Expand All @@ -48,17 +48,11 @@ pub struct Deposit<'info> {
pub token_1_account: Box<InterfaceAccount<'info, TokenAccount>>,

/// The address that holds pool tokens for token_0
#[account(
mut,
constraint = token_0_vault.key() == pool_state.token_0_vault
)]
#[account(mut)]
pub token_0_vault: Box<InterfaceAccount<'info, TokenAccount>>,

/// The address that holds pool tokens for token_1
#[account(
mut,
constraint = token_1_vault.key() == pool_state.token_1_vault
)]
#[account(mut)]
pub token_1_vault: Box<InterfaceAccount<'info, TokenAccount>>,

/// token Program
Expand All @@ -83,10 +77,7 @@ pub struct Deposit<'info> {
pub vault_1_mint: Box<InterfaceAccount<'info, Mint>>,

/// Lp mint
#[account(
mut,
address = pool_state.lp_mint @ ErrorCode::IncorrectLpMint
)]
#[account(mut)]
pub lp_mint: Box<InterfaceAccount<'info, Mint>>,

pub system_program: Program<'info, System>,
Expand All @@ -103,7 +94,25 @@ pub fn deposit(
) -> Result<()> {
require_gt!(lp_token_amount, 0);
let pool_id = ctx.accounts.pool_state.key();
let pool_state = &mut ctx.accounts.pool_state;
let pool_state = &mut ctx.accounts.pool_state.load_mut()?;

// Validate vault and lp_mint addresses
require_keys_eq!(
ctx.accounts.token_0_vault.key(),
pool_state.token_0_vault,
ErrorCode::InvalidVault
);
require_keys_eq!(
ctx.accounts.token_1_vault.key(),
pool_state.token_1_vault,
ErrorCode::InvalidVault
);
require_keys_eq!(
ctx.accounts.lp_mint.key(),
pool_state.lp_mint,
ErrorCode::IncorrectLpMint
);

if !pool_state.get_status_by_bit(PoolStatusBitIndex::Deposit) {
return err!(ErrorCode::NotApproved);
}
Expand Down Expand Up @@ -213,6 +222,7 @@ pub fn deposit(
authority: ctx.accounts.authority.to_account_info(),
system_program: ctx.accounts.system_program.to_account_info(),
max_top_up: None,
fee_payer: None,
}
.invoke_signed(&[&[crate::AUTH_SEED.as_bytes(), &[pool_state.auth_bump]]])?;
pool_state.recent_epoch = Clock::get()?.epoch;
Expand Down
Loading