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

Group e2e tests #95

Merged
merged 8 commits into from
Dec 5, 2023
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
4,114 changes: 4 additions & 4,110 deletions src/lib.rs

Large diffs are not rendered by default.

89 changes: 54 additions & 35 deletions src/test_helpers/snippets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ macro_rules! init_basic_pool {
tick_spacing: 10,
};
let alice = ink_e2e::alice();
add_fee_tier!($client, $dex, $dex_address, fee_tier, alice);
add_fee_tier!($client, $dex, $dex_address, fee_tier, alice).unwrap();

let init_tick = 0;
create_pool!(
Expand All @@ -206,7 +206,8 @@ macro_rules! init_basic_pool {
fee_tier,
init_tick,
alice
);
)
.unwrap();
}};
}

Expand All @@ -218,7 +219,7 @@ macro_rules! init_slippage_pool_with_liquidity {
tick_spacing: 10,
};
let alice = ink_e2e::alice();
add_fee_tier!($client, $dex, $dex_address, fee_tier, alice);
add_fee_tier!($client, $dex, $dex_address, fee_tier, alice).unwrap();

let init_tick = 0;
create_pool!(
Expand All @@ -230,7 +231,8 @@ macro_rules! init_slippage_pool_with_liquidity {
fee_tier,
init_tick,
alice
);
)
.unwrap();
let fee_tier = FeeTier {
fee: Percentage::from_scale(6, 3),
tick_spacing: 10,
Expand All @@ -245,15 +247,17 @@ macro_rules! init_slippage_pool_with_liquidity {
$dex_address,
mint_amount,
alice
);
)
.unwrap();
approve!(
$client,
$token,
$token_y_address,
$dex_address,
mint_amount,
alice
);
)
.unwrap();

let pool_key = PoolKey::new($token_x_address, $token_y_address, fee_tier).unwrap();
let lower_tick = -1000;
Expand Down Expand Up @@ -282,7 +286,8 @@ macro_rules! init_slippage_pool_with_liquidity {
slippage_limit_lower,
slippage_limit_upper,
alice
);
)
.unwrap();

let pool_after = get_pool!(
$client,
Expand Down Expand Up @@ -317,15 +322,17 @@ macro_rules! init_basic_position {
$dex_address,
mint_amount,
alice
);
)
.unwrap();
approve!(
$client,
$token,
$token_y_address,
$dex_address,
mint_amount,
alice
);
)
.unwrap();

let pool_key = PoolKey::new($token_x_address, $token_y_address, fee_tier).unwrap();
let lower_tick = -20;
Expand Down Expand Up @@ -354,7 +361,8 @@ macro_rules! init_basic_position {
slippage_limit_lower,
slippage_limit_upper,
alice
);
)
.unwrap();

let pool_after = get_pool!(
$client,
Expand Down Expand Up @@ -387,15 +395,17 @@ macro_rules! init_cross_position {
$dex_address,
mint_amount,
alice
);
)
.unwrap();
approve!(
$client,
$token,
$token_y_address,
$dex_address,
mint_amount,
alice
);
)
.unwrap();

let pool_key = PoolKey::new($token_x_address, $token_y_address, fee_tier).unwrap();
let lower_tick = -40;
Expand Down Expand Up @@ -424,7 +434,8 @@ macro_rules! init_cross_position {
slippage_limit_lower,
slippage_limit_upper,
alice
);
)
.unwrap();

let pool_after = get_pool!(
$client,
Expand Down Expand Up @@ -458,10 +469,11 @@ macro_rules! init_basic_swap {
address_of!(Bob),
amount,
bob
);
)
.unwrap();
let amount_x = balance_of!($client, $token, $token_x_address, address_of!(Bob));
assert_eq!(amount_x, amount);
approve!($client, $token, $token_x_address, $dex_address, amount, bob);
approve!($client, $token, $token_x_address, $dex_address, amount, bob).unwrap();

let amount_x = balance_of!($client, $token, $token_x_address, $dex_address);
let amount_y = balance_of!($client, $token, $token_y_address, $dex_address);
Expand Down Expand Up @@ -490,7 +502,8 @@ macro_rules! init_basic_swap {
true,
slippage,
bob
);
)
.unwrap();

let pool_after = get_pool!(
$client,
Expand Down Expand Up @@ -544,10 +557,11 @@ macro_rules! init_cross_swap {
address_of!(Bob),
amount,
bob
);
)
.unwrap();
let amount_x = balance_of!($client, $token, $token_x_address, address_of!(Bob));
assert_eq!(amount_x, amount);
approve!($client, $token, $token_x_address, $dex_address, amount, bob);
approve!($client, $token, $token_x_address, $dex_address, amount, bob).unwrap();

