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
use crate::{
    instruction::{
        token_account_setup::{
            push_close_wsol_if_needed, push_create_or_wrap_user_token_account,
            push_create_user_token_account,
        },
        utils::meteora_damm_v2::{
            accounts, get_event_authority_pda, SWAP2_DISCRIMINATOR, SWAP_MODE_EXACT_IN,
            SWAP_MODE_EXACT_OUT, SWAP_MODE_PARTIAL_FILL,
        },
    },
    trading::core::{
        params::{MeteoraDammV2Params, SwapParams},
        traits::InstructionBuilder,
    },
};
use anyhow::{anyhow, Result};
use solana_sdk::{
    instruction::{AccountMeta, Instruction},
    signer::Signer,
};

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

#[async_trait::async_trait]
impl InstructionBuilder for MeteoraDammV2InstructionBuilder {
    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::<MeteoraDammV2Params>()
            .ok_or_else(|| anyhow!("Invalid protocol params for MeteoraDammV2"))?;

        let is_wsol = protocol_params.token_a_mint == crate::constants::WSOL_TOKEN_ACCOUNT
            || protocol_params.token_b_mint == crate::constants::WSOL_TOKEN_ACCOUNT;
        let is_usdc = protocol_params.token_a_mint == crate::constants::USDC_TOKEN_ACCOUNT
            || protocol_params.token_b_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_a_in = protocol_params.token_a_mint == crate::constants::WSOL_TOKEN_ACCOUNT
            || protocol_params.token_a_mint == crate::constants::USDC_TOKEN_ACCOUNT;
        let input_mint =
            if is_a_in { protocol_params.token_a_mint } else { protocol_params.token_b_mint };
        let input_token_program =
            if is_a_in { protocol_params.token_a_program } else { protocol_params.token_b_program };
        let output_mint =
            if is_a_in { protocol_params.token_b_mint } else { protocol_params.token_a_mint };
        let output_token_program =
            if is_a_in { protocol_params.token_b_program } else { protocol_params.token_a_program };
        let amount_in: u64 = params.input_amount.unwrap_or(0);
        let (amount_0, amount_1) = match protocol_params.swap_mode {
            SWAP_MODE_EXACT_OUT => {
                let amount_out = params.fixed_output_amount.ok_or_else(|| {
                    anyhow!("fixed_output_amount must be set for MeteoraDammV2 exact-out swap2")
                })?;
                (amount_out, amount_in)
            }
            SWAP_MODE_EXACT_IN | SWAP_MODE_PARTIAL_FILL => {
                let minimum_amount_out = params.fixed_output_amount.ok_or_else(|| {
                    anyhow!("fixed_output_amount must be set for MeteoraDammV2 swap2 min output")
                })?;
                (amount_in, minimum_amount_out)
            }
            mode => return Err(anyhow!("Unsupported MeteoraDammV2 swap_mode {}", mode)),
        };

