fusionamm-core 1.0.34

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
//
// 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::math::get_limit_order_output_amount;
use crate::{
    tick_index_to_sqrt_price, try_apply_swap_fee, try_mul_div, try_reverse_apply_swap_fee, CoreError, FusionPoolFacade, LimitOrderDecreaseQuote,
    LimitOrderFacade, TickFacade, AMOUNT_EXCEEDS_LIMIT_ORDER_INPUT_AMOUNT, AMOUNT_EXCEEDS_MAX_U64, FEE_RATE_DENOMINATOR,
    LIMIT_ORDER_AND_POOL_ARE_OUT_OF_SYNC, MAX_CLP_TO_OLP_REWARD_RATIO, PROTOCOL_FEE_RATE_MUL_VALUE,
};

#[cfg(feature = "wasm")]
use fusionamm_macros::wasm_expose;

/// Computes the limit order output amount by input amount.
/// ### Parameters
/// - `amount_in` - The input token amount of a limit order.
/// - `a_to_b_order` - The limit order direction.
/// - `tick_index` - The tick index of an order.
/// - `fusion_pool` - The fusion_pool state.
#[cfg_attr(feature = "wasm", wasm_expose)]
pub fn limit_order_quote_by_input_token(
    amount_in: u64,
    a_to_b_order: bool,
    tick_index: i32,
    fusion_pool: FusionPoolFacade,
) -> Result<u64, CoreError> {
    let sqrt_price: u128 = tick_index_to_sqrt_price(tick_index).into();
    let mut amount_out = get_limit_order_output_amount(amount_in, a_to_b_order, sqrt_price, false)?;

    // The total swap fee.
    let swap_fee = try_reverse_apply_swap_fee(amount_out.into(), fusion_pool.fee_rate)? - amount_out;

    // Calculate the order liquidity provider reward.
    let mut olp_reward = swap_fee - try_mul_div(swap_fee, fusion_pool.clp_to_olp_reward_ratio as u128, MAX_CLP_TO_OLP_REWARD_RATIO as u128, false)?;

    // Subtract the protocol fee from the reward.
    olp_reward -= try_mul_div(olp_reward, fusion_pool.protocol_fee_rate as u128, PROTOCOL_FEE_RATE_MUL_VALUE, false)?;

    amount_out = try_apply_swap_fee(amount_out.into(), fusion_pool.order_protocol_fee_rate)?;
    amount_out += olp_reward;

    Ok(amount_out)
}

/// Computes the limit order input amount by output amount.
/// ### Parameters
/// - `amount_out` - The output token amount of a limit order.
/// - `a_to_b_order` - The limit order direction.
/// - `tick_index` - The tick index of an order.
/// - `fusion_pool` - The fusion_pool state.
#[cfg_attr(feature = "wasm", wasm_expose)]
pub fn limit_order_quote_by_output_token(
    amount_out: u64,
    a_to_b_order: bool,
    tick_index: i32,
    fusion_pool: FusionPoolFacade,
) -> Result<u64, CoreError> {
    let sqrt_price: u128 = tick_index_to_sqrt_price(tick_index).into();

    let f = fusion_pool.fee_rate as f64 / FEE_RATE_DENOMINATOR as f64;
    let po = fusion_pool.order_protocol_fee_rate as f64 / FEE_RATE_DENOMINATOR as f64;
    let p = fusion_pool.protocol_fee_rate as f64 / PROTOCOL_FEE_RATE_MUL_VALUE as f64;
    let r = fusion_pool.clp_to_olp_reward_ratio as f64 / MAX_CLP_TO_OLP_REWARD_RATIO as f64;

    let denominator = 1.0 - po + (f * (1.0 - r) * (1.0 - p) / (1.0 - f));
    let amount_out_with_fees = amount_out as f64 / denominator;

    if amount_out_with_fees < 0.0 || amount_out_with_fees > u64::MAX as f64 {
        return Err(AMOUNT_EXCEEDS_MAX_U64);
    }

    let amount_in = get_limit_order_output_amount(amount_out_with_fees as u64, !a_to_b_order, sqrt_price, true)?;

    Ok(amount_in)
}

