Skip to content
Draft
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
477 changes: 315 additions & 162 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ codegen-units = 1
opt-level = 3
incremental = false
codegen-units = 1

[patch.crates-io]
light-client = { path = "../light-protocol/sdk-libs/client" }
light-program-test = { path = "../light-protocol/sdk-libs/program-test" }
light-sdk = { path = "../light-protocol/sdk-libs/sdk" }
light-token = { path = "../light-protocol/sdk-libs/token-sdk" }
light-hasher = { path = "../light-protocol/program-libs/hasher" }
10 changes: 5 additions & 5 deletions programs/cp-swap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ 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-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"
Expand All @@ -48,8 +48,8 @@ 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 = { version = ">=0.18.0", features = ["devenv"] }
light-client = { version = ">=0.18.0" }
tokio = { version = "1", features = ["full"] }
spl-token = "7.0.0"
solana-keypair = { version = "2.2" }
Expand Down
14 changes: 14 additions & 0 deletions programs/cp-swap/src/instructions/admin/collect_fund_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,22 @@ pub struct CollectFundFee<'info> {

/// CHECK: light_token CPI authority.
pub light_token_cpi_authority: AccountInfo<'info>,

/// Optional SPL interface PDA for token_0 (only needed if token_0 is SPL)
#[account(mut)]
pub spl_interface_pda_0: Option<AccountInfo<'info>>,

/// Optional SPL interface PDA for token_1 (only needed if token_1 is SPL)
#[account(mut)]
pub spl_interface_pda_1: Option<AccountInfo<'info>>,
}

