Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pool: high liquidity amounts in tests #379

Merged
merged 3 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 56 additions & 49 deletions contracts/pool/src/tests/liquidity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
};

#[test]
fn provide_liqudity() {
fn provide_liquidity() {
let env = Env::default();
env.mock_all_auths();
env.budget().reset_unlimited();
Expand Down Expand Up @@ -49,18 +49,18 @@ fn provide_liqudity() {
let share_token_address = pool.query_share_token_address();
let token_share = token_contract::Client::new(&env, &share_token_address);

token1.mint(&user1, &10_000);
assert_eq!(token1.balance(&user1), 10_000);
token1.mint(&user1, &1_000_000_000_000_000);
assert_eq!(token1.balance(&user1), 1_000_000_000_000_000);

token2.mint(&user1, &10_000);
assert_eq!(token2.balance(&user1), 10_000);
token2.mint(&user1, &1_000_000_000_000_000);
assert_eq!(token2.balance(&user1), 1_000_000_000_000_000);

pool.provide_liquidity(
&user1,
&Some(10_000),
&Some(10_000),
&Some(10_000),
&Some(10_000),
&Some(1_000_000_000_000_000),
&Some(1_000_000_000_000_000),
&Some(1_000_000_000_000_000),
&Some(1_000_000_000_000_000),
&None,
&None::<u64>,
);
Expand All @@ -75,10 +75,10 @@ fn provide_liqudity() {
Symbol::new(&env, "provide_liquidity"),
(
&user1,
Some(10_000i128),
Some(10_000i128),
Some(10_000i128),
Some(10_000i128),
Some(1_000_000_000_000_000_i128),
Some(1_000_000_000_000_000_i128),
Some(1_000_000_000_000_000_i128),
Some(1_000_000_000_000_000_i128),
None::<i64>,
None::<u64>
)
Expand All @@ -89,15 +89,15 @@ fn provide_liqudity() {
function: AuthorizedFunction::Contract((
token1.address.clone(),
symbol_short!("transfer"),
(&user1, &pool.address, 10_000_i128).into_val(&env)
(&user1, &pool.address, 1_000_000_000_000_000_i128).into_val(&env)
)),
sub_invocations: std::vec![],
},
AuthorizedInvocation {
function: AuthorizedFunction::Contract((
token2.address.clone(),
symbol_short!("transfer"),
(&user1, &pool.address, 10_000_i128).into_val(&env)
(&user1, &pool.address, 1_000_000_000_000_000_i128).into_val(&env)
)),
sub_invocations: std::vec![],
},
Expand All @@ -106,33 +106,33 @@ fn provide_liqudity() {
),]
);

assert_eq!(token_share.balance(&user1), 9_000);
assert_eq!(token_share.balance(&user1), 999999999999000);
assert_eq!(token_share.balance(&pool.address), 1_000);
assert_eq!(token1.balance(&user1), 0);
assert_eq!(token1.balance(&pool.address), 10_000);
assert_eq!(token1.balance(&pool.address), 1_000_000_000_000_000);
assert_eq!(token2.balance(&user1), 0);
assert_eq!(token2.balance(&pool.address), 10_000);
assert_eq!(token2.balance(&pool.address), 1_000_000_000_000_000);

let result = pool.query_pool_info();
assert_eq!(
result,
PoolResponse {
asset_a: Asset {
address: token1.address,
amount: 10_000i128
amount: 1_000_000_000_000_000_i128
},
asset_b: Asset {
address: token2.address,
amount: 10_000i128
amount: 1_000_000_000_000_000_i128
},
asset_lp_share: Asset {
address: share_token_address,
amount: 10_000i128
amount: 1_000_000_000_000_000_i128
},
stake_address: result.clone().stake_address,
}
);
assert_eq!(pool.query_total_issued_lp(), 10_000);
assert_eq!(pool.query_total_issued_lp(), 1_000_000_000_000_000);
}

#[test]
Expand Down Expand Up @@ -170,27 +170,27 @@ fn withdraw_liquidity() {
let share_token_address = pool.query_share_token_address();
let token_share = token_contract::Client::new(&env, &share_token_address);

token1.mint(&user1, &100_000);
token2.mint(&user1, &100_000);
token1.mint(&user1, &1_000_000_000_000_000);
token2.mint(&user1, &1_000_000_000_000_000);
pool.provide_liquidity(
&user1,
&Some(100_000),
&Some(100_000),
&Some(100_000),
&Some(100_000),
&Some(1_000_000_000_000_000),
&Some(1_000_000_000_000_000),
&Some(1_000_000_000_000_000),
&Some(1_000_000_000_000_000),
&None,
&None::<u64>,
);

assert_eq!(token1.balance(&user1), 0);
assert_eq!(token_share.balance(&user1), 99_000);
assert_eq!(token1.balance(&pool.address), 100_000);
assert_eq!(token_share.balance(&user1), 999_999_999_999_000);
assert_eq!(token1.balance(&pool.address), 1_000_000_000_000_000);
assert_eq!(token2.balance(&user1), 0);
assert_eq!(token2.balance(&pool.address), 100_000);
assert_eq!(token2.balance(&pool.address), 1_000_000_000_000_000);

let share_amount = 50_000;
let min_a = 50_000;
let min_b = 50_000;
let share_amount = 500_000_000_000_000;
let min_a = 500_000_000_000_000;
let min_b = 500_000_000_000_000;
pool.withdraw_liquidity(&user1, &share_amount, &min_a, &min_b, &None::<u64>);

assert_eq!(
Expand All @@ -201,42 +201,49 @@ fn withdraw_liquidity() {
function: AuthorizedFunction::Contract((
pool.address.clone(),
Symbol::new(&env, "withdraw_liquidity"),
(&user1, 50_000i128, 50_000i128, 50_000i128, None::<u64>).into_val(&env),
(
&user1,
500_000_000_000_000_i128,
500_000_000_000_000_i128,
500_000_000_000_000_i128,
None::<u64>
)
.into_val(&env),
)),
sub_invocations: std::vec![AuthorizedInvocation {
function: AuthorizedFunction::Contract((
share_token_address.clone(),
symbol_short!("transfer"),
(&user1, &pool.address, 50_000_i128).into_val(&env)
(&user1, &pool.address, 500_000_000_000_000_i128).into_val(&env)
)),
sub_invocations: std::vec![],
},],
}
),]
);

