dexter-stable-pool 1.1.1

A Dexter DEX pool implementing the stableswap and metastable AMM models
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
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
use std::ops::Div;

use cosmwasm_std::{Decimal256, StdError, StdResult, Uint128, Uint256, Uint64};
use dexter::asset::{AssetInfo, Decimal256Ext, DecimalAsset};
use dexter::error::ContractError;
use dexter::pool::{FeeStructs, SpotPrice};
use itertools::Itertools;

/// The maximum number of calculation steps for Newton's method.
const ITERATIONS: u8 = 32;

pub const MAX_AMP: u64 = 1_000_000;
pub const MAX_AMP_CHANGE: u64 = 10;
pub const MIN_AMP_CHANGING_TIME: u64 = 86400;
pub const AMP_PRECISION: u64 = 100;

// ----------------x----------------x----------------
// ----------------x   STABLE-3-Pool Math     x------
// ----------------x----------------x----------------

/// ## Description
/// Computes the stableswap invariant (D).
///
/// * **Equation**
/// D invariant calculation in non-overflowing integer operations
/// iteratively

/// A * sum(x_i) * n**n + D = A * D * n**n + D**(n+1) / (n**n * prod(x_i))

/// Converging solution:
/// D[j+1] = (A * n**n * sum(x_i) - D[j]**(n+1) / (n**n prod(x_i))) / (A * n**n - 1)///
///
/// ## Params
/// * **amp** is an object of type [`Uint64`].
/// * **pools** is a vector with values of type [`Decimal256`].
/// * **greatest_precision** object of type [`u8`].
pub(crate) fn compute_d(amp: Uint64, pools: &[Decimal256]) -> StdResult<Decimal256> {
    if pools.iter().any(|pool| pool.is_zero()) {
        return Ok(Decimal256::zero());
    }

    let sum_x = pools.iter().fold(Uint256::zero(), |acc, x| acc + x.atomics());

    let n_coins = pools.len() as u8;
    let ann = Uint256::from(amp.u64().div(AMP_PRECISION) * n_coins as u64);
    let n_coins = Uint256::from(n_coins as u64);

    // Initial D = sum_x, which is the sum of all the pools liquidity
    let mut d = sum_x;
    let ann_sum_x = ann.checked_mul(sum_x)?;

    // while abs(D - Dprev) > 1:
    for _ in 0..ITERATIONS {

        let mut dp = d;
        for (_, pool) in pools.iter().enumerate() {
            let denominator = pool.atomics().checked_mul(n_coins.into())?;
            let fraction = (d, denominator);
            dp = dp.checked_mul_floor(fraction).unwrap();
        }

        let d_prev = d;
        
        let numerator = (ann_sum_x + dp.checked_mul(n_coins).unwrap()).checked_mul(d)?;
        let denominator = (ann - Uint256::one()).checked_mul(d)?
            .checked_add(n_coins.checked_add(Uint256::one())?.checked_mul(dp)?)?;

        d = numerator.checked_div(denominator)?;

        if d >= d_prev {
            if d - d_prev <= Uint256::from(1u8) {
                return Ok(Decimal256::from_atomics(d, Decimal256::DECIMAL_PLACES).unwrap());
            }
        } else if d_prev - d <= Uint256::from(1u8) {
            return Ok(Decimal256::from_atomics(d, Decimal256::DECIMAL_PLACES).unwrap());
        }
    }

    Ok(Decimal256::from_atomics(d, Decimal256::DECIMAL_PLACES).unwrap())
}

