Skip to content

Commit

Permalink
min resreve withdraw liquidity tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JanKuczma committed Jul 4, 2024
1 parent 9d99e65 commit f24facb
Showing 1 changed file with 220 additions and 6 deletions.
226 changes: 220 additions & 6 deletions amm/drink-tests/src/stable_swap_tests/min_reserve_tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use drink::{runtime::MinimalRuntime, session::Session};
use super::*;
use drink::{runtime::MinimalRuntime, session::Session};

const USDT_DEC: u8 = 6;
const USDC_DEC: u8 = 6;
Expand All @@ -11,14 +11,72 @@ fn test_min_initial_reserve(
initial_reserves: Vec<u128>,
expected_result: Result<(u128, u128), StablePoolError>,
) {
let (stable_swap, _) =
setup_stable_swap_with_tokens(session, token_decimals, initial_reserves.clone(), 1000, 25, 6, BOB);
let res = stable_swap::add_liquidity(
let (stable_swap, _) = setup_stable_swap_with_tokens(
session,
token_decimals,
initial_reserves.clone(),
1000,
25,
6,
BOB,
);
let res = stable_swap::add_liquidity(session, stable_swap, BOB, 1, initial_reserves, bob());
assert_eq!(expected_result, res, "Unexpected result");
}

fn test_min_reserve_withdraw_liquidity(
session: &mut Session<MinimalRuntime>,
token_decimals: Vec<u8>,
initial_reserves: Vec<u128>,
withdraw_amounts: Vec<u128>,
expected_result: Result<(u128, u128), StablePoolError>,
) {
let (stable_swap, _) = setup_stable_swap_with_tokens(
session,
token_decimals,
initial_reserves.clone(),
1000,
25,
6,
BOB,
);
_ = stable_swap::add_liquidity(session, stable_swap, BOB, 1, initial_reserves, bob());

let res = stable_swap::remove_liquidity_by_amounts(
session,
stable_swap,
BOB,
u128::MAX, // allow inifite max share to burn
withdraw_amounts,
bob(),
);
assert_eq!(expected_result, res, "Unexpected result");
}

fn test_min_reserve_withdraw_liquidity_by_shares(
session: &mut Session<MinimalRuntime>,
token_decimals: Vec<u8>,
initial_reserves: Vec<u128>,
withdraw_shares: u128,
expected_result: Result<Vec<u128>, StablePoolError>,
) {
let (stable_swap, _) = setup_stable_swap_with_tokens(
session,
token_decimals,
initial_reserves.clone(),
1000,
25,
6,
BOB,
);
_ = stable_swap::add_liquidity(session, stable_swap, BOB, 1, initial_reserves, bob());

let res = stable_swap::remove_liquidity_by_shares(
session,
stable_swap,
BOB,
1,
initial_reserves,
withdraw_shares,
vec![1, 1, 1], // min withdraw amounts
bob(),
);
assert_eq!(expected_result, res, "Unexpected result");
Expand Down Expand Up @@ -74,7 +132,163 @@ fn test_05(session: &mut Session) {
test_min_initial_reserve(
&mut session,
vec![USDT_DEC, DAI_DEC],
vec![1_000_000u128, 100_000_000_000_000_000u128],
Err(StablePoolError::MinReserve()),
);
}

#[drink::test]
fn test_06(session: &mut Session) {
// reserves one 1 usdt, 1 dai

This comment has been minimized.

Copy link
@deuszx

deuszx Jul 4, 2024

Contributor

what's this one doing here? (and everywhere else for that matter:))

This comment has been minimized.

Copy link
@JanKuczma

JanKuczma Jul 4, 2024

Author Collaborator

Typo -.-. My bad

// withdraw 1 usdt, 1 dai
test_min_reserve_withdraw_liquidity(
&mut session,
vec![USDT_DEC, DAI_DEC],
vec![1_000_000u128, 1_000_000_000_000_000_000u128],
vec![1_000_000u128, 1_000_000_000_000_000_000u128],
Err(StablePoolError::MinReserve()),

This comment has been minimized.

Copy link
@deuszx

deuszx Jul 4, 2024

Contributor

I feel like extracting

let initial_reserves = vec![1_000_000u128, 1_000_000_000_000_000_000u128];

and then using that in both places (as the withdrawal amounts) will make the test much clearer.

This comment has been minimized.

Copy link
@JanKuczma

JanKuczma Jul 4, 2024

Author Collaborator

Sure thing

);
}

#[drink::test]
fn test_07(session: &mut Session) {
// reserves one 1 usdt, 1 dai
// withdraw 0.1 usdt, 0.1 dai
test_min_reserve_withdraw_liquidity(
&mut session,
vec![USDT_DEC, DAI_DEC],
vec![1_000_000u128, 1_000_000_000_000_000_000u128],
vec![100_000u128, 100_000_000_000_000_000u128],
Err(StablePoolError::MinReserve()),
);
}

#[drink::test]
fn test_08(session: &mut Session) {
// reserves one 2 usdt, 2 dai
// withdraw 1 usdt, 1 dai
test_min_reserve_withdraw_liquidity(
&mut session,
vec![USDT_DEC, DAI_DEC],
vec![2_000_000u128, 2_000_000_000_000_000_000u128],

This comment has been minimized.

Copy link
@deuszx

deuszx Jul 4, 2024

Contributor

Can we extract these to let ONE_USDT = 1_000_000u128 , let ONE_DAI = ... and then simply do 2 * ONE_DAI and in withdrawals as well vec![ONE_USDT, ONE_DAI].

This comment has been minimized.

Copy link
@JanKuczma

JanKuczma Jul 4, 2024

Author Collaborator

Absolutely

vec![1_000_000u128, 1_000_000_000_000_000_000u128],
Ok((2000000000000000000, 0)),
);
}

#[drink::test]
fn test_09(session: &mut Session) {
// reserves one 2 usdt, 2 dai
// withdraw 1.1 usdt, 1.1 dai
test_min_reserve_withdraw_liquidity(
&mut session,
vec![USDT_DEC, DAI_DEC],
vec![2_000_000u128, 2_000_000_000_000_000_000u128],
vec![1_100_000u128, 1_100_000_000_000_000_000u128],
Err(StablePoolError::MinReserve()),
);
}

#[drink::test]
fn test_10(session: &mut Session) {
// reserves one 2 usdt, 2 dai
// withdraw 1.5 usdt, 0.5 dai
test_min_reserve_withdraw_liquidity(
&mut session,
vec![USDT_DEC, DAI_DEC],
vec![2_000_000u128, 2_000_000_000_000_000_000u128],
vec![1_500_000u128, 500_000_000_000_000_000u128],
Err(StablePoolError::MinReserve()),
);
}

#[drink::test]
fn test_11(session: &mut Session) {
// reserves one 2 usdt, 2 dai
// withdraw 0.5 usdt, 0.5 dai
test_min_reserve_withdraw_liquidity(
&mut session,
vec![USDT_DEC, DAI_DEC],
vec![2_000_000u128, 2_000_000_000_000_000_000u128],
vec![500_000u128, 500_000_000_000_000_000u128],
Ok((1000000000000000000, 0)),
);
}

#[drink::test]
fn test_12(session: &mut Session) {
// reserves one 1 usdt, 1 dai
// withdraw 1 usdt, 1 dai
test_min_reserve_withdraw_liquidity_by_shares(
&mut session,
vec![USDT_DEC, DAI_DEC],
vec![1_000_000u128, 1_000_000_000_000_000_000u128],
2000000000000000000, // 100%
Err(StablePoolError::MinReserve()),
);
}

#[drink::test]
fn test_13(session: &mut Session) {
// reserves one 1 usdt, 1 dai
// withdraw 0.1 usdt, 0.1 dai
test_min_reserve_withdraw_liquidity_by_shares(
&mut session,
vec![USDT_DEC, DAI_DEC],
vec![1_000_000u128, 1_000_000_000_000_000_000u128],
2000000000000000000 / 10, // 10%
Err(StablePoolError::MinReserve()),
);
}

#[drink::test]
fn test_14(session: &mut Session) {
// reserves one 2 usdt, 2 dai
// withdraw 1 usdt, 1 dai
test_min_reserve_withdraw_liquidity_by_shares(
&mut session,
vec![USDT_DEC, DAI_DEC],
vec![2_000_000u128, 2_000_000_000_000_000_000u128],
2000000000000000000, // 50%
Ok(vec![1_000_000u128, 1_000_000_000_000_000_000u128]),
);
}

#[drink::test]
fn test_15(session: &mut Session) {
// reserves one 2 usdt, 2 dai
// withdraw 1.1 usdt, 1.1 dai
test_min_reserve_withdraw_liquidity_by_shares(
&mut session,
vec![USDT_DEC, DAI_DEC],
vec![2_000_000u128, 2_000_000_000_000_000_000u128],
4000000000000000000 * 51 / 100, // 51 %
Err(StablePoolError::MinReserve()),
);
}

#[drink::test]
fn test_16(session: &mut Session) {
// reserves one 2 usdt, 2 dai
// withdraw 1.5 usdt, 0.5 dai
test_min_reserve_withdraw_liquidity_by_shares(
&mut session,
vec![USDT_DEC, DAI_DEC],
vec![2_000_000u128, 2_000_000_000_000_000_000u128],
4000000000000000000 * 3 / 4, // 75%
Err(StablePoolError::MinReserve()),
);
}

#[drink::test]
fn test_17(session: &mut Session) {
// reserves one 2 usdt, 2 dai
// withdraw 0.5 usdt, 0.5 dai
test_min_reserve_withdraw_liquidity_by_shares(
&mut session,
vec![USDT_DEC, DAI_DEC],
vec![2_000_000u128, 2_000_000_000_000_000_000u128],
4000000000000000000 / 4, // 25%
Ok(vec![500_000u128, 500_000_000_000_000_000u128]),
);
}

0 comments on commit f24facb

Please sign in to comment.