assert_eq!(token_share.balance(&user1), 49_000);
assert_eq!(token_share.balance(&user1), 499_999_999_999_000);
assert_eq!(token_share.balance(&pool.address), 1_000); // sanity check
assert_eq!(token1.balance(&user1), 50_000);
assert_eq!(token1.balance(&pool.address), 50_000);
assert_eq!(token2.balance(&user1), 50_000);
assert_eq!(token2.balance(&pool.address), 50_000);
assert_eq!(token1.balance(&user1), 500_000_000_000_000);
assert_eq!(token1.balance(&pool.address), 500_000_000_000_000);
assert_eq!(token2.balance(&user1), 500_000_000_000_000);
assert_eq!(token2.balance(&pool.address), 500_000_000_000_000);

let result = pool.query_pool_info();
assert_eq!(
result,
PoolResponse {
asset_a: Asset {
address: token1.address.clone(),
amount: 50_000i128,
amount: 500_000_000_000_000_i128,
},
asset_b: Asset {
address: token2.address.clone(),
amount: 50_000i128,
amount: 500_000_000_000_000_i128,
},
asset_lp_share: Asset {
address: share_token_address.clone(),
amount: 50_000i128,
amount: 500_000_000_000_000_i128,
},
stake_address: result.clone().stake_address,
}
Expand All @@ -245,15 +252,15 @@ fn withdraw_liquidity() {
// clear the pool
pool.withdraw_liquidity(
&user1,
&49_000, /* leftover shares */
&49_000,
&49_000,
&499_999_999_999_000, /* leftover shares */
&499_999_999_999_000,
&499_999_999_999_000,
&None::<u64>,
);
assert_eq!(token_share.balance(&user1), 0);
assert_eq!(token_share.balance(&pool.address), 1_000); // Because of the minted 1_000 lp shares
assert_eq!(token1.balance(&user1), 99_000);
assert_eq!(token2.balance(&user1), 99_000);
assert_eq!(token1.balance(&user1), 999_999_999_999_000);
assert_eq!(token2.balance(&user1), 999_999_999_999_000);
// those tokens are locked because of the initial amount of LP shared tokens that are locked
// Thanks to that all the subsequent users will be having a proper fair share
assert_eq!(token1.balance(&pool.address), 1_000);
Expand Down
82 changes: 46 additions & 36 deletions contracts/pool/src/tests/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,30 @@ fn simple_swap() {
stake_owner,
);

token1.mint(&user1, &1_001_000);
token2.mint(&user1, &1_001_000);
let liquidity_amount = 100_000_000_000_000_i128; // 10 million with 7 decimal places
token1.mint(&user1, &(liquidity_amount + 10_000_000_000_000)); // 11,000,000.0000000
token2.mint(&user1, &(liquidity_amount + 10_000_000_000_000)); // 11,000,000.0000000

pool.provide_liquidity(
&user1,
&Some(1_000_000),
&Some(1_000_000),
&Some(1_000_000),
&Some(1_000_000),
&Some(liquidity_amount),
&Some(liquidity_amount),
&Some(liquidity_amount),
&Some(liquidity_amount),
&None,
&None::<u64>,
);

// selling just one token with 1% max spread allowed
let spread = 100i64; // 1% maximum spread allowed
pool.swap(
// Swapping 100,000 tokens with 7 decimal places
let swap_amount = 1_000_000_000_000_i128; // 100,000.0000000 with 7 decimals

// Execute the swap
let output_amount = pool.swap(
&user1,
// FIXM: Disable Referral struct
// &None::<Referral>,
&token1.address,
&1,
&swap_amount,
&None,
&Some(spread),
&Some(100), // 1% spread as allowed
&None::<u64>,
&None,
);
Expand All @@ -81,9 +83,9 @@ fn simple_swap() {
// FIXM: Disable Referral struct
// None::<Referral>,
token1.address.clone(),
1_i128,
1_000_000_000_000_i128,
None::<i64>,
spread,
Some(100i64),
None::<u64>,
None::<i64>
)
Expand All @@ -94,7 +96,7 @@ fn simple_swap() {
function: AuthorizedFunction::Contract((
token1.address.clone(),
symbol_short!("transfer"),
(&user1, &pool.address, 1_i128).into_val(&env)
(&user1, &pool.address, 1_000_000_000_000_i128).into_val(&env)
)),
sub_invocations: std::vec![],
}),
Expand All @@ -103,63 +105,71 @@ fn simple_swap() {
)]
);