pub fn collect_fund_fee(
ctx: Context<CollectFundFee>,
amount_0_requested: u64,
amount_1_requested: u64,
spl_interface_bump_0: Option<u8>,
spl_interface_bump_1: Option<u8>,
) -> Result<()> {
let amount_0: u64;
let amount_1: u64;
Expand Down Expand Up @@ -109,6 +119,8 @@ pub fn collect_fund_fee(
ctx.accounts.owner.to_account_info(),
ctx.accounts.light_token_cpi_authority.to_account_info(),
ctx.accounts.system_program.to_account_info(),
ctx.accounts.spl_interface_pda_0.clone(),
spl_interface_bump_0,
)?;

transfer_from_pool_vault_to_user(
Expand All @@ -126,6 +138,8 @@ pub fn collect_fund_fee(
ctx.accounts.owner.to_account_info(),
ctx.accounts.light_token_cpi_authority.to_account_info(),
ctx.accounts.system_program.to_account_info(),
ctx.accounts.spl_interface_pda_1.clone(),
spl_interface_bump_1,
)?;

Ok(())
Expand Down
14 changes: 14 additions & 0 deletions programs/cp-swap/src/instructions/admin/collect_protocol_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,22 @@ pub struct CollectProtocolFee<'info> {

/// CHECK: light_token CPI authority.
pub light_token_cpi_authority: AccountInfo<'info>,

/// Optional SPL interface PDA for token_0 (only needed if token_0 is SPL)
#[account(mut)]
pub spl_interface_pda_0: Option<AccountInfo<'info>>,

/// Optional SPL interface PDA for token_1 (only needed if token_1 is SPL)
#[account(mut)]
pub spl_interface_pda_1: Option<AccountInfo<'info>>,
}

pub fn collect_protocol_fee(
ctx: Context<CollectProtocolFee>,
amount_0_requested: u64,
amount_1_requested: u64,
spl_interface_bump_0: Option<u8>,
spl_interface_bump_1: Option<u8>,
) -> Result<()> {
let amount_0: u64;
let amount_1: u64;
Expand Down Expand Up @@ -118,6 +128,8 @@ pub fn collect_protocol_fee(
ctx.accounts.owner.to_account_info(),
ctx.accounts.light_token_cpi_authority.to_account_info(),
ctx.accounts.system_program.to_account_info(),
ctx.accounts.spl_interface_pda_0.clone(),
spl_interface_bump_0,
)?;

transfer_from_pool_vault_to_user(
Expand All @@ -135,6 +147,8 @@ pub fn collect_protocol_fee(
ctx.accounts.owner.to_account_info(),
ctx.accounts.light_token_cpi_authority.to_account_info(),
ctx.accounts.system_program.to_account_info(),
ctx.accounts.spl_interface_pda_1.clone(),
spl_interface_bump_1,
)?;

Ok(())
Expand Down
17 changes: 16 additions & 1 deletion programs/cp-swap/src/instructions/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::utils::token::*;
use anchor_lang::prelude::*;
use light_anchor_spl::token::Token;
use light_anchor_spl::token_interface::Token2022;
use light_anchor_spl::token_interface::{Mint, TokenAccount, TokenInterface};
use light_token::instruction::MintToCpi;
use light_anchor_spl::token_interface::{TokenAccount, Mint,TokenInterface};

#[derive(Accounts)]
pub struct Deposit<'info> {
Expand Down Expand Up @@ -93,13 +93,23 @@ pub struct Deposit<'info> {

/// CHECK: light-token CPI authority.
pub light_token_cpi_authority: AccountInfo<'info>,

/// Optional SPL interface PDA for token_0 (only needed if token_0 is SPL)
#[account(mut)]
pub spl_interface_pda_0: Option<AccountInfo<'info>>,

/// Optional SPL interface PDA for token_1 (only needed if token_1 is SPL)
#[account(mut)]
pub spl_interface_pda_1: Option<AccountInfo<'info>>,
}

pub fn deposit(
ctx: Context<Deposit>,
lp_token_amount: u64,
maximum_token_0_amount: u64,
maximum_token_1_amount: u64,
spl_interface_bump_0: Option<u8>,
spl_interface_bump_1: Option<u8>,
) -> Result<()> {
require_gt!(lp_token_amount, 0);
let pool_id = ctx.accounts.pool_state.key();
Expand Down Expand Up @@ -186,6 +196,8 @@ pub fn deposit(
ctx.accounts.owner.to_account_info(),
ctx.accounts.light_token_cpi_authority.to_account_info(),
ctx.accounts.system_program.to_account_info(),
ctx.accounts.spl_interface_pda_0.clone(),
spl_interface_bump_0,
)?;

transfer_from_user_to_pool_vault(
Expand All @@ -202,11 +214,14 @@ pub fn deposit(
ctx.accounts.owner.to_account_info(),
ctx.accounts.light_token_cpi_authority.to_account_info(),
ctx.accounts.system_program.to_account_info(),
ctx.accounts.spl_interface_pda_1.clone(),
spl_interface_bump_1,
)?;

pool_state.lp_supply = pool_state.lp_supply.checked_add(lp_token_amount).unwrap();

MintToCpi {
fee_payer: Some(ctx.accounts.owner.to_account_info()),
mint: ctx.accounts.lp_mint.to_account_info(),
destination: ctx.accounts.owner_lp_token.to_account_info(),
amount: lp_token_amount,
Expand Down
45 changes: 35 additions & 10 deletions programs/cp-swap/src/instructions/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ pub struct InitializeParams {
pub lp_mint_signer_bump: u8,
pub creator_lp_token_bump: u8,
pub authority_bump: u8,
/// SPL interface PDA bump for token_0 (None for Light tokens)
pub spl_interface_bump_0: Option<u8>,
/// SPL interface PDA bump for token_1 (None for Light tokens)
pub spl_interface_bump_1: Option<u8>,
}

#[derive(Accounts, LightAccounts)]
Expand Down Expand Up @@ -83,11 +87,13 @@ pub struct Initialize<'info> {

#[account(mut)]
#[light_account(init, mint,
mint_signer = lp_mint_signer,
authority = authority,
decimals = 9,
mint_seeds = &[LP_MINT_SIGNER_SEED, self.pool_state.to_account_info().key.as_ref(), &[params.lp_mint_signer_bump]],
authority_seeds = &[crate::AUTH_SEED.as_bytes(), &[params.authority_bump]]
mint::signer = lp_mint_signer,
mint::authority = authority,
mint::decimals = 9,
mint::seeds = &[LP_MINT_SIGNER_SEED, self.pool_state.to_account_info().key.as_ref()],
mint::bump = params.lp_mint_signer_bump,
mint::authority_seeds = &[crate::AUTH_SEED.as_bytes()],
mint::authority_bump = params.authority_bump
)]
pub lp_mint: UncheckedAccount<'info>,

Expand Down Expand Up @@ -117,7 +123,7 @@ pub struct Initialize<'info> {
],
bump,
)]
#[light_account(token, authority = [crate::AUTH_SEED.as_bytes()])]
#[light_account(token, token::authority = [crate::AUTH_SEED.as_bytes()])]
pub token_0_vault: UncheckedAccount<'info>,

#[account(
Expand All @@ -129,7 +135,7 @@ pub struct Initialize<'info> {
],
bump,
)]
#[light_account(token, authority = [crate::AUTH_SEED.as_bytes()])]
#[light_account(token, token::authority = [crate::AUTH_SEED.as_bytes()])]
pub token_1_vault: UncheckedAccount<'info>,

#[account(
Expand Down Expand Up @@ -164,6 +170,14 @@ pub struct Initialize<'info> {

/// CHECK: light-token CPI authority.
pub light_token_cpi_authority: AccountInfo<'info>,

/// Optional SPL interface PDA for token_0 (only needed if token_0 is SPL)
#[account(mut)]
pub spl_interface_pda_0: Option<AccountInfo<'info>>,

/// Optional SPL interface PDA for token_1 (only needed if token_1 is SPL)
#[account(mut)]
pub spl_interface_pda_1: Option<AccountInfo<'info>>,
}