#[cfg_attr(feature = "wasm", wasm_expose)]
pub fn decrease_limit_order_quote(
    fusion_pool: FusionPoolFacade,
    limit_order: LimitOrderFacade,
    tick: TickFacade,
    amount: u64,
) -> Result<LimitOrderDecreaseQuote, CoreError> {
    if amount > limit_order.amount {
        return Err(AMOUNT_EXCEEDS_LIMIT_ORDER_INPUT_AMOUNT);
    }

    let protocol_fee_rate = fusion_pool.order_protocol_fee_rate;

    // Not filled
    let (amount_in, mut amount_out) = if limit_order.age == tick.age {
        (amount, 0)
    }
    // Partially filled
    else if limit_order.age + 1 == tick.age {
        if tick.part_filled_orders_input == 0 {
            return Err(LIMIT_ORDER_AND_POOL_ARE_OUT_OF_SYNC);
        }
        let sqrt_price: u128 = tick_index_to_sqrt_price(limit_order.tick_index).into();
        let remaining_input = try_mul_div(amount, tick.part_filled_orders_remaining_input as u128, tick.part_filled_orders_input as u128, false)?;
        let amount_out = get_limit_order_output_amount(amount - remaining_input, limit_order.a_to_b, sqrt_price, false)?;
        (remaining_input, amount_out)
    }
    // Fulfilled
    else if limit_order.age + 2 <= tick.age {
        let sqrt_price: u128 = tick_index_to_sqrt_price(limit_order.tick_index).into();
        let amount_out = get_limit_order_output_amount(amount, limit_order.a_to_b, sqrt_price, false)?;
        (0, amount_out)
    } else {
        return Err(LIMIT_ORDER_AND_POOL_ARE_OUT_OF_SYNC);
    };

    // Apply protocol fee to the output amount.
    let amount_out_with_fee = amount_out;
    amount_out = try_apply_swap_fee(amount_out, protocol_fee_rate)?;
    let protocol_fee = amount_out_with_fee - amount_out;

    let amount_out_a;
    let amount_out_b;
    let mut fee_a = 0;
    let mut fee_b = 0;
    let mut reward_a = 0;
    let mut reward_b = 0;

    if limit_order.a_to_b {
        let filled_amount = amount - amount_in;
        // Fees and rewards are paid in the output token B of a limit order. The reward amount is based on the portion of the order that is filled.
        fee_b = protocol_fee;
        if filled_amount > 0 {
            if fusion_pool.orders_filled_amount_a == 0 {
                return Err(LIMIT_ORDER_AND_POOL_ARE_OUT_OF_SYNC);
            }
            reward_b = try_mul_div(fusion_pool.olp_fee_owed_b, filled_amount as u128, fusion_pool.orders_filled_amount_a as u128, false)?;
        }
        // How much of tokens A and B transfer to the owner.
        amount_out_a = amount_in;
        amount_out_b = amount_out + reward_b;
    } else {
        let filled_amount = amount - amount_in;
        // Fees and rewards are paid in the output token A of a limit order. The reward amount is based on the portion of the order that is filled.
        fee_a = protocol_fee;
        if filled_amount > 0 {
            if fusion_pool.orders_filled_amount_b == 0 {
                return Err(LIMIT_ORDER_AND_POOL_ARE_OUT_OF_SYNC);
            }
            reward_a = try_mul_div(fusion_pool.olp_fee_owed_a, filled_amount as u128, fusion_pool.orders_filled_amount_b as u128, false)?;
        }
        // How much of tokens A and B transfer to the owner.
        amount_out_a = amount_out + reward_a;
        amount_out_b = amount_in;
    }

    Ok(LimitOrderDecreaseQuote {
        amount_out_a,
        amount_out_b,
        fee_a,
        fee_b,
        reward_a,
        reward_b,
    })
}

#[cfg(all(test, not(feature = "wasm")))]
mod tests {
    use crate::{
        decrease_limit_order_quote, limit_order_quote_by_input_token, limit_order_quote_by_output_token, price_to_tick_index,
        sqrt_price_to_tick_index, FusionPoolFacade, LimitOrderFacade, TickFacade,
    };
    const ONE_PCT: u16 = 10000;

