@@ -798,13 +798,6 @@ pub fn compute_mint_amount_for_deposit(
798
798
}
799
799
}
800
800
801
- /*
802
- ############ trying to fix tests
803
- */
804
-
805
- /// Number of coins in a swap. Hardcoded to 3 to reuse previous tests
806
- pub const N_COINS : u8 = 3 ;
807
-
808
801
/// Compute the swap amount `y` in proportion to `x`.
809
802
///
810
803
/// Solve for `y`:
@@ -815,13 +808,14 @@ pub const N_COINS: u8 = 3;
815
808
/// ```
816
809
#[ allow( clippy:: many_single_char_names, clippy:: unwrap_used) ]
817
810
pub fn compute_y_raw (
811
+ n_coins : u8 ,
818
812
amp_factor : & u64 ,
819
813
swap_in : Uint128 ,
820
814
//swap_out: Uint128,
821
815
no_swap : Uint128 ,
822
816
d : Uint256 ,
823
817
) -> Option < Uint256 > {
824
- let ann = amp_factor. checked_mul ( N_COINS . into ( ) ) ?; // A * n ** n
818
+ let ann = amp_factor. checked_mul ( n_coins . into ( ) ) ?; // A * n ** n
825
819
826
820
// sum' = prod' = x
827
821
// c = D ** (n + 1) / (n ** (2 * n) * prod' * A)
@@ -830,18 +824,18 @@ pub fn compute_y_raw(
830
824
c = c
831
825
. checked_mul ( d)
832
826
. unwrap ( )
833
- . checked_div ( swap_in. checked_mul ( N_COINS . into ( ) ) . unwrap ( ) . into ( ) )
827
+ . checked_div ( swap_in. checked_mul ( n_coins . into ( ) ) . unwrap ( ) . into ( ) )
834
828
. unwrap ( ) ;
835
829
836
830
c = c
837
831
. checked_mul ( d)
838
832
. unwrap ( )
839
- . checked_div ( no_swap. checked_mul ( N_COINS . into ( ) ) . unwrap ( ) . into ( ) )
833
+ . checked_div ( no_swap. checked_mul ( n_coins . into ( ) ) . unwrap ( ) . into ( ) )
840
834
. unwrap ( ) ;
841
835
c = c
842
836
. checked_mul ( d)
843
837
. unwrap ( )
844
- . checked_div ( ann. checked_mul ( N_COINS . into ( ) ) . unwrap ( ) . into ( ) )
838
+ . checked_div ( ann. checked_mul ( n_coins . into ( ) ) . unwrap ( ) . into ( ) )
845
839
. unwrap ( ) ;
846
840
// b = sum(swap_in, no_swap) + D // Ann - D
847
841
// not subtracting D here because that could result in a negative.
@@ -881,14 +875,21 @@ pub fn compute_y_raw(
881
875
882
876
/// Computes the swap amount `y` in proportion to `x`.
883
877
#[ allow( clippy:: unwrap_used) ]
884
- pub fn compute_y ( amp_factor : & u64 , x : Uint128 , no_swap : Uint128 , d : Uint256 ) -> Option < Uint128 > {
885
- let amount = compute_y_raw ( & amp_factor, x, no_swap, d) ?;
878
+ pub fn compute_y (
879
+ n_coins : u8 ,
880
+ amp_factor : & u64 ,
881
+ x : Uint128 ,
882
+ no_swap : Uint128 ,
883
+ d : Uint256 ,
884
+ ) -> Option < Uint128 > {
885
+ let amount = compute_y_raw ( n_coins, & amp_factor, x, no_swap, d) ?;
886
886
Some ( Uint128 :: try_from ( amount) . unwrap ( ) )
887
887
}
888
888
889
889
/// Compute SwapResult after an exchange
890
890
#[ allow( clippy:: unwrap_used) ]
891
891
pub fn swap_to (
892
+ n_coins : u8 ,
892
893
amp_factor : & u64 ,
893
894
source_amount : Uint128 ,
894
895
swap_source_amount : Uint128 ,
@@ -901,6 +902,7 @@ pub fn swap_to(
901
902
coin( unswaped_amount. u128 ( ) , "denom3" ) ,
902
903
] ;
903
904
let y = compute_y (
905
+ n_coins,
904
906
amp_factor,
905
907
swap_source_amount. checked_add ( source_amount) . unwrap ( ) ,
906
908
unswaped_amount,
@@ -947,6 +949,9 @@ mod tests {
947
949
/// Maximum number of tokens to swap at once.
948
950
pub const MAX_TOKENS_IN : Uint128 = Uint128 :: new ( 2u128 << 110 ) ;
949
951
952
+ /// Number of coins in a swap. Hardcoded to 3 to reuse previous tests
953
+ pub const N_COINS : u8 = 3 ;
954
+
950
955
fn check_d ( model : & Model , amount_a : u128 , amount_b : u128 , amount_c : u128 ) -> Uint256 {
951
956
let deposits = vec ! [
952
957
coin( amount_a, "denom1" ) ,
@@ -960,6 +965,7 @@ mod tests {
960
965
961
966
fn check_y ( model : & Model , swap_in : u128 , no_swap : u128 , d : Uint256 ) {
962
967
let y = compute_y_raw (
968
+ N_COINS ,
963
969
& model. amp_factor ,
964
970
Uint128 :: new ( swap_in) ,
965
971
Uint128 :: new ( no_swap) ,
@@ -1076,6 +1082,7 @@ mod tests {
1076
1082
amount_swapped,
1077
1083
..
1078
1084
} = swap_to (
1085
+ N_COINS ,
1079
1086
& self . amp_factor ,
1080
1087
source_amount,
1081
1088
swap_source_amount,
@@ -1285,7 +1292,7 @@ mod tests {
1285
1292
1286
1293
let d0 = compute_d( & amp_factor, & deposits) . unwrap( ) ;
1287
1294
1288
- let swap_result = swap_to( & amp_factor, source_token_amount. into( ) , swap_source_amount. into( ) , swap_destination_amount. into( ) , unswapped_amount. into( ) ) ;
1295
+ let swap_result = swap_to( N_COINS , & amp_factor, source_token_amount. into( ) , swap_source_amount. into( ) , swap_destination_amount. into( ) , unswapped_amount. into( ) ) ;
1289
1296
prop_assume!( swap_result. is_some( ) ) ;
1290
1297
1291
1298
let swap_result = swap_result. unwrap( ) ;
0 commit comments