// Query pool info after swap
let share_token_address = pool.query_share_token_address();
let result = pool.query_pool_info();

// Spread amount computed by the DEX contract during the swap
let spread_amount = 990_0990099i128;

// Corrected assertion based on the results from the swap
assert_eq!(
result,
PoolResponse {
asset_a: Asset {
address: token1.address.clone(),
amount: 1_000_001i128,
amount: liquidity_amount + swap_amount,
},
asset_b: Asset {
address: token2.address.clone(),
amount: 999_999i128,
amount: liquidity_amount - swap_amount + spread_amount
},
asset_lp_share: Asset {
address: share_token_address.clone(),
amount: 1_000_000i128,
amount: liquidity_amount, // Liquidity pool share remains unchanged
},
stake_address: result.clone().stake_address,
stake_address: result.stake_address.clone(),
}
);
assert_eq!(token1.balance(&user1), 999); // -1 from the swap
assert_eq!(token2.balance(&user1), 1001); // 1 from the swap

// this time 100 units
let output_amount = pool.swap(
assert_eq!(token1.balance(&user1), 10_000_000_000_000 - swap_amount); // 11,000,000.0000000 - 100,000.0000000
assert_eq!(token2.balance(&user1), 10_000_000_000_000 + output_amount); // Reflect the swap return after spread

// This time swapping 100,000 tokens of token2
let output_amount_2 = pool.swap(
&user1,
// FIXM: Disable Referral struct
// &None::<Referral>,
&token2.address,
&1_000,
&swap_amount,
&None,
&Some(spread),
&Some(200), // 2% spread as allowed
&None::<u64>,
&None,
);
let result = pool.query_pool_info();

// Corrected assertion based on the second swap
assert_eq!(
result,
PoolResponse {
asset_a: Asset {
address: token1.address.clone(),
amount: 1_000_001 - 1_000, // previous balance minus 1_000
amount: 99_990_099_990_099, // Reflecting the state after the second swap, with spread
},
asset_b: Asset {
address: token2.address.clone(),
amount: 999_999 + 1000,
amount: 100_009_900_990_099, // Reflecting the state after the second swap, with spread
},
asset_lp_share: Asset {
address: share_token_address,
amount: 1_000_000i128, // this has not changed
address: share_token_address.clone(),
amount: liquidity_amount, // Liquidity pool share remains unchanged
},
stake_address: result.clone().stake_address,
stake_address: result.stake_address.clone(),
}
);
assert_eq!(output_amount, 1000);
assert_eq!(token1.balance(&user1), 1999); // 999 + 1_000 as a result of swap
assert_eq!(token2.balance(&user1), 1001 - 1000); // user1 sold 1k of token B on second swap

assert_eq!(output_amount_2, 1_009_900_009_901); // Expected output after accounting for the spread
assert_eq!(token1.balance(&user1), 10_009_900_009_901); // Reflecting final balances after swaps
assert_eq!(token2.balance(&user1), 9_990_099_009_901); // Reflecting final balances after swaps
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion contracts/stake_rewards/src/tests/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn deploy_staking_rewards_contract<'a>(

staking_rewards.initialize(
admin,
&staking_contract,
staking_contract,
reward_token,
&MAX_COMPLEXITY,
&MIN_REWARD,
Expand Down
Loading