fusionamm-core 1.0.80

FusionAMM core rust package.
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
//
// Copyright (c) Cryptic Dot
//
// Modification based on Orca Whirlpools (https://github.com/orca-so/whirlpools),
// originally licensed under the Apache License, Version 2.0, prior to February 26, 2025.
//
// Modifications licensed under FusionAMM SDK Source-Available License v1.0
// See the LICENSE file in the project root for license information.
//

use crate::{
    CoreError, TransferFee, AMOUNT_EXCEEDS_MAX_U64, ARITHMETIC_OVERFLOW, BPS_DENOMINATOR, FEE_RATE_MUL_VALUE, INVALID_SLIPPAGE_TOLERANCE,
    INVALID_TRANSFER_FEE, MAX_SQRT_PRICE, MIN_SQRT_PRICE, SQRT_PRICE_OUT_OF_BOUNDS,
};

use crate::math::liquidity::{get_amount_a_from_liquidity, get_amount_b_from_liquidity};
use crate::math::u256_math::{mul_u256, U256Muldiv};
use ethnum::U256;
#[cfg(feature = "wasm")]
use fusionamm_macros::wasm_expose;

/// Calculate the amount A delta between two sqrt_prices
///
/// # Parameters
/// - `sqrt_price_1`: The first square root price
/// - `sqrt_price_2`: The second square root price
/// - `liquidity`: The liquidity
/// - `round_up`: Whether to round up or not
///
/// # Returns
/// - `u64`: The amount delta
#[cfg_attr(feature = "wasm", wasm_expose)]
pub fn try_get_amount_delta_a(sqrt_price_1: u128, sqrt_price_2: u128, liquidity: u128, round_up: bool) -> Result<u64, CoreError> {
    let (sqrt_price_lower, sqrt_price_upper) = order_prices(sqrt_price_1, sqrt_price_2);
    get_amount_a_from_liquidity(liquidity, sqrt_price_lower, sqrt_price_upper, round_up)
}

/// Calculate the amount B delta between two sqrt_prices
///
/// # Parameters
/// - `sqrt_price_1`: The first square root price
/// - `sqrt_price_2`: The second square root price
/// - `liquidity`: The liquidity
/// - `round_up`: Whether to round up or not
///
/// # Returns
/// - `u64`: The amount delta
#[cfg_attr(feature = "wasm", wasm_expose)]
pub fn try_get_amount_delta_b(sqrt_price_1: u128, sqrt_price_2: u128, liquidity: u128, round_up: bool) -> Result<u64, CoreError> {
    let (sqrt_price_lower, sqrt_price_upper) = order_prices(sqrt_price_1, sqrt_price_2);
    get_amount_b_from_liquidity(liquidity, sqrt_price_lower, sqrt_price_upper, round_up)
}

/// Calculate the next square root price
///
/// # Parameters
/// - `current_sqrt_price`: The current square root price
/// - `current_liquidity`: The current liquidity
/// - `amount`: The amount
/// - `specified_input`: Whether the input is specified
///
/// # Returns
/// - `u128`: The next square root price
#[cfg_attr(feature = "wasm", wasm_expose)]
pub fn try_get_next_sqrt_price_from_a(
    current_sqrt_price: u128,
    current_liquidity: u128,
    amount: u64,
    specified_input: bool,
) -> Result<u128, CoreError> {
    if amount == 0 {
        return Ok(current_sqrt_price);
    }

    let p = <U256>::from(current_sqrt_price).checked_mul(amount.into()).ok_or(ARITHMETIC_OVERFLOW)?;
    let numerator = <U256>::from(current_liquidity)
        .checked_mul(current_sqrt_price.into())
        .ok_or(ARITHMETIC_OVERFLOW)?
        .checked_shl(64)
        .ok_or(ARITHMETIC_OVERFLOW)?;

    let current_liquidity_shifted = <U256>::from(current_liquidity).checked_shl(64).ok_or(ARITHMETIC_OVERFLOW)?;
    let denominator = if specified_input {
        current_liquidity_shifted + p
    } else {
        current_liquidity_shifted - p
    };

    let quotient: U256 = numerator / denominator;
    let remainder: U256 = numerator % denominator;

    let result = if remainder != 0 { quotient + 1 } else { quotient };

    if !(MIN_SQRT_PRICE..=MAX_SQRT_PRICE).contains(&result) {
        return Err(SQRT_PRICE_OUT_OF_BOUNDS);
    }

    Ok(result.as_u128())
}