/// ## Description
/// Computes the new balance of a `to` pool if one makes `from` pool = `new_amount`.
///
/// Done by solving quadratic equation iteratively.
/// `x_1**2 + x_1 * (sum' - (A*n**n - 1) * D / (A * n**n)) = D ** (n + 1) / (n ** (2 * n) * prod' * A)`
/// `x_1**2 + b*x_1 = c`
///
/// `x_1 = (x_1**2 + c) / (2*x_1 + b)`
pub(crate) fn calc_y(
    from_asset: &DecimalAsset,
    to: &AssetInfo,
    new_amount: Decimal256,
    pools: &[DecimalAsset],
    amp: u64,
    output_precision: u8,
) -> StdResult<Uint128> {
    let amp = Uint64::from(amp);

    if from_asset.info.equal(to) {
        return Err(StdError::generic_err(
            ContractError::SameAssets {}.to_string(),
        ));
    }

    let n_coins = Uint64::from(pools.len() as u8);
    let ann = Uint256::from(amp.checked_mul(n_coins)?.u64() / AMP_PRECISION);
    let mut sum = Uint256::zero();
    let pool_values = pools.iter().map(|asset| asset.amount).collect_vec();

    // d is computed with the largest precision possible i.e Decimal256::DECIMAL_PLACES i.e 18
    let d = compute_d(amp, &pool_values)?.to_uint256_with_precision(Decimal256::DECIMAL_PLACES)?;
    let mut c = d;

    for pool in pools {
        let pool_amount: Decimal256 = if pool.info.eq(&from_asset.info) {
            new_amount
        } else if !pool.info.eq(to) {
            pool.amount
        } else {
            continue;
        };
        c = c
            .checked_multiply_ratio(
                d,
                pool_amount.to_uint256_with_precision(Decimal256::DECIMAL_PLACES)?
                    * Uint256::from(n_coins),
            )
            .map_err(|_| StdError::generic_err("CheckedMultiplyRatioError"))?;
        sum += pool_amount.to_uint256_with_precision(Decimal256::DECIMAL_PLACES)?;
    }

    let c = c * d / (ann * Uint256::from(n_coins));
    let b = sum + d / ann;

    let mut y = d;
    let d = y;

    for _ in 0..ITERATIONS {
        let y_prev = y;
        y = (y * y + c) / (y + y + b - d);

        if y >= y_prev {
            if y - y_prev <= Uint256::from(1u8) {
                // We need to scale the value from the MAX_PRECISION to the precision of the asset
                // We do this by dividing the value by the ratio of the two precisions
                let decimal_difference = Decimal256::DECIMAL_PLACES - output_precision as u32; // this is safe because ask_asset_precision is always <= 18
                let precision_ratio = Uint256::from(10u8).pow(decimal_difference as u32);
                let y = y.checked_div(precision_ratio)?;

                return Ok(y.try_into()?);
            }
        } else if y_prev - y <= Uint256::from(1u8) {
            // We need to scale the value from the MAX_PRECISION to the precision of the asset
            // We do this by dividing the value by the ratio of the two precisions
            let decimal_difference = Decimal256::DECIMAL_PLACES - output_precision as u32; // this is safe because ask_asset_precision is always <= 18
            let precision_ratio = Uint256::from(10u8).pow(decimal_difference as u32);
            let y = y.checked_div(precision_ratio)?;
            return Ok(y.try_into()?);
        }
    }

    // Should definitely converge in 32 iterations.
    Err(StdError::generic_err("y is not converging"))
}