    fn test_fusion_pool(sqrt_price: u128, fee_rate: u16, order_protocol_fee_rate: u16, protocol_fee_rate: u16) -> FusionPoolFacade {
        let tick_current_index = sqrt_price_to_tick_index(sqrt_price);
        FusionPoolFacade {
            tick_current_index,
            fee_rate,
            order_protocol_fee_rate,
            protocol_fee_rate,
            sqrt_price,
            tick_spacing: 2,
            ..FusionPoolFacade::default()
        }
    }

    #[test]
    // Equal to a similar test in limit_order_manager::calculate_modify_limit_order_unit_tests of the FusionAMM program.
    fn partially_decrease_not_filled_a_to_b_order() {
        let quote = decrease_limit_order_quote(
            FusionPoolFacade {
                order_protocol_fee_rate: ONE_PCT,
                ..FusionPoolFacade::default()
            },
            LimitOrderFacade {
                tick_index: 128,
                amount: 50_000,
                a_to_b: true,
                age: 5,
            },
            TickFacade {
                age: 5,
                open_orders_input: 100_000,
                part_filled_orders_input: 0,
                part_filled_orders_remaining_input: 0,
                fulfilled_a_to_b_orders_input: 0,
                fulfilled_b_to_a_orders_input: 0,
                ..TickFacade::default()
            },
            25_000,
        )
        .unwrap();

        assert_eq!(quote.amount_out_a, 25_000);
        assert_eq!(quote.amount_out_b, 0);
        assert_eq!(quote.fee_a, 0);
        assert_eq!(quote.fee_b, 0);
    }

    #[test]
    // Equal to a similar test in limit_order_manager::calculate_modify_limit_order_unit_tests of the FusionAMM program.
    fn partially_decrease_semi_filled_a_to_b_order() {
        let quote = decrease_limit_order_quote(
            FusionPoolFacade {
                order_protocol_fee_rate: ONE_PCT,
                orders_filled_amount_a: 80_000,
                olp_fee_owed_b: 500,
                ..FusionPoolFacade::default()
            },
            LimitOrderFacade {
                tick_index: 128,
                amount: 50_000,
                a_to_b: true,
                age: 5,
            },
            TickFacade {
                age: 6,
                open_orders_input: 0,
                part_filled_orders_input: 200_000,
                part_filled_orders_remaining_input: 120_000,
                fulfilled_a_to_b_orders_input: 0,
                fulfilled_b_to_a_orders_input: 0,
                ..TickFacade::default()
            },
            25_000,
        )
        .unwrap();

        assert_eq!(quote.amount_out_a, 15000);
        assert_eq!(quote.amount_out_b, 10088);
        assert_eq!(quote.fee_a, 0);
        assert_eq!(quote.fee_b, 102);
        assert_eq!(quote.reward_a, 0);
        assert_eq!(quote.reward_b, 62);
    }

    #[test]
    // Equal to a similar test in limit_order_manager::calculate_modify_limit_order_unit_tests of the FusionAMM program.
    fn partially_decrease_semi_filled_b_to_a_order() {
        let quote = decrease_limit_order_quote(
            FusionPoolFacade {
                order_protocol_fee_rate: ONE_PCT,
                orders_filled_amount_b: 80_000,
                olp_fee_owed_a: 500,
                ..FusionPoolFacade::default()
            },
            LimitOrderFacade {
                tick_index: 128,
                amount: 50_000,
                a_to_b: false,
                age: 5,
            },
            TickFacade {
                age: 6,
                open_orders_input: 0,
                part_filled_orders_input: 200_000,
                part_filled_orders_remaining_input: 120_000,
                fulfilled_a_to_b_orders_input: 0,
                fulfilled_b_to_a_orders_input: 0,
                ..TickFacade::default()
            },
            25_000,
        )
        .unwrap();

        assert_eq!(quote.amount_out_a, 9835);
        assert_eq!(quote.amount_out_b, 15000);
        assert_eq!(quote.fee_a, 99);
        assert_eq!(quote.fee_b, 0);
        assert_eq!(quote.reward_a, 62);
        assert_eq!(quote.reward_b, 0);
    }

