sol-trade-sdk 6.0.2

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
use crate::instruction::utils::raydium_cpmm::accounts::{
    CREATOR_FEE_RATE, FEE_RATE_DENOMINATOR_VALUE, FUND_FEE_RATE, PROTOCOL_FEE_RATE, TRADE_FEE_RATE,
};

use super::common::calculate_min_amount_out;
use crate::trading::core::params::RaydiumCpmmParams;

/// Computes trading fee using ceiling division.
///
/// # Arguments
/// * `amount` - The amount to calculate fee for
/// * `fee_rate` - The fee rate to apply
///
/// # Returns
/// The calculated trading fee
#[inline(always)]
fn compute_trading_fee(amount: u64, fee_rate: u64) -> u64 {
    let numerator = (amount as u128) * (fee_rate as u128);
    ((numerator + FEE_RATE_DENOMINATOR_VALUE - 1) / FEE_RATE_DENOMINATOR_VALUE) as u64
}

/// Computes protocol or fund fee using floor division.
///
/// # Arguments
/// * `amount` - The amount to calculate fee for
/// * `fee_rate` - The fee rate to apply
///
/// # Returns
/// The calculated protocol or fund fee
#[inline(always)]
fn compute_protocol_fund_fee(amount: u64, fee_rate: u64) -> u64 {
    let numerator = (amount as u128) * (fee_rate as u128);
    (numerator / FEE_RATE_DENOMINATOR_VALUE) as u64
}

/// Computes creator fee using ceiling division.
///
/// # Arguments
/// * `amount` - The amount to calculate fee for
/// * `fee_rate` - The fee rate to apply
///
/// # Returns
/// The calculated creator fee
#[inline(always)]
fn compute_creator_fee_new(amount: u64, fee_rate: u64) -> u64 {
    let numerator = (amount as u128) * (fee_rate as u128);
    ((numerator + FEE_RATE_DENOMINATOR_VALUE - 1) / FEE_RATE_DENOMINATOR_VALUE) as u64
}

/// Parameters for computing swap amounts and fees.
#[derive(Debug, Clone)]
pub struct ComputeSwapParams {
    /// Whether the entire input amount is traded
    pub all_trade: bool,
    /// The input amount for the swap
    pub amount_in: u64,
    /// The expected output amount from the swap
    pub amount_out: u64,
    /// The minimum acceptable output amount (considering slippage_basis_points)
    pub min_amount_out: u64,
    /// The trading fee amount
    pub fee: u64,
}

/// Result of a swap calculation containing all relevant amounts and fees.
#[derive(Debug, Clone)]
pub struct SwapResult {
    /// The new amount in the input vault after the swap
    pub new_input_vault_amount: u64,
    /// The new amount in the output vault after the swap
    pub new_output_vault_amount: u64,
    /// The actual input amount used in the swap
    pub input_amount: u64,
    /// The actual output amount received from the swap
    pub output_amount: u64,
    /// The trading fee charged
    pub trade_fee: u64,
    /// The protocol fee charged
    pub protocol_fee: u64,
    /// The fund fee charged
    pub fund_fee: u64,
    /// The creator fee charged
    pub creator_fee: u64,
}