/// Calculate the next square root price
///
/// # Parameters
/// - `current_sqrt_price`: The current square root price
/// - `current_liquidity`: The current liquidity
/// - `amount`: The amount
/// - `specified_input`: Whether the input is specified
///
/// # Returns
/// - `u128`: The next square root price
#[cfg_attr(feature = "wasm", wasm_expose)]
pub fn try_get_next_sqrt_price_from_b(
    current_sqrt_price: u128,
    current_liquidity: u128,
    amount: u64,
    specified_input: bool,
) -> Result<u128, CoreError> {
    if amount == 0 {
        return Ok(current_sqrt_price);
    }
    let current_sqrt_price = <U256>::from(current_sqrt_price);
    let current_liquidity = <U256>::from(current_liquidity);
    let amount_shifted = <U256>::from(amount).checked_shl(64).ok_or(ARITHMETIC_OVERFLOW)?;

    let quotient: U256 = amount_shifted / current_liquidity;
    let remainder: U256 = amount_shifted % current_liquidity;

    let delta = if !specified_input && remainder != 0 { quotient + 1 } else { quotient };

    let result = if specified_input {
        current_sqrt_price + delta
    } else {
        current_sqrt_price - delta
    };

    if !(MIN_SQRT_PRICE..=MAX_SQRT_PRICE).contains(&result) {
        return Err(SQRT_PRICE_OUT_OF_BOUNDS);
    }

    Ok(result.as_u128())
}

/// Apply a transfer fee to an amount
/// e.g. You send 10000 amount with 100 fee rate. The fee amount will be 100.
/// So the amount after fee will be 9900.
///
/// # Parameters
/// - `amount`: The amount to apply the fee to
/// - `transfer_fee`: The transfer fee to apply
///
/// # Returns
/// - `u64`: The amount after the fee has been applied
#[cfg_attr(feature = "wasm", wasm_expose)]
pub fn try_apply_transfer_fee(amount: u64, transfer_fee: TransferFee) -> Result<u64, CoreError> {
    if transfer_fee.fee_bps > BPS_DENOMINATOR {
        return Err(INVALID_TRANSFER_FEE);
    }
    if transfer_fee.fee_bps == 0 || amount == 0 {
        return Ok(amount);
    }
    let numerator = <u128>::from(amount).checked_mul(transfer_fee.fee_bps.into()).ok_or(ARITHMETIC_OVERFLOW)?;
    let raw_fee: u64 = numerator
        .div_ceil(BPS_DENOMINATOR.into())
        .try_into()
        .map_err(|_| AMOUNT_EXCEEDS_MAX_U64)?;
    let fee_amount = raw_fee.min(transfer_fee.max_fee);
    Ok(amount - fee_amount)
}

/// Reverse the application of a transfer fee to an amount
/// e.g. You received 9900 amount with 100 fee rate. The fee amount will be 100.
/// So the amount before fee will be 10000.
///
/// # Parameters
/// - `amount`: The amount to reverse the fee from
/// - `transfer_fee`: The transfer fee to reverse
///
/// # Returns
/// - `u64`: The amount before the fee has been applied
#[cfg_attr(feature = "wasm", wasm_expose)]
pub fn try_reverse_apply_transfer_fee(amount: u64, transfer_fee: TransferFee) -> Result<u64, CoreError> {
    if transfer_fee.fee_bps > BPS_DENOMINATOR {
        Err(INVALID_TRANSFER_FEE)
    } else if transfer_fee.fee_bps == 0 {
        Ok(amount)
    } else if amount == 0 {
        Ok(0)
    } else if transfer_fee.fee_bps == BPS_DENOMINATOR {
        amount.checked_add(transfer_fee.max_fee).ok_or(AMOUNT_EXCEEDS_MAX_U64)
    } else {
        let numerator = <u128>::from(amount).checked_mul(BPS_DENOMINATOR.into()).ok_or(ARITHMETIC_OVERFLOW)?;
        let denominator = <u128>::from(BPS_DENOMINATOR) - <u128>::from(transfer_fee.fee_bps);
        let raw_pre_fee_amount = numerator.div_ceil(denominator);
        let fee_amount = raw_pre_fee_amount.checked_sub(amount.into()).ok_or(AMOUNT_EXCEEDS_MAX_U64)?;
        if fee_amount >= transfer_fee.max_fee as u128 {
            amount.checked_add(transfer_fee.max_fee).ok_or(AMOUNT_EXCEEDS_MAX_U64)
        } else {
            raw_pre_fee_amount.try_into().map_err(|_| AMOUNT_EXCEEDS_MAX_U64)
        }
    }
}

