raydium_cp_swap/
lib.rs

1pub mod curve;
2pub mod error;
3pub mod instructions;
4pub mod states;
5pub mod utils;
6
7use anchor_lang::prelude::*;
8use instructions::*;
9
10#[cfg(not(feature = "no-entrypoint"))]
11solana_security_txt::security_txt! {
12    name: "raydium-cp-swap",
13    project_url: "https://raydium.io",
14    contacts: "link:https://immunefi.com/bounty/raydium",
15    policy: "https://immunefi.com/bounty/raydium",
16    source_code: "https://github.com/raydium-io/raydium-cp-swap",
17    preferred_languages: "en",
18    auditors: "https://github.com/raydium-io/raydium-docs/blob/master/audit/MadShield%20Q1%202024/raydium-cp-swap-v-1.0.0.pdf"
19}
20
21#[cfg(feature = "devnet")]
22declare_id!("CPMDWBwJDtYax9qW7AyRuVC19Cc4L4Vcy4n2BHAbHkCW");
23#[cfg(not(feature = "devnet"))]
24declare_id!("CVF4q3yFpyQwV8DLDiJ9Ew6FFLE1vr5ToRzsXYQTaNrj");
25
26pub mod admin {
27    use anchor_lang::prelude::declare_id;
28    #[cfg(feature = "devnet")]
29    declare_id!("adMCyoCgfkg7bQiJ9aBJ59H3BXLY3r5LNLfPpQfMzBe");
30    #[cfg(not(feature = "devnet"))]
31    declare_id!("99VXriv7RXJSypeJDBQtGRsak1n5o2NBzbtMXhHW2RNG");
32}
33
34pub const AUTH_SEED: &str = "vault_and_lp_mint_auth_seed";
35
36#[program]
37pub mod raydium_cp_swap {
38    use super::*;
39
40    // The configuation of AMM protocol, include trade fee and protocol fee
41    /// # Arguments
42    ///
43    /// * `ctx`- The accounts needed by instruction.
44    /// * `index` - The index of amm config, there may be multiple config.
45    /// * `trade_fee_rate` - Trade fee rate, can be changed.
46    /// * `protocol_fee_rate` - The rate of protocol fee within tarde fee.
47    /// * `fund_fee_rate` - The rate of fund fee within tarde fee.
48    ///
49    pub fn create_amm_config(
50        ctx: Context<CreateAmmConfig>,
51        index: u64,
52        token_1_lp_rate: u64,
53        token_0_lp_rate: u64,
54        token_0_creator_rate: u64,
55        token_1_creator_rate: u64,
56    ) -> Result<()> {
57        instructions::create_amm_config(
58            ctx,
59            index,
60            token_1_lp_rate,
61            token_0_lp_rate,
62            token_0_creator_rate,
63            token_1_creator_rate,
64        )
65    }
66
67    /// Updates the owner of the amm config
68    /// Must be called by the current owner or admin
69    ///
70    /// # Arguments
71    ///
72    /// * `ctx`- The context of accounts
73    /// * `trade_fee_rate`- The new trade fee rate of amm config, be set when `param` is 0
74    /// * `protocol_fee_rate`- The new protocol fee rate of amm config, be set when `param` is 1
75    /// * `fund_fee_rate`- The new fund fee rate of amm config, be set when `param` is 2
76    /// * `new_owner`- The config's new owner, be set when `param` is 3
77    /// * `new_fund_owner`- The config's new fund owner, be set when `param` is 4
78    /// * `param`- The vaule can be 0 | 1 | 2 | 3 | 4, otherwise will report a error
79    ///
80
81    /// Update pool status for given vaule
82    ///
83    /// # Arguments
84    ///
85    /// * `ctx`- The context of accounts
86    /// * `status` - The v
87
88    /// Collect the protocol fee accrued to the pool
89    ///
90    /// # Arguments
91    ///
92    /// * `ctx` - The context of accounts
93    /// * `amount_0_requested` - The maximum amount of token_0 to send, can be 0 to collect fees in only token_1
94    /// * `amount_1_requested` - The maximum amount of token_1 to send, can be 0 to collect fees in only token_0
95    ///
96    pub fn collect_protocol_fee(
97        ctx: Context<CollectProtocolFee>,
98        amount_0_requested: u64,
99        amount_1_requested: u64,
100    ) -> Result<()> {
101        instructions::collect_protocol_fee(ctx, amount_0_requested, amount_1_requested)
102    }
103
104    /// Collect the fund fee accrued to the pool
105    ///
106    /// # Arguments
107    ///
108    /// * `ctx` - The context of accounts
109    /// * `amount_0_requested` - The maximum amount of token_0 to send, can be 0 to collect fees in only token_1
110    /// * `amount_1_requested` - The maximum amount of token_1 to send, can be 0 to collect fees in only token_0
111    ///
112    pub fn collect_fund_fee(
113        ctx: Context<CollectFundFee>,
114        amount_0_requested: u64,
115        amount_1_requested: u64,
116    ) -> Result<()> {
117        instructions::collect_fund_fee(ctx, amount_0_requested, amount_1_requested)
118    }
119
120    /// Creates a pool for the given token pair and the initial price
121    ///
122    /// # Arguments
123    ///
124    /// * `ctx`- The context of accounts
125    /// * `init_amount_0` - the initial amount_0 to deposit
126    /// * `init_amount_1` - the initial amount_1 to deposit
127    /// * `open_time` - the timestamp allowed for swap
128    ///
129    pub fn initialize(
130        ctx: Context<Initialize>,
131        init_amount_0: u64,
132        init_amount_1: u64,
133        open_time: u64,
134    ) -> Result<()> {
135        instructions::initialize(ctx, init_amount_0, init_amount_1, open_time)
136    }
137    /// Initialize metadata for the LP token
138    ///
139    /// # Arguments
140    ///
141    /// * `ctx` - The context of accounts
142    /// * `name` - The name of the LP token
143    /// * `symbol` - The symbol of the LP token
144    /// * `uri` - The URI for the LP token metadata
145    ///
146    pub fn initialize_metadata(
147        ctx: Context<InitializeMetadata>,
148        name: String,
149        symbol: String,
150        uri: String,
151    ) -> Result<()> {
152        instructions::initialize_metadata(ctx, name, symbol, uri)
153    }
154
155    /// Creates a pool for the given token pair and the initial price
156    ///
157    /// # Arguments
158    ///
159    /// * `ctx`- The context of accounts
160    /// * `lp_token_amount` - Pool token amount to transfer. token_a and token_b amount are set by the current exchange rate and size of the pool
161    /// * `maximum_token_0_amount` -  Maximum token 0 amount to deposit, prevents excessive slippage
162    /// * `maximum_token_1_amount` - Maximum token 1 amount to deposit, prevents excessive slippage
163    ///
164    pub fn deposit<'info>(
165        ctx: Context<'_, '_, '_, 'info, Deposit<'info>>,
166        lp_token_amount: u64,
167        maximum_token_0_amount: u64,
168        maximum_token_1_amount: u64,
169    ) -> Result<()> {
170        instructions::deposit(
171            ctx,
172            lp_token_amount,
173            maximum_token_0_amount,
174            maximum_token_1_amount,
175        )
176    }
177
178    /// Withdraw lp for token0 ande token1
179    ///
180    /// # Arguments
181    ///
182    /// * `ctx`- The context of accounts
183    /// * `lp_token_amount` - Amount of pool tokens to burn. User receives an output of token a and b based on the percentage of the pool tokens that are returned.
184    /// * `minimum_token_0_amount` -  Minimum amount of token 0 to receive, prevents excessive slippage
185    /// * `minimum_token_1_amount` -  Minimum amount of token 1 to receive, prevents excessive slippage
186    ///
187    pub fn withdraw<'info>(
188        ctx: Context<'_, '_, '_, 'info, Withdraw<'info>>,
189        lp_token_amount: u64,
190        minimum_token_0_amount: u64,
191        minimum_token_1_amount: u64,
192    ) -> Result<()> {
193        instructions::withdraw(
194            ctx,
195            lp_token_amount,
196            minimum_token_0_amount,
197            minimum_token_1_amount,
198        )
199    }
200
201    /// Swap the tokens in the pool base input amount
202    ///
203    /// # Arguments
204    ///
205    /// * `ctx`- The context of accounts
206    /// * `amount_in` -  input amount to transfer, output to DESTINATION is based on the exchange rate
207    /// * `minimum_amount_out` -  Minimum amount of output token, prevents excessive slippage
208    ///
209    pub fn swap_base_input(
210        ctx: Context<Swap>,
211        amount_in: u64,
212        minimum_amount_out: u64,
213    ) -> Result<()> {
214        instructions::swap_base_input(ctx, amount_in, minimum_amount_out)
215    }
216
217    /// Swap the tokens in the pool base output amount
218    ///
219    /// # Arguments
220    ///
221    /// * `ctx`- The context of accounts
222    /// * `max_amount_in` -  input amount prevents excessive slippage
223    /// * `amount_out` -  amount of output token
224    ///
225    pub fn swap_base_output(ctx: Context<Swap>, max_amount_in: u64, amount_out: u64) -> Result<()> {
226        instructions::swap_base_output(ctx, max_amount_in, amount_out)
227    }
228}