/// Performs a swap calculation based on input amount.
///
/// Calculates the output amount and all associated fees when swapping a specific input amount.
///
/// # Arguments
/// * `input_amount` - The amount of input tokens to swap
/// * `input_vault_amount` - Current amount in the input token vault
/// * `output_vault_amount` - Current amount in the output token vault
/// * `trade_fee_rate` - The trading fee rate
/// * `creator_fee_rate` - The creator fee rate
/// * `protocol_fee_rate` - The protocol fee rate
/// * `fund_fee_rate` - The fund fee rate
/// * `is_creator_fee_on_input` - Whether creator fee is charged on input tokens
///
/// # Returns
/// A `SwapResult` containing all swap calculations and fees
#[inline]
fn swap_base_input(
    input_amount: u64,
    input_vault_amount: u64,
    output_vault_amount: u64,
    trade_fee_rate: u64,
    creator_fee_rate: u64,
    protocol_fee_rate: u64,
    fund_fee_rate: u64,
    is_creator_fee_on_input: bool,
) -> SwapResult {
    let mut creator_fee = 0u64;
    let trade_fee: u64;

    let input_amount_less_fees = if is_creator_fee_on_input {
        let total_fee_rate = trade_fee_rate.saturating_add(creator_fee_rate);
        let total_fee = compute_trading_fee(input_amount, total_fee_rate);
        creator_fee = if total_fee_rate == 0 {
            0
        } else {
            ((total_fee as u128) * (creator_fee_rate as u128) / (total_fee_rate as u128)) as u64
        };
        trade_fee = total_fee.saturating_sub(creator_fee);
        input_amount.saturating_sub(total_fee)
    } else {
        trade_fee = compute_trading_fee(input_amount, trade_fee_rate);
        input_amount.saturating_sub(trade_fee)
    };

    let protocol_fee = compute_protocol_fund_fee(trade_fee, protocol_fee_rate);
    let fund_fee = compute_protocol_fund_fee(trade_fee, fund_fee_rate);

    // The legacy infallible API represents an empty pool as a zero quote;
    // the validated pool API rejects it before reaching this calculation.
    let denominator = u128::from(input_vault_amount) + u128::from(input_amount_less_fees);
    let output_amount_swapped = (u128::from(output_vault_amount)
        * u128::from(input_amount_less_fees))
    .checked_div(denominator)
    .unwrap_or(0) as u64;

    let output_amount = if is_creator_fee_on_input {
        output_amount_swapped
    } else {
        creator_fee = compute_creator_fee_new(output_amount_swapped, creator_fee_rate);
        output_amount_swapped.saturating_sub(creator_fee)
    };

    SwapResult {
        new_input_vault_amount: input_vault_amount.saturating_add(input_amount_less_fees),
        new_output_vault_amount: output_vault_amount.saturating_sub(output_amount_swapped),
        input_amount,
        output_amount,
        trade_fee,
        protocol_fee,
        fund_fee,
        creator_fee,
    }
}

/// Computes swap parameters including amounts, fees, and slippage protection.
///
/// This function calculates the expected output amount, minimum output amount (with slippage),
/// and trading fees for a given input amount in a CPMM (Constant Product Market Maker) pool.
///
/// # Arguments
/// * `base_reserve` - The current reserve amount of the base token in the pool
/// * `quote_reserve` - The current reserve amount of the quote token in the pool  
/// * `is_base_in` - Whether the input token is the base token (true) or quote token (false)
/// * `amount_in` - The amount of input tokens to swap
/// * `slippage_basis_points` - The acceptable slippage in basis points (e.g., 100 for 1%)
///
/// # Returns
/// A `ComputeSwapParams` struct containing all computed swap parameters
#[inline]
pub fn compute_swap_amount(
    base_reserve: u64,
    quote_reserve: u64,
    is_base_in: bool,
    amount_in: u64,
    slippage_basis_points: u64,
) -> ComputeSwapParams {
    let (input_reserve, output_reserve) =
        if is_base_in { (base_reserve, quote_reserve) } else { (quote_reserve, base_reserve) };

    let swap_result = swap_base_input(
        amount_in,
        input_reserve,
        output_reserve,
        TRADE_FEE_RATE,
        CREATOR_FEE_RATE,
        PROTOCOL_FEE_RATE,
        FUND_FEE_RATE,
        true,
    );

    let min_amount_out = calculate_min_amount_out(swap_result.output_amount, slippage_basis_points);

    let all_trade = swap_result.input_amount == amount_in;

    ComputeSwapParams {
        all_trade,
        amount_in,
        amount_out: swap_result.output_amount,
        min_amount_out,
        fee: swap_result.trade_fee,
    }
}