/// Get the maximum amount with a slippage tolerance
/// e.g. Your estimated amount you send is 10000 with 100 slippage tolerance. The max you send will be 10100.
///
/// # Parameters
/// - `amount`: The amount to apply the fee to
/// - `slippage_tolerance_bps`: The slippage tolerance in bps (should be in range 0..BPS_DENOMINATOR)
///
/// # Returns
/// - `u64`: The maximum amount
#[cfg_attr(feature = "wasm", wasm_expose)]
pub fn try_get_max_amount_with_slippage_tolerance(amount: u64, slippage_tolerance_bps: u16) -> Result<u64, CoreError> {
    if slippage_tolerance_bps > BPS_DENOMINATOR {
        return Err(INVALID_SLIPPAGE_TOLERANCE);
    }
    let product = <u128>::from(BPS_DENOMINATOR) + <u128>::from(slippage_tolerance_bps);
    let result = try_mul_div(amount, product, BPS_DENOMINATOR.into(), true)?;
    Ok(result)
}

/// Get the minimum amount with a slippage tolerance
/// e.g. Your estimated amount you receive is 10000 with 100 slippage tolerance. The min amount you receive will be 9900.
///
/// # Parameters
/// - `amount`: The amount to apply the fee to
/// - `slippage_tolerance_bps`: The slippage tolerance in bps (should be in range 0..BPS_DENOMINATOR)
///
/// # Returns
/// - `u64`: The minimum amount
#[cfg_attr(feature = "wasm", wasm_expose)]
pub fn try_get_min_amount_with_slippage_tolerance(amount: u64, slippage_tolerance_bps: u16) -> Result<u64, CoreError> {
    if slippage_tolerance_bps > BPS_DENOMINATOR {
        return Err(INVALID_SLIPPAGE_TOLERANCE);
    }
    let product = <u128>::from(BPS_DENOMINATOR) - <u128>::from(slippage_tolerance_bps);
    let result = try_mul_div(amount, product, BPS_DENOMINATOR.into(), false)?;
    Ok(result)
}

/// Apply a swap fee to an amount
/// e.g. You send 10000 amount with 10000 fee rate. The fee amount will be 100.
/// So the amount after fee will be 9900.
///
/// # Parameters
/// - `amount`: The amount to apply the fee to
/// - `fee_rate`: The fee rate to apply denominated in 1e6
///
/// # Returns
/// - `u64`: The amount after the fee has been applied
#[cfg_attr(feature = "wasm", wasm_expose)]
pub fn try_apply_swap_fee(amount: u64, fee_rate: u16) -> Result<u64, CoreError> {
    let product = <u128>::from(FEE_RATE_MUL_VALUE) - <u128>::from(fee_rate);
    let result = try_mul_div(amount, product, FEE_RATE_MUL_VALUE.into(), false)?;
    Ok(result)
}

/// Reverse the application of a swap fee to an amount
/// e.g. You received 9900 amount with 10000 fee rate. The fee amount will be 100.
/// So the amount before fee will be 10000.
///
/// # Parameters
/// - `amount`: The amount to reverse the fee from
/// - `fee_rate`: The fee rate to reverse denominated in 1e6
///
/// # Returns
/// - `u64`: The amount before the fee has been applied
#[cfg_attr(feature = "wasm", wasm_expose)]
pub fn try_reverse_apply_swap_fee(amount: u64, fee_rate: u16) -> Result<u64, CoreError> {
    let denominator = <u128>::from(FEE_RATE_MUL_VALUE) - <u128>::from(fee_rate);
    let result = try_mul_div(amount, FEE_RATE_MUL_VALUE.into(), denominator, true)?;
    Ok(result)
}

pub fn try_mul_div(amount: u64, product: u128, denominator: u128, round_up: bool) -> Result<u64, CoreError> {
    if amount == 0 || product == 0 {
        return Ok(0);
    }

    let amount: u128 = amount.into();
    let numerator = amount.checked_mul(product).ok_or(ARITHMETIC_OVERFLOW)?;
    let quotient = numerator / denominator;
    let remainder = numerator % denominator;

    let result = if round_up && remainder != 0 { quotient + 1 } else { quotient };

    result.try_into().map_err(|_| AMOUNT_EXCEEDS_MAX_U64)
}

