sol-trade-sdk 4.0.9

A high-performance Rust SDK for Solana DEX trading.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
use crate::{
    constants::trade::trade::DEFAULT_SLIPPAGE,
    instruction::{
        token_account_setup::{
            push_close_wsol_if_needed, push_create_or_wrap_user_token_account,
            push_create_user_token_account,
        },
        utils::raydium_amm_v4::{
            accounts, SWAP_BASE_IN_DISCRIMINATOR, SWAP_BASE_OUT_DISCRIMINATOR,
        },
    },
    trading::core::{
        params::{RaydiumAmmV4Params, SwapParams},
        traits::InstructionBuilder,
    },
    utils::calc::raydium_amm_v4::compute_swap_amount,
};
use anyhow::{anyhow, Result};
use solana_sdk::{
    instruction::{AccountMeta, Instruction},
    pubkey::Pubkey,
    signer::Signer,
};

/// Instruction builder for RaydiumCpmm protocol
pub struct RaydiumAmmV4InstructionBuilder;

fn ensure_market_accounts(params: &RaydiumAmmV4Params) -> Result<()> {
    let required = [
        ("amm_open_orders", params.amm_open_orders),
        ("amm_target_orders", params.amm_target_orders),
        ("serum_program", params.serum_program),
        ("serum_market", params.serum_market),
        ("serum_bids", params.serum_bids),
        ("serum_asks", params.serum_asks),
        ("serum_event_queue", params.serum_event_queue),
        ("serum_coin_vault_account", params.serum_coin_vault_account),
        ("serum_pc_vault_account", params.serum_pc_vault_account),
        ("serum_vault_signer", params.serum_vault_signer),
    ];

    for (name, account) in required {
        if account == Pubkey::default() {
            return Err(anyhow!(
                "Raydium AMM v4 requires {}; use RaydiumAmmV4Params::from_amm_address_by_rpc or with_market_accounts",
                name
            ));
        }
    }
    Ok(())
}