/// Computes an exact-input quote using the current on-chain CPMM config,
/// accrued-fee-adjusted reserves, creator fee mode, and Token-2022 fees.
pub fn compute_swap_amount_for_pool(
    protocol_params: &RaydiumCpmmParams,
    is_base_in: bool,
    amount_in: u64,
    slippage_basis_points: u64,
) -> Result<ComputeSwapParams, anyhow::Error> {
    if amount_in == 0 || protocol_params.base_reserve == 0 || protocol_params.quote_reserve == 0 {
        return Err(anyhow::anyhow!("Raydium CPMM amount and pool reserves must be nonzero"));
    }
    let creator_fee_rate =
        if protocol_params.enable_creator_fee { protocol_params.creator_fee_rate } else { 0 };
    let total_input_fee_rate = protocol_params
        .trade_fee_rate
        .checked_add(creator_fee_rate)
        .ok_or_else(|| anyhow::anyhow!("Raydium CPMM fee rate overflow"))?;
    if protocol_params.trade_fee_rate > FEE_RATE_DENOMINATOR_VALUE as u64
        || creator_fee_rate > FEE_RATE_DENOMINATOR_VALUE as u64
        || total_input_fee_rate >= FEE_RATE_DENOMINATOR_VALUE as u64
        || protocol_params.protocol_fee_rate > FEE_RATE_DENOMINATOR_VALUE as u64
        || protocol_params.fund_fee_rate > FEE_RATE_DENOMINATOR_VALUE as u64
        || !protocol_params
            .protocol_fee_rate
            .checked_add(protocol_params.fund_fee_rate)
            .is_some_and(|rate| rate <= FEE_RATE_DENOMINATOR_VALUE as u64)
        || protocol_params.base_transfer_fee.basis_points > 10_000
        || protocol_params.quote_transfer_fee.basis_points > 10_000
    {
        return Err(anyhow::anyhow!("Invalid Raydium CPMM fee configuration"));
    }
    let (input_reserve, output_reserve, input_transfer_fee, output_transfer_fee) = if is_base_in {
        (
            protocol_params.base_reserve,
            protocol_params.quote_reserve,
            protocol_params.base_transfer_fee,
            protocol_params.quote_transfer_fee,
        )
    } else {
        (
            protocol_params.quote_reserve,
            protocol_params.base_reserve,
            protocol_params.quote_transfer_fee,
            protocol_params.base_transfer_fee,
        )
    };
    let is_creator_fee_on_input = match protocol_params.creator_fee_on {
        0 => true,
        1 => is_base_in,
        2 => !is_base_in,
        value => return Err(anyhow::anyhow!("Invalid Raydium CPMM creator fee mode: {}", value)),
    };
    let actual_amount_in = amount_in
        .checked_sub(input_transfer_fee.calculate(amount_in))
        .filter(|amount| *amount > 0)
        .ok_or_else(|| anyhow::anyhow!("Raydium CPMM transfer fee consumes the input"))?;
    // The curve uses u128, but SPL token account balances are u64. Reserves
    // exclude accrued fees and are only a lower bound on the vault balance:
    // overflow here necessarily makes the incoming transfer impossible. Use
    // the received amount, before swap fees, rather than the gross transfer.
    input_reserve
        .checked_add(actual_amount_in)
        .ok_or_else(|| anyhow::anyhow!("Raydium CPMM input vault balance overflow"))?;
    let swap_result = swap_base_input(
        actual_amount_in,
        input_reserve,
        output_reserve,
        protocol_params.trade_fee_rate,
        creator_fee_rate,
        protocol_params.protocol_fee_rate,
        protocol_params.fund_fee_rate,
        is_creator_fee_on_input,
    );
    let received_amount = swap_result
        .output_amount
        .saturating_sub(output_transfer_fee.calculate(swap_result.output_amount));

    if received_amount == 0 {
        return Err(anyhow::anyhow!("Raydium CPMM swap produces zero output"));
    }
    Ok(ComputeSwapParams {
        all_trade: true,
        amount_in,
        amount_out: received_amount,
        min_amount_out: calculate_min_amount_out(received_amount, slippage_basis_points),
        fee: swap_result.trade_fee,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::utils::calc::common::{calculate_min_amount_out, MAX_SLIPPAGE_BASIS_POINTS};

    fn pool_params() -> RaydiumCpmmParams {
        use solana_sdk::pubkey::Pubkey;
        RaydiumCpmmParams::from_trade(
            Pubkey::new_unique(),
            Pubkey::new_unique(),
            Pubkey::new_unique(),
            Pubkey::new_unique(),
            Pubkey::new_unique(),
            Pubkey::new_unique(),
            crate::constants::TOKEN_PROGRAM,
            crate::constants::TOKEN_PROGRAM,
            Pubkey::new_unique(),
            1_000_000,
            2_000_000,
        )
    }

    #[test]
    fn empty_legacy_quote_does_not_panic_and_validated_quotes_reject_empty_inputs() {
        assert_eq!(compute_swap_amount(0, 0, true, 0, 0).amount_out, 0);
        assert_eq!(compute_swap_amount(0, 100, true, 1, 0).amount_out, 0);
        for base_in in [false, true] {
            for (base, quote, amount) in [(0, 100, 100), (100, 0, 100), (100, 100, 0)] {
                let mut params = pool_params();
                params.base_reserve = base;
                params.quote_reserve = quote;
                assert!(compute_swap_amount_for_pool(&params, base_in, amount, 0).is_err());
            }
            assert!(compute_swap_amount_for_pool(&pool_params(), base_in, 1, 0).is_err());
        }
    }

    #[test]
    fn validated_quotes_reject_invalid_fee_rates_and_fully_taxed_input() {
        use crate::trading::core::params::TokenTransferFee;
        let mut params = pool_params();
        params.trade_fee_rate = 1_000_000;
        assert!(compute_swap_amount_for_pool(&params, true, 100, 0).is_err());
        params = pool_params();
        params.protocol_fee_rate = 600_000;
        params.fund_fee_rate = 500_000;
        assert!(compute_swap_amount_for_pool(&params, true, 100, 0).is_err());
        params = pool_params();
        params.base_transfer_fee = TokenTransferFee { basis_points: 10_000, maximum_fee: 100 };
        assert!(compute_swap_amount_for_pool(&params, true, 100, 0).is_err());
        // A capped 100% transfer fee can still leave a valid positive input.
        assert!(compute_swap_amount_for_pool(&params, true, 1000, 0).is_ok());
        params.quote_transfer_fee.basis_points = 10_001;
        assert!(compute_swap_amount_for_pool(&params, true, 1000, 0).is_err());
        let quote = compute_swap_amount_for_pool(&pool_params(), true, 1000, 10_000).unwrap();
        assert!(quote.amount_out > 0);
        assert_eq!(quote.min_amount_out, 0);
    }

    #[test]
    fn validated_quote_checks_received_input_balance_capacity_in_both_directions() {
        use crate::trading::core::params::TokenTransferFee;
        for base_in in [false, true] {
            let mut params = pool_params();
            params.base_reserve = u64::MAX;
            params.quote_reserve = u64::MAX;
            let error = compute_swap_amount_for_pool(&params, base_in, 1_000, 0).unwrap_err();
            assert!(error.to_string().contains("input vault balance overflow"));
            // Even when the curve's post-swap-fee input fits, the token vault
            // receives the full post-transfer-fee input before booking fees.
            if base_in {
                params.base_reserve = u64::MAX - 999;
            } else {
                params.quote_reserve = u64::MAX - 999;
            }
            assert!(compute_swap_amount_for_pool(&params, base_in, 1_000, 0).is_err());
            let fee = TokenTransferFee { basis_points: 1_000, maximum_fee: 100 };
            if base_in {
                params.base_reserve = u64::MAX - 900;
                params.base_transfer_fee = fee;
            } else {
                params.quote_reserve = u64::MAX - 900;
                params.quote_transfer_fee = fee;
            }
            // Gross input overflows reserve + input, but the net 900 fits exactly.
            assert!(compute_swap_amount_for_pool(&params, base_in, 1_000, 0).is_ok());
            if base_in {
                params.base_reserve += 1;
            } else {
                params.quote_reserve += 1;
            }
            assert!(compute_swap_amount_for_pool(&params, base_in, 1_000, 0).is_err());
        }
    }

    #[test]
    fn min_amount_out_uses_exact_integer_slippage() {
        let no_slippage = compute_swap_amount(u64::MAX, u64::MAX, true, u64::MAX, 0);
        let one_percent = compute_swap_amount(u64::MAX, u64::MAX, true, u64::MAX, 100);

        assert_eq!(one_percent.amount_out, no_slippage.amount_out);
        assert_eq!(
            one_percent.min_amount_out,
            calculate_min_amount_out(one_percent.amount_out, 100)
        );
    }

    #[test]
    fn min_amount_out_rounds_down_after_applying_slippage() {
        assert_eq!(calculate_min_amount_out(101, 100), 99);
    }

    #[test]
    fn excessive_slippage_is_clamped_without_underflow() {
        let excessive = compute_swap_amount(1_000_000, 2_000_000, true, 100_000, u64::MAX);
        let clamped =
            compute_swap_amount(1_000_000, 2_000_000, true, 100_000, MAX_SLIPPAGE_BASIS_POINTS);

        assert_eq!(excessive.min_amount_out, clamped.min_amount_out);
        assert_eq!(
            excessive.min_amount_out,
            calculate_min_amount_out(excessive.amount_out, MAX_SLIPPAGE_BASIS_POINTS)
        );
    }

    #[test]
    fn creator_fee_on_input_matches_official_combined_fee_rounding() {
        let result = swap_base_input(101, 1_000_000, 2_000_000, 2_500, 10_000, 0, 0, true);

        // ceil(101 * 12_500 / 1_000_000) is 2. Splitting that combined fee
        // yields creator=1 and trade=1; separately ceiling each fee would charge 3.
        assert_eq!(result.creator_fee, 1);
        assert_eq!(result.trade_fee, 1);
        assert_eq!(result.new_input_vault_amount, 1_000_099);
    }
}