    #[test]
    // Equal to a similar test in limit_order_manager::calculate_modify_limit_order_unit_tests of the FusionAMM program.
    fn partially_decrease_fulfilled_a_to_b() {
        let quote = decrease_limit_order_quote(
            FusionPoolFacade {
                order_protocol_fee_rate: ONE_PCT,
                orders_filled_amount_a: 100_000,
                olp_fee_owed_b: 500,
                ..FusionPoolFacade::default()
            },
            LimitOrderFacade {
                tick_index: 128,
                amount: 100_000,
                a_to_b: true,
                age: 5,
            },
            TickFacade {
                age: 7,
                open_orders_input: 0,
                part_filled_orders_input: 0,
                part_filled_orders_remaining_input: 0,
                fulfilled_a_to_b_orders_input: 100_000,
                fulfilled_b_to_a_orders_input: 80_000,
                ..TickFacade::default()
            },
            10_000,
        )
        .unwrap();

        assert_eq!(quote.amount_out_a, 0);
        assert_eq!(quote.amount_out_b, 10076);
        assert_eq!(quote.fee_a, 0);
        assert_eq!(quote.fee_b, 102);
        assert_eq!(quote.reward_a, 0);
        assert_eq!(quote.reward_b, 50);
    }

    #[test]
    // Equal to a similar test in limit_order_manager::calculate_modify_limit_order_unit_tests of the FusionAMM program.
    fn partially_decrease_fulfilled_b_to_a() {
        let quote = decrease_limit_order_quote(
            FusionPoolFacade {
                order_protocol_fee_rate: ONE_PCT,
                orders_filled_amount_b: 80_000,
                olp_fee_owed_a: 500,
                ..FusionPoolFacade::default()
            },
            LimitOrderFacade {
                tick_index: 128,
                amount: 100_000,
                a_to_b: false,
                age: 5,
            },
            TickFacade {
                age: 7,
                open_orders_input: 0,
                part_filled_orders_input: 0,
                part_filled_orders_remaining_input: 0,
                fulfilled_a_to_b_orders_input: 100_000,
                fulfilled_b_to_a_orders_input: 80_000,
                ..TickFacade::default()
            },
            10_000,
        )
        .unwrap();

        assert_eq!(quote.amount_out_a, 9835);
        assert_eq!(quote.amount_out_b, 0);
        assert_eq!(quote.fee_a, 99);
        assert_eq!(quote.fee_b, 0);
        assert_eq!(quote.reward_a, 62);
        assert_eq!(quote.reward_b, 0);
    }

    #[test]
    fn test_limit_order_quote_by_input_token() {
        assert_eq!(
            limit_order_quote_by_input_token(10_000, true, price_to_tick_index(2.0, 1, 1), test_fusion_pool(1 << 64, 0, 0, 1000)).unwrap(),
            19998
        );

        assert_eq!(
            limit_order_quote_by_input_token(10_000, true, price_to_tick_index(2.0, 1, 1), test_fusion_pool(1 << 64, 0, ONE_PCT, 1000)).unwrap(),
            19798
        );

        assert_eq!(
            limit_order_quote_by_input_token(10_000, true, price_to_tick_index(2.0, 1, 1), test_fusion_pool(1 << 64, ONE_PCT, 0, 0)).unwrap(),
            20200
        );

        assert_eq!(
            limit_order_quote_by_input_token(10_000, true, price_to_tick_index(2.0, 1, 1), test_fusion_pool(1 << 64, ONE_PCT, 0, 1000)).unwrap(),
            20180
        );
    }

    #[test]
    fn test_limit_order_quote_by_output_token() {
        assert_eq!(
            limit_order_quote_by_output_token(19998, true, price_to_tick_index(2.0, 1, 1), test_fusion_pool(1 << 64, 0, 0, 1000)).unwrap(),
            10_000
        );

        assert_eq!(
            limit_order_quote_by_output_token(19798, true, price_to_tick_index(2.0, 1, 1), test_fusion_pool(1 << 64, 0, ONE_PCT, 1000)).unwrap(),
            10_000
        );

        assert_eq!(
            limit_order_quote_by_output_token(20200, true, price_to_tick_index(2.0, 1, 1), test_fusion_pool(1 << 64, ONE_PCT, 0, 0)).unwrap(),
            10_000
        );

        assert_eq!(
            limit_order_quote_by_output_token(20180, true, price_to_tick_index(2.0, 1, 1), test_fusion_pool(1 << 64, ONE_PCT, 0, 1000)).unwrap(),
            10_000
        );
    }
}