Skip to content

Commit

Permalink
Pool: adds parametarized test instead of 3 separate ones
Browse files Browse the repository at this point in the history
  • Loading branch information
gangov committed Dec 5, 2023
1 parent 2c5f9d2 commit 690f7a7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 154 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions contracts/pool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ soroban-sdk = { workspace = true }
[dev_dependencies]
soroban-sdk = { workspace = true, features = ["testutils"] }
pretty_assertions = { workspace = true }
test-case = "3.3.1"
167 changes: 15 additions & 152 deletions contracts/pool/src/tests/swap.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
extern crate std;

use pretty_assertions::assert_eq;
use soroban_sdk::testutils::{AuthorizedFunction, AuthorizedInvocation};
use soroban_sdk::{symbol_short, testutils::Address as _, Address, Env, IntoVal};
use test_case::test_case;

use super::setup::{deploy_liquidity_pool_contract, deploy_token_contract};
use crate::storage::{
Expand Down Expand Up @@ -640,81 +640,10 @@ fn swap_simulation_one_third_pool() {
);
}

#[test]
fn bug_issue_169_10_percent() {
let env = Env::default();
env.mock_all_auths();
env.budget().reset_unlimited();

let mut token1 = deploy_token_contract(&env, &Address::random(&env));
let mut token2 = deploy_token_contract(&env, &Address::random(&env));
if token2.address < token1.address {
std::mem::swap(&mut token1, &mut token2);
}

let swap_fees = 1_000i64; // 10% bps
let pool = deploy_liquidity_pool_contract(
&env,
None,
(&token1.address, &token2.address),
swap_fees,
Address::random(&env),
10_000i64,
10_000i64,
);

let initial_liquidity = 110_358_880_127; // taken from the current amount of tokens in pool
let user1 = Address::random(&env);
token1.mint(&user1, &initial_liquidity);
token2.mint(&user1, &initial_liquidity);
pool.provide_liquidity(
&user1,
&Some(initial_liquidity),
&Some(initial_liquidity),
&Some(initial_liquidity),
&Some(initial_liquidity),
&None,
);

// simulating a swap with 1_000_000_000 units
let offer_amount = 1_000_000_000i128;
let result = pool.simulate_swap(&token1.address, &offer_amount);

// XYK pool formula as follows
// Y_new = (X_in * Y_old) / (X_in + X_old)
// Y_new = (1_000_000_000 * 110358880127) / (1_000_000_000 + 110358880127)
// Y_new = 991020024.637
// Y_rnd = 991020025
let output_amount = 991020025; // rounding
let fees = Decimal::percent(10) * output_amount;
assert_eq!(
result,
SimulateSwapResponse {
ask_amount: output_amount - fees,
spread_amount: 8979975,
commission_amount: fees,
total_return: 1000000000,
}
);

// 991020025 is the request, so 10% of that should be what's on the left hand side
assert_eq!(99102002, result.commission_amount);

let result = pool.simulate_reverse_swap(&token1.address, &(output_amount - fees));
let output_amount = 991020025i128;
let fees = Decimal::percent(10) * output_amount;
assert_eq!(
result,
SimulateReverseSwapResponse {
offer_amount: 1000000000i128,
spread_amount: Decimal::from_ratio(offer_amount, initial_liquidity) * output_amount,
commission_amount: fees,
}
);
}