        let input_token_account =
            crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
                &params.payer.pubkey(),
                &input_mint,
                &input_token_program,
                params.open_seed_optimize,
            );
        let output_token_account =
            crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
                &params.payer.pubkey(),
                &output_mint,
                &output_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,
                &input_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,
                &output_token_program,
                params.open_seed_optimize,
            );
        }

        // Create buy instruction
        let mut account_metas = Vec::with_capacity(
            13 + usize::from(protocol_params.referral_token_account.is_some())
                + usize::from(protocol_params.include_rate_limiter_sysvar),
        );
        account_metas.extend([
            accounts::AUTHORITY_META,                      // Pool Authority (readonly)
            AccountMeta::new(protocol_params.pool, false), // Pool
            AccountMeta::new(input_token_account, false),  // Input Token Account
            AccountMeta::new(output_token_account, false), // Output Token Account
            AccountMeta::new(protocol_params.token_a_vault, false), // Token A Vault
            AccountMeta::new(protocol_params.token_b_vault, false), // Token B Vault
            AccountMeta::new_readonly(protocol_params.token_a_mint, false), // Token A Mint (readonly)
            AccountMeta::new_readonly(protocol_params.token_b_mint, false), // Token B Mint (readonly)
            AccountMeta::new(params.payer.pubkey(), true), // User Transfer Authority
            AccountMeta::new_readonly(protocol_params.token_a_program, false), // Token Program (readonly)
            AccountMeta::new_readonly(protocol_params.token_b_program, false), // Token Program (readonly)
        ]);
        if let Some(referral_token_account) = protocol_params.referral_token_account {
            account_metas.push(AccountMeta::new(referral_token_account, false));
        }
        account_metas.extend([
            AccountMeta::new_readonly(get_event_authority_pda(), false), // Event Authority (readonly)
            accounts::METEORA_DAMM_V2_META,                              // Program (readonly)
        ]);
        if protocol_params.include_rate_limiter_sysvar {
            account_metas.push(accounts::SYSVAR_INSTRUCTIONS_META);
        }
        // Create instruction data
        let mut data = [0u8; 25];
        data[..8].copy_from_slice(&SWAP2_DISCRIMINATOR);
        data[8..16].copy_from_slice(&amount_0.to_le_bytes());
        data[16..24].copy_from_slice(&amount_1.to_le_bytes());
        data[24] = protocol_params.swap_mode;

        instructions.push(Instruction::new_with_bytes(
            accounts::METEORA_DAMM_V2,
            &data,
            account_metas,
        ));

        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::<MeteoraDammV2Params>()
            .ok_or_else(|| anyhow!("Invalid protocol params for MeteoraDammV2"))?;

        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.token_b_mint == crate::constants::WSOL_TOKEN_ACCOUNT
            || protocol_params.token_a_mint == crate::constants::WSOL_TOKEN_ACCOUNT;
        let is_usdc = protocol_params.token_b_mint == crate::constants::USDC_TOKEN_ACCOUNT
            || protocol_params.token_a_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_a_in = protocol_params.token_b_mint == crate::constants::WSOL_TOKEN_ACCOUNT
            || protocol_params.token_b_mint == crate::constants::USDC_TOKEN_ACCOUNT;
        let input_mint =
            if is_a_in { protocol_params.token_a_mint } else { protocol_params.token_b_mint };
        let input_token_program =
            if is_a_in { protocol_params.token_a_program } else { protocol_params.token_b_program };
        let output_mint =
            if is_a_in { protocol_params.token_b_mint } else { protocol_params.token_a_mint };
        let output_token_program =
            if is_a_in { protocol_params.token_b_program } else { protocol_params.token_a_program };
        let amount_in = params.input_amount.unwrap_or(0);
        let (amount_0, amount_1) = match protocol_params.swap_mode {
            SWAP_MODE_EXACT_OUT => {
                let amount_out = params.fixed_output_amount.ok_or_else(|| {
                    anyhow!("fixed_output_amount must be set for MeteoraDammV2 exact-out swap2")
                })?;
                (amount_out, amount_in)
            }
            SWAP_MODE_EXACT_IN | SWAP_MODE_PARTIAL_FILL => {
                let minimum_amount_out = params.fixed_output_amount.ok_or_else(|| {
                    anyhow!("fixed_output_amount must be set for MeteoraDammV2 swap2 min output")
                })?;
                (amount_in, minimum_amount_out)
            }
            mode => return Err(anyhow!("Unsupported MeteoraDammV2 swap_mode {}", mode)),
        };

        let input_token_account =
            crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
                &params.payer.pubkey(),
                &input_mint,
                &input_token_program,
                params.open_seed_optimize,
            );
        let output_token_account =
            crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
                &params.payer.pubkey(),
                &output_mint,
                &output_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,
                &output_token_program,
                params.open_seed_optimize,
            );
        }

        // Create buy instruction
        let mut account_metas = Vec::with_capacity(
            13 + usize::from(protocol_params.referral_token_account.is_some())
                + usize::from(protocol_params.include_rate_limiter_sysvar),
        );
        account_metas.extend([
            accounts::AUTHORITY_META,                      // Pool Authority (readonly)
            AccountMeta::new(protocol_params.pool, false), // Pool
            AccountMeta::new(input_token_account, false),  // Input Token Account
            AccountMeta::new(output_token_account, false), // Output Token Account
            AccountMeta::new(protocol_params.token_a_vault, false), // Token A Vault
            AccountMeta::new(protocol_params.token_b_vault, false), // Token B Vault
            AccountMeta::new_readonly(protocol_params.token_a_mint, false), // Token A Mint (readonly)
            AccountMeta::new_readonly(protocol_params.token_b_mint, false), // Token B Mint (readonly)
            AccountMeta::new(params.payer.pubkey(), true), // User Transfer Authority
            AccountMeta::new_readonly(protocol_params.token_a_program, false), // Token Program (readonly)
            AccountMeta::new_readonly(protocol_params.token_b_program, false), // Token Program (readonly)
        ]);
        if let Some(referral_token_account) = protocol_params.referral_token_account {
            account_metas.push(AccountMeta::new(referral_token_account, false));
        }
        account_metas.extend([
            AccountMeta::new_readonly(get_event_authority_pda(), false), // Event Authority (readonly)
            accounts::METEORA_DAMM_V2_META,                              // Program (readonly)
        ]);
        if protocol_params.include_rate_limiter_sysvar {
            account_metas.push(accounts::SYSVAR_INSTRUCTIONS_META);
        }
        // Create instruction data
        let mut data = [0u8; 25];
        data[..8].copy_from_slice(&SWAP2_DISCRIMINATOR);
        data[8..16].copy_from_slice(&amount_0.to_le_bytes());
        data[16..24].copy_from_slice(&amount_1.to_le_bytes());
        data[24] = protocol_params.swap_mode;

        instructions.push(Instruction::new_with_bytes(
            accounts::METEORA_DAMM_V2,
            &data,
            account_metas,
        ));

        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(
                &input_token_program,
                &input_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 meteora_params(referral: Option<Pubkey>) -> MeteoraDammV2Params {
        let params = MeteoraDammV2Params::new(
            pk(1),
            pk(2),
            pk(3),
            crate::constants::WSOL_TOKEN_ACCOUNT,
            pk(4),
            crate::constants::TOKEN_PROGRAM,
            crate::constants::TOKEN_PROGRAM,
        );
        match referral {
            Some(account) => params.with_referral_token_account(account),
            None => params,
        }
    }

    fn swap_params(protocol_params: MeteoraDammV2Params) -> 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(4),
            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::MeteoraDammV2(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: Some(1),
            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 meteora_omits_optional_referral_account() {
        let instructions = MeteoraDammV2InstructionBuilder
            .build_buy_instructions(&swap_params(meteora_params(None)))
            .await
            .unwrap();
        let ix = instructions.last().unwrap();

        assert_eq!(ix.accounts.len(), 13);
        assert_eq!(ix.accounts[11].pubkey, get_event_authority_pda());
        assert_eq!(ix.accounts[12].pubkey, accounts::METEORA_DAMM_V2);
        assert_eq!(&ix.data[..8], SWAP2_DISCRIMINATOR);
        assert_eq!(ix.data[24], SWAP_MODE_PARTIAL_FILL);
    }

    #[tokio::test]
    async fn meteora_includes_writable_referral_account_when_set() {
        let referral = pk(9);
        let instructions = MeteoraDammV2InstructionBuilder
            .build_buy_instructions(&swap_params(meteora_params(Some(referral))))
            .await
            .unwrap();
        let ix = instructions.last().unwrap();

        assert_eq!(ix.accounts.len(), 14);
        assert_eq!(ix.accounts[11].pubkey, referral);
        assert!(ix.accounts[11].is_writable);
        assert_eq!(ix.accounts[12].pubkey, get_event_authority_pda());
        assert_eq!(ix.accounts[13].pubkey, accounts::METEORA_DAMM_V2);
    }

    #[tokio::test]
    async fn meteora_includes_sysvar_only_when_rate_limiter_is_set() {
        let protocol_params = meteora_params(None).with_rate_limiter_sysvar(true);
        let instructions = MeteoraDammV2InstructionBuilder
            .build_buy_instructions(&swap_params(protocol_params))
            .await
            .unwrap();
        let ix = instructions.last().unwrap();

        assert_eq!(ix.accounts.len(), 14);
        assert_eq!(ix.accounts[11].pubkey, get_event_authority_pda());
        assert_eq!(ix.accounts[12].pubkey, accounts::METEORA_DAMM_V2);
        assert_eq!(ix.accounts[13].pubkey, accounts::SYSVAR_INSTRUCTIONS);
    }

    #[tokio::test]
    async fn meteora_swap2_exact_out_uses_amount_out_then_max_input() {
        let protocol_params = meteora_params(None).with_swap_mode(SWAP_MODE_EXACT_OUT);
        let instructions = MeteoraDammV2InstructionBuilder
            .build_buy_instructions(&swap_params(protocol_params))
            .await
            .unwrap();
        let ix = instructions.last().unwrap();

        assert_eq!(&ix.data[..8], SWAP2_DISCRIMINATOR);
        assert_eq!(u64::from_le_bytes(ix.data[8..16].try_into().unwrap()), 1);
        assert_eq!(u64::from_le_bytes(ix.data[16..24].try_into().unwrap()), 100_000);
        assert_eq!(ix.data[24], SWAP_MODE_EXACT_OUT);
    }

    #[tokio::test]
    async fn meteora_sol_buy_uses_pool_wsol_mint_for_user_input_account() {
        let mut params = swap_params(meteora_params(None));
        params.input_mint = crate::constants::SOL_TOKEN_ACCOUNT;

        let instructions =
            MeteoraDammV2InstructionBuilder.build_buy_instructions(&params).await.unwrap();
        let ix = instructions.last().unwrap();
        let expected_wsol_ata =
            crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
                &params.payer.pubkey(),
                &crate::constants::WSOL_TOKEN_ACCOUNT,
                &crate::constants::TOKEN_PROGRAM,
                params.open_seed_optimize,
            );
        let wrong_sol_ata =
            crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
                &params.payer.pubkey(),
                &crate::constants::SOL_TOKEN_ACCOUNT,
                &crate::constants::TOKEN_PROGRAM,
                params.open_seed_optimize,
            );

        assert_eq!(ix.accounts[2].pubkey, expected_wsol_ata);
        assert_ne!(ix.accounts[2].pubkey, wrong_sol_ata);
    }
}