pub(crate) fn calc_spot_price(
    from: &AssetInfo,
    to: &AssetInfo,
    from_asset_scaling_factor: &Decimal256,
    to_asset_scaling_factor: &Decimal256,
    pools: &[DecimalAsset],
    fee: FeeStructs,
    amp: u64,
) -> StdResult<SpotPrice> {
    // first we figure out the amount of the from asset in the pool
    let from_asset = pools.iter().find(|asset| asset.info.eq(from)).unwrap();
    let to_asset = pools.iter().find(|asset| asset.info.eq(to)).unwrap();

    // if from asset amount is zero, then return 0 as the spot price
    // this is becasuse ideally both will be zero if the pool is empty, but we'll just check for from_asset
    if from_asset.amount.is_zero() {
        return Ok(SpotPrice {
            from: from.clone(),
            to: to.clone(),
            price: Decimal256::zero(),
            price_including_fee: Decimal256::zero(),
        });
    }

    // now, since it's really hard to find the price derivative of the stableswap invariant, we'll just use the
    // approximation as the price of a very very small trade. This is the same as the spot price.
    // We will define a very small trade as 0.001% of the pool liquidity
    // For most 6 decimal precision assets, even 1 unit = 1e6, so 0.001% = 1e6 / 1e5 = 1e = 10 units which is still 
    // a decent small trade size for having a good approximation without running into decimal precision issues.
    // Also, since we always use 18 decimal precision in our calculations, we actually have decent precision in our
    // calculations for having a very accurate spot price.
    let from_asset_small_trade_amount = from_asset.amount.checked_mul(Decimal256::from_ratio(
        Uint256::from(1u128),
        Uint256::from(100000u128),
    ))?;
    let fee_on_trade = from_asset_small_trade_amount.checked_mul(Decimal256::from_ratio(
        fee.total_fee_bps,
        Uint256::from(10000u128),
    ))?;

    let from_asset_small_trade_amount_after_fee =
        from_asset_small_trade_amount.checked_sub(fee_on_trade)?;

    let new_from_asset = from_asset
        .amount
        .checked_add(from_asset_small_trade_amount)?;
    let new_from_asset_after_fee_deduction = from_asset
        .amount
        .checked_add(from_asset_small_trade_amount_after_fee)?;

    let y = calc_y(
        from_asset,
        to,
        new_from_asset,
        pools,
        amp,
        Decimal256::DECIMAL_PLACES as u8,
    )?;

    let y_including_fee = calc_y(
        from_asset,
        to,
        new_from_asset_after_fee_deduction,
        pools,
        amp,
        Decimal256::DECIMAL_PLACES as u8,
    )?;

    let y_price_decimal = Decimal256::with_precision(y, Decimal256::DECIMAL_PLACES)?;
    let y_price_including_fee_decimal =
        Decimal256::with_precision(y_including_fee, Decimal256::DECIMAL_PLACES)?;

    let y_diff = to_asset.amount.checked_sub(y_price_decimal)?;
    let y_diff_including_fee = to_asset.amount.checked_sub(y_price_including_fee_decimal)?;

    let y_diff_unscaled = y_diff.without_scaling_factor(*to_asset_scaling_factor)?;

    let y_diff_including_fee_scaled = y_diff_including_fee.without_scaling_factor(*to_asset_scaling_factor)?;

    let from_asset_small_trade_amount_unscaled =
        from_asset_small_trade_amount.without_scaling_factor(*from_asset_scaling_factor)?;

    let spot_price = y_diff_unscaled
        .checked_div(from_asset_small_trade_amount_unscaled)
        .unwrap();

    let spot_price_with_fee = y_diff_including_fee_scaled
        .checked_div(from_asset_small_trade_amount_unscaled)
        .unwrap();

    let spot_price = SpotPrice {
        from: from.clone(),
        to: to.clone(),
        price: spot_price,
        price_including_fee: spot_price_with_fee,
    };

    Ok(spot_price)
}

#[cfg(test)]
mod tests {
    
    use std::str::FromStr;
    use super::*;
    use dexter::{asset::{native_asset, Asset}, uint128_with_precision};
    use sim::StableSwapModel;

    fn decimal_asset_pools_with_precision(pools: Vec<Asset>, precision: u8) -> Vec<DecimalAsset> {
        pools
            .iter()
            .map(|pool| pool.to_decimal_asset(precision).unwrap())
            .collect::<Vec<DecimalAsset>>()
    }