pub fn mul_by_sqrt_price_squared(amount: u64, sqrt_price: u128, round_up: bool) -> Result<u64, CoreError> {
    let price = mul_u256(sqrt_price, sqrt_price).shift_word_right();
    let value_u256 = U256Muldiv::new(0, amount as u128).mul(price);
    let value_u128 = if round_up && value_u256.get_word(0) > 0 {
        value_u256.shift_word_right().try_into_u128()? + 1
    } else {
        value_u256.shift_word_right().try_into_u128()?
    };
    u64::try_from(value_u128).or(Err(ARITHMETIC_OVERFLOW))
}

pub fn div_by_sqrt_price_squared(amount: u64, sqrt_price: u128, round_up: bool) -> Result<u64, CoreError> {
    let price = mul_u256(sqrt_price, sqrt_price);
    let (value_u256, reminder_u256) = U256Muldiv::new(amount as u128, 0).div(price, true);
    let value_u128 = if round_up && reminder_u256.gt(U256Muldiv::new(0, 0)) {
        value_u256.try_into_u128()? + 1
    } else {
        value_u256.try_into_u128()?
    };
    u64::try_from(value_u128).or(Err(ARITHMETIC_OVERFLOW))
}

// Private functions

fn order_prices(a: u128, b: u128) -> (u128, u128) {
    if a < b {
        (a, b)
    } else {
        (b, a)
    }
}

#[cfg(all(test, not(feature = "wasm")))]
mod tests {
    use super::*;

    #[test]
    fn test_get_amount_delta_a() {
        assert_eq!(try_get_amount_delta_a(4 << 64, 2 << 64, 4, true), Ok(1));
        assert_eq!(try_get_amount_delta_a(4 << 64, 2 << 64, 4, false), Ok(1));

        assert_eq!(try_get_amount_delta_a(4 << 64, 4 << 64, 4, true), Ok(0));
        assert_eq!(try_get_amount_delta_a(4 << 64, 4 << 64, 4, false), Ok(0));
    }

    #[test]
    fn test_get_amount_delta_b() {
        assert_eq!(try_get_amount_delta_b(4 << 64, 2 << 64, 4, true), Ok(8));
        assert_eq!(try_get_amount_delta_b(4 << 64, 2 << 64, 4, false), Ok(8));

        assert_eq!(try_get_amount_delta_b(4 << 64, 4 << 64, 4, true), Ok(0));
        assert_eq!(try_get_amount_delta_b(4 << 64, 4 << 64, 4, false), Ok(0));
    }

    #[test]
    fn test_get_next_sqrt_price_from_a() {
        assert_eq!(try_get_next_sqrt_price_from_a(4 << 64, 4, 1, true), Ok(2 << 64));
        assert_eq!(try_get_next_sqrt_price_from_a(2 << 64, 4, 1, false), Ok(4 << 64));

        assert_eq!(try_get_next_sqrt_price_from_a(4 << 64, 4, 0, true), Ok(4 << 64));
        assert_eq!(try_get_next_sqrt_price_from_a(4 << 64, 4, 0, false), Ok(4 << 64));
    }

    #[test]
    fn test_get_next_sqrt_price_from_b() {
        assert_eq!(try_get_next_sqrt_price_from_b(2 << 64, 4, 8, true), Ok(4 << 64));
        assert_eq!(try_get_next_sqrt_price_from_b(4 << 64, 4, 8, false), Ok(2 << 64));

        assert_eq!(try_get_next_sqrt_price_from_b(4 << 64, 4, 0, true), Ok(4 << 64));
        assert_eq!(try_get_next_sqrt_price_from_b(4 << 64, 4, 0, false), Ok(4 << 64));
    }