let amount_x = balance_of!($client, $token, $token_x_address, $dex_address);
let amount_y = balance_of!($client, $token, $token_y_address, $dex_address);
Expand Down Expand Up @@ -576,7 +590,8 @@ macro_rules! init_cross_swap {
true,
slippage,
bob
);
)
.unwrap();

let pool_after = get_pool!(
$client,
Expand Down Expand Up @@ -646,7 +661,8 @@ macro_rules! swap_exact_limit {
$by_amount_in,
quote_result.target_sqrt_price,
$caller
);
)
.unwrap();
}};
}

Expand All @@ -657,17 +673,17 @@ macro_rules! big_deposit_and_swap {

let mint_amount = 2u128.pow(75) - 1;
let alice = ink_e2e::alice();
approve!($client, $token, token_x, dex, u128::MAX, alice);
approve!($client, $token, token_y, dex, u128::MAX, alice);
approve!($client, $token, token_x, dex, u128::MAX, alice).unwrap();
approve!($client, $token, token_y, dex, u128::MAX, alice).unwrap();

let fee_tier = FeeTier {
fee: Percentage::from_scale(6, 3),
tick_spacing: 1,
};
add_fee_tier!($client, $dex, dex, fee_tier, alice);
add_fee_tier!($client, $dex, dex, fee_tier, alice).unwrap();

let init_tick = 0;
create_pool!($client, $dex, dex, token_x, token_y, fee_tier, init_tick, alice);
create_pool!($client, $dex, dex, token_x, token_y, fee_tier, init_tick, alice).unwrap();

let lower_tick = if $x_to_y {
-(fee_tier.tick_spacing as i32)
Expand Down Expand Up @@ -717,7 +733,8 @@ macro_rules! big_deposit_and_swap {
slippage_limit_lower,
slippage_limit_upper,
alice
);
)
.unwrap();

let amount_x = balance_of!($client, $token, token_x, address_of!(Alice));
let amount_y = balance_of!($client, $token, token_y, address_of!(Alice));
Expand Down Expand Up @@ -745,7 +762,8 @@ macro_rules! big_deposit_and_swap {
true,
sqrt_price_limit,
alice
);
)
.unwrap();

let amount_x = balance_of!($client, $token, token_x, address_of!(Alice));
let amount_y = balance_of!($client, $token, token_y, address_of!(Alice));
Expand All @@ -769,14 +787,14 @@ macro_rules! multiple_swap {
tick_spacing: 1,
};
let alice = ink_e2e::alice();
add_fee_tier!($client, $dex, dex, fee_tier, alice);
add_fee_tier!($client, $dex, dex, fee_tier, alice).unwrap();

let init_tick = 0;
create_pool!($client, $dex, dex, token_x, token_y, fee_tier, init_tick, alice);
create_pool!($client, $dex, dex, token_x, token_y, fee_tier, init_tick, alice).unwrap();

let mint_amount = 10u128.pow(10);
approve!($client, $token, token_x, dex, mint_amount, alice);
approve!($client, $token, token_y, dex, mint_amount, alice);
approve!($client, $token, token_x, dex, mint_amount, alice).unwrap();
approve!($client, $token, token_y, dex, mint_amount, alice).unwrap();

let pool_key = PoolKey::new(token_x, token_y, fee_tier).unwrap();
let mut upper_tick = 953;
Expand Down Expand Up @@ -810,19 +828,20 @@ macro_rules! multiple_swap {
slippage_limit_lower,
slippage_limit_upper,
alice
);
)
.unwrap();

let bob = ink_e2e::bob();
if $x_to_y {
mint!($client, $token, token_x, address_of!(Bob), amount, bob);
mint!($client, $token, token_x, address_of!(Bob), amount, bob).unwrap();
let amount_x = balance_of!($client, $token, token_x, address_of!(Bob));
assert_eq!(amount_x, amount);
approve!($client, $token, token_x, dex, amount, bob);
approve!($client, $token, token_x, dex, amount, bob).unwrap();
} else {
mint!($client, $token, token_y, address_of!(Bob), amount, bob);
mint!($client, $token, token_y, address_of!(Bob), amount, bob).unwrap();
let amount_y = balance_of!($client, $token, token_y, address_of!(Bob));
assert_eq!(amount_y, amount);
approve!($client, $token, token_y, dex, amount, bob);
approve!($client, $token, token_y, dex, amount, bob).unwrap();
}