#[test]
fn bug_issue_169_1_percent() {
#[test_case(1_000i64, 10, 99102002 ; "when fee is 10%")]
#[test_case(100, 1, 9910200 ; "when fee is 1%")]
#[test_case(30, 30, 2973060 ; "when fee is 0.3%")]
fn test_swap_fee_variants(swap_fees: i64, fee_percentage: i64, commission_fee: i128) {
let env = Env::default();
env.mock_all_auths();
env.budget().reset_unlimited();
Expand All @@ -725,7 +654,7 @@ fn bug_issue_169_1_percent() {
std::mem::swap(&mut token1, &mut token2);
}

let swap_fees = 1_00i64; // 1% bps
let swap_fees = swap_fees; // 10% bps
let pool = deploy_liquidity_pool_contract(
&env,
None,
Expand Down Expand Up @@ -756,83 +685,17 @@ fn bug_issue_169_1_percent() {
// XYK pool formula as follows
// Y_new = (X_in * Y_old) / (X_in + X_old)
// Y_new = (1_000_000_000 * 110358880127) / (1_000_000_000 + 110358880127)
// Y_new = 991020024.637
// Y_new = 991020024.637
// Y_rnd = 991020025
let output_amount = 991020025; // rounding
let fees = Decimal::percent(1) * output_amount;
assert_eq!(
result,
SimulateSwapResponse {
ask_amount: output_amount - fees,
spread_amount: 8979975,
commission_amount: fees,
total_return: 1000000000,
}
);

// 991020025 is the request, so 1% of that should be what's on the left hand side
assert_eq!(9910200, result.commission_amount);

let result = pool.simulate_reverse_swap(&token1.address, &(output_amount - fees));
let output_amount = 991020025i128;
let fees = Decimal::percent(1) * output_amount;
assert_eq!(
result,
SimulateReverseSwapResponse {
offer_amount: 1000000000i128,
spread_amount: Decimal::from_ratio(offer_amount, initial_liquidity) * output_amount,
commission_amount: fees,
}
);
}
let fees;

#[test]
fn bug_issue_169_below_1_percent() {
let env = Env::default();
env.mock_all_auths();
env.budget().reset_unlimited();

let mut token1 = deploy_token_contract(&env, &Address::random(&env));
let mut token2 = deploy_token_contract(&env, &Address::random(&env));
if token2.address < token1.address {
std::mem::swap(&mut token1, &mut token2);
if swap_fees < 100 {
fees = Decimal::bps(fee_percentage) * output_amount;
} else {
fees = Decimal::percent(fee_percentage) * output_amount;
}

let swap_fees = 30i64; // 1% bps
let pool = deploy_liquidity_pool_contract(
&env,
None,
(&token1.address, &token2.address),
swap_fees,
Address::random(&env),
10_000i64,
10_000i64,
);

let initial_liquidity = 110_358_880_127; // taken from the current amount of tokens in pool
let user1 = Address::random(&env);
token1.mint(&user1, &initial_liquidity);
token2.mint(&user1, &initial_liquidity);
pool.provide_liquidity(
&user1,
&Some(initial_liquidity),
&Some(initial_liquidity),
&Some(initial_liquidity),
&Some(initial_liquidity),
&None,
);

// simulating a swap with 1_000_000_000 units
let offer_amount = 1_000_000_000i128;
let result = pool.simulate_swap(&token1.address, &offer_amount);

// XYK pool formula as follows
// Y_new = (X_in * Y_old) / (X_in + X_old)
// Y_new = (1_000_000_000 * 110358880127) / (1_000_000_000 + 110358880127)
// Y_new = 991020024.637
// Y_rnd = 991020025
let output_amount = 991020025; // rounding
let fees = Decimal::bps(30) * output_amount;
assert_eq!(
result,
SimulateSwapResponse {
Expand All @@ -843,12 +706,12 @@ fn bug_issue_169_below_1_percent() {
}
);

// 991020025 is the request, so 0.3% of that should be what's on the left hand side
assert_eq!(2973060, result.commission_amount);
// 991020025 is the request, so 10% of that should be what's on the left hand side
assert_eq!(commission_fee, result.commission_amount);

let result = pool.simulate_reverse_swap(&token1.address, &(output_amount - fees));
let output_amount = 991020025i128;
let fees = Decimal::bps(30) * output_amount;
// let fees = Decimal::percent(fee_percentage) * output_amount;
assert_eq!(
result,
SimulateReverseSwapResponse {
Expand Down

0 comments on commit 690f7a7

Please sign in to comment.