#[async_trait::async_trait]
impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
    async fn build_buy_instructions(&self, params: &SwapParams) -> Result<Vec<Instruction>> {
        // ========================================
        // Parameter validation and basic data preparation
        // ========================================
        if params.input_amount.unwrap_or(0) == 0 {
            return Err(anyhow!("Amount cannot be zero"));
        }
        let protocol_params = params
            .protocol_params
            .as_any()
            .downcast_ref::<RaydiumAmmV4Params>()
            .ok_or_else(|| anyhow!("Invalid protocol params for RaydiumAmmV4"))?;
        ensure_market_accounts(protocol_params)?;

        let is_wsol = protocol_params.coin_mint == crate::constants::WSOL_TOKEN_ACCOUNT
            || protocol_params.pc_mint == crate::constants::WSOL_TOKEN_ACCOUNT;

        let is_usdc = protocol_params.coin_mint == crate::constants::USDC_TOKEN_ACCOUNT
            || protocol_params.pc_mint == crate::constants::USDC_TOKEN_ACCOUNT;

        if !is_wsol && !is_usdc {
            return Err(anyhow!("Pool must contain WSOL or USDC"));
        }

        // ========================================
        // Trade calculation and account address preparation
        // ========================================
        let is_base_in = protocol_params.coin_mint == crate::constants::WSOL_TOKEN_ACCOUNT
            || protocol_params.coin_mint == crate::constants::USDC_TOKEN_ACCOUNT;
        let amount_in: u64 = params.input_amount.unwrap_or(0);
        let input_mint =
            if is_base_in { protocol_params.coin_mint } else { protocol_params.pc_mint };
        let output_mint =
            if is_base_in { protocol_params.pc_mint } else { protocol_params.coin_mint };

        let user_source_token_account =
            crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
                &params.payer.pubkey(),
                &input_mint,
                &crate::constants::TOKEN_PROGRAM,
                params.open_seed_optimize,
            );
        let user_destination_token_account =
            crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
                &params.payer.pubkey(),
                &output_mint,
                &crate::constants::TOKEN_PROGRAM,
                params.open_seed_optimize,
            );

        // ========================================
        // Build instructions
        // ========================================
        let mut instructions = Vec::with_capacity(6);

        if params.create_input_mint_ata {
            push_create_or_wrap_user_token_account(
                &mut instructions,
                &params.payer.pubkey(),
                &input_mint,
                &crate::constants::TOKEN_PROGRAM,
                amount_in,
                params.open_seed_optimize,
            );
        }

        if params.create_output_mint_ata {
            push_create_user_token_account(
                &mut instructions,
                &params.payer.pubkey(),
                &output_mint,
                &crate::constants::TOKEN_PROGRAM,
                params.open_seed_optimize,
            );
        }

        // Create buy instruction
        let accounts: [AccountMeta; 18] = [
            crate::constants::TOKEN_PROGRAM_META, // Token Program (readonly)
            AccountMeta::new(protocol_params.amm, false), // Amm
            accounts::AUTHORITY_META,             // Authority (readonly)
            AccountMeta::new(protocol_params.amm_open_orders, false), // Amm Open Orders
            AccountMeta::new(protocol_params.amm_target_orders, false), // Amm Target Orders
            AccountMeta::new(protocol_params.token_coin, false), // Pool Coin Token Account
            AccountMeta::new(protocol_params.token_pc, false), // Pool Pc Token Account
            AccountMeta::new_readonly(protocol_params.serum_program, false), // Serum Program
            AccountMeta::new(protocol_params.serum_market, false), // Serum Market
            AccountMeta::new(protocol_params.serum_bids, false), // Serum Bids
            AccountMeta::new(protocol_params.serum_asks, false), // Serum Asks
            AccountMeta::new(protocol_params.serum_event_queue, false), // Serum Event Queue
            AccountMeta::new(protocol_params.serum_coin_vault_account, false), // Serum Coin Vault Account
            AccountMeta::new(protocol_params.serum_pc_vault_account, false), // Serum Pc Vault Account
            AccountMeta::new_readonly(protocol_params.serum_vault_signer, false), // Serum Vault Signer
            AccountMeta::new(user_source_token_account, false), // User Source Token Account
            AccountMeta::new(user_destination_token_account, false), // User Destination Token Account
            AccountMeta::new(params.payer.pubkey(), true),           // User Source Owner
        ];
        // Create instruction data
        let mut data = [0u8; 17];
        if let Some(amount_out) = params.fixed_output_amount {
            data[..1].copy_from_slice(&SWAP_BASE_OUT_DISCRIMINATOR);
            data[1..9].copy_from_slice(&amount_in.to_le_bytes());
            data[9..17].copy_from_slice(&amount_out.to_le_bytes());
        } else {
            let minimum_amount_out = compute_swap_amount(
                protocol_params.coin_reserve,
                protocol_params.pc_reserve,
                is_base_in,
                amount_in,
                params.slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE),
            )
            .min_amount_out;
            data[..1].copy_from_slice(&SWAP_BASE_IN_DISCRIMINATOR);
            data[1..9].copy_from_slice(&amount_in.to_le_bytes());
            data[9..17].copy_from_slice(&minimum_amount_out.to_le_bytes());
        }

        instructions.push(Instruction::new_with_bytes(
            accounts::RAYDIUM_AMM_V4,
            &data,
            accounts.to_vec(),
        ));

        if params.close_input_mint_ata {
            push_close_wsol_if_needed(&mut instructions, &params.payer.pubkey(), &input_mint);
        }

        Ok(instructions)
    }

    async fn build_sell_instructions(&self, params: &SwapParams) -> Result<Vec<Instruction>> {
        // ========================================
        // Parameter validation and basic data preparation
        // ========================================
        let protocol_params = params
            .protocol_params
            .as_any()
            .downcast_ref::<RaydiumAmmV4Params>()
            .ok_or_else(|| anyhow!("Invalid protocol params for RaydiumAmmV4"))?;
        ensure_market_accounts(protocol_params)?;

        if params.input_amount.is_none() || params.input_amount.unwrap_or(0) == 0 {
            return Err(anyhow!("Token amount is not set"));
        }

        let is_wsol = protocol_params.coin_mint == crate::constants::WSOL_TOKEN_ACCOUNT
            || protocol_params.pc_mint == crate::constants::WSOL_TOKEN_ACCOUNT;

        let is_usdc = protocol_params.coin_mint == crate::constants::USDC_TOKEN_ACCOUNT
            || protocol_params.pc_mint == crate::constants::USDC_TOKEN_ACCOUNT;

        if !is_wsol && !is_usdc {
            return Err(anyhow!("Pool must contain WSOL or USDC"));
        }

        // ========================================
        // Trade calculation and account address preparation
        // ========================================
        let is_base_in = protocol_params.pc_mint == crate::constants::WSOL_TOKEN_ACCOUNT
            || protocol_params.pc_mint == crate::constants::USDC_TOKEN_ACCOUNT;
        let input_mint =
            if is_base_in { protocol_params.coin_mint } else { protocol_params.pc_mint };
        let output_mint =
            if is_base_in { protocol_params.pc_mint } else { protocol_params.coin_mint };

        let user_source_token_account =
            crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
                &params.payer.pubkey(),
                &input_mint,
                &crate::constants::TOKEN_PROGRAM,
                params.open_seed_optimize,
            );
        let user_destination_token_account =
            crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
                &params.payer.pubkey(),
                &output_mint,
                &crate::constants::TOKEN_PROGRAM,
                params.open_seed_optimize,
            );

        // ========================================
        // Build instructions
        // ========================================
        let mut instructions = Vec::with_capacity(4);

        if params.create_output_mint_ata {
            push_create_user_token_account(
                &mut instructions,
                &params.payer.pubkey(),
                &output_mint,
                &crate::constants::TOKEN_PROGRAM,
                params.open_seed_optimize,
            );
        }

        // Create buy instruction
        let accounts: [AccountMeta; 18] = [
            crate::constants::TOKEN_PROGRAM_META, // Token Program (readonly)
            AccountMeta::new(protocol_params.amm, false), // Amm
            accounts::AUTHORITY_META,             // Authority (readonly)
            AccountMeta::new(protocol_params.amm_open_orders, false), // Amm Open Orders
            AccountMeta::new(protocol_params.amm_target_orders, false), // Amm Target Orders
            AccountMeta::new(protocol_params.token_coin, false), // Pool Coin Token Account
            AccountMeta::new(protocol_params.token_pc, false), // Pool Pc Token Account
            AccountMeta::new_readonly(protocol_params.serum_program, false), // Serum Program
            AccountMeta::new(protocol_params.serum_market, false), // Serum Market
            AccountMeta::new(protocol_params.serum_bids, false), // Serum Bids
            AccountMeta::new(protocol_params.serum_asks, false), // Serum Asks
            AccountMeta::new(protocol_params.serum_event_queue, false), // Serum Event Queue
            AccountMeta::new(protocol_params.serum_coin_vault_account, false), // Serum Coin Vault Account
            AccountMeta::new(protocol_params.serum_pc_vault_account, false), // Serum Pc Vault Account
            AccountMeta::new_readonly(protocol_params.serum_vault_signer, false), // Serum Vault Signer
            AccountMeta::new(user_source_token_account, false), // User Source Token Account
            AccountMeta::new(user_destination_token_account, false), // User Destination Token Account
            AccountMeta::new(params.payer.pubkey(), true),           // User Source Owner
        ];
        // Create instruction data
        let mut data = [0u8; 17];
        let amount_in = params.input_amount.unwrap_or(0);
        if let Some(amount_out) = params.fixed_output_amount {
            data[..1].copy_from_slice(&SWAP_BASE_OUT_DISCRIMINATOR);
            data[1..9].copy_from_slice(&amount_in.to_le_bytes());
            data[9..17].copy_from_slice(&amount_out.to_le_bytes());
        } else {
            let minimum_amount_out = compute_swap_amount(
                protocol_params.coin_reserve,
                protocol_params.pc_reserve,
                is_base_in,
                amount_in,
                params.slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE),
            )
            .min_amount_out;
            data[..1].copy_from_slice(&SWAP_BASE_IN_DISCRIMINATOR);
            data[1..9].copy_from_slice(&amount_in.to_le_bytes());
            data[9..17].copy_from_slice(&minimum_amount_out.to_le_bytes());
        }

        instructions.push(Instruction::new_with_bytes(
            accounts::RAYDIUM_AMM_V4,
            &data,
            accounts.to_vec(),
        ));

        if params.close_output_mint_ata {
            push_close_wsol_if_needed(&mut instructions, &params.payer.pubkey(), &output_mint);
        }
        if params.close_input_mint_ata {
            instructions.push(crate::common::spl_token::close_account(
                &crate::constants::TOKEN_PROGRAM,
                &user_source_token_account,
                &params.payer.pubkey(),
                &params.payer.pubkey(),
                &[&params.payer.pubkey()],
            )?);
        }

        Ok(instructions)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        common::GasFeeStrategy,
        swqos::TradeType,
        trading::core::params::{DexParamEnum, SwapParams},
    };
    use solana_sdk::{pubkey::Pubkey, signature::Keypair};
    use std::sync::Arc;

    fn pk(seed: u8) -> Pubkey {
        Pubkey::new_from_array([seed; 32])
    }

    fn market_params() -> RaydiumAmmV4Params {
        RaydiumAmmV4Params::new(
            pk(1),
            crate::constants::WSOL_TOKEN_ACCOUNT,
            pk(2),
            pk(3),
            pk(4),
            1_000_000_000,
            2_000_000_000,
        )
        .with_market_accounts(
            pk(5),
            pk(6),
            pk(7),
            pk(8),
            pk(9),
            pk(10),
            pk(11),
            pk(12),
            pk(13),
            pk(14),
        )
    }

    fn swap_params(
        protocol_params: RaydiumAmmV4Params,
        fixed_output_amount: Option<u64>,
    ) -> SwapParams {
        SwapParams {
            rpc: None,
            payer: Arc::new(Keypair::new()),
            trade_type: TradeType::Buy,
            input_mint: crate::constants::WSOL_TOKEN_ACCOUNT,
            input_token_program: None,
            output_mint: pk(2),
            output_token_program: None,
            input_amount: Some(100_000),
            slippage_basis_points: Some(100),
            address_lookup_table_account: None,
            recent_blockhash: None,
            wait_tx_confirmed: false,
            protocol_params: DexParamEnum::RaydiumAmmV4(protocol_params),
            open_seed_optimize: true,
            swqos_clients: Arc::new(Vec::new()),
            middleware_manager: None,
            durable_nonce: None,
            with_tip: false,
            create_input_mint_ata: false,
            close_input_mint_ata: false,
            create_output_mint_ata: false,
            close_output_mint_ata: false,
            fixed_output_amount,
            gas_fee_strategy: GasFeeStrategy::new(),
            simulate: true,
            log_enabled: false,
            use_dedicated_sender_threads: false,
            sender_thread_cores: None,
            max_sender_concurrency: 0,
            effective_core_ids: Arc::new(Vec::new()),
            check_min_tip: false,
            grpc_recv_us: None,
            use_exact_sol_amount: None,
            use_pumpfun_v2: false,
        }
    }

    #[tokio::test]
    async fn raydium_amm_v4_uses_idl_market_account_order() {
        let instructions = RaydiumAmmV4InstructionBuilder
            .build_buy_instructions(&swap_params(market_params(), None))
            .await
            .unwrap();
        let ix = instructions.last().unwrap();

        assert_eq!(ix.accounts.len(), 18);
        assert_eq!(&ix.data[..1], SWAP_BASE_IN_DISCRIMINATOR);
        assert_eq!(ix.accounts[3].pubkey, pk(5));
        assert_eq!(ix.accounts[4].pubkey, pk(6));
        assert_eq!(ix.accounts[7].pubkey, pk(7));
        assert_eq!(ix.accounts[8].pubkey, pk(8));
        assert_eq!(ix.accounts[9].pubkey, pk(9));
        assert_eq!(ix.accounts[10].pubkey, pk(10));
        assert_eq!(ix.accounts[11].pubkey, pk(11));
        assert_eq!(ix.accounts[12].pubkey, pk(12));
        assert_eq!(ix.accounts[13].pubkey, pk(13));
        assert_eq!(ix.accounts[14].pubkey, pk(14));
        assert!(!ix.accounts[7].is_writable);
        assert!(!ix.accounts[14].is_writable);
    }

    #[tokio::test]
    async fn raydium_amm_v4_uses_base_out_when_fixed_output_is_set() {
        let instructions = RaydiumAmmV4InstructionBuilder
            .build_buy_instructions(&swap_params(market_params(), Some(42)))
            .await
            .unwrap();
        let ix = instructions.last().unwrap();

        assert_eq!(&ix.data[..1], SWAP_BASE_OUT_DISCRIMINATOR);
        assert_eq!(u64::from_le_bytes(ix.data[1..9].try_into().unwrap()), 100_000);
        assert_eq!(u64::from_le_bytes(ix.data[9..17].try_into().unwrap()), 42);
    }

    #[tokio::test]
    async fn raydium_amm_v4_rejects_placeholder_market_accounts() {
        let err = RaydiumAmmV4InstructionBuilder
            .build_buy_instructions(&swap_params(
                RaydiumAmmV4Params::new(
                    pk(1),
                    crate::constants::WSOL_TOKEN_ACCOUNT,
                    pk(2),
                    pk(3),
                    pk(4),
                    1_000_000_000,
                    2_000_000_000,
                ),
                None,
            ))
            .await
            .unwrap_err();
        assert!(err.to_string().contains("amm_open_orders"));
    }

    #[tokio::test]
    async fn raydium_amm_v4_usdc_buy_create_input_builds_usdc_ata() {
        let mut protocol_params = market_params();
        protocol_params.coin_mint = crate::constants::USDC_TOKEN_ACCOUNT;

        let mut params = swap_params(protocol_params, Some(42));
        params.input_mint = crate::constants::USDC_TOKEN_ACCOUNT;
        params.create_input_mint_ata = true;
        params.open_seed_optimize = false;

        let instructions =
            RaydiumAmmV4InstructionBuilder.build_buy_instructions(&params).await.unwrap();
        let create_ix = instructions.first().unwrap();

        assert_eq!(create_ix.program_id, crate::constants::ASSOCIATED_TOKEN_PROGRAM_ID);
        assert_eq!(create_ix.accounts[3].pubkey, crate::constants::USDC_TOKEN_ACCOUNT);
    }
}