    #[test]
    fn test_spot_price() {

        let amp = 100u64;
        let amp_final = amp * AMP_PRECISION;

        let fee = FeeStructs {
            total_fee_bps: 30,
        };

        let pools = vec![
            native_asset("test1".to_string(), uint128_with_precision!(100_000u128, 6)),
            native_asset("test2".to_string(), uint128_with_precision!(100_000u128, 6)),
            native_asset("test3".to_string(), uint128_with_precision!(100_000u128, 6)),
        ];

        let pools_decimal = decimal_asset_pools_with_precision(pools.clone(), 6);
        let spot_price = calc_spot_price(
            &pools[0].info,
            &pools[1].info, 
            &Decimal256::from_str("1").unwrap(),
            &Decimal256::from_str("1").unwrap(),
            &pools_decimal, 
            fee.clone(), 
            amp_final
        );
        assert_eq!(spot_price.is_ok(), true);
        assert_eq!(spot_price.unwrap().price, Decimal256::from_str("0.999999900990108804").unwrap());

        // test opposite direction
        let spot_price = calc_spot_price(
            &pools[0].info,
            &pools[1].info, 
            &Decimal256::from_str("1").unwrap(),
            &Decimal256::from_str("1").unwrap(),
            &pools_decimal, 
            fee.clone(), 
            amp_final
        );
        assert_eq!(spot_price.is_ok(), true);
        assert_eq!(spot_price.unwrap().price, Decimal256::from_str("0.999999900990108804").unwrap());


        let pools = vec![
            native_asset("test1".to_string(), uint128_with_precision!(1000u128, 6)),
            native_asset("test2".to_string(), uint128_with_precision!(1000u128, 6)),
            native_asset("test3".to_string(), uint128_with_precision!(1000u128, 6)),
        ];

  
        let pools_decimal = decimal_asset_pools_with_precision(pools.clone(), 6);
        let spot_price = calc_spot_price(
            &pools[0].info,
            &pools[1].info, 
            &Decimal256::from_str("1").unwrap(),
            &Decimal256::from_str("1").unwrap(),
            &pools_decimal, 
            fee.clone(), 
            amp_final
        );
        assert_eq!(spot_price.is_ok(), true);
        assert_eq!(spot_price.unwrap().price, Decimal256::from_str("0.9999999009901089").unwrap());
        
        let pools = vec![
            native_asset("test1".to_string(), uint128_with_precision!(1u128, 6)),
            native_asset("test2".to_string(), uint128_with_precision!(1u128, 6)),
            native_asset("test3".to_string(), uint128_with_precision!(1u128, 6)),
        ];

        let pools_decimal = decimal_asset_pools_with_precision(pools.clone(), 6);
        let spot_price = calc_spot_price(
            &pools[0].info,
            &pools[1].info, 
            &Decimal256::from_str("1").unwrap(),
            &Decimal256::from_str("1").unwrap(),
            &pools_decimal, 
            fee.clone(), 
            amp_final
        );
        assert_eq!(spot_price.is_ok(), true);
        assert_eq!(spot_price.unwrap().price, Decimal256::from_str("0.9999999009902").unwrap());
        
        let pools = vec![
            native_asset("test1".to_string(), uint128_with_precision!(1u128, 6)),
            // 1/5th the liquidity of test1 for test2. Let's see the effect of this on the spot price.
            native_asset("test2".to_string(), Uint128::from(200000u128)),
        ];
            
        let pools_decimal = decimal_asset_pools_with_precision(pools.clone(), 6);
        let spot_price = calc_spot_price(
            &pools[0].info,
            &pools[1].info, 
            &Decimal256::from_str("1").unwrap(),
            &Decimal256::from_str("1").unwrap(),
            &pools_decimal, 
            fee.clone(), 
            amp_final
        );
        assert_eq!(spot_price.is_ok(), true);
        assert_eq!(spot_price.unwrap().price, Decimal256::from_str("0.9594664670086").unwrap());

        // test opposite direction
        let spot_price = calc_spot_price(
            &pools[1].info,
            &pools[0].info, 
            &Decimal256::from_str("1").unwrap(),
            &Decimal256::from_str("1").unwrap(),
            &pools_decimal, 
            fee.clone(), 
            amp_final
        );
        assert_eq!(spot_price.is_ok(), true);
        assert_eq!(spot_price.unwrap().price, Decimal256::from_str("1.0422433526755").unwrap());

        let pools = vec![
            native_asset("test1".to_string(), uint128_with_precision!(1u128, 6)),
            // 1/10th the liquidity of test1 for test2. Let's see the effect of this on the spot price.
            native_asset("test2".to_string(), Uint128::from(100000u128)),
        ];
        
        let pools_decimal = decimal_asset_pools_with_precision(pools.clone(), 6);
        let spot_price = calc_spot_price(
            &pools[0].info,
            &pools[1].info, 
            &Decimal256::from_str("1").unwrap(),
            &Decimal256::from_str("1").unwrap(),
            &pools_decimal, 
            fee.clone(), 
            amp_final
        );
        assert_eq!(spot_price.is_ok(), true);
        assert_eq!(spot_price.unwrap().price, Decimal256::from_str("0.8748087775978").unwrap());

        // test opposite direction
        let spot_price = calc_spot_price(
            &pools[1].info,
            &pools[0].info, 
            &Decimal256::from_str("1").unwrap(),
            &Decimal256::from_str("1").unwrap(),
            &pools_decimal, 
            fee.clone(), 
            amp_final
        );
        assert_eq!(spot_price.is_ok(), true);
        let spot_price = spot_price.unwrap();
        
        assert_eq!(spot_price.clone().price, Decimal256::from_str("1.143093026548").unwrap());
        assert_eq!(spot_price.price_including_fee, Decimal256::from_str("1.139663751743").unwrap());


        // let's try even smaller pools
        let pools = vec![
            native_asset("test1".to_string(), Uint128::from(10u128)),
            native_asset("test2".to_string(), Uint128::from(10u128)),
        ];


        let pools_decimal = decimal_asset_pools_with_precision(pools.clone(), 6);
        let spot_price = calc_spot_price(
            &pools[0].info,
            &pools[1].info, 
            &Decimal256::from_str("1").unwrap(),
            &Decimal256::from_str("1").unwrap(),
            &pools_decimal, 
            fee.clone(), 
            amp_final
        );
        assert_eq!(spot_price.is_ok(), true);
        assert_eq!(spot_price.unwrap().price, Decimal256::from_str("0.99999991").unwrap());


        // let's try even smaller pools
        let pools = vec![
            native_asset("test1".to_string(), uint128_with_precision!(1u64, 12)),
            native_asset("test2".to_string(), uint128_with_precision!(1u64, 12)),
        ];

        let pools_decimal = decimal_asset_pools_with_precision(pools.clone(), 18);

        let spot_price = calc_spot_price(
            &pools[0].info,
            &pools[1].info, 
            &Decimal256::from_str("1").unwrap(),
            &Decimal256::from_str("1").unwrap(),
            &pools_decimal, 
            fee.clone(), 
            amp_final
        );
        assert_eq!(spot_price.is_ok(), true);
        // simulation breaks at this value. 
        // with integer invariant calculation, we must be able to go below this value.
        assert_eq!(spot_price.unwrap().price, Decimal256::from_str("1").unwrap());

    }