let swap_amount = TokenAmount(10);
Expand Down
103 changes: 103 additions & 0 deletions src/tests/add_fee_tier.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#[cfg(test)]
pub mod e2e_tests {
use crate::math::types::percentage::Percentage;
use crate::{
contract::ContractRef,
contracts::{entrypoints::Invariant, FeeTier},
};
use decimal::*;
use ink_e2e::build_message;
use test_helpers::{add_fee_tier, create_dex, fee_tier_exist, get_fee_tiers};

type E2EResult<T> = Result<T, Box<dyn std::error::Error>>;

#[ink_e2e::test]
async fn add_multiple_fee_tiers(mut client: ink_e2e::Client<C, E>) -> E2EResult<()> {
let admin = ink_e2e::alice();
let dex = create_dex!(client, ContractRef, Percentage::new(0));

let first_fee_tier = FeeTier::new(Percentage::from_scale(2, 4), 1).unwrap();
add_fee_tier!(client, ContractRef, dex, first_fee_tier, admin).unwrap();

let second_fee_tier = FeeTier::new(Percentage::from_scale(2, 4), 2).unwrap();
add_fee_tier!(client, ContractRef, dex, second_fee_tier, admin).unwrap();

let third_fee_tier = FeeTier::new(Percentage::from_scale(2, 4), 4).unwrap();
add_fee_tier!(client, ContractRef, dex, third_fee_tier, admin).unwrap();

let exist = fee_tier_exist!(
client,
ContractRef,
dex,
FeeTier::new(Percentage::from_scale(2, 4), 1u16).unwrap()
);
assert!(exist);

let exist = fee_tier_exist!(
client,
ContractRef,
dex,
FeeTier::new(Percentage::from_scale(2, 4), 2u16).unwrap()
);
assert!(exist);

let exist = fee_tier_exist!(
client,
ContractRef,
dex,
FeeTier::new(Percentage::from_scale(2, 4), 4u16).unwrap()
);
assert!(exist);

let fee_tiers = get_fee_tiers!(client, ContractRef, dex);
assert_eq!(fee_tiers.len(), 3);
assert_eq!(fee_tiers[0], first_fee_tier);
assert_eq!(fee_tiers[1], second_fee_tier);
assert_eq!(fee_tiers[2], third_fee_tier);

Ok(())
}

#[ink_e2e::test]
#[should_panic]
async fn add_existing_fee_tier(mut client: ink_e2e::Client<C, E>) -> () {
let admin = ink_e2e::alice();
let dex = create_dex!(client, ContractRef, Percentage::new(0));

let fee_tier = FeeTier::new(Percentage::from_scale(2, 4), 1).unwrap();
add_fee_tier!(client, ContractRef, dex, fee_tier, admin).unwrap();

let fee_tier = FeeTier::new(Percentage::from_scale(2, 4), 1).unwrap();
add_fee_tier!(client, ContractRef, dex, fee_tier, admin).unwrap();
}

#[ink_e2e::test]
#[should_panic]
async fn add_fee_tier_not_admin(mut client: ink_e2e::Client<C, E>) -> () {
let user = ink_e2e::bob();
let dex = create_dex!(client, ContractRef, Percentage::new(0));

let fee_tier = FeeTier::new(Percentage::from_scale(2, 4), 1).unwrap();
add_fee_tier!(client, ContractRef, dex, fee_tier, user).unwrap();
}

#[ink_e2e::test]
async fn add_fee_tier_zero_fee(mut client: ink_e2e::Client<C, E>) -> E2EResult<()> {
let admin = ink_e2e::alice();
let dex = create_dex!(client, ContractRef, Percentage::new(0));

let fee_tier = FeeTier::new(Percentage::new(0), 10).unwrap();
add_fee_tier!(client, ContractRef, dex, fee_tier, admin).unwrap();
Ok(())
}

#[ink_e2e::test]
#[should_panic]
async fn add_fee_tier_tick_spacing_zero(mut client: ink_e2e::Client<C, E>) -> () {
let admin = ink_e2e::alice();
let dex = create_dex!(client, ContractRef, Percentage::new(0));

let fee_tier = FeeTier::new(Percentage::from_scale(2, 4), 0).unwrap();
add_fee_tier!(client, ContractRef, dex, fee_tier, admin).unwrap();
}
}
Loading
Loading