Skip to content

Commit 7cc0327

Browse files
committed
feat(smart-contracts): address PR comments
1 parent 361938d commit 7cc0327

File tree

1 file changed

+21
-14
lines changed
  • contracts/liquidity_hub/pool-manager/src

1 file changed

+21
-14
lines changed

contracts/liquidity_hub/pool-manager/src/helpers.rs

+21-14
Original file line numberDiff line numberDiff line change
@@ -798,13 +798,6 @@ pub fn compute_mint_amount_for_deposit(
798798
}
799799
}
800800

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-
808801
/// Compute the swap amount `y` in proportion to `x`.
809802
///
810803
/// Solve for `y`:
@@ -815,13 +808,14 @@ pub const N_COINS: u8 = 3;
815808
/// ```
816809
#[allow(clippy::many_single_char_names, clippy::unwrap_used)]
817810
pub fn compute_y_raw(
811+
n_coins: u8,
818812
amp_factor: &u64,
819813
swap_in: Uint128,
820814
//swap_out: Uint128,
821815
no_swap: Uint128,
822816
d: Uint256,
823817
) -> 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
825819

826820
// sum' = prod' = x
827821
// c = D ** (n + 1) / (n ** (2 * n) * prod' * A)
@@ -830,18 +824,18 @@ pub fn compute_y_raw(
830824
c = c
831825
.checked_mul(d)
832826
.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())
834828
.unwrap();
835829

836830
c = c
837831
.checked_mul(d)
838832
.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())
840834
.unwrap();
841835
c = c
842836
.checked_mul(d)
843837
.unwrap()
844-
.checked_div(ann.checked_mul(N_COINS.into()).unwrap().into())
838+
.checked_div(ann.checked_mul(n_coins.into()).unwrap().into())
845839
.unwrap();
846840
// b = sum(swap_in, no_swap) + D // Ann - D
847841
// not subtracting D here because that could result in a negative.
@@ -881,14 +875,21 @@ pub fn compute_y_raw(
881875

882876
/// Computes the swap amount `y` in proportion to `x`.
883877
#[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)?;
886886
Some(Uint128::try_from(amount).unwrap())
887887
}
888888

889889
/// Compute SwapResult after an exchange
890890
#[allow(clippy::unwrap_used)]
891891
pub fn swap_to(
892+
n_coins: u8,
892893
amp_factor: &u64,
893894
source_amount: Uint128,
894895
swap_source_amount: Uint128,
@@ -901,6 +902,7 @@ pub fn swap_to(
901902
coin(unswaped_amount.u128(), "denom3"),
902903
];
903904
let y = compute_y(
905+
n_coins,
904906
amp_factor,
905907
swap_source_amount.checked_add(source_amount).unwrap(),
906908
unswaped_amount,
@@ -947,6 +949,9 @@ mod tests {
947949
/// Maximum number of tokens to swap at once.
948950
pub const MAX_TOKENS_IN: Uint128 = Uint128::new(2u128 << 110);
949951

952+
/// Number of coins in a swap. Hardcoded to 3 to reuse previous tests
953+
pub const N_COINS: u8 = 3;
954+
950955
fn check_d(model: &Model, amount_a: u128, amount_b: u128, amount_c: u128) -> Uint256 {
951956
let deposits = vec![
952957
coin(amount_a, "denom1"),
@@ -960,6 +965,7 @@ mod tests {
960965

961966
fn check_y(model: &Model, swap_in: u128, no_swap: u128, d: Uint256) {
962967
let y = compute_y_raw(
968+
N_COINS,
963969
&model.amp_factor,
964970
Uint128::new(swap_in),
965971
Uint128::new(no_swap),
@@ -1076,6 +1082,7 @@ mod tests {
10761082
amount_swapped,
10771083
..
10781084
} = swap_to(
1085+
N_COINS,
10791086
&self.amp_factor,
10801087
source_amount,
10811088
swap_source_amount,
@@ -1285,7 +1292,7 @@ mod tests {
12851292

12861293
let d0 = compute_d(&amp_factor, &deposits).unwrap();
12871294

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());
12891296
prop_assume!(swap_result.is_some());
12901297

12911298
let swap_result = swap_result.unwrap();

0 commit comments

Comments
 (0)