    #[test]
    fn test_compute_d() {
        // we multiply amp with AMP_PRECISION to avoid floating point arithmetic errors, so amp will always be >= AMP_PRECISION
        // -----------x-----------x-----------x-----------x-----------
        // -----------x-----------x   Test-1  x-----------x-----------
        // -----------x-----------x-----------x-----------x-----------

        let amp = Uint64::from(100u64);
        let pool1 = Uint128::from(100_000u128);
        let pool2 = Uint128::from(100_000u128);
        let pool3 = Uint128::from(100_000u128);
        let model = StableSwapModel::new(
            amp.u64().into(),
            vec![pool1.u128(), pool2.u128(), pool3.u128()],
            3,
        );

        let sim_d = model.sim_d();
        let d = compute_d(
            amp,
            &vec![
                Decimal256::from_integer(pool1.u128()),
                Decimal256::from_integer(pool2.u128()),
                Decimal256::from_integer(pool3.u128()),
            ],
        )
        .unwrap();
        assert_eq!(Uint256::from(sim_d), d.to_uint256());

        // -----------x-----------x-----------x-----------x-----------
        // -----------x-----------x   Test-2  x-----------x-----------
        // -----------x-----------x-----------x-----------x-----------

        // sum_x: "4240000000"
        // ann (amp * n_coins) : "3"
        // sum_x = d: "4240000000"
        // ann_sum_x = ann * sum_x: "12720000000"
        // -------------x-------------x-------------x-------------
        // Start Loop: while abs(D - Dprev) > 1
        // -- Start For Loop: D_P = D_P * D / (_x * N_COINS)
        // d_p: 4240000000
        // pool_liq: 1242000000 n_coins: 3
        // denominator (pool_liq * n_coins) : "3726000000000000000000000000"
        // new d_p: Ok(Decimal256(Uint256(4824906065485775626800000000)))
        // -----
        // d_p: 4824906065.4857756268
        // pool_liq: 1542000000 n_coins: 3
        // denominator (pool_liq * n_coins) : "4626000000000000000000000000"
        // new d_p: Ok(Decimal256(Uint256(4422309061318566502912266567)))
        // -----
        // d_p: 4422309061.318566502912266567
        // pool_liq: 1456000000 n_coins: 3
        // denominator (pool_liq * n_coins) : "4368000000000000000000000000"
        // new d_p: Ok(Decimal256(Uint256(4292717586994212901464610765)))
        // ------
        // new d_p (calculated via D_P = D_P * D / (_x * N_COINS) ): 4292717586.994212901464610765
        // d_prev: 4240000000
        // d = (Ann * S + D_P * self.n) * D // ((Ann - 1) * D + (self.n + 1) * D_P) : 4231285965.512156881371063356
        // -- Start For Loop: D_P = D_P * D / (_x * N_COINS)
        // -----
        // d_p: 4231285965.512156881371063356
        // pool_liq: 1242000000 n_coins: 3
        // denominator (pool_liq * n_coins) : "3726000000000000000000000000"
        // new d_p: Ok(Decimal256(Uint256(4805094181948509301844637442)))
        // -----
        // d_p: 4805094181.948509301844637442
        // pool_liq: 1542000000 n_coins: 3
        // denominator (pool_liq * n_coins) : "4626000000000000000000000000"
        // new d_p: Ok(Decimal256(Uint256(4395098913757640684595323001)))
        // -----
        // d_p: 4395098913.757640684595323001
        // pool_liq: 1456000000 n_coins: 3
        // denominator (pool_liq * n_coins) : "4368000000000000000000000000"
        // new d_p: Ok(Decimal256(Uint256(4257536710352662679544225681)))
        // ------
        // new d_p (calculated via D_P = D_P * D / (_x * N_COINS) ): 4257536710.352662679544225681
        // d_prev: 4231285965.512156881371063356
        // d = (Ann * S + D_P * self.n) * D // ((Ann - 1) * D + (self.n + 1) * D_P) : 4231267933.197213235689277215
        // -- Start For Loop: D_P = D_P * D / (_x * N_COINS)
        // -----
        // d_p: 4231267933.197213235689277215
        // pool_liq: 1242000000 n_coins: 3
        // denominator (pool_liq * n_coins) : "3726000000000000000000000000"
        // new d_p: Ok(Decimal256(Uint256(4805053226651373206008817831)))
        // -----
        // d_p: 4805053226.651373206008817831
        // pool_liq: 1542000000 n_coins: 3
        // denominator (pool_liq * n_coins) : "4626000000000000000000000000"
        // new d_p: Ok(Decimal256(Uint256(4395042722705524534375018302)))
        // -----
        // d_p: 4395042722.705524534375018302
        // pool_liq: 1456000000 n_coins: 3
        // denominator (pool_liq * n_coins) : "4368000000000000000000000000"
        // new d_p: Ok(Decimal256(Uint256(4257464134069518670739830360)))
        // ------
        // new d_p (calculated via D_P = D_P * D / (_x * N_COINS) ): 4257464134.06951867073983036
        // d_prev: 4231267933.197213235689277215
        // d = (Ann * S + D_P * self.n) * D // ((Ann - 1) * D + (self.n + 1) * D_P) : 4231267933.120206881338543707
        // -- Start For Loop: D_P = D_P * D / (_x * N_COINS)
        // -----
        // d_p: 4231267933.120206881338543707
        // pool_liq: 1242000000 n_coins: 3
        // denominator (pool_liq * n_coins) : "3726000000000000000000000000"
        // new d_p: Ok(Decimal256(Uint256(4805053226476475448918363438)))
        // -----
        // d_p: 4805053226.476475448918363438
        // pool_liq: 1542000000 n_coins: 3
        // denominator (pool_liq * n_coins) : "4626000000000000000000000000"
        // new d_p: Ok(Decimal256(Uint256(4395042722465563683718690834)))
        // -----
        // d_p: 4395042722.465563683718690834
        // pool_liq: 1456000000 n_coins: 3
        // denominator (pool_liq * n_coins) : "4368000000000000000000000000"
        // new d_p: Ok(Decimal256(Uint256(4257464133759586236627241436)))
        // ------
        // new d_p (calculated via D_P = D_P * D / (_x * N_COINS) ): 4257464133.759586236627241436
        // d_prev: 4231267933.120206881338543707
        // d = (Ann * S + D_P * self.n) * D // ((Ann - 1) * D + (self.n + 1) * D_P) : 4231267933.120206881454012313
        // d: 4231267933.120206881454012313

        let amp = Uint64::from(100u64);
        let pool1 = Uint128::from(1242_000000u128);
        let pool2 = Uint128::from(1542_000000u128);
        let pool3 = Uint128::from(1456_000000u128);
        let d = compute_d(
            amp,
            &vec![
                Decimal256::from_integer(pool1.u128()),
                Decimal256::from_integer(pool2.u128()),
                Decimal256::from_integer(pool3.u128()),
            ],
        )
        .unwrap();

        assert_eq!(Uint256::from(4231267933u128), d.to_uint256());
    }