pub fn initialize<'info>(
Expand Down Expand Up @@ -199,7 +213,9 @@ pub fn initialize<'info>(
owner: ctx.accounts.authority.key(),
}
.rent_free(
ctx.accounts.light_token_compressible_config.to_account_info(),
ctx.accounts
.light_token_compressible_config
.to_account_info(),
ctx.accounts.light_token_rent_sponsor.to_account_info(),
ctx.accounts.system_program.to_account_info(),
&crate::ID,
Expand All @@ -219,7 +235,9 @@ pub fn initialize<'info>(
owner: ctx.accounts.authority.key(),
}
.rent_free(
ctx.accounts.light_token_compressible_config.to_account_info(),
ctx.accounts
.light_token_compressible_config
.to_account_info(),
ctx.accounts.light_token_rent_sponsor.to_account_info(),
ctx.accounts.system_program.to_account_info(),
&crate::ID,
Expand All @@ -242,6 +260,8 @@ pub fn initialize<'info>(
ctx.accounts.creator.to_account_info(),
ctx.accounts.light_token_cpi_authority.to_account_info(),
ctx.accounts.system_program.to_account_info(),
ctx.accounts.spl_interface_pda_0.clone(),
params.spl_interface_bump_0,
)?;

transfer_from_user_to_pool_vault(
Expand All @@ -254,6 +274,8 @@ pub fn initialize<'info>(
ctx.accounts.creator.to_account_info(),
ctx.accounts.light_token_cpi_authority.to_account_info(),
ctx.accounts.system_program.to_account_info(),
ctx.accounts.spl_interface_pda_1.clone(),
params.spl_interface_bump_1,
)?;

// Get vault balances - supports both light token and spl token accounts
Expand Down Expand Up @@ -332,14 +354,17 @@ pub fn initialize<'info>(
}
.idempotent()
.rent_free(
ctx.accounts.light_token_compressible_config.to_account_info(),
ctx.accounts
.light_token_compressible_config
.to_account_info(),
ctx.accounts.light_token_rent_sponsor.to_account_info(),
ctx.accounts.system_program.to_account_info(),
)
.invoke()?;

// Mint LP tokens to creator
MintToCpi {
fee_payer: Some(ctx.accounts.creator.to_account_info()),
mint: ctx.accounts.lp_mint.to_account_info(),
destination: ctx.accounts.creator_lp_token.to_account_info(),
amount: user_lp_amount,
Expand Down
36 changes: 35 additions & 1 deletion programs/cp-swap/src/instructions/swap_base_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,23 @@ pub struct Swap<'info> {

/// CHECK: light_token CPI authority.
pub light_token_cpi_authority: AccountInfo<'info>,

/// Optional SPL interface PDA for token_0 (only needed if token_0 is SPL)
#[account(mut)]
pub spl_interface_pda_0: Option<AccountInfo<'info>>,

/// Optional SPL interface PDA for token_1 (only needed if token_1 is SPL)
#[account(mut)]
pub spl_interface_pda_1: Option<AccountInfo<'info>>,
}

pub fn swap_base_input(ctx: Context<Swap>, amount_in: u64, minimum_amount_out: u64) -> Result<()> {
pub fn swap_base_input(
ctx: Context<Swap>,
amount_in: u64,
minimum_amount_out: u64,
spl_interface_bump_0: Option<u8>,
spl_interface_bump_1: Option<u8>,
) -> Result<()> {
let block_timestamp = solana_program::clock::Clock::get()?.unix_timestamp as u64;
let pool_id = ctx.accounts.pool_state.key();
let pool_state = &mut ctx.accounts.pool_state;
Expand Down Expand Up @@ -233,6 +247,22 @@ pub fn swap_base_input(ctx: Context<Swap>, amount_in: u64, minimum_amount_out: u
});
require_gte!(constant_after, constant_before);

// Select SPL interface PDAs based on trade direction
let (input_spl_pda, input_spl_bump, output_spl_pda, output_spl_bump) = match trade_direction {
TradeDirection::ZeroForOne => (
ctx.accounts.spl_interface_pda_0.clone(),
spl_interface_bump_0,
ctx.accounts.spl_interface_pda_1.clone(),
spl_interface_bump_1,
),
TradeDirection::OneForZero => (
ctx.accounts.spl_interface_pda_1.clone(),
spl_interface_bump_1,
ctx.accounts.spl_interface_pda_0.clone(),
spl_interface_bump_0,
),
};

transfer_from_user_to_pool_vault(
ctx.accounts.payer.to_account_info(),
ctx.accounts.input_token_account.to_account_info(),
Expand All @@ -243,6 +273,8 @@ pub fn swap_base_input(ctx: Context<Swap>, amount_in: u64, minimum_amount_out: u
ctx.accounts.payer.to_account_info(),
ctx.accounts.light_token_cpi_authority.to_account_info(),
ctx.accounts.system_program.to_account_info(),
input_spl_pda,
input_spl_bump,
)?;

transfer_from_pool_vault_to_user(
Expand All @@ -256,6 +288,8 @@ pub fn swap_base_input(ctx: Context<Swap>, amount_in: u64, minimum_amount_out: u
ctx.accounts.payer.to_account_info(),
ctx.accounts.light_token_cpi_authority.to_account_info(),
ctx.accounts.system_program.to_account_info(),
output_spl_pda,
output_spl_bump,
)?;

// update the previous price to the observation
Expand Down
Loading