This repository has been archived by the owner on Jan 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathconstant_product.rs
565 lines (528 loc) · 19.8 KB
/
constant_product.rs
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
//! The Uniswap invariant calculator.
use {
crate::{
curve::calculator::{
map_zero_to_none, CurveCalculator, DynPack, RoundDirection, SwapWithoutFeesResult,
TradeDirection, TradingTokenResult,
},
error::SwapError,
},
solana_program::{
program_error::ProgramError,
program_pack::{IsInitialized, Pack, Sealed},
},
spl_math::{checked_ceil_div::CheckedCeilDiv, precise_number::PreciseNumber},
};
/// ConstantProductCurve struct implementing CurveCalculator
#[derive(Clone, Debug, Default, PartialEq)]
pub struct ConstantProductCurve;
/// The constant product swap calculation, factored out of its class for reuse.
///
/// This is guaranteed to work for all values such that:
/// - 1 <= swap_source_amount * swap_destination_amount <= u128::MAX
/// - 1 <= source_amount <= u64::MAX
pub fn swap(
source_amount: u128,
swap_source_amount: u128,
swap_destination_amount: u128,
) -> Option<SwapWithoutFeesResult> {
let invariant = swap_source_amount.checked_mul(swap_destination_amount)?;
let new_swap_source_amount = swap_source_amount.checked_add(source_amount)?;
let (new_swap_destination_amount, new_swap_source_amount) =
invariant.checked_ceil_div(new_swap_source_amount)?;
let source_amount_swapped = new_swap_source_amount.checked_sub(swap_source_amount)?;
let destination_amount_swapped =
map_zero_to_none(swap_destination_amount.checked_sub(new_swap_destination_amount)?)?;
Some(SwapWithoutFeesResult {
source_amount_swapped,
destination_amount_swapped,
})
}
/// Get the amount of trading tokens for the given amount of pool tokens,
/// provided the total trading tokens and supply of pool tokens.
///
/// The constant product implementation is a simple ratio calculation for how
/// many trading tokens correspond to a certain number of pool tokens
pub fn pool_tokens_to_trading_tokens(
pool_tokens: u128,
pool_token_supply: u128,
swap_token_a_amount: u128,
swap_token_b_amount: u128,
round_direction: RoundDirection,
) -> Option<TradingTokenResult> {
let mut token_a_amount = pool_tokens
.checked_mul(swap_token_a_amount)?
.checked_div(pool_token_supply)?;
let mut token_b_amount = pool_tokens
.checked_mul(swap_token_b_amount)?
.checked_div(pool_token_supply)?;
let (token_a_amount, token_b_amount) = match round_direction {
RoundDirection::Floor => (token_a_amount, token_b_amount),
RoundDirection::Ceiling => {
let token_a_remainder = pool_tokens
.checked_mul(swap_token_a_amount)?
.checked_rem(pool_token_supply)?;
// Also check for 0 token A and B amount to avoid taking too much
// for tiny amounts of pool tokens. For example, if someone asks
// for 1 pool token, which is worth 0.01 token A, we avoid the
// ceiling of taking 1 token A and instead return 0, for it to be
// rejected later in processing.
if token_a_remainder > 0 && token_a_amount > 0 {
token_a_amount += 1;
}
let token_b_remainder = pool_tokens
.checked_mul(swap_token_b_amount)?
.checked_rem(pool_token_supply)?;
if token_b_remainder > 0 && token_b_amount > 0 {
token_b_amount += 1;
}
(token_a_amount, token_b_amount)
}
};
Some(TradingTokenResult {
token_a_amount,
token_b_amount,
})
}
/// Get the amount of pool tokens for the deposited amount of token A or B.
///
/// The constant product implementation uses the Balancer formulas found at
/// <https://balancer.finance/whitepaper/#single-asset-deposit>, specifically
/// in the case for 2 tokens, each weighted at 1/2.
pub fn deposit_single_token_type(
source_amount: u128,
swap_token_a_amount: u128,
swap_token_b_amount: u128,
pool_supply: u128,
trade_direction: TradeDirection,
round_direction: RoundDirection,
) -> Option<u128> {
let swap_source_amount = match trade_direction {
TradeDirection::AtoB => swap_token_a_amount,
TradeDirection::BtoA => swap_token_b_amount,
};
let swap_source_amount = PreciseNumber::new(swap_source_amount)?;
let source_amount = PreciseNumber::new(source_amount)?;
let ratio = source_amount.checked_div(&swap_source_amount)?;
let one = PreciseNumber::new(1)?;
let base = one.checked_add(&ratio)?;
let root = base.sqrt()?.checked_sub(&one)?;
let pool_supply = PreciseNumber::new(pool_supply)?;
let pool_tokens = pool_supply.checked_mul(&root)?;
match round_direction {
RoundDirection::Floor => pool_tokens.floor()?.to_imprecise(),
RoundDirection::Ceiling => pool_tokens.ceiling()?.to_imprecise(),
}
}
/// Get the amount of pool tokens for the withdrawn amount of token A or B.
///
/// The constant product implementation uses the Balancer formulas found at
/// <https://balancer.finance/whitepaper/#single-asset-withdrawal>, specifically
/// in the case for 2 tokens, each weighted at 1/2.
pub fn withdraw_single_token_type_exact_out(
source_amount: u128,
swap_token_a_amount: u128,
swap_token_b_amount: u128,
pool_supply: u128,
trade_direction: TradeDirection,
round_direction: RoundDirection,
) -> Option<u128> {
let swap_source_amount = match trade_direction {
TradeDirection::AtoB => swap_token_a_amount,
TradeDirection::BtoA => swap_token_b_amount,
};
let swap_source_amount = PreciseNumber::new(swap_source_amount)?;
let source_amount = PreciseNumber::new(source_amount)?;
let ratio = source_amount.checked_div(&swap_source_amount)?;
let one = PreciseNumber::new(1)?;
let base = one
.checked_sub(&ratio)
.unwrap_or_else(|| PreciseNumber::new(0).unwrap());
let root = one.checked_sub(&base.sqrt()?)?;
let pool_supply = PreciseNumber::new(pool_supply)?;
let pool_tokens = pool_supply.checked_mul(&root)?;
match round_direction {
RoundDirection::Floor => pool_tokens.floor()?.to_imprecise(),
RoundDirection::Ceiling => pool_tokens.ceiling()?.to_imprecise(),
}
}
/// Calculates the total normalized value of the curve given the liquidity
/// parameters.
///
/// The constant product implementation for this function gives the square root
/// of the Uniswap invariant.
pub fn normalized_value(
swap_token_a_amount: u128,
swap_token_b_amount: u128,
) -> Option<PreciseNumber> {
let swap_token_a_amount = PreciseNumber::new(swap_token_a_amount)?;
let swap_token_b_amount = PreciseNumber::new(swap_token_b_amount)?;
swap_token_a_amount
.checked_mul(&swap_token_b_amount)?
.sqrt()
}
impl CurveCalculator for ConstantProductCurve {
/// Constant product swap ensures x * y = constant
fn swap_without_fees(
&self,
source_amount: u128,
swap_source_amount: u128,
swap_destination_amount: u128,
_trade_direction: TradeDirection,
) -> Option<SwapWithoutFeesResult> {
swap(source_amount, swap_source_amount, swap_destination_amount)
}
/// The constant product implementation is a simple ratio calculation for
/// how many trading tokens correspond to a certain number of pool
/// tokens
fn pool_tokens_to_trading_tokens(
&self,
pool_tokens: u128,
pool_token_supply: u128,
swap_token_a_amount: u128,
swap_token_b_amount: u128,
round_direction: RoundDirection,
) -> Option<TradingTokenResult> {
pool_tokens_to_trading_tokens(
pool_tokens,
pool_token_supply,
swap_token_a_amount,
swap_token_b_amount,
round_direction,
)
}
/// Get the amount of pool tokens for the deposited amount of token A or B.
fn deposit_single_token_type(
&self,
source_amount: u128,
swap_token_a_amount: u128,
swap_token_b_amount: u128,
pool_supply: u128,
trade_direction: TradeDirection,
) -> Option<u128> {
deposit_single_token_type(
source_amount,
swap_token_a_amount,
swap_token_b_amount,
pool_supply,
trade_direction,
RoundDirection::Floor,
)
}
fn withdraw_single_token_type_exact_out(
&self,
source_amount: u128,
swap_token_a_amount: u128,
swap_token_b_amount: u128,
pool_supply: u128,
trade_direction: TradeDirection,
round_direction: RoundDirection,
) -> Option<u128> {
withdraw_single_token_type_exact_out(
source_amount,
swap_token_a_amount,
swap_token_b_amount,
pool_supply,
trade_direction,
round_direction,
)
}
fn normalized_value(
&self,
swap_token_a_amount: u128,
swap_token_b_amount: u128,
) -> Option<PreciseNumber> {
normalized_value(swap_token_a_amount, swap_token_b_amount)
}
fn validate(&self) -> Result<(), SwapError> {
Ok(())
}
}
/// IsInitialized is required to use `Pack::pack` and `Pack::unpack`
impl IsInitialized for ConstantProductCurve {
fn is_initialized(&self) -> bool {
true
}
}
impl Sealed for ConstantProductCurve {}
impl Pack for ConstantProductCurve {
const LEN: usize = 0;
fn pack_into_slice(&self, output: &mut [u8]) {
(self as &dyn DynPack).pack_into_slice(output);
}
fn unpack_from_slice(_input: &[u8]) -> Result<ConstantProductCurve, ProgramError> {
Ok(Self {})
}
}
impl DynPack for ConstantProductCurve {
fn pack_into_slice(&self, _output: &mut [u8]) {}
}
#[cfg(test)]
mod tests {
use {
super::*,
crate::curve::calculator::{
test::{
check_curve_value_from_swap, check_deposit_token_conversion,
check_pool_value_from_deposit, check_pool_value_from_withdraw,
check_withdraw_token_conversion, total_and_intermediate,
CONVERSION_BASIS_POINTS_GUARANTEE,
},
RoundDirection, INITIAL_SWAP_POOL_AMOUNT,
},
proptest::prelude::*,
};
#[test]
fn initial_pool_amount() {
let calculator = ConstantProductCurve {};
assert_eq!(calculator.new_pool_supply(), INITIAL_SWAP_POOL_AMOUNT);
}
fn check_pool_token_rate(
token_a: u128,
token_b: u128,
deposit: u128,
supply: u128,
expected_a: u128,
expected_b: u128,
) {
let calculator = ConstantProductCurve {};
let results = calculator
.pool_tokens_to_trading_tokens(
deposit,
supply,
token_a,
token_b,
RoundDirection::Ceiling,
)
.unwrap();
assert_eq!(results.token_a_amount, expected_a);
assert_eq!(results.token_b_amount, expected_b);
}
#[test]
fn trading_token_conversion() {
check_pool_token_rate(2, 49, 5, 10, 1, 25);
check_pool_token_rate(100, 202, 5, 101, 5, 10);
check_pool_token_rate(5, 501, 2, 10, 1, 101);
}
#[test]
fn fail_trading_token_conversion() {
let calculator = ConstantProductCurve {};
let results =
calculator.pool_tokens_to_trading_tokens(5, 10, u128::MAX, 0, RoundDirection::Floor);
assert!(results.is_none());
let results =
calculator.pool_tokens_to_trading_tokens(5, 10, 0, u128::MAX, RoundDirection::Floor);
assert!(results.is_none());
}
#[test]
fn pack_constant_product_curve() {
let curve = ConstantProductCurve {};
let mut packed = [0u8; ConstantProductCurve::LEN];
Pack::pack_into_slice(&curve, &mut packed[..]);
let unpacked = ConstantProductCurve::unpack(&packed).unwrap();
assert_eq!(curve, unpacked);
let packed = vec![];
let unpacked = ConstantProductCurve::unpack(&packed).unwrap();
assert_eq!(curve, unpacked);
}
fn test_truncation(
curve: &ConstantProductCurve,
source_amount: u128,
swap_source_amount: u128,
swap_destination_amount: u128,
expected_source_amount_swapped: u128,
expected_destination_amount_swapped: u128,
) {
let invariant = swap_source_amount * swap_destination_amount;
let result = curve
.swap_without_fees(
source_amount,
swap_source_amount,
swap_destination_amount,
TradeDirection::AtoB,
)
.unwrap();
assert_eq!(result.source_amount_swapped, expected_source_amount_swapped);
assert_eq!(
result.destination_amount_swapped,
expected_destination_amount_swapped
);
let new_invariant = (swap_source_amount + result.source_amount_swapped)
* (swap_destination_amount - result.destination_amount_swapped);
assert!(new_invariant >= invariant);
}
#[test]
fn constant_product_swap_rounding() {
let curve = ConstantProductCurve;
// much too small
assert!(curve
.swap_without_fees(10, 70_000_000_000, 4_000_000, TradeDirection::AtoB)
.is_none()); // spot: 10 * 4m / 70b = 0
let tests: &[(u128, u128, u128, u128, u128)] = &[
// spot: 10 * 70b / ~4m = 174,999.99
(10, 4_000_000, 70_000_000_000, 10, 174_999),
// spot: 20 * 1 / 3.000 = 6.6667 (source can be 18 to get 6 dest.)
(20, 30_000 - 20, 10_000, 18, 6),
// spot: 19 * 1 / 2.999 = 6.3334 (source can be 18 to get 6 dest.)
(19, 30_000 - 20, 10_000, 18, 6),
// spot: 18 * 1 / 2.999 = 6.0001
(18, 30_000 - 20, 10_000, 18, 6),
// spot: 10 * 3 / 2.0010 = 14.99
(10, 20_000, 30_000, 10, 14),
// spot: 10 * 3 / 2.0001 = 14.999
(10, 20_000 - 9, 30_000, 10, 14),
// spot: 10 * 3 / 2.0000 = 15
(10, 20_000 - 10, 30_000, 10, 15),
// spot: 100 * 3 / 6.001 = 49.99 (source can be 99 to get 49 dest.)
(100, 60_000, 30_000, 99, 49),
// spot: 99 * 3 / 6.001 = 49.49
(99, 60_000, 30_000, 99, 49),
// spot: 98 * 3 / 6.001 = 48.99 (source can be 97 to get 48 dest.)
(98, 60_000, 30_000, 97, 48),
];
for (
source_amount,
swap_source_amount,
swap_destination_amount,
expected_source_amount,
expected_destination_amount,
) in tests.iter()
{
test_truncation(
&curve,
*source_amount,
*swap_source_amount,
*swap_destination_amount,
*expected_source_amount,
*expected_destination_amount,
);
}
}
proptest! {
#[test]
fn deposit_token_conversion(
// in the pool token conversion calcs, we simulate trading half of
// source_token_amount, so this needs to be at least 2
source_token_amount in 2..u64::MAX,
swap_source_amount in 1..u64::MAX,
swap_destination_amount in 1..u64::MAX,
pool_supply in INITIAL_SWAP_POOL_AMOUNT..u64::MAX as u128,
) {
let curve = ConstantProductCurve {};
check_deposit_token_conversion(
&curve,
source_token_amount as u128,
swap_source_amount as u128,
swap_destination_amount as u128,
TradeDirection::AtoB,
pool_supply,
CONVERSION_BASIS_POINTS_GUARANTEE,
);
check_deposit_token_conversion(
&curve,
source_token_amount as u128,
swap_source_amount as u128,
swap_destination_amount as u128,
TradeDirection::BtoA,
pool_supply,
CONVERSION_BASIS_POINTS_GUARANTEE,
);
}
}
proptest! {
#[test]
fn withdraw_token_conversion(
(pool_token_supply, pool_token_amount) in total_and_intermediate(u64::MAX),
swap_token_a_amount in 1..u64::MAX,
swap_token_b_amount in 1..u64::MAX,
) {
let curve = ConstantProductCurve {};
check_withdraw_token_conversion(
&curve,
pool_token_amount as u128,
pool_token_supply as u128,
swap_token_a_amount as u128,
swap_token_b_amount as u128,
TradeDirection::AtoB,
CONVERSION_BASIS_POINTS_GUARANTEE
);
check_withdraw_token_conversion(
&curve,
pool_token_amount as u128,
pool_token_supply as u128,
swap_token_a_amount as u128,
swap_token_b_amount as u128,
TradeDirection::BtoA,
CONVERSION_BASIS_POINTS_GUARANTEE
);
}
}
proptest! {
#[test]
fn curve_value_does_not_decrease_from_swap(
source_token_amount in 1..u64::MAX,
swap_source_amount in 1..u64::MAX,
swap_destination_amount in 1..u64::MAX,
) {
let curve = ConstantProductCurve {};
check_curve_value_from_swap(
&curve,
source_token_amount as u128,
swap_source_amount as u128,
swap_destination_amount as u128,
TradeDirection::AtoB
);
}
}
proptest! {
#[test]
fn curve_value_does_not_decrease_from_deposit(
pool_token_amount in 1..u64::MAX,
pool_token_supply in 1..u64::MAX,
swap_token_a_amount in 1..u64::MAX,
swap_token_b_amount in 1..u64::MAX,
) {
let pool_token_amount = pool_token_amount as u128;
let pool_token_supply = pool_token_supply as u128;
let swap_token_a_amount = swap_token_a_amount as u128;
let swap_token_b_amount = swap_token_b_amount as u128;
// Make sure we will get at least one trading token out for each
// side, otherwise the calculation fails
prop_assume!(pool_token_amount * swap_token_a_amount / pool_token_supply >= 1);
prop_assume!(pool_token_amount * swap_token_b_amount / pool_token_supply >= 1);
let curve = ConstantProductCurve {};
check_pool_value_from_deposit(
&curve,
pool_token_amount,
pool_token_supply,
swap_token_a_amount,
swap_token_b_amount,
);
}
}
proptest! {
#[test]
fn curve_value_does_not_decrease_from_withdraw(
(pool_token_supply, pool_token_amount) in total_and_intermediate(u64::MAX),
swap_token_a_amount in 1..u64::MAX,
swap_token_b_amount in 1..u64::MAX,
) {
let pool_token_amount = pool_token_amount as u128;
let pool_token_supply = pool_token_supply as u128;
let swap_token_a_amount = swap_token_a_amount as u128;
let swap_token_b_amount = swap_token_b_amount as u128;
// Make sure we will get at least one trading token out for each
// side, otherwise the calculation fails
prop_assume!(pool_token_amount * swap_token_a_amount / pool_token_supply >= 1);
prop_assume!(pool_token_amount * swap_token_b_amount / pool_token_supply >= 1);
let curve = ConstantProductCurve {};
check_pool_value_from_withdraw(
&curve,
pool_token_amount,
pool_token_supply,
swap_token_a_amount,
swap_token_b_amount,
);
}
}
}