    #[test]
    fn test_compute_y() {
        pub const NATIVE_TOKEN_PRECISION: u8 = 6;
        let amp = 100u64;

        // -----------x-----------x-----------x-----------x-----------
        // -----------x-----------x   Test-1  x-----------x-----------
        // -----------x-----------x-----------x-----------x-----------

        let pool1 = Uint128::from(100_000_000000u128);
        let pool2 = Uint128::from(100_000_000000u128);
        let pool3 = Uint128::from(100_000_000000u128);
        let pools = vec![
            native_asset("test1".to_string(), pool1),
            native_asset("test2".to_string(), pool2),
            native_asset("test3".to_string(), pool3),
        ];

        let offer_amount = Uint128::from(100_000000u128);
        let y = calc_y(
            &pools[0].to_decimal_asset(NATIVE_TOKEN_PRECISION).unwrap(),
            &pools[1].info,
            Decimal256::with_precision(pools[0].amount + offer_amount, NATIVE_TOKEN_PRECISION)
                .unwrap(),
            &pools
                .iter()
                .map(|pool| pool.to_decimal_asset(NATIVE_TOKEN_PRECISION).unwrap())
                .collect::<Vec<DecimalAsset>>(),
            amp * AMP_PRECISION,
            NATIVE_TOKEN_PRECISION,
        )
        .unwrap()
        .u128();

        let model = StableSwapModel::new(
            amp as u128,
            vec![pool1.u128(), pool2.u128(), pool3.u128()],
            3,
        );
        let sim_y = model.sim_y(0, 1, pool1.u128() + offer_amount.u128());

        assert_eq!(sim_y, y);

        // -----------x-----------x-----------x-----------x-----------
        // -----------x-----------x   Test-2  x-----------x-----------
        // -----------x-----------x-----------x-----------x-----------

        // pool_values: [Decimal256(Uint256(1546325000000000000000000)), Decimal256(Uint256(1728525000000000000000000)), Decimal256(Uint256(1335325000000000000000000))]
        // compute_d() Function
        // d: 4609919816251
        // c: 4609919816251
        // Start Loop: c = c * d / (pool_amount * n_coins)
        // -- pool_amount: 1546998
        // c: 4579053692433
        // sum: 1546998
        // -- pool_amount: 1335325
        // c: 5269396428191
        // sum: 2882323
        // --------------------------------
        // c: 26990550015555478262591
        // sum: 2882323000000
        // b (sum + d / ann): 2897689399387
        // y: 4609919816251
        // d: 4609919816251
        // Start Loop: y = (y*y + c) / (2*y + b - d)
        // Returned y: 1727851292418
        // let pool1 = Uint128::from(1546325_000000u128);
        // let pool2 = Uint128::from(1728525_000000u128);
        // let pool3 = Uint128::from(1335325_000000u128);
        // let pools = vec![
        //     native_asset("test1".to_string(), pool1),
        //     native_asset("test2".to_string(), pool2),
        //     native_asset("test3".to_string(), pool3),
        // ];

        // The comments above aren't exactly for the tests below. But, better to keep them for easy remembering purposes.

        let offer_amount = Uint128::from(673_000000u128);
        let y = calc_y(
            &pools[0].to_decimal_asset(NATIVE_TOKEN_PRECISION).unwrap(),
            &pools[1].info,
            Decimal256::with_precision(pools[0].amount + offer_amount, NATIVE_TOKEN_PRECISION)
                .unwrap(),
            &pools
                .iter()
                .map(|pool| pool.to_decimal_asset(NATIVE_TOKEN_PRECISION).unwrap())
                .collect::<Vec<DecimalAsset>>(),
            amp * AMP_PRECISION,
            NATIVE_TOKEN_PRECISION,
        )
        .unwrap()
        .u128();

        let model = StableSwapModel::new(
            amp as u128,
            vec![pool1.u128(), pool2.u128(), pool3.u128()],
            3,
        );
        // pool1 --> pool2
        let sim_y = model.sim_y(0, 1, pool1.u128() + offer_amount.u128());
        assert_eq!(sim_y, y);

        // -----------x-----------x-----------x-----------x-----------
        // -----------x-----------x   Test-3  x-----------x-----------
        // -----------x-----------x-----------x-----------x-----------

        // n_coins: 3
        // pool_values: [Decimal256(Uint256(1546325000000000000000000)), Decimal256(Uint256(1728525000000000000000000)), Decimal256(Uint256(1335325000000000000000000))]
        // compute_d() Function
        // d: 4609919816251
        // c: 4609919816251
        // Start Loop: c = c * d / (pool_amount * n_coins)
        // -- pool_amount: 1734269
        // c: 4084595241042
        // sum: 1734269
        // -- pool_amount: 1335325
        // c: 4700392923831
        // sum: 3069594
        // ------------------------
        // c: 24076038315260560176641
        // sum: 3069594000000
        // b (sum + d / ann): 3084960399387
        // y: 4609919816251
        // d: 4609919816251
        // Start Loop: y = (y*y + c) / (2*y + b - d)
        // Returned y: 1540587248612
        let offer_amount = Uint128::from(5744_000000u128);
        let y = calc_y(
            &pools[1].to_decimal_asset(NATIVE_TOKEN_PRECISION).unwrap(),
            &pools[0].info,
            Decimal256::with_precision(pools[1].amount + offer_amount, NATIVE_TOKEN_PRECISION)
                .unwrap(),
            &pools
                .iter()
                .map(|pool| pool.to_decimal_asset(NATIVE_TOKEN_PRECISION).unwrap())
                .collect::<Vec<DecimalAsset>>(),
            amp * AMP_PRECISION,
            NATIVE_TOKEN_PRECISION,
        )
        .unwrap()
        .u128();

        let model = StableSwapModel::new(
            amp as u128,
            vec![pool1.u128(), pool2.u128(), pool3.u128()],
            3,
        );
        // pool2 -->pool1
        let sim_y = model.sim_y(1, 0, pool2.u128() + offer_amount.u128());
        assert_eq!(sim_y, y);
    }
}