    #[test]
    fn test_apply_transfer_fee() {
        assert_eq!(try_apply_transfer_fee(0, TransferFee::new(100)), Ok(0));
        assert_eq!(try_apply_transfer_fee(10000, TransferFee::new(0)), Ok(10000));
        assert_eq!(try_apply_transfer_fee(10000, TransferFee::new(100)), Ok(9900));
        assert_eq!(try_apply_transfer_fee(10000, TransferFee::new(1000)), Ok(9000));
        assert_eq!(try_apply_transfer_fee(10000, TransferFee::new(10000)), Ok(0));
        assert_eq!(try_apply_transfer_fee(u64::MAX, TransferFee::new(1000)), Ok(16602069666338596453));
        assert_eq!(try_apply_transfer_fee(u64::MAX, TransferFee::new(10000)), Ok(0));
        assert_eq!(try_apply_transfer_fee(10000, TransferFee::new(10001)), Err(INVALID_TRANSFER_FEE));
        assert_eq!(try_apply_transfer_fee(10000, TransferFee::new(u16::MAX)), Err(INVALID_TRANSFER_FEE));
    }

    #[test]
    fn test_apply_transfer_fee_with_max() {
        assert_eq!(try_apply_transfer_fee(0, TransferFee::new_with_max(100, 500)), Ok(0));
        assert_eq!(try_apply_transfer_fee(10000, TransferFee::new_with_max(0, 500)), Ok(10000));
        assert_eq!(try_apply_transfer_fee(10000, TransferFee::new_with_max(100, 500)), Ok(9900));
        assert_eq!(try_apply_transfer_fee(10000, TransferFee::new_with_max(1000, 500)), Ok(9500));
        assert_eq!(try_apply_transfer_fee(10000, TransferFee::new_with_max(10000, 500)), Ok(9500));
        assert_eq!(try_apply_transfer_fee(u64::MAX, TransferFee::new_with_max(1000, 500)), Ok(18446744073709551115));
        assert_eq!(try_apply_transfer_fee(u64::MAX, TransferFee::new_with_max(10000, 500)), Ok(18446744073709551115));
        assert_eq!(try_apply_transfer_fee(10000, TransferFee::new_with_max(10001, 500)), Err(INVALID_TRANSFER_FEE));
        assert_eq!(try_apply_transfer_fee(10000, TransferFee::new_with_max(u16::MAX, 500)), Err(INVALID_TRANSFER_FEE));
    }

    #[test]
    fn test_reverse_apply_transfer_fee() {
        assert_eq!(try_reverse_apply_transfer_fee(0, TransferFee::new(100)), Ok(0));
        assert_eq!(try_reverse_apply_transfer_fee(10000, TransferFee::new(0)), Ok(10000));
        assert_eq!(try_reverse_apply_transfer_fee(9900, TransferFee::new(100)), Ok(10000));
        assert_eq!(try_reverse_apply_transfer_fee(9000, TransferFee::new(1000)), Ok(10000));
        assert_eq!(try_reverse_apply_transfer_fee(5000, TransferFee::new(10000)), Err(AMOUNT_EXCEEDS_MAX_U64));
        assert_eq!(try_reverse_apply_transfer_fee(0, TransferFee::new(10000)), Ok(0));
        assert_eq!(try_reverse_apply_transfer_fee(u64::MAX, TransferFee::new(10000)), Err(AMOUNT_EXCEEDS_MAX_U64));
        assert_eq!(try_reverse_apply_transfer_fee(10000, TransferFee::new(10001)), Err(INVALID_TRANSFER_FEE));
        assert_eq!(try_reverse_apply_transfer_fee(10000, TransferFee::new(u16::MAX)), Err(INVALID_TRANSFER_FEE));
    }

    #[test]
    fn test_reverse_apply_transfer_fee_with_max() {
        assert_eq!(try_reverse_apply_transfer_fee(0, TransferFee::new_with_max(100, 500)), Ok(0));
        assert_eq!(try_reverse_apply_transfer_fee(10000, TransferFee::new_with_max(0, 500)), Ok(10000));
        assert_eq!(try_reverse_apply_transfer_fee(9900, TransferFee::new_with_max(100, 500)), Ok(10000));
        assert_eq!(try_reverse_apply_transfer_fee(9500, TransferFee::new_with_max(1000, 500)), Ok(10000));
        assert_eq!(try_reverse_apply_transfer_fee(9500, TransferFee::new_with_max(10000, 500)), Ok(10000));
        assert_eq!(try_reverse_apply_transfer_fee(0, TransferFee::new_with_max(10000, 500)), Ok(0));
        assert_eq!(try_reverse_apply_transfer_fee(u64::MAX - 500, TransferFee::new_with_max(10000, 500)), Ok(u64::MAX));
        assert_eq!(try_reverse_apply_transfer_fee(u64::MAX, TransferFee::new_with_max(10000, 500)), Err(AMOUNT_EXCEEDS_MAX_U64));
        assert_eq!(try_reverse_apply_transfer_fee(10000, TransferFee::new_with_max(10001, 500)), Err(INVALID_TRANSFER_FEE));
        assert_eq!(try_reverse_apply_transfer_fee(10000, TransferFee::new_with_max(u16::MAX, 500)), Err(INVALID_TRANSFER_FEE));
    }

    #[test]
    fn test_get_max_amount_with_slippage_tolerance() {
        assert_eq!(try_get_max_amount_with_slippage_tolerance(0, 100), Ok(0));
        assert_eq!(try_get_max_amount_with_slippage_tolerance(10000, 0), Ok(10000));
        assert_eq!(try_get_max_amount_with_slippage_tolerance(10000, 100), Ok(10100));
        assert_eq!(try_get_max_amount_with_slippage_tolerance(10000, 1000), Ok(11000));
        assert_eq!(try_get_max_amount_with_slippage_tolerance(10000, 10000), Ok(20000));
        assert_eq!(try_get_max_amount_with_slippage_tolerance(u64::MAX, 10000), Err(AMOUNT_EXCEEDS_MAX_U64));
        assert_eq!(try_get_max_amount_with_slippage_tolerance(10000, 10001), Err(INVALID_SLIPPAGE_TOLERANCE));
        assert_eq!(try_get_max_amount_with_slippage_tolerance(10000, u16::MAX), Err(INVALID_SLIPPAGE_TOLERANCE));
    }

    #[test]
    fn test_get_min_amount_with_slippage_tolerance() {
        assert_eq!(try_get_min_amount_with_slippage_tolerance(0, 100), Ok(0));
        assert_eq!(try_get_min_amount_with_slippage_tolerance(10000, 0), Ok(10000));
        assert_eq!(try_get_min_amount_with_slippage_tolerance(10000, 100), Ok(9900));
        assert_eq!(try_get_min_amount_with_slippage_tolerance(10000, 1000), Ok(9000));
        assert_eq!(try_get_min_amount_with_slippage_tolerance(10000, 10000), Ok(0));
        assert_eq!(try_get_min_amount_with_slippage_tolerance(u64::MAX, 10000), Ok(0));
        assert_eq!(try_get_min_amount_with_slippage_tolerance(u64::MAX, 1000), Ok(16602069666338596453));
        assert_eq!(try_get_min_amount_with_slippage_tolerance(10000, 10001), Err(INVALID_SLIPPAGE_TOLERANCE));
        assert_eq!(try_get_min_amount_with_slippage_tolerance(10000, u16::MAX), Err(INVALID_SLIPPAGE_TOLERANCE));
    }

    #[test]
    fn test_apply_swap_fee() {
        assert_eq!(try_apply_swap_fee(0, 1000), Ok(0));
        assert_eq!(try_apply_swap_fee(10000, 0), Ok(10000));
        assert_eq!(try_apply_swap_fee(10000, 1000), Ok(9990));
        assert_eq!(try_apply_swap_fee(10000, 10000), Ok(9900));
        assert_eq!(try_apply_swap_fee(10000, u16::MAX), Ok(9344));
        assert_eq!(try_apply_swap_fee(u64::MAX, 1000), Ok(18428297329635842063));
        assert_eq!(try_apply_swap_fee(u64::MAX, 10000), Ok(18262276632972456098));
    }

    #[test]
    fn test_reverse_apply_swap_fee() {
        assert_eq!(try_reverse_apply_swap_fee(0, 1000), Ok(0));
        assert_eq!(try_reverse_apply_swap_fee(10000, 0), Ok(10000));
        assert_eq!(try_reverse_apply_swap_fee(9990, 1000), Ok(10000));
        assert_eq!(try_reverse_apply_swap_fee(9900, 10000), Ok(10000));
        assert_eq!(try_reverse_apply_swap_fee(9344, u16::MAX), Ok(10000));
        assert_eq!(try_reverse_apply_swap_fee(u64::MAX, 1000), Err(AMOUNT_EXCEEDS_MAX_U64));
        assert_eq!(try_reverse_apply_swap_fee(u64::MAX, 10000), Err(AMOUNT_EXCEEDS_MAX_U64));
    }
}