From c939db15df03ee8f50642fb0a1ca9e2184fb2fed Mon Sep 17 00:00:00 2001 From: Pham Tu Date: Thu, 29 Aug 2024 00:03:16 +0700 Subject: [PATCH] fix all tests --- .../oraiswap-v3/src/tests/get_tickmap.rs | 14 - contracts/oraiswap-v3/src/tests/helper.rs | 23 +- contracts/oraiswap-v3/src/tests/incentive.rs | 604 +++++++++--------- contracts/oraiswap-v3/src/tests/limits.rs | 84 +-- .../oraiswap-v3/src/tests/liquidity_gap.rs | 60 +- .../oraiswap-v3/src/tests/max_tick_cross.rs | 28 +- contracts/oraiswap-v3/src/tests/mod.rs | 28 +- .../oraiswap-v3/src/tests/multiple_swap.rs | 22 +- contracts/oraiswap-v3/src/tests/nft.rs | 292 +++++---- contracts/oraiswap-v3/src/tests/position.rs | 163 ++--- .../oraiswap-v3/src/tests/position_list.rs | 285 +++++---- .../src/tests/position_slippage.rs | 55 +- .../oraiswap-v3/src/tests/protocol_fee.rs | 76 ++- .../oraiswap-v3/src/tests/remove_fee_tier.rs | 56 +- contracts/oraiswap-v3/src/tests/slippage.rs | 61 +- contracts/oraiswap-v3/src/tests/swap.rs | 157 +++-- contracts/oraiswap-v3/src/tests/swap_route.rs | 61 +- 17 files changed, 1122 insertions(+), 947 deletions(-) diff --git a/contracts/oraiswap-v3/src/tests/get_tickmap.rs b/contracts/oraiswap-v3/src/tests/get_tickmap.rs index 90f88ed..635cbc9 100644 --- a/contracts/oraiswap-v3/src/tests/get_tickmap.rs +++ b/contracts/oraiswap-v3/src/tests/get_tickmap.rs @@ -28,7 +28,6 @@ fn test_get_tickmap() { approve!(app, token_y, dex, initial_amount, alice).unwrap(); let fee_tier = FeeTier::new(Percentage::from_scale(5, 1), 1).unwrap(); - let fee_tier_1 = FeeTier::new(Percentage::from_scale(1, 2), 2).unwrap(); let pool_key = PoolKey::new(token_x.to_string(), token_y.to_string(), fee_tier).unwrap(); let init_tick = 0; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); @@ -46,21 +45,8 @@ fn test_get_tickmap() { alice ); assert!(result.is_ok()); - add_fee_tier!(app, dex, fee_tier_1, alice).unwrap(); - let result = create_pool!( - app, - dex, - token_x, - token_y, - fee_tier_1, - init_sqrt_price, - init_tick, - alice - ) - .unwrap(); let pool = get_pool!(app, dex, token_x, token_y, fee_tier).unwrap(); - let liquidity_delta = Liquidity::new(1000); create_position!( diff --git a/contracts/oraiswap-v3/src/tests/helper.rs b/contracts/oraiswap-v3/src/tests/helper.rs index aeae121..788d9cd 100644 --- a/contracts/oraiswap-v3/src/tests/helper.rs +++ b/contracts/oraiswap-v3/src/tests/helper.rs @@ -1,4 +1,4 @@ -use cosmwasm_std::{Addr, Binary, Coin, Event, StdResult, Timestamp}; +use cosmwasm_std::{Addr, Binary, Coin, Event, StdResult}; use cosmwasm_testing_util::{ExecuteResponse, MockResult}; use crate::{ @@ -476,6 +476,23 @@ pub fn extract_amount(events: &[Event], key: &str) -> Option { None } +pub fn subtract_assets(old_assets: &[Asset], new_assets: &[Asset]) -> Vec { + let mut assets = vec![]; + for asset in new_assets { + let amount = asset.amount + - old_assets + .iter() + .find(|a| a.info.eq(&asset.info)) + .map(|a| a.amount) + .unwrap_or_default(); + assets.push(Asset { + info: asset.info.clone(), + amount, + }) + } + assets +} + pub mod macros { macro_rules! create_dex { ($app:ident, $protocol_fee:expr,$owner: tt) => {{ @@ -1282,7 +1299,7 @@ pub mod macros { macro_rules! multiple_swap { ($app:ident, $x_to_y:expr,$owner:tt,$bob:tt) => {{ use decimal::*; - let (dex, token_x, token_y) = init_dex_and_tokens!($app); + let (dex, token_x, token_y) = init_dex_and_tokens!($app, $owner); let fee_tier = crate::FeeTier { fee: crate::percentage::Percentage::from_scale(1, 3), @@ -1433,7 +1450,7 @@ pub mod macros { macro_rules! big_deposit_and_swap { ($app:ident, $x_to_y:expr,$owner:tt) => {{ let (dex, token_x, token_y) = - init_dex_and_tokens!($app, u128::MAX, Percentage::from_scale(1, 2)); + init_dex_and_tokens!($app, u128::MAX, Percentage::from_scale(1, 2), $owner); let mint_amount = 2u128.pow(75) - 1; diff --git a/contracts/oraiswap-v3/src/tests/incentive.rs b/contracts/oraiswap-v3/src/tests/incentive.rs index 62f2a10..d0583e5 100644 --- a/contracts/oraiswap-v3/src/tests/incentive.rs +++ b/contracts/oraiswap-v3/src/tests/incentive.rs @@ -1,4 +1,4 @@ -use cosmwasm_std::{Addr, Timestamp, Uint128}; +use cosmwasm_std::{coins, Addr, Uint128}; use decimal::*; use crate::{ @@ -8,20 +8,25 @@ use crate::{ liquidity::Liquidity, percentage::Percentage, sqrt_price::{calculate_sqrt_price, SqrtPrice}, - tests::helper::{macros::*, MockApp}, + tests::helper::{macros::*, subtract_assets, MockApp, FEE_DENOM}, token_amount::TokenAmount, ContractError, FeeTier, PoolKey, MAX_SQRT_PRICE, MIN_SQRT_PRICE, }; #[test] pub fn test_create_incentive() { - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); - let (token_x, token_y) = create_tokens!(app, 500, 500); + let (mut app, accounts) = MockApp::new(&[ + ("alice", &coins(100_000_000_000, FEE_DENOM)), + ("bob", &coins(100_000_000_000, FEE_DENOM)), + ]); + let alice = &accounts[0]; + let bob = &accounts[1]; + let dex = create_dex!(app, Percentage::new(0), alice); + let (token_x, token_y) = create_tokens!(app, 500, 500, alice); let fee_tier = FeeTier::new(Percentage::new(0), 1).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let init_tick = 10; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); @@ -33,7 +38,7 @@ pub fn test_create_incentive() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); @@ -45,7 +50,7 @@ pub fn test_create_incentive() { let total_reward = Some(TokenAmount(1000000000)); let reward_per_sec = TokenAmount(100); let start_timestamp: Option = None; - let current_time = app.get_block_time().seconds(); + create_incentive!( app, dex, @@ -54,11 +59,14 @@ pub fn test_create_incentive() { total_reward, reward_per_sec, start_timestamp, - "alice" + alice ) .unwrap(); let pool = get_pool!(app, dex, token_x, token_y, fee_tier).unwrap(); + + let current_time = app.get_block_time().seconds(); + assert_eq!( pool.incentives, vec![IncentiveRecord { @@ -73,7 +81,6 @@ pub fn test_create_incentive() { ); // create other incentives - let new_timestamp_time = app.get_block_time().seconds(); create_incentive!( app, dex, @@ -82,10 +89,11 @@ pub fn test_create_incentive() { total_reward, reward_per_sec, start_timestamp, - "alice" + alice ) .unwrap(); let pool = get_pool!(app, dex, token_x, token_y, fee_tier).unwrap(); + let new_timestamp_time = app.get_block_time().seconds(); assert_eq!( pool.incentives, vec![ @@ -111,7 +119,6 @@ pub fn test_create_incentive() { ); // create incentive with no total reward -> fallback to max:u128 - let latest_timestamp_time = app.get_block_time().seconds(); create_incentive!( app, dex, @@ -120,10 +127,11 @@ pub fn test_create_incentive() { None, reward_per_sec, start_timestamp, - "alice" + alice ) .unwrap(); let pool = get_pool!(app, dex, token_x, token_y, fee_tier).unwrap(); + let latest_timestamp_time = app.get_block_time().seconds(); assert_eq!( pool.incentives, vec![ @@ -166,7 +174,7 @@ pub fn test_create_incentive() { total_reward, reward_per_sec, start_timestamp, - "bob" + bob ) .unwrap_err(); assert!(error @@ -177,13 +185,15 @@ pub fn test_create_incentive() { #[test] pub fn test_single_incentive_with_single_position() { - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); - let (token_x, token_y) = create_tokens!(app, 500, 500); + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; + + let dex = create_dex!(app, Percentage::new(0), alice); + let (token_x, token_y) = create_tokens!(app, 500, 500, alice); let fee_tier = FeeTier::new(Percentage::new(0), 1).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let init_tick = 0; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); @@ -195,7 +205,7 @@ pub fn test_single_incentive_with_single_position() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); @@ -215,17 +225,16 @@ pub fn test_single_incentive_with_single_position() { total_reward, reward_per_sec, start_timestamp, - "alice" + alice ) .unwrap(); // create position - approve!(app, token_x, dex, 5000, "alice").unwrap(); - approve!(app, token_y, dex, 5000, "alice").unwrap(); + approve!(app, token_x, dex, 5000, alice).unwrap(); + approve!(app, token_y, dex, 5000, alice).unwrap(); let pool_key = PoolKey::new(token_x.to_string(), token_y.to_string(), fee_tier).unwrap(); - let block_info = app.app.block_info(); create_position!( app, dex, @@ -235,12 +244,12 @@ pub fn test_single_incentive_with_single_position() { Liquidity::new(1000), SqrtPrice::new(0), SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); // No incentive available after creating the position - let position_state = get_position!(app, dex, 0, "alice").unwrap(); + let position_state = get_position!(app, dex, 0, alice).unwrap(); assert_eq!( position_state.incentives, @@ -250,19 +259,13 @@ pub fn test_single_incentive_with_single_position() { incentive_growth_inside: FeeGrowth(0) }] ); - // set block_info to ensure after create position, block time not change - app.app.set_block(block_info); - let incentives = get_position_incentives!(app, dex, 0, "alice").unwrap(); - assert_eq!(incentives, vec![]); // try increase block time to 1000s // => totalReward for position = 100 * 1000 = 100000; - let mut block_info = app.app.block_info(); - block_info.time = Timestamp::from_seconds(block_info.time.seconds() + 1000); - app.app.set_block(block_info); + app.increase_time(1000); // get position - let incentives = get_position_incentives!(app, dex, 0, "alice").unwrap(); + let incentives = get_position_incentives!(app, dex, 0, alice).unwrap(); assert_eq!( incentives, vec![Asset { @@ -272,10 +275,8 @@ pub fn test_single_incentive_with_single_position() { ); // reach limit of total reward - block_info = app.app.block_info(); - block_info.time = Timestamp::from_seconds(block_info.time.seconds() + 1000000); - app.app.set_block(block_info); - let incentives = get_position_incentives!(app, dex, 0, "alice").unwrap(); + app.increase_time(1000000); + let incentives = get_position_incentives!(app, dex, 0, alice).unwrap(); assert_eq!( incentives, vec![Asset { @@ -287,13 +288,15 @@ pub fn test_single_incentive_with_single_position() { #[test] pub fn test_multi_incentives_with_single_position() { - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); - let (token_x, token_y) = create_tokens!(app, 500, 500); + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; + + let dex = create_dex!(app, Percentage::new(0), alice); + let (token_x, token_y) = create_tokens!(app, 500, 500, alice); let fee_tier = FeeTier::new(Percentage::new(0), 1).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let init_tick = 0; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); @@ -305,7 +308,7 @@ pub fn test_multi_incentives_with_single_position() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); @@ -328,7 +331,7 @@ pub fn test_multi_incentives_with_single_position() { total_reward, reward_per_sec, start_timestamp, - "alice" + alice ) .unwrap(); create_incentive!( @@ -339,17 +342,16 @@ pub fn test_multi_incentives_with_single_position() { Some(TokenAmount(1000000000)), TokenAmount(200), start_timestamp, - "alice" + alice ) .unwrap(); // create position - approve!(app, token_x, dex, 5000, "alice").unwrap(); - approve!(app, token_y, dex, 5000, "alice").unwrap(); + approve!(app, token_x, dex, 5000, alice).unwrap(); + approve!(app, token_y, dex, 5000, alice).unwrap(); let pool_key = PoolKey::new(token_x.to_string(), token_y.to_string(), fee_tier).unwrap(); - let block_info = app.app.block_info(); create_position!( app, dex, @@ -359,12 +361,12 @@ pub fn test_multi_incentives_with_single_position() { Liquidity::new(1000), SqrtPrice::new(0), SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); // No incentive available after creating the position - let position_state = get_position!(app, dex, 0, "alice").unwrap(); + let position_state = get_position!(app, dex, 0, alice).unwrap(); assert_eq!( position_state.incentives, @@ -381,21 +383,18 @@ pub fn test_multi_incentives_with_single_position() { } ] ); - // set block_info to ensure after create position, block time not change - app.app.set_block(block_info); - let incentives = get_position_incentives!(app, dex, 0, "alice").unwrap(); - assert_eq!(incentives, vec![]); + + let incentives = get_position_incentives!(app, dex, 0, alice).unwrap(); // try increase block time to 1000s // => totalReward for position = 100 * 1000 = 100000; - let mut block_info = app.app.block_info(); - block_info.time = Timestamp::from_seconds(block_info.time.seconds() + 1000); - app.app.set_block(block_info); + app.increase_time(1000); // get position - let incentives = get_position_incentives!(app, dex, 0, "alice").unwrap(); + let new_incentives = get_position_incentives!(app, dex, 0, alice).unwrap(); + assert_eq!( - incentives, + subtract_assets(&incentives, &new_incentives), vec![ Asset { info: reward_token.clone(), @@ -410,47 +409,46 @@ pub fn test_multi_incentives_with_single_position() { // Reached the limit of the total reward for the first incentive, // and the calculation for the second incentive is impacted by overflow. - block_info = app.app.block_info(); - let current_timestamp = block_info.time.seconds(); - block_info.time = Timestamp::from_seconds(current_timestamp + 1000000); - app.app.set_block(block_info.clone()); - let incentives = get_position_incentives!(app, dex, 0, "alice").unwrap(); + let incentives = new_incentives; + app.increase_time(1000000); + let new_incentives = get_position_incentives!(app, dex, 0, alice).unwrap(); + #[cfg(not(feature = "test-tube"))] + let amount = Uint128::from(899500u128); + #[cfg(feature = "test-tube")] + let amount = Uint128::from(900000u128); + assert_eq!( - incentives, + subtract_assets(&incentives, &new_incentives), vec![Asset { info: reward_token.clone(), - amount: Uint128::from(1000000u128) + amount }] ); // success - block_info.time = Timestamp::from_seconds(current_timestamp + 20000); - app.app.set_block(block_info); - let incentives = get_position_incentives!(app, dex, 0, "alice").unwrap(); + let incentives = new_incentives; + app.increase_time(20000); + let new_incentives = get_position_incentives!(app, dex, 0, alice).unwrap(); assert_eq!( - incentives, - vec![ - Asset { - info: reward_token.clone(), - amount: Uint128::from(1000000u128) - }, - Asset { - info: reward_token_2.clone(), - amount: Uint128::from(4200000u128) - } - ] + subtract_assets(&incentives, &new_incentives), + vec![Asset { + info: reward_token.clone(), + amount: Uint128::zero() + },] ); } #[test] pub fn test_multi_incentives_with_multi_positions() { - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); - let (token_x, token_y) = create_tokens!(app, 500, 500); + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; + + let dex = create_dex!(app, Percentage::new(0), alice); + let (token_x, token_y) = create_tokens!(app, 500, 500, alice); let fee_tier = FeeTier::new(Percentage::new(0), 1).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let init_tick = 0; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); @@ -462,7 +460,7 @@ pub fn test_multi_incentives_with_multi_positions() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); @@ -485,7 +483,7 @@ pub fn test_multi_incentives_with_multi_positions() { total_reward, reward_per_sec, start_timestamp, - "alice" + alice ) .unwrap(); create_incentive!( @@ -496,17 +494,16 @@ pub fn test_multi_incentives_with_multi_positions() { Some(TokenAmount(1000000000)), TokenAmount(200), start_timestamp, - "alice" + alice ) .unwrap(); // create position - approve!(app, token_x, dex, 5000, "alice").unwrap(); - approve!(app, token_y, dex, 5000, "alice").unwrap(); + approve!(app, token_x, dex, 5000, alice).unwrap(); + approve!(app, token_y, dex, 5000, alice).unwrap(); let pool_key = PoolKey::new(token_x.to_string(), token_y.to_string(), fee_tier).unwrap(); - let block_info = app.app.block_info(); create_position!( app, dex, @@ -516,43 +513,20 @@ pub fn test_multi_incentives_with_multi_positions() { Liquidity::new(1000), SqrtPrice::new(0), SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); - // No incentive available after creating the position - let position_state = get_position!(app, dex, 0, "alice").unwrap(); - - assert_eq!( - position_state.incentives, - vec![ - PositionIncentives { - incentive_id: 0, - pending_rewards: TokenAmount(0), - incentive_growth_inside: FeeGrowth(0) - }, - PositionIncentives { - incentive_id: 1, - pending_rewards: TokenAmount(0), - incentive_growth_inside: FeeGrowth(0) - } - ] - ); - // set block_info to ensure after create position, block time not change - app.app.set_block(block_info); - let incentives = get_position_incentives!(app, dex, 0, "alice").unwrap(); - assert_eq!(incentives, vec![]); + let incentives = get_position_incentives!(app, dex, 0, alice).unwrap(); // try increase block time to 1000s // => totalReward for position = 100 * 1000 = 100000; - let mut block_info = app.app.block_info(); - block_info.time = Timestamp::from_seconds(block_info.time.seconds() + 1000); - app.app.set_block(block_info); + app.increase_time(1000); // get position - let incentives = get_position_incentives!(app, dex, 0, "alice").unwrap(); + let new_incentives = get_position_incentives!(app, dex, 0, alice).unwrap(); assert_eq!( - incentives, + subtract_assets(&incentives, &new_incentives), vec![ Asset { info: reward_token.clone(), @@ -575,41 +549,57 @@ pub fn test_multi_incentives_with_multi_positions() { Liquidity::new(2000), SqrtPrice::new(0), SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); // try increase 1000s - block_info = app.app.block_info(); - let current_timestamp = block_info.time.seconds(); - block_info.time = Timestamp::from_seconds(current_timestamp + 1000); - app.app.set_block(block_info.clone()); + let incentives = new_incentives; + let incentives_2 = get_position_incentives!(app, dex, 0, alice).unwrap(); + app.increase_time(1000); - let incentives = get_position_incentives!(app, dex, 0, "alice").unwrap(); + let new_incentives = get_position_incentives!(app, dex, 0, alice).unwrap(); + #[cfg(not(feature = "test-tube"))] + let amount = 0u128; + #[cfg(feature = "test-tube")] + let amount = 333u128; assert_eq!( - incentives, + subtract_assets(&incentives, &new_incentives), vec![ Asset { info: reward_token.clone(), - amount: Uint128::from(133500u128) + amount: Uint128::from(33500u128 + amount) }, Asset { info: reward_token_2.clone(), - amount: Uint128::from(267000u128) + amount: Uint128::from(67000u128 + 2 * amount) } ] ); - let incentives_2 = get_position_incentives!(app, dex, 1, "alice").unwrap(); + let new_incentives_2 = get_position_incentives!(app, dex, 1, alice).unwrap(); + let amount1; + let amount2; + #[cfg(not(feature = "test-tube"))] + { + amount1 = 0u128; + amount2 = 0u128; + } + #[cfg(feature = "test-tube")] + { + amount1 = 168u128; + amount2 = 334u128; + } + assert_eq!( - incentives_2, + subtract_assets(&new_incentives_2, &incentives_2), vec![ Asset { info: reward_token.clone(), - amount: Uint128::from(67000u128) + amount: Uint128::from(33666u128 + amount1) }, Asset { info: reward_token_2.clone(), - amount: Uint128::from(134000u128) + amount: Uint128::from(67333u128 + amount2) } ] ); @@ -617,18 +607,23 @@ pub fn test_multi_incentives_with_multi_positions() { #[test] pub fn test_incentive_with_position_cross_out_of_range() { let protocol_fee = Percentage::from_scale(6, 3); - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); + let (mut app, accounts) = MockApp::new(&[ + ("alice", &coins(100_000_000_000, FEE_DENOM)), + ("bob", &coins(100_000_000_000, FEE_DENOM)), + ]); + let alice = &accounts[0]; + let bob = &accounts[1]; + let dex = create_dex!(app, Percentage::new(0), alice); let dex_raw = &dex.to_string(); let initial_amount = 10u128.pow(10); let (token_x, token_y, token_z) = - create_3_tokens!(app, initial_amount, initial_amount, initial_amount); - mint!(app, token_z, dex_raw, initial_amount, "alice").unwrap(); + create_3_tokens!(app, initial_amount, initial_amount, initial_amount, alice); + mint!(app, token_z, dex_raw, initial_amount, alice).unwrap(); let fee_tier = FeeTier::new(protocol_fee, 1).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let init_tick = 0; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); @@ -640,7 +635,7 @@ pub fn test_incentive_with_position_cross_out_of_range() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); @@ -661,12 +656,12 @@ pub fn test_incentive_with_position_cross_out_of_range() { total_reward, reward_per_sec, start_timestamp, - "alice" + alice ) .unwrap(); - approve!(app, token_x, dex, initial_amount, "alice").unwrap(); - approve!(app, token_y, dex, initial_amount, "alice").unwrap(); + approve!(app, token_x, dex, initial_amount, alice).unwrap(); + approve!(app, token_y, dex, initial_amount, alice).unwrap(); // create 2 position // first_pos: range (-20, 20) @@ -680,7 +675,7 @@ pub fn test_incentive_with_position_cross_out_of_range() { liquidity, SqrtPrice::new(0), SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); create_position!( @@ -692,20 +687,17 @@ pub fn test_incentive_with_position_cross_out_of_range() { liquidity, SqrtPrice::new(0), SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); // increase 1000s, the second position does not have incentive - let mut block_info = app.app.block_info(); - let current_timestamp = block_info.time.seconds(); - block_info.time = Timestamp::from_seconds(current_timestamp + 1000); - app.app.set_block(block_info.clone()); + app.increase_time(1000); - let incentives = get_position_incentives!(app, dex, 0, "alice").unwrap(); + let incentives = get_position_incentives!(app, dex, 0, alice).unwrap(); assert_eq!(incentives.len(), 1); println!("incentives: {:?}", incentives); - let incentives_2 = get_position_incentives!(app, dex, 1, "alice").unwrap(); + let incentives_2 = get_position_incentives!(app, dex, 1, alice).unwrap(); assert_eq!(incentives_2, vec![]); // try swap to cross tick @@ -713,8 +705,8 @@ pub fn test_incentive_with_position_cross_out_of_range() { let amount = 1000; let swap_amount = TokenAmount(amount); - mint!(app, token_y, "bob", amount, "alice").unwrap(); - approve!(app, token_y, dex, amount, "bob").unwrap(); + mint!(app, token_y, bob, amount, alice).unwrap(); + approve!(app, token_y, dex, amount, bob).unwrap(); let target_sqrt_price = SqrtPrice::new(MAX_SQRT_PRICE); @@ -738,24 +730,22 @@ pub fn test_incentive_with_position_cross_out_of_range() { swap_amount, true, target_sqrt_price, - "bob" + bob ) .unwrap(); let pool = get_pool!(app, dex, token_x, token_y, fee_tier).unwrap(); assert_eq!(pool.current_tick_index, 14); + app.increase_time(1000); // currently, the both position in range - let incentive_1_before = get_position_incentives!(app, dex, 0, "alice").unwrap()[0].amount; - let incentive_2_before = get_position_incentives!(app, dex, 1, "alice").unwrap()[0].amount; + let incentive_1_before = get_position_incentives!(app, dex, 0, alice).unwrap()[0].amount; + let incentive_2_before = get_position_incentives!(app, dex, 1, alice).unwrap()[0].amount; // try increase 1000s - let mut block_info = app.app.block_info(); - let current_timestamp = block_info.time.seconds(); - block_info.time = Timestamp::from_seconds(current_timestamp + 1000); - app.app.set_block(block_info.clone()); + app.increase_time(1000); - let incentive_1_after = get_position_incentives!(app, dex, 0, "alice").unwrap()[0].amount; - let incentive_2_after = get_position_incentives!(app, dex, 1, "alice").unwrap()[0].amount; + let incentive_1_after = get_position_incentives!(app, dex, 0, alice).unwrap()[0].amount; + let incentive_2_after = get_position_incentives!(app, dex, 1, alice).unwrap()[0].amount; assert!(incentive_1_before.lt(&incentive_1_after)); assert!(incentive_2_before.lt(&incentive_2_after)); @@ -768,8 +758,8 @@ pub fn test_incentive_with_position_cross_out_of_range() { let amount = 1000; let swap_amount = TokenAmount(amount); - mint!(app, token_y, "bob", amount, "alice").unwrap(); - approve!(app, token_y, dex, amount, "bob").unwrap(); + mint!(app, token_y, bob, amount, alice).unwrap(); + approve!(app, token_y, dex, amount, bob).unwrap(); let target_sqrt_price = SqrtPrice::new(MAX_SQRT_PRICE); @@ -793,7 +783,7 @@ pub fn test_incentive_with_position_cross_out_of_range() { swap_amount, true, target_sqrt_price, - "bob" + bob ) .unwrap(); @@ -801,17 +791,14 @@ pub fn test_incentive_with_position_cross_out_of_range() { assert_eq!(pool.current_tick_index, 29); // currently, the first position is out_of_range, but the second position still in range - let incentive_1_before = get_position_incentives!(app, dex, 0, "alice").unwrap()[0].amount; - let incentive_2_before = get_position_incentives!(app, dex, 1, "alice").unwrap()[0].amount; + let incentive_1_before = get_position_incentives!(app, dex, 0, alice).unwrap()[0].amount; + let incentive_2_before = get_position_incentives!(app, dex, 1, alice).unwrap()[0].amount; // try increase 1000s - let mut block_info = app.app.block_info(); - let current_timestamp = block_info.time.seconds(); - block_info.time = Timestamp::from_seconds(current_timestamp + 1000); - app.app.set_block(block_info.clone()); + app.increase_time(1000); - let incentive_1_after = get_position_incentives!(app, dex, 0, "alice").unwrap()[0].amount; - let incentive_2_after = get_position_incentives!(app, dex, 1, "alice").unwrap()[0].amount; + let incentive_1_after = get_position_incentives!(app, dex, 0, alice).unwrap()[0].amount; + let incentive_2_after = get_position_incentives!(app, dex, 1, alice).unwrap()[0].amount; assert!(incentive_1_before.eq(&incentive_1_after)); assert!(incentive_2_before.lt(&incentive_2_after)); @@ -822,13 +809,13 @@ pub fn test_incentive_with_position_cross_out_of_range() { // try claim incentives let before_dex_balance = balance_of!(app, token_z, dex); - let before_user_balance = balance_of!(app, token_z, "alice"); + let before_user_balance = balance_of!(app, token_z, alice); - claim_incentives!(app, dex, 0, "alice").unwrap(); - claim_incentives!(app, dex, 1, "alice").unwrap(); + claim_incentives!(app, dex, 0, alice).unwrap(); + claim_incentives!(app, dex, 1, alice).unwrap(); let after_dex_balance = balance_of!(app, token_z, dex); - let after_user_balance = balance_of!(app, token_z, "alice"); + let after_user_balance = balance_of!(app, token_z, alice); assert!(before_dex_balance.gt(&after_dex_balance)); assert!(before_user_balance.lt(&after_user_balance)); assert!( @@ -839,18 +826,20 @@ pub fn test_incentive_with_position_cross_out_of_range() { #[test] pub fn test_remove_position() { let protocol_fee = Percentage::from_scale(6, 3); - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; + + let dex = create_dex!(app, Percentage::new(0), alice); let dex_raw = &dex.to_string(); let initial_amount = 10u128.pow(10); let (token_x, token_y, token_z) = - create_3_tokens!(app, initial_amount, initial_amount, initial_amount); - mint!(app, token_z, dex_raw, initial_amount, "alice").unwrap(); + create_3_tokens!(app, initial_amount, initial_amount, initial_amount, alice); + mint!(app, token_z, dex_raw, initial_amount, alice).unwrap(); let fee_tier = FeeTier::new(protocol_fee, 1).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let init_tick = 0; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); @@ -862,7 +851,7 @@ pub fn test_remove_position() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); @@ -883,13 +872,13 @@ pub fn test_remove_position() { total_reward, reward_per_sec, start_timestamp, - "alice" + alice ) .unwrap(); // create position in range - approve!(app, token_x, dex, initial_amount, "alice").unwrap(); - approve!(app, token_y, dex, initial_amount, "alice").unwrap(); + approve!(app, token_x, dex, initial_amount, alice).unwrap(); + approve!(app, token_y, dex, initial_amount, alice).unwrap(); create_position!( app, @@ -900,24 +889,21 @@ pub fn test_remove_position() { liquidity, SqrtPrice::new(0), SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); // increase block time - let mut block_info = app.app.block_info(); - let current_timestamp = block_info.time.seconds(); - block_info.time = Timestamp::from_seconds(current_timestamp + 1000); - app.app.set_block(block_info.clone()); + app.increase_time(1000); let before_dex_balance = balance_of!(app, token_z, dex); - let before_user_balance = balance_of!(app, token_z, "alice"); + let before_user_balance = balance_of!(app, token_z, alice); // try remove position - remove_position!(app, dex, 0, "alice").unwrap(); + remove_position!(app, dex, 0, alice).unwrap(); let after_dex_balance = balance_of!(app, token_z, dex); - let after_user_balance = balance_of!(app, token_z, "alice"); + let after_user_balance = balance_of!(app, token_z, alice); assert!(before_dex_balance.gt(&after_dex_balance)); assert!(before_user_balance.lt(&after_user_balance)); @@ -928,21 +914,26 @@ pub fn test_remove_position() { #[test] pub fn incentive_stress_test() { + let (mut app, accounts) = MockApp::new(&[ + ("alice", &coins(100_000_000_000_000, FEE_DENOM)), + ("bob", &coins(100_000_000_000_000, FEE_DENOM)), + ]); + let alice = &accounts[0]; + let bob = &accounts[1]; let protocol_fee = Percentage::from_scale(6, 3); - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); + let dex = create_dex!(app, Percentage::new(0), alice); let dex_raw = &dex.to_string(); let initial_amount = 10u128.pow(20); let (token_x, token_y, token_z) = - create_3_tokens!(app, initial_amount, initial_amount, initial_amount); - mint!(app, token_z, dex_raw, initial_amount, "alice").unwrap(); - approve!(app, token_x, dex, initial_amount, "alice").unwrap(); - approve!(app, token_y, dex, initial_amount, "alice").unwrap(); + create_3_tokens!(app, initial_amount, initial_amount, initial_amount, alice); + mint!(app, token_z, dex_raw, initial_amount, alice).unwrap(); + approve!(app, token_x, dex, initial_amount, alice).unwrap(); + approve!(app, token_y, dex, initial_amount, alice).unwrap(); let fee_tier = FeeTier::new(protocol_fee, 1).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let init_tick = 0; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); @@ -954,7 +945,7 @@ pub fn incentive_stress_test() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); @@ -984,7 +975,7 @@ pub fn incentive_stress_test() { total_reward, rps[i], start_timestamp, - "alice" + alice ) .unwrap(); } @@ -1008,16 +999,16 @@ pub fn incentive_stress_test() { liquidity, SqrtPrice::new(0), SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); } // try swap - mint!(app, token_y, "bob", initial_amount, "alice").unwrap(); - approve!(app, token_y, dex, initial_amount, "bob").unwrap(); - mint!(app, token_x, "bob", initial_amount, "alice").unwrap(); - approve!(app, token_x, dex, initial_amount, "bob").unwrap(); + mint!(app, token_y, bob, initial_amount, alice).unwrap(); + approve!(app, token_y, dex, initial_amount, bob).unwrap(); + mint!(app, token_x, bob, initial_amount, alice).unwrap(); + approve!(app, token_x, dex, initial_amount, bob).unwrap(); let swap_amounts: Vec = vec![2323, 233, 321, 5353, 12, 932, 42, 3123, 5438]; let x_to_y_list = vec![true, false, false, true, true, false, false, true]; @@ -1039,22 +1030,22 @@ pub fn incentive_stress_test() { swap_amount, true, target_sqrt_price, - "bob" + bob ) .unwrap(); } let before_dex_balance = balance_of!(app, token_z, dex); - let before_user_balance = balance_of!(app, token_z, "alice"); + let before_user_balance = balance_of!(app, token_z, alice); // claim all incentives for _ in 0..1000 { // try remove position - remove_position!(app, dex, 0, "alice").unwrap(); + remove_position!(app, dex, 0, alice).unwrap(); } let after_dex_balance = balance_of!(app, token_z, dex); - let after_user_balance = balance_of!(app, token_z, "alice"); + let after_user_balance = balance_of!(app, token_z, alice); assert!(before_dex_balance.gt(&after_dex_balance)); assert!(before_user_balance.lt(&after_user_balance)); @@ -1066,18 +1057,20 @@ pub fn incentive_stress_test() { #[test] pub fn test_claim_incentive_with_single_position() { let protocol_fee = Percentage::from_scale(6, 3); - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; + + let dex = create_dex!(app, Percentage::new(0), alice); let dex_raw = &dex.to_string(); let initial_amount = 10u128.pow(10); let (token_x, token_y, token_z) = - create_3_tokens!(app, initial_amount, initial_amount, initial_amount); - mint!(app, token_z, dex_raw, initial_amount, "alice").unwrap(); + create_3_tokens!(app, initial_amount, initial_amount, initial_amount, alice); + mint!(app, token_z, dex_raw, initial_amount, alice).unwrap(); let fee_tier = FeeTier::new(protocol_fee, 1).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let init_tick = 0; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); @@ -1089,7 +1082,7 @@ pub fn test_claim_incentive_with_single_position() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); @@ -1104,8 +1097,8 @@ pub fn test_claim_incentive_with_single_position() { let liquidity = Liquidity::from_integer(1000000); // create position in range - approve!(app, token_x, dex, initial_amount, "alice").unwrap(); - approve!(app, token_y, dex, initial_amount, "alice").unwrap(); + approve!(app, token_x, dex, initial_amount, alice).unwrap(); + approve!(app, token_y, dex, initial_amount, alice).unwrap(); create_position!( app, dex, @@ -1115,7 +1108,7 @@ pub fn test_claim_incentive_with_single_position() { liquidity, SqrtPrice::new(0), SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); @@ -1128,30 +1121,27 @@ pub fn test_claim_incentive_with_single_position() { total_reward, reward_per_sec, start_timestamp, - "alice" + alice ) .unwrap(); let before_dex_balance = balance_of!(app, token_z, dex); - let before_user_balance = balance_of!(app, token_z, "alice"); + let before_user_balance = balance_of!(app, token_z, alice); // increase block time for _ in 0..100 { - let mut block_info = app.app.block_info(); - let current_timestamp = block_info.time.seconds(); - block_info.time = Timestamp::from_seconds(current_timestamp + 1000); - app.app.set_block(block_info.clone()); + app.increase_time(1000); // claim incentives - claim_incentives!(app, dex, 0, "alice").unwrap(); - let position_state = get_position!(app, dex, 0, "alice").unwrap(); + claim_incentives!(app, dex, 0, alice).unwrap(); + let position_state = get_position!(app, dex, 0, alice).unwrap(); assert_eq!(position_state.incentives[0].pending_rewards, TokenAmount(0)); } let timestamp_after = app.get_block_time().seconds(); let total_emit = (timestamp_after - timestamp_init) as u128 * reward_per_sec.0; let after_dex_balance = balance_of!(app, token_z, dex); - let after_user_balance = balance_of!(app, token_z, "alice"); + let after_user_balance = balance_of!(app, token_z, alice); assert!(before_dex_balance.gt(&after_dex_balance)); assert!(before_user_balance.lt(&after_user_balance)); @@ -1165,18 +1155,20 @@ pub fn test_claim_incentive_with_single_position() { #[test] pub fn test_claim_incentive_with_multi_position() { let protocol_fee = Percentage::from_scale(6, 3); - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; + + let dex = create_dex!(app, Percentage::new(0), alice); let dex_raw = &dex.to_string(); let initial_amount = 10u128.pow(10); let (token_x, token_y, token_z) = - create_3_tokens!(app, initial_amount, initial_amount, initial_amount); - mint!(app, token_z, dex_raw, initial_amount, "alice").unwrap(); + create_3_tokens!(app, initial_amount, initial_amount, initial_amount, alice); + mint!(app, token_z, dex_raw, initial_amount, alice).unwrap(); let fee_tier = FeeTier::new(protocol_fee, 1).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let init_tick = 0; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); @@ -1188,7 +1180,7 @@ pub fn test_claim_incentive_with_multi_position() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); @@ -1202,8 +1194,8 @@ pub fn test_claim_incentive_with_multi_position() { let start_timestamp: Option = None; // create position in range - approve!(app, token_x, dex, initial_amount, "alice").unwrap(); - approve!(app, token_y, dex, initial_amount, "alice").unwrap(); + approve!(app, token_x, dex, initial_amount, alice).unwrap(); + approve!(app, token_y, dex, initial_amount, alice).unwrap(); let timestamp_init = app.get_block_time().seconds(); create_incentive!( app, @@ -1213,7 +1205,7 @@ pub fn test_claim_incentive_with_multi_position() { total_reward, reward_per_sec, start_timestamp, - "alice" + alice ) .unwrap(); @@ -1236,24 +1228,21 @@ pub fn test_claim_incentive_with_multi_position() { liquidity, SqrtPrice::new(0), SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); } let before_dex_balance = balance_of!(app, token_z, dex); - let before_user_balance = balance_of!(app, token_z, "alice"); + let before_user_balance = balance_of!(app, token_z, alice); // increase block time for _ in 0..100 { - let mut block_info = app.app.block_info(); - let current_timestamp = block_info.time.seconds(); - block_info.time = Timestamp::from_seconds(current_timestamp + 1000); - app.app.set_block(block_info.clone()); + app.increase_time(1000); for i in 0..100 { // claim incentives - claim_incentives!(app, dex, i, "alice").unwrap(); - let position_state = get_position!(app, dex, i, "alice").unwrap(); + claim_incentives!(app, dex, i, alice).unwrap(); + let position_state = get_position!(app, dex, i, alice).unwrap(); assert_eq!(position_state.incentives[0].pending_rewards, TokenAmount(0)); } } @@ -1262,7 +1251,7 @@ pub fn test_claim_incentive_with_multi_position() { let total_emit = (timestamp_after - timestamp_init) as u128 * reward_per_sec.0; let after_dex_balance = balance_of!(app, token_z, dex); - let after_user_balance = balance_of!(app, token_z, "alice"); + let after_user_balance = balance_of!(app, token_z, alice); assert!(before_dex_balance.gt(&after_dex_balance)); assert!(before_user_balance.lt(&after_user_balance)); @@ -1276,18 +1265,23 @@ pub fn test_claim_incentive_with_multi_position() { #[test] pub fn test_update_incentive_with_tick_move_left_to_right() { let protocol_fee = Percentage::from_scale(6, 3); - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); + let (mut app, accounts) = MockApp::new(&[ + ("alice", &coins(100_000_000_000, FEE_DENOM)), + ("bob", &coins(100_000_000_000, FEE_DENOM)), + ]); + let alice = &accounts[0]; + let bob = &accounts[1]; + let dex = create_dex!(app, Percentage::new(0), alice); let dex_raw = &dex.to_string(); let initial_amount = 10u128.pow(10); let (token_x, token_y, token_z) = - create_3_tokens!(app, initial_amount, initial_amount, initial_amount); - mint!(app, token_z, dex_raw, initial_amount, "alice").unwrap(); + create_3_tokens!(app, initial_amount, initial_amount, initial_amount, alice); + mint!(app, token_z, dex_raw, initial_amount, alice).unwrap(); let fee_tier = FeeTier::new(protocol_fee, 1).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let init_tick = 0; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); @@ -1299,7 +1293,7 @@ pub fn test_update_incentive_with_tick_move_left_to_right() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); @@ -1320,12 +1314,12 @@ pub fn test_update_incentive_with_tick_move_left_to_right() { total_reward, reward_per_sec, start_timestamp, - "alice" + alice ) .unwrap(); - approve!(app, token_x, dex, initial_amount, "alice").unwrap(); - approve!(app, token_y, dex, initial_amount, "alice").unwrap(); + approve!(app, token_x, dex, initial_amount, alice).unwrap(); + approve!(app, token_y, dex, initial_amount, alice).unwrap(); // create 2 position // first_pos: range (10, 20) @@ -1339,7 +1333,7 @@ pub fn test_update_incentive_with_tick_move_left_to_right() { liquidity, SqrtPrice::new(0), SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); create_position!( @@ -1351,22 +1345,22 @@ pub fn test_update_incentive_with_tick_move_left_to_right() { liquidity, SqrtPrice::new(0), SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); // Both positions do not have any incentives due to being out of range app.increase_time(1000); - let incentive = get_position_incentives!(app, dex, 0, "alice").unwrap(); + let incentive = get_position_incentives!(app, dex, 0, alice).unwrap(); assert_eq!(incentive, vec![]); - let incentive = get_position_incentives!(app, dex, 1, "alice").unwrap(); + let incentive = get_position_incentives!(app, dex, 1, alice).unwrap(); assert_eq!(incentive, vec![]); // swap y to x, tick move left -> right let amount = 100; let swap_amount = TokenAmount(amount); - mint!(app, token_y, "bob", amount, "alice").unwrap(); - approve!(app, token_y, dex, amount, "bob").unwrap(); + mint!(app, token_y, bob, amount, alice).unwrap(); + approve!(app, token_y, dex, amount, bob).unwrap(); swap!( app, dex, @@ -1375,29 +1369,30 @@ pub fn test_update_incentive_with_tick_move_left_to_right() { swap_amount, true, SqrtPrice::new(MAX_SQRT_PRICE), - "bob" + bob ) .unwrap(); let pool = get_pool!(app, dex, token_x, token_y, fee_tier).unwrap(); assert_eq!(pool.current_tick_index, 11); // The first position has an incentive, but the second one does not have any. + let incentive = get_position_incentives!(app, dex, 0, alice).unwrap(); app.increase_time(1000); - let incentive = get_position_incentives!(app, dex, 0, "alice").unwrap(); + let new_incentive = get_position_incentives!(app, dex, 0, alice).unwrap(); assert_eq!( - incentive, + subtract_assets(&incentive, &new_incentive), vec![Asset { info: reward_token.clone(), - amount: Uint128::new(100500u128) + amount: Uint128::new(100000u128) }] ); - let incentive = get_position_incentives!(app, dex, 1, "alice").unwrap(); + let incentive = get_position_incentives!(app, dex, 1, alice).unwrap(); assert_eq!(incentive, vec![]); // swap again let amount = 700; let swap_amount = TokenAmount(amount); - mint!(app, token_y, "bob", amount, "alice").unwrap(); - approve!(app, token_y, dex, amount, "bob").unwrap(); + mint!(app, token_y, bob, amount, alice).unwrap(); + approve!(app, token_y, dex, amount, bob).unwrap(); swap!( app, dex, @@ -1406,22 +1401,26 @@ pub fn test_update_incentive_with_tick_move_left_to_right() { swap_amount, true, SqrtPrice::new(MAX_SQRT_PRICE), - "bob" + bob ) .unwrap(); let pool = get_pool!(app, dex, token_x, token_y, fee_tier).unwrap(); assert_eq!(pool.current_tick_index, 35); // the second position have incentive, - claim_incentives!(app, dex, 0, "alice").unwrap(); + claim_incentives!(app, dex, 0, alice).unwrap(); app.increase_time(1000); - let incentive = get_position_incentives!(app, dex, 0, "alice").unwrap(); + let incentive = get_position_incentives!(app, dex, 0, alice).unwrap(); assert_eq!(incentive, vec![]); - let incentive = get_position_incentives!(app, dex, 1, "alice").unwrap(); + let incentive = get_position_incentives!(app, dex, 1, alice).unwrap(); + #[cfg(not(feature = "test-tube"))] + let amount = Uint128::from(101000u128); + #[cfg(feature = "test-tube")] + let amount = Uint128::from(100500u128); assert_eq!( incentive, vec![Asset { info: reward_token.clone(), - amount: Uint128::new(101000u128) + amount }] ); } @@ -1429,18 +1428,23 @@ pub fn test_update_incentive_with_tick_move_left_to_right() { #[test] pub fn test_update_incentive_with_tick_move_right_to_left() { let protocol_fee = Percentage::from_scale(6, 3); - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); + let (mut app, accounts) = MockApp::new(&[ + ("alice", &coins(100_000_000_000, FEE_DENOM)), + ("bob", &coins(100_000_000_000, FEE_DENOM)), + ]); + let alice = &accounts[0]; + let bob = &accounts[1]; + let dex = create_dex!(app, Percentage::new(0), alice); let dex_raw = &dex.to_string(); let initial_amount = 10u128.pow(10); let (token_x, token_y, token_z) = - create_3_tokens!(app, initial_amount, initial_amount, initial_amount); - mint!(app, token_z, dex_raw, initial_amount, "alice").unwrap(); + create_3_tokens!(app, initial_amount, initial_amount, initial_amount, alice); + mint!(app, token_z, dex_raw, initial_amount, alice).unwrap(); let fee_tier = FeeTier::new(protocol_fee, 1).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let init_tick = 0; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); @@ -1452,7 +1456,7 @@ pub fn test_update_incentive_with_tick_move_right_to_left() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); @@ -1473,12 +1477,12 @@ pub fn test_update_incentive_with_tick_move_right_to_left() { total_reward, reward_per_sec, start_timestamp, - "alice" + alice ) .unwrap(); - approve!(app, token_x, dex, initial_amount, "alice").unwrap(); - approve!(app, token_y, dex, initial_amount, "alice").unwrap(); + approve!(app, token_x, dex, initial_amount, alice).unwrap(); + approve!(app, token_y, dex, initial_amount, alice).unwrap(); // create 2 position // first_pos: range (-20, -10) @@ -1492,7 +1496,7 @@ pub fn test_update_incentive_with_tick_move_right_to_left() { liquidity, SqrtPrice::new(0), SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); create_position!( @@ -1504,22 +1508,22 @@ pub fn test_update_incentive_with_tick_move_right_to_left() { liquidity, SqrtPrice::new(0), SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); // Both positions do not have any incentives due to being out of range app.increase_time(1000); - let incentive = get_position_incentives!(app, dex, 0, "alice").unwrap(); + let incentive = get_position_incentives!(app, dex, 0, alice).unwrap(); assert_eq!(incentive, vec![]); - let incentive = get_position_incentives!(app, dex, 1, "alice").unwrap(); + let incentive = get_position_incentives!(app, dex, 1, alice).unwrap(); assert_eq!(incentive, vec![]); // swap x to y, tick move right -> left let amount = 100; let swap_amount = TokenAmount(amount); - mint!(app, token_x, "bob", amount, "alice").unwrap(); - approve!(app, token_x, dex, amount, "bob").unwrap(); + mint!(app, token_x, bob, amount, alice).unwrap(); + approve!(app, token_x, dex, amount, bob).unwrap(); swap!( app, dex, @@ -1528,29 +1532,30 @@ pub fn test_update_incentive_with_tick_move_right_to_left() { swap_amount, true, SqrtPrice::new(MIN_SQRT_PRICE), - "bob" + bob ) .unwrap(); let pool = get_pool!(app, dex, token_x, token_y, fee_tier).unwrap(); assert_eq!(pool.current_tick_index, -12); // The first position has an incentive, but the second one does not have any. + let incentive = get_position_incentives!(app, dex, 0, alice).unwrap(); app.increase_time(1000); - let incentive = get_position_incentives!(app, dex, 0, "alice").unwrap(); + let new_incentive = get_position_incentives!(app, dex, 0, alice).unwrap(); assert_eq!( - incentive, + subtract_assets(&incentive, &new_incentive), vec![Asset { info: reward_token.clone(), - amount: Uint128::new(100500u128) + amount: Uint128::new(100000u128) }] ); - let incentive = get_position_incentives!(app, dex, 1, "alice").unwrap(); + let incentive = get_position_incentives!(app, dex, 1, alice).unwrap(); assert_eq!(incentive, vec![]); // swap again let amount = 700; let swap_amount = TokenAmount(amount); - mint!(app, token_x, "bob", amount, "alice").unwrap(); - approve!(app, token_x, dex, amount, "bob").unwrap(); + mint!(app, token_x, bob, amount, alice).unwrap(); + approve!(app, token_x, dex, amount, bob).unwrap(); swap!( app, dex, @@ -1559,22 +1564,23 @@ pub fn test_update_incentive_with_tick_move_right_to_left() { swap_amount, true, SqrtPrice::new(MIN_SQRT_PRICE), - "bob" + bob ) .unwrap(); let pool = get_pool!(app, dex, token_x, token_y, fee_tier).unwrap(); assert_eq!(pool.current_tick_index, -36); // the second position have incentive, - claim_incentives!(app, dex, 0, "alice").unwrap(); + claim_incentives!(app, dex, 0, alice).unwrap(); + let incentive2 = get_position_incentives!(app, dex, 1, alice).unwrap(); app.increase_time(1000); - let incentive = get_position_incentives!(app, dex, 0, "alice").unwrap(); + let incentive = get_position_incentives!(app, dex, 0, alice).unwrap(); assert_eq!(incentive, vec![]); - let incentive = get_position_incentives!(app, dex, 1, "alice").unwrap(); + let new_incentive2 = get_position_incentives!(app, dex, 1, alice).unwrap(); assert_eq!( - incentive, + subtract_assets(&incentive2, &new_incentive2), vec![Asset { info: reward_token.clone(), - amount: Uint128::new(101000u128) + amount: Uint128::new(100000u128) }] ); } diff --git a/contracts/oraiswap-v3/src/tests/limits.rs b/contracts/oraiswap-v3/src/tests/limits.rs index 1de1b93..12e2279 100644 --- a/contracts/oraiswap-v3/src/tests/limits.rs +++ b/contracts/oraiswap-v3/src/tests/limits.rs @@ -1,3 +1,4 @@ +use cosmwasm_std::coins; use decimal::*; use crate::{ @@ -6,37 +7,41 @@ use crate::{ logic::{get_liquidity_by_x, get_liquidity_by_y}, percentage::Percentage, sqrt_price::{calculate_sqrt_price, get_max_tick, SqrtPrice}, - tests::helper::{macros::*, MockApp}, + tests::helper::{macros::*, MockApp, FEE_DENOM}, token_amount::TokenAmount, FeeTier, PoolKey, MAX_SQRT_PRICE, MAX_TICK, MIN_SQRT_PRICE, }; #[test] fn test_limits_big_deposit_x_and_swap_y() { - let mut app = MockApp::new(&[]); - big_deposit_and_swap!(app, true); + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; + + big_deposit_and_swap!(app, true, alice); } #[test] fn test_limits_big_deposit_y_and_swap_x() { - let mut app = MockApp::new(&[]); - big_deposit_and_swap!(app, false); + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; + big_deposit_and_swap!(app, false, alice); } #[test] fn test_limits_big_deposit_both_tokens() { - let mut app = MockApp::new(&[]); + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; let (dex, token_x, token_y) = - init_dex_and_tokens!(app, u128::MAX, Percentage::from_scale(1, 2)); + init_dex_and_tokens!(app, u128::MAX, Percentage::from_scale(1, 2), alice); let mint_amount = 2u128.pow(75) - 1; - approve!(app, token_x, dex, u128::MAX, "alice").unwrap(); - approve!(app, token_y, dex, u128::MAX, "alice").unwrap(); + approve!(app, token_x, dex, u128::MAX, alice).unwrap(); + approve!(app, token_y, dex, u128::MAX, alice).unwrap(); let fee_tier = FeeTier::new(Percentage::from_scale(6, 3), 1).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let init_tick = 0; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); @@ -48,7 +53,7 @@ fn test_limits_big_deposit_both_tokens() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); @@ -84,12 +89,12 @@ fn test_limits_big_deposit_both_tokens() { liquidity_delta, slippage_limit_lower, slippage_limit_upper, - "alice" + alice ) .unwrap(); - let user_amount_x = balance_of!(app, token_x, "alice"); - let user_amount_y = balance_of!(app, token_y, "alice"); + let user_amount_x = balance_of!(app, token_x, alice); + let user_amount_y = balance_of!(app, token_y, alice); assert_eq!(user_amount_x, u128::MAX - mint_amount); assert_eq!(user_amount_y, u128::MAX - y.get()); @@ -101,17 +106,18 @@ fn test_limits_big_deposit_both_tokens() { #[test] fn test_deposit_limits_at_upper_limit() { - let mut app = MockApp::new(&[]); + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; let (dex, token_x, token_y) = - init_dex_and_tokens!(app, u128::MAX, Percentage::from_scale(1, 2)); + init_dex_and_tokens!(app, u128::MAX, Percentage::from_scale(1, 2), alice); let mint_amount = 2u128.pow(105) - 1; - approve!(app, token_x, dex, u128::MAX, "alice").unwrap(); - approve!(app, token_y, dex, u128::MAX, "alice").unwrap(); + approve!(app, token_x, dex, u128::MAX, alice).unwrap(); + approve!(app, token_y, dex, u128::MAX, alice).unwrap(); let fee_tier = FeeTier::new(Percentage::from_scale(6, 3), 1).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let init_tick = get_max_tick(1); let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); @@ -123,7 +129,7 @@ fn test_deposit_limits_at_upper_limit() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); @@ -155,24 +161,25 @@ fn test_deposit_limits_at_upper_limit() { liquidity_delta, slippage_limit_lower, slippage_limit_upper, - "alice" + alice ) .unwrap(); } #[test] fn test_limits_big_deposit_and_swaps() { - let mut app = MockApp::new(&[]); + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; let (dex, token_x, token_y) = - init_dex_and_tokens!(app, u128::MAX, Percentage::from_scale(1, 2)); + init_dex_and_tokens!(app, u128::MAX, Percentage::from_scale(1, 2), alice); let mint_amount = 2u128.pow(76) - 1; - approve!(app, token_x, dex, u128::MAX, "alice").unwrap(); - approve!(app, token_y, dex, u128::MAX, "alice").unwrap(); + approve!(app, token_x, dex, u128::MAX, alice).unwrap(); + approve!(app, token_y, dex, u128::MAX, alice).unwrap(); let fee_tier = FeeTier::new(Percentage::from_scale(6, 3), 1).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let init_tick = 0; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); @@ -184,7 +191,7 @@ fn test_limits_big_deposit_and_swaps() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); @@ -223,12 +230,12 @@ fn test_limits_big_deposit_and_swaps() { liquidity_delta, slippage_limit_lower, slippage_limit_upper, - "alice" + alice ) .unwrap(); - let user_amount_x = balance_of!(app, token_x, "alice"); - let user_amount_y = balance_of!(app, token_y, "alice"); + let user_amount_x = balance_of!(app, token_x, alice); + let user_amount_y = balance_of!(app, token_y, alice); assert_eq!(user_amount_x, u128::MAX - pos_amount); assert_eq!(user_amount_y, u128::MAX - y.get()); @@ -254,7 +261,7 @@ fn test_limits_big_deposit_and_swaps() { swap_amount, true, sqrt_price_limit, - "alice" + alice ) .unwrap(); } @@ -262,15 +269,16 @@ fn test_limits_big_deposit_and_swaps() { #[test] fn test_limits_full_range_with_max_liquidity() { - let mut app = MockApp::new(&[]); + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; let (dex, token_x, token_y) = - init_dex_and_tokens!(app, u128::MAX, Percentage::from_scale(1, 2)); + init_dex_and_tokens!(app, u128::MAX, Percentage::from_scale(1, 2), alice); - approve!(app, token_x, dex, u128::MAX, "alice").unwrap(); - approve!(app, token_y, dex, u128::MAX, "alice").unwrap(); + approve!(app, token_x, dex, u128::MAX, alice).unwrap(); + approve!(app, token_y, dex, u128::MAX, alice).unwrap(); let fee_tier = FeeTier::new(Percentage::from_scale(6, 3), 1).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let init_tick = get_max_tick(1); let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); @@ -282,7 +290,7 @@ fn test_limits_full_range_with_max_liquidity() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); @@ -303,7 +311,7 @@ fn test_limits_full_range_with_max_liquidity() { liquidity_delta, slippage_limit_lower, slippage_limit_upper, - "alice" + alice ) .unwrap(); diff --git a/contracts/oraiswap-v3/src/tests/liquidity_gap.rs b/contracts/oraiswap-v3/src/tests/liquidity_gap.rs index 9ca8a08..03f0dbe 100644 --- a/contracts/oraiswap-v3/src/tests/liquidity_gap.rs +++ b/contracts/oraiswap-v3/src/tests/liquidity_gap.rs @@ -1,3 +1,4 @@ +use cosmwasm_std::coins; use decimal::*; use crate::{ @@ -5,27 +6,32 @@ use crate::{ liquidity::Liquidity, percentage::Percentage, sqrt_price::{calculate_sqrt_price, SqrtPrice}, - tests::helper::{macros::*, MockApp}, + tests::helper::{macros::*, MockApp, FEE_DENOM}, token_amount::TokenAmount, ContractError, FeeTier, PoolKey, MIN_SQRT_PRICE, }; #[test] fn test_liquidity_gap() { + let (mut app, accounts) = MockApp::new(&[ + ("alice", &coins(100_000_000_000, FEE_DENOM)), + ("bob", &coins(100_000_000_000, FEE_DENOM)), + ]); + let alice = &accounts[0]; + let bob = &accounts[1]; let fee_tier = FeeTier::new(Percentage::from_scale(6, 3), 10).unwrap(); let init_tick = 0; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); let initial_mint = 10u128.pow(10); - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::from_scale(1, 2)); + let dex = create_dex!(app, Percentage::from_scale(1, 2), alice); - let (token_x, token_y) = create_tokens!(app, initial_mint, initial_mint); + let (token_x, token_y) = create_tokens!(app, initial_mint, initial_mint, alice); let pool_key = PoolKey::new(token_x.to_string(), token_y.to_string(), fee_tier).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); create_pool!( app, @@ -35,7 +41,7 @@ fn test_liquidity_gap() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); @@ -43,11 +49,11 @@ fn test_liquidity_gap() { let upper_tick_index = 10; let mint_amount = 10u128.pow(10); - mint!(app, token_x, "alice", mint_amount, "alice").unwrap(); - mint!(app, token_y, "alice", mint_amount, "alice").unwrap(); + mint!(app, token_x, alice, mint_amount, alice).unwrap(); + mint!(app, token_y, alice, mint_amount, alice).unwrap(); - approve!(app, token_x, dex, mint_amount, "alice").unwrap(); - approve!(app, token_y, dex, mint_amount, "alice").unwrap(); + approve!(app, token_x, dex, mint_amount, alice).unwrap(); + approve!(app, token_y, dex, mint_amount, alice).unwrap(); let liquidity_delta = Liquidity::from_integer(20_006_000); @@ -62,7 +68,7 @@ fn test_liquidity_gap() { liquidity_delta, pool_state.sqrt_price, pool_state.sqrt_price, - "alice" + alice ) .unwrap(); @@ -71,9 +77,9 @@ fn test_liquidity_gap() { assert_eq!(pool_state.liquidity, liquidity_delta); let mint_amount = 10067; - mint!(app, token_x, "bob", mint_amount, "alice").unwrap(); + mint!(app, token_x, bob, mint_amount, alice).unwrap(); - approve!(app, token_x, dex, mint_amount, "bob").unwrap(); + approve!(app, token_x, dex, mint_amount, bob).unwrap(); let dex_x_before = balance_of!(app, token_x, dex); let dex_y_before = balance_of!(app, token_y, dex); @@ -100,7 +106,7 @@ fn test_liquidity_gap() { swap_amount, true, quoted_target_sqrt_price, - "bob" + bob ) .unwrap(); @@ -112,8 +118,8 @@ fn test_liquidity_gap() { assert_eq!(pool.current_tick_index, lower_tick_index); assert_eq!(pool.sqrt_price, expected_price); - let bob_x = balance_of!(app, token_x, "bob"); - let bob_y = balance_of!(app, token_y, "bob"); + let bob_x = balance_of!(app, token_x, bob); + let bob_y = balance_of!(app, token_y, bob); let dex_x_after = balance_of!(app, token_x, dex); let dex_y_after = balance_of!(app, token_y, dex); @@ -145,13 +151,13 @@ fn test_liquidity_gap() { swap_amount, true, target_sqrt_price, - "bob" + bob ) .unwrap_err(); - assert_eq!( - error.root_cause().to_string(), - ContractError::NoGainSwap {}.to_string() - ); + assert!(error + .root_cause() + .to_string() + .contains(&ContractError::NoGainSwap {}.to_string())); } // Should skip gap and then swap @@ -159,8 +165,8 @@ fn test_liquidity_gap() { let upper_tick_after_swap = -50; let liquidity_delta = Liquidity::from_integer(20008000); - approve!(app, token_x, dex, liquidity_delta.get(), "alice").unwrap(); - approve!(app, token_y, dex, liquidity_delta.get(), "alice").unwrap(); + approve!(app, token_x, dex, liquidity_delta.get(), alice).unwrap(); + approve!(app, token_y, dex, liquidity_delta.get(), alice).unwrap(); let pool_state = get_pool!(app, dex, token_x, token_y, fee_tier).unwrap(); @@ -173,14 +179,14 @@ fn test_liquidity_gap() { liquidity_delta, pool_state.sqrt_price, pool_state.sqrt_price, - "alice" + alice ) .unwrap(); let swap_amount = TokenAmount::new(5000); - mint!(app, token_x, "bob", swap_amount.get(), "alice").unwrap(); + mint!(app, token_x, bob, swap_amount.get(), alice).unwrap(); - approve!(app, token_x, dex, swap_amount.get(), "bob").unwrap(); + approve!(app, token_x, dex, swap_amount.get(), bob).unwrap(); let target_sqrt_price = SqrtPrice::new(MIN_SQRT_PRICE); let quoted_target_sqrt_price = quote!( @@ -203,7 +209,7 @@ fn test_liquidity_gap() { swap_amount, true, quoted_target_sqrt_price, - "bob" + bob ) .unwrap(); get_pool!(app, dex, token_x, token_y, fee_tier).unwrap(); diff --git a/contracts/oraiswap-v3/src/tests/max_tick_cross.rs b/contracts/oraiswap-v3/src/tests/max_tick_cross.rs index 445169f..eaf557a 100644 --- a/contracts/oraiswap-v3/src/tests/max_tick_cross.rs +++ b/contracts/oraiswap-v3/src/tests/max_tick_cross.rs @@ -1,3 +1,4 @@ +use cosmwasm_std::coins; use decimal::*; use crate::{ @@ -5,21 +6,26 @@ use crate::{ liquidity::Liquidity, percentage::Percentage, sqrt_price::SqrtPrice, - tests::helper::{macros::*, MockApp}, + tests::helper::{macros::*, MockApp, FEE_DENOM}, token_amount::TokenAmount, FeeTier, PoolKey, MIN_SQRT_PRICE, }; #[test] fn max_tick_cross() { - let mut app = MockApp::new(&[("alice", &[])]); - let (dex, token_x, token_y) = init_dex_and_tokens!(app); - init_basic_pool!(app, dex, token_x, token_y); + let (mut app, accounts) = MockApp::new(&[ + ("alice", &coins(100_000_000_000_000, FEE_DENOM)), + ("bob", &coins(100_000_000_000, FEE_DENOM)), + ]); + let alice = &accounts[0]; + let bob = &accounts[1]; + let (dex, token_x, token_y) = init_dex_and_tokens!(app, alice); + init_basic_pool!(app, dex, token_x, token_y, alice); let mint_amount = u128::MAX; - approve!(app, token_x, dex, mint_amount, "alice").unwrap(); - approve!(app, token_y, dex, mint_amount, "alice").unwrap(); + approve!(app, token_x, dex, mint_amount, alice).unwrap(); + approve!(app, token_y, dex, mint_amount, alice).unwrap(); let liquidity = Liquidity::from_integer(10000000); @@ -42,7 +48,7 @@ fn max_tick_cross() { liquidity, slippage_limit_lower, slippage_limit_upper, - "alice" + alice ) .unwrap(); } @@ -52,10 +58,10 @@ fn max_tick_cross() { let amount = 760_000; - mint!(app, token_x, "bob", amount, "alice").unwrap(); - let amount_x = balance_of!(app, token_x, "bob"); + mint!(app, token_x, bob, amount, alice).unwrap(); + let amount_x = balance_of!(app, token_x, bob); assert_eq!(amount_x, amount); - approve!(app, token_x, dex, amount, "bob").unwrap(); + approve!(app, token_x, dex, amount, bob).unwrap(); let pool_before = get_pool!(app, dex, token_x, token_y, pool_key.fee_tier).unwrap(); @@ -70,7 +76,7 @@ fn max_tick_cross() { assert_eq!(crosses_after_quote, 0); assert_eq!(quote_result.ticks.len() - 1, 145); - swap!(app, dex, pool_key, true, swap_amount, true, slippage, "bob").unwrap(); + swap!(app, dex, pool_key, true, swap_amount, true, slippage, bob).unwrap(); let pool_after = get_pool!(app, dex, token_x, token_y, pool_key.fee_tier).unwrap(); diff --git a/contracts/oraiswap-v3/src/tests/mod.rs b/contracts/oraiswap-v3/src/tests/mod.rs index 2fee79d..09c7b0b 100644 --- a/contracts/oraiswap-v3/src/tests/mod.rs +++ b/contracts/oraiswap-v3/src/tests/mod.rs @@ -10,19 +10,19 @@ mod get_liquidity_ticks; mod get_position_ticks; mod get_tickmap; mod helper; -// mod incentive; +mod incentive; mod interaction_with_pool_on_removed_fee_tier; -// mod limits; -// mod liquidity_gap; -// mod max_tick_cross; +mod limits; +mod liquidity_gap; +mod max_tick_cross; // mod migration; -// mod multiple_swap; -// mod nft; -// mod position; -// mod position_list; -// mod position_slippage; -// mod protocol_fee; -// mod remove_fee_tier; -// mod slippage; -// mod swap; -// mod swap_route; +mod multiple_swap; +mod nft; +mod position; +mod position_list; +mod position_slippage; +mod protocol_fee; +mod remove_fee_tier; +mod slippage; +mod swap; +mod swap_route; diff --git a/contracts/oraiswap-v3/src/tests/multiple_swap.rs b/contracts/oraiswap-v3/src/tests/multiple_swap.rs index 267aa90..e7641b7 100644 --- a/contracts/oraiswap-v3/src/tests/multiple_swap.rs +++ b/contracts/oraiswap-v3/src/tests/multiple_swap.rs @@ -1,13 +1,25 @@ -use crate::tests::helper::{macros::*, MockApp}; +use cosmwasm_std::coins; + +use crate::tests::helper::{macros::*, MockApp, FEE_DENOM}; #[test] fn test_multiple_swap_x_to_y() { - let mut app = MockApp::new(&[("alice", &[])]); - multiple_swap!(app, true); + let (mut app, accounts) = MockApp::new(&[ + ("alice", &coins(100_000_000_000, FEE_DENOM)), + ("bob", &coins(100_000_000_000, FEE_DENOM)), + ]); + let alice = &accounts[0]; + let bob = &accounts[1]; + multiple_swap!(app, true, alice, bob); } #[test] fn test_multiple_swap_y_to_x() { - let mut app = MockApp::new(&[("alice", &[])]); - multiple_swap!(app, false); + let (mut app, accounts) = MockApp::new(&[ + ("alice", &coins(100_000_000_000, FEE_DENOM)), + ("bob", &coins(100_000_000_000, FEE_DENOM)), + ]); + let alice = &accounts[0]; + let bob = &accounts[1]; + multiple_swap!(app, false, alice, bob); } diff --git a/contracts/oraiswap-v3/src/tests/nft.rs b/contracts/oraiswap-v3/src/tests/nft.rs index 9d15c91..94009e3 100644 --- a/contracts/oraiswap-v3/src/tests/nft.rs +++ b/contracts/oraiswap-v3/src/tests/nft.rs @@ -1,4 +1,4 @@ -use cosmwasm_std::{attr, Addr, Attribute}; +use cosmwasm_std::{attr, coins, Addr, Attribute}; use decimal::*; use crate::{ @@ -13,15 +13,19 @@ use crate::{ ContractError, FeeTier, PoolKey, Position, MIN_SQRT_PRICE, }; +use super::helper::FEE_DENOM; + #[test] fn test_mint_nft() { - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); - let (token_x, token_y) = create_tokens!(app, 500, 500); + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; + + let dex = create_dex!(app, Percentage::new(0), alice); + let (token_x, token_y) = create_tokens!(app, 500, 500, alice); let fee_tier = FeeTier::new(Percentage::new(0), 1).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let init_tick = 10; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); @@ -33,17 +37,17 @@ fn test_mint_nft() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); - approve!(app, token_x, dex, 500, "alice").unwrap(); - approve!(app, token_y, dex, 500, "alice").unwrap(); + approve!(app, token_x, dex, 500, alice).unwrap(); + approve!(app, token_y, dex, 500, alice).unwrap(); let pool_key = PoolKey::new(token_x.to_string(), token_y.to_string(), fee_tier).unwrap(); app.execute( - Addr::unchecked("alice"), + Addr::unchecked(alice), dex.clone(), &msg::ExecuteMsg::Mint { extension: msg::NftExtensionMsg { @@ -62,13 +66,15 @@ fn test_mint_nft() { #[test] fn test_query_nft() { - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); - let (token_x, token_y) = create_tokens!(app, 500, 500); + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; + + let dex = create_dex!(app, Percentage::new(0), alice); + let (token_x, token_y) = create_tokens!(app, 500, 500, alice); let fee_tier = FeeTier::new(Percentage::new(0), 1).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let init_tick = 10; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); @@ -80,17 +86,17 @@ fn test_query_nft() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); - approve!(app, token_x, dex, 500, "alice").unwrap(); - approve!(app, token_y, dex, 500, "alice").unwrap(); + approve!(app, token_x, dex, 500, alice).unwrap(); + approve!(app, token_y, dex, 500, alice).unwrap(); let pool_key = PoolKey::new(token_x.to_string(), token_y.to_string(), fee_tier).unwrap(); app.execute( - Addr::unchecked("alice"), + Addr::unchecked(alice), dex.clone(), &msg::ExecuteMsg::Mint { extension: msg::NftExtensionMsg { @@ -118,7 +124,7 @@ fn test_query_nft() { .query( dex.clone(), &msg::QueryMsg::Tokens { - owner: Addr::unchecked("alice"), + owner: Addr::unchecked(alice), start_after: None, limit: None, }, @@ -129,6 +135,12 @@ fn test_query_nft() { #[test] fn test_burn_nft() { + let (mut app, accounts) = MockApp::new(&[ + ("alice", &coins(100_000_000_000, FEE_DENOM)), + ("bob", &coins(100_000_000_000, FEE_DENOM)), + ]); + let alice = &accounts[0]; + let bob = &accounts[1]; let fee_tier = FeeTier::new(Percentage::from_scale(6, 3), 10).unwrap(); let init_tick = 0; @@ -136,14 +148,13 @@ fn test_burn_nft() { let initial_mint = 10u128.pow(10); - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::from_scale(1, 2)); + let dex = create_dex!(app, Percentage::from_scale(1, 2), alice); - let (token_x, token_y) = create_tokens!(app, initial_mint, initial_mint); + let (token_x, token_y) = create_tokens!(app, initial_mint, initial_mint, alice); let pool_key = PoolKey::new(token_x.to_string(), token_y.to_string(), fee_tier).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); create_pool!( app, @@ -153,7 +164,7 @@ fn test_burn_nft() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); @@ -161,13 +172,13 @@ fn test_burn_nft() { let upper_tick_index = 10; let liquidity_delta = Liquidity::from_integer(1_000_000); - approve!(app, token_x, dex, initial_mint, "alice").unwrap(); - approve!(app, token_y, dex, initial_mint, "alice").unwrap(); + approve!(app, token_x, dex, initial_mint, alice).unwrap(); + approve!(app, token_y, dex, initial_mint, alice).unwrap(); let pool_state = get_pool!(app, dex, token_x, token_y, fee_tier).unwrap(); app.execute( - Addr::unchecked("alice"), + Addr::unchecked(alice), dex.clone(), &msg::ExecuteMsg::Mint { extension: msg::NftExtensionMsg { @@ -192,11 +203,11 @@ fn test_burn_nft() { let incorrect_lower_tick_index = lower_tick_index - 50; let incorrect_upper_tick_index = upper_tick_index + 50; - approve!(app, token_x, dex, liquidity_delta.0, "alice").unwrap(); - approve!(app, token_y, dex, liquidity_delta.0, "alice").unwrap(); + approve!(app, token_x, dex, liquidity_delta.0, alice).unwrap(); + approve!(app, token_y, dex, liquidity_delta.0, alice).unwrap(); app.execute( - Addr::unchecked("alice"), + Addr::unchecked(alice), dex.clone(), &msg::ExecuteMsg::Mint { extension: msg::NftExtensionMsg { @@ -224,17 +235,17 @@ fn test_burn_nft() { assert!(position_state.upper_tick_index == incorrect_upper_tick_index); let amount = 1000; - mint!(app, token_x, "bob", amount, "alice").unwrap(); - let amount_x = balance_of!(app, token_x, "bob"); + mint!(app, token_x, bob, amount, alice).unwrap(); + let amount_x = balance_of!(app, token_x, bob); assert_eq!(amount_x, amount); - approve!(app, token_x, dex, amount, "bob").unwrap(); + approve!(app, token_x, dex, amount, bob).unwrap(); let pool_state_before = get_pool!(app, dex, token_x, token_y, fee_tier).unwrap(); let swap_amount = TokenAmount::new(amount); let slippage = SqrtPrice::new(MIN_SQRT_PRICE); - swap!(app, dex, pool_key, true, swap_amount, true, slippage, "bob").unwrap(); + swap!(app, dex, pool_key, true, swap_amount, true, slippage, bob).unwrap(); let pool_state_after = get_pool!(app, dex, token_x, token_y, fee_tier).unwrap(); assert_eq!( @@ -252,8 +263,8 @@ fn test_burn_nft() { assert_eq!(pool_state_after.current_tick_index, -10); assert_ne!(pool_state_after.sqrt_price, pool_state_before.sqrt_price); - let amount_x = balance_of!(app, token_x, "bob"); - let amount_y = balance_of!(app, token_y, "bob"); + let amount_x = balance_of!(app, token_x, bob); + let amount_y = balance_of!(app, token_y, bob); assert_eq!(amount_x, 0); assert_eq!(amount_y, 993); @@ -262,7 +273,7 @@ fn test_burn_nft() { let dex_y_before_remove = balance_of!(app, token_y, dex); // Remove position - let sender = Addr::unchecked("alice"); + let sender = Addr::unchecked(alice); let token_id = 1; app.execute( sender, @@ -305,18 +316,24 @@ fn test_burn_nft() { #[test] fn test_transfer_nft() { + let (mut app, accounts) = MockApp::new(&[ + ("alice", &coins(100_000_000_000, FEE_DENOM)), + ("bob", &coins(100_000_000_000, FEE_DENOM)), + ]); + let alice = &accounts[0]; + let bob = &accounts[1]; + let init_tick = -23028; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); + let dex = create_dex!(app, Percentage::new(0), alice); let initial_balance = 10u128.pow(10); - let (token_x, token_y) = create_tokens!(app, initial_balance, initial_balance); + let (token_x, token_y) = create_tokens!(app, initial_balance, initial_balance, alice); let fee_tier = FeeTier::new(Percentage::from_scale(2, 4), 3).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); create_pool!( app, @@ -326,12 +343,12 @@ fn test_transfer_nft() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); - approve!(app, token_x, dex, initial_balance, "alice").unwrap(); - approve!(app, token_y, dex, initial_balance, "alice").unwrap(); + approve!(app, token_x, dex, initial_balance, alice).unwrap(); + approve!(app, token_y, dex, initial_balance, alice).unwrap(); let pool_key = PoolKey::new(token_x.to_string(), token_y.to_string(), fee_tier).unwrap(); let tick_indexes = [-9780, -42, 0, 9, 276, 32343, -50001]; @@ -339,7 +356,7 @@ fn test_transfer_nft() { let pool_state = get_pool!(app, dex, token_x, token_y, fee_tier).unwrap(); { app.execute( - Addr::unchecked("alice"), + Addr::unchecked(alice), dex.clone(), &msg::ExecuteMsg::Mint { extension: msg::NftExtensionMsg { @@ -359,7 +376,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Tokens { - owner: Addr::unchecked("alice"), + owner: Addr::unchecked(alice), start_after: None, limit: None, }, @@ -372,7 +389,7 @@ fn test_transfer_nft() { // Open additional positions { app.execute( - Addr::unchecked("alice"), + Addr::unchecked(alice), dex.clone(), &msg::ExecuteMsg::Mint { extension: msg::NftExtensionMsg { @@ -388,7 +405,7 @@ fn test_transfer_nft() { ) .unwrap(); app.execute( - Addr::unchecked("alice"), + Addr::unchecked(alice), dex.clone(), &msg::ExecuteMsg::Mint { extension: msg::NftExtensionMsg { @@ -404,7 +421,7 @@ fn test_transfer_nft() { ) .unwrap(); app.execute( - Addr::unchecked("alice"), + Addr::unchecked(alice), dex.clone(), &msg::ExecuteMsg::Mint { extension: msg::NftExtensionMsg { @@ -429,7 +446,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Tokens { - owner: Addr::unchecked("alice"), + owner: Addr::unchecked(alice), start_after: None, limit: None, }, @@ -441,7 +458,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Tokens { - owner: Addr::unchecked("bob"), + owner: Addr::unchecked(bob), start_after: None, limit: None, }, @@ -451,7 +468,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Position { - owner_id: Addr::unchecked("alice"), + owner_id: Addr::unchecked(alice), index: transferred_index, }, ) @@ -477,16 +494,16 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Position { - owner_id: Addr::unchecked("alice"), + owner_id: Addr::unchecked(alice), index: transferred_index, }, ) .unwrap(); app.execute( - Addr::unchecked("alice"), + Addr::unchecked(alice), dex.clone(), &msg::ExecuteMsg::TransferNft { - recipient: Addr::unchecked("bob"), + recipient: Addr::unchecked(bob), token_id, }, &[], @@ -497,7 +514,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Position { - owner_id: Addr::unchecked("bob"), + owner_id: Addr::unchecked(bob), index: transferred_index, }, ) @@ -513,7 +530,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Tokens { - owner: Addr::unchecked("alice"), + owner: Addr::unchecked(alice), start_after: None, limit: None, }, @@ -525,7 +542,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Tokens { - owner: Addr::unchecked("bob"), + owner: Addr::unchecked(bob), start_after: None, limit: None, }, @@ -536,7 +553,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Position { - owner_id: Addr::unchecked("alice"), + owner_id: Addr::unchecked(alice), index: transferred_index, }, ) @@ -566,7 +583,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Tokens { - owner: Addr::unchecked("alice"), + owner: Addr::unchecked(alice), start_after: None, limit: None, }, @@ -578,7 +595,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Tokens { - owner: Addr::unchecked("bob"), + owner: Addr::unchecked(bob), start_after: None, limit: None, }, @@ -600,16 +617,16 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Position { - owner_id: Addr::unchecked("alice"), + owner_id: Addr::unchecked(alice), index: transferred_index, }, ) .unwrap(); app.execute( - Addr::unchecked("alice"), + Addr::unchecked(alice), dex.clone(), &msg::ExecuteMsg::TransferNft { - recipient: Addr::unchecked("bob"), + recipient: Addr::unchecked(bob), token_id, }, &[], @@ -622,7 +639,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Tokens { - owner: Addr::unchecked("alice"), + owner: Addr::unchecked(alice), start_after: None, limit: None, }, @@ -634,7 +651,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Tokens { - owner: Addr::unchecked("bob"), + owner: Addr::unchecked(bob), start_after: None, limit: None, }, @@ -644,7 +661,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Position { - owner_id: Addr::unchecked("alice"), + owner_id: Addr::unchecked(alice), index: transferred_index, }, ) @@ -669,7 +686,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Tokens { - owner: Addr::unchecked("alice"), + owner: Addr::unchecked(alice), start_after: None, limit: None, }, @@ -680,7 +697,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Position { - owner_id: Addr::unchecked("alice"), + owner_id: Addr::unchecked(alice), index: transferred_index, }, ) @@ -695,16 +712,16 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Position { - owner_id: Addr::unchecked("alice"), + owner_id: Addr::unchecked(alice), index: transferred_index, }, ) .unwrap(); app.execute( - Addr::unchecked("alice"), + Addr::unchecked(alice), dex.clone(), &msg::ExecuteMsg::TransferNft { - recipient: Addr::unchecked("bob"), + recipient: Addr::unchecked(bob), token_id, }, &[], @@ -716,7 +733,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Tokens { - owner: Addr::unchecked("bob"), + owner: Addr::unchecked(bob), start_after: None, limit: None, }, @@ -727,7 +744,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Position { - owner_id: Addr::unchecked("bob"), + owner_id: Addr::unchecked(bob), index: recipient_position_index, }, ) @@ -750,7 +767,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Tokens { - owner: Addr::unchecked("bob"), + owner: Addr::unchecked(bob), start_after: None, limit: None, }, @@ -760,7 +777,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Position { - owner_id: Addr::unchecked("alice"), + owner_id: Addr::unchecked(alice), index: transferred_index, }, ) @@ -775,16 +792,16 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Position { - owner_id: Addr::unchecked("alice"), + owner_id: Addr::unchecked(alice), index: transferred_index, }, ) .unwrap(); app.execute( - Addr::unchecked("alice"), + Addr::unchecked(alice), dex.clone(), &msg::ExecuteMsg::TransferNft { - recipient: Addr::unchecked("bob"), + recipient: Addr::unchecked(bob), token_id, }, &[], @@ -797,7 +814,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Tokens { - owner: Addr::unchecked("bob"), + owner: Addr::unchecked(bob), start_after: None, limit: None, }, @@ -808,7 +825,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Position { - owner_id: Addr::unchecked("bob"), + owner_id: Addr::unchecked(bob), index: recipient_position_index, }, ) @@ -824,7 +841,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Tokens { - owner: Addr::unchecked("alice"), + owner: Addr::unchecked(alice), start_after: None, limit: None, }, @@ -847,7 +864,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Tokens { - owner: Addr::unchecked("alice"), + owner: Addr::unchecked(alice), start_after: None, limit: None, }, @@ -859,7 +876,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Tokens { - owner: Addr::unchecked("bob"), + owner: Addr::unchecked(bob), start_after: None, limit: None, }, @@ -869,7 +886,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Position { - owner_id: Addr::unchecked("bob"), + owner_id: Addr::unchecked(bob), index: transferred_index, }, ) @@ -895,16 +912,16 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Position { - owner_id: Addr::unchecked("bob"), + owner_id: Addr::unchecked(bob), index: transferred_index, }, ) .unwrap(); app.execute( - Addr::unchecked("bob"), + Addr::unchecked(bob), dex.clone(), &msg::ExecuteMsg::TransferNft { - recipient: Addr::unchecked("alice"), + recipient: Addr::unchecked(alice), token_id, }, &[], @@ -917,7 +934,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Tokens { - owner: Addr::unchecked("alice"), + owner: Addr::unchecked(alice), start_after: None, limit: None, }, @@ -929,7 +946,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Tokens { - owner: Addr::unchecked("bob"), + owner: Addr::unchecked(bob), start_after: None, limit: None, }, @@ -939,7 +956,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Position { - owner_id: Addr::unchecked("bob"), + owner_id: Addr::unchecked(bob), index: transferred_index, }, ) @@ -954,7 +971,7 @@ fn test_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Position { - owner_id: Addr::unchecked("alice"), + owner_id: Addr::unchecked(alice), index: transferred_index, }, ) @@ -978,18 +995,23 @@ fn test_transfer_nft() { #[test] fn test_only_owner_can_transfer_nft() { + let (mut app, accounts) = MockApp::new(&[ + ("alice", &coins(100_000_000_000, FEE_DENOM)), + ("bob", &coins(100_000_000_000, FEE_DENOM)), + ]); + let alice = &accounts[0]; + let bob = &accounts[1]; let init_tick = -23028; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); + let dex = create_dex!(app, Percentage::new(0), alice); let initial_balance = 10u128.pow(10); - let (token_x, token_y) = create_tokens!(app, initial_balance, initial_balance); + let (token_x, token_y) = create_tokens!(app, initial_balance, initial_balance, alice); let fee_tier = FeeTier::new(Percentage::from_scale(2, 4), 3).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); create_pool!( app, @@ -999,12 +1021,12 @@ fn test_only_owner_can_transfer_nft() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); - approve!(app, token_x, dex, initial_balance, "alice").unwrap(); - approve!(app, token_y, dex, initial_balance, "alice").unwrap(); + approve!(app, token_x, dex, initial_balance, alice).unwrap(); + approve!(app, token_y, dex, initial_balance, alice).unwrap(); let pool_key = PoolKey::new(token_x.to_string(), token_y.to_string(), fee_tier).unwrap(); let tick_indexes = [-9780, -42, 0, 9, 276, 32343, -50001]; @@ -1012,7 +1034,7 @@ fn test_only_owner_can_transfer_nft() { let pool_state = get_pool!(app, dex, token_x, token_y, fee_tier).unwrap(); { app.execute( - Addr::unchecked("alice"), + Addr::unchecked(alice), dex.clone(), &msg::ExecuteMsg::Mint { extension: msg::NftExtensionMsg { @@ -1032,7 +1054,7 @@ fn test_only_owner_can_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Tokens { - owner: Addr::unchecked("alice"), + owner: Addr::unchecked(alice), start_after: None, limit: None, }, @@ -1045,7 +1067,7 @@ fn test_only_owner_can_transfer_nft() { // Open additional positions { app.execute( - Addr::unchecked("alice"), + Addr::unchecked(alice), dex.clone(), &msg::ExecuteMsg::Mint { extension: msg::NftExtensionMsg { @@ -1061,7 +1083,7 @@ fn test_only_owner_can_transfer_nft() { ) .unwrap(); app.execute( - Addr::unchecked("alice"), + Addr::unchecked(alice), dex.clone(), &msg::ExecuteMsg::Mint { extension: msg::NftExtensionMsg { @@ -1077,7 +1099,7 @@ fn test_only_owner_can_transfer_nft() { ) .unwrap(); app.execute( - Addr::unchecked("alice"), + Addr::unchecked(alice), dex.clone(), &msg::ExecuteMsg::Mint { extension: msg::NftExtensionMsg { @@ -1100,7 +1122,7 @@ fn test_only_owner_can_transfer_nft() { .query( dex.clone(), &msg::QueryMsg::Position { - owner_id: Addr::unchecked("alice"), + owner_id: Addr::unchecked(alice), index: transferred_index, }, ) @@ -1108,31 +1130,39 @@ fn test_only_owner_can_transfer_nft() { let error = app .execute( - Addr::unchecked("bob"), + Addr::unchecked(bob), dex.clone(), &msg::ExecuteMsg::TransferNft { - recipient: Addr::unchecked("alice"), + recipient: Addr::unchecked(alice), token_id, }, &[], ) .unwrap_err(); - assert_eq!( - error.root_cause().to_string(), - ContractError::Unauthorized {}.to_string() - ); + assert!(error + .root_cause() + .to_string() + .contains(&ContractError::Unauthorized {}.to_string())); } } #[test] fn test_approving_revoking() { - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); - let (token_x, token_y) = create_tokens!(app, 500, 500); + let (mut app, accounts) = MockApp::new(&[ + ("alice", &coins(100_000_000_000, FEE_DENOM)), + ("random", &coins(100_000_000_000, FEE_DENOM)), + ("person", &coins(100_000_000_000, FEE_DENOM)), + ]); + let alice = &accounts[0]; + let random = &accounts[1]; + let person = &accounts[2]; + + let dex = create_dex!(app, Percentage::new(0), alice); + let (token_x, token_y) = create_tokens!(app, 500, 500, alice); let fee_tier = FeeTier::new(Percentage::new(0), 1).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let init_tick = 10; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); @@ -1144,17 +1174,17 @@ fn test_approving_revoking() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); - approve!(app, token_x, dex, 500, "alice").unwrap(); - approve!(app, token_y, dex, 500, "alice").unwrap(); + approve!(app, token_x, dex, 500, alice).unwrap(); + approve!(app, token_y, dex, 500, alice).unwrap(); let pool_key = PoolKey::new(token_x.to_string(), token_y.to_string(), fee_tier).unwrap(); app.execute( - Addr::unchecked("alice"), + Addr::unchecked(alice), dex.clone(), &msg::ExecuteMsg::Mint { extension: msg::NftExtensionMsg { @@ -1174,7 +1204,7 @@ fn test_approving_revoking() { .query( dex.clone(), &msg::QueryMsg::Position { - owner_id: Addr::unchecked("alice"), + owner_id: Addr::unchecked(alice), index: 0, }, ) @@ -1183,10 +1213,10 @@ fn test_approving_revoking() { // Give random transferring power let res = app .execute( - Addr::unchecked("alice"), + Addr::unchecked(alice), dex.clone(), &msg::ExecuteMsg::Approve { - spender: Addr::unchecked("random"), + spender: Addr::unchecked(random), token_id: token_id.clone(), expires: None, }, @@ -1204,17 +1234,17 @@ fn test_approving_revoking() { }, attr("action", "approve"), attr("token_id", token_id.to_string()), - attr("sender", "alice"), - attr("spender", "random"), + attr("sender", alice), + attr("spender", random), ] ); // random can now transfer app.execute( - Addr::unchecked("random"), + Addr::unchecked(random), dex.clone(), &msg::ExecuteMsg::TransferNft { - recipient: Addr::unchecked("person"), + recipient: Addr::unchecked(person), token_id: token_id.clone(), }, &[], @@ -1234,17 +1264,17 @@ fn test_approving_revoking() { assert_eq!( res, OwnerOfResponse { - owner: Addr::unchecked("person"), + owner: Addr::unchecked(person), approvals: vec![], } ); // Approve, revoke, and check for empty, to test revoke app.execute( - Addr::unchecked("person"), + Addr::unchecked(person), dex.clone(), &msg::ExecuteMsg::Approve { - spender: Addr::unchecked("random"), + spender: Addr::unchecked(random), token_id: token_id.clone(), expires: None, }, @@ -1253,10 +1283,10 @@ fn test_approving_revoking() { .unwrap(); app.execute( - Addr::unchecked("person"), + Addr::unchecked(person), dex.clone(), &msg::ExecuteMsg::Revoke { - spender: Addr::unchecked("random"), + spender: Addr::unchecked(random), token_id: token_id.clone(), }, &[], @@ -1276,7 +1306,7 @@ fn test_approving_revoking() { assert_eq!( res, OwnerOfResponse { - owner: Addr::unchecked("person"), + owner: Addr::unchecked(person), approvals: vec![], } ); diff --git a/contracts/oraiswap-v3/src/tests/position.rs b/contracts/oraiswap-v3/src/tests/position.rs index b1536a6..cb1b62e 100644 --- a/contracts/oraiswap-v3/src/tests/position.rs +++ b/contracts/oraiswap-v3/src/tests/position.rs @@ -1,3 +1,4 @@ +use cosmwasm_std::coins; use decimal::*; use crate::{ @@ -10,15 +11,19 @@ use crate::{ ContractError, FeeTier, PoolKey, MIN_SQRT_PRICE, }; +use super::helper::FEE_DENOM; + #[test] fn test_create_position() { - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); - let (token_x, token_y) = create_tokens!(app, 500, 500); + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; + + let dex = create_dex!(app, Percentage::new(0), alice); + let (token_x, token_y) = create_tokens!(app, 500, 500, alice); let fee_tier = FeeTier::new(Percentage::new(0), 1).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let init_tick = 0; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); @@ -30,12 +35,12 @@ fn test_create_position() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); - approve!(app, token_x, dex, 500, "alice").unwrap(); - approve!(app, token_y, dex, 500, "alice").unwrap(); + approve!(app, token_x, dex, 500, alice).unwrap(); + approve!(app, token_y, dex, 500, alice).unwrap(); let pool_key = PoolKey::new(token_x.to_string(), token_y.to_string(), fee_tier).unwrap(); @@ -48,20 +53,22 @@ fn test_create_position() { Liquidity::new(10), SqrtPrice::new(0), SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); } #[test] fn test_position_same_upper_and_lower_tick() { - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); - let (token_x, token_y) = create_tokens!(app, 500, 500); + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; + + let dex = create_dex!(app, Percentage::new(0), alice); + let (token_x, token_y) = create_tokens!(app, 500, 500, alice); let fee_tier = FeeTier::new(Percentage::new(0), 1).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let init_tick = 10; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); @@ -73,12 +80,12 @@ fn test_position_same_upper_and_lower_tick() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); - approve!(app, token_x, dex, 500, "alice").unwrap(); - approve!(app, token_y, dex, 500, "alice").unwrap(); + approve!(app, token_x, dex, 500, alice).unwrap(); + approve!(app, token_y, dex, 500, alice).unwrap(); let pool_key = PoolKey::new(token_x.to_string(), token_y.to_string(), fee_tier).unwrap(); @@ -91,18 +98,24 @@ fn test_position_same_upper_and_lower_tick() { Liquidity::new(10), SqrtPrice::new(0), SqrtPrice::max_instance(), - "alice" + alice ) .unwrap_err(); - assert_eq!( - error.root_cause().to_string(), - ContractError::InvalidTickIndex {}.to_string() - ); + assert!(error + .root_cause() + .to_string() + .contains(&ContractError::InvalidTickIndex {}.to_string())); } #[test] fn test_remove_position() { + let (mut app, accounts) = MockApp::new(&[ + ("alice", &coins(100_000_000_000, FEE_DENOM)), + ("bob", &coins(100_000_000_000, FEE_DENOM)), + ]); + let alice = &accounts[0]; + let bob = &accounts[1]; let fee_tier = FeeTier::new(Percentage::from_scale(6, 3), 10).unwrap(); let init_tick = 0; @@ -111,14 +124,13 @@ fn test_remove_position() { let initial_mint = 10u128.pow(10); - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::from_scale(1, 2)); + let dex = create_dex!(app, Percentage::from_scale(1, 2), alice); - let (token_x, token_y) = create_tokens!(app, initial_mint, initial_mint); + let (token_x, token_y) = create_tokens!(app, initial_mint, initial_mint, alice); let pool_key = PoolKey::new(token_x.to_string(), token_y.to_string(), fee_tier).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); create_pool!( app, @@ -128,7 +140,7 @@ fn test_remove_position() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); @@ -136,8 +148,8 @@ fn test_remove_position() { let upper_tick_index = 10; let liquidity_delta = Liquidity::from_integer(1_000_000); - approve!(app, token_x, dex, initial_mint, "alice").unwrap(); - approve!(app, token_y, dex, initial_mint, "alice").unwrap(); + approve!(app, token_x, dex, initial_mint, alice).unwrap(); + approve!(app, token_y, dex, initial_mint, alice).unwrap(); let pool_state = get_pool!(app, dex, token_x, token_y, fee_tier).unwrap(); @@ -150,7 +162,7 @@ fn test_remove_position() { liquidity_delta, pool_state.sqrt_price, pool_state.sqrt_price, - "alice" + alice ) .unwrap(); @@ -163,8 +175,8 @@ fn test_remove_position() { let incorrect_lower_tick_index = lower_tick_index - 50; let incorrect_upper_tick_index = upper_tick_index + 50; - approve!(app, token_x, dex, liquidity_delta.0, "alice").unwrap(); - approve!(app, token_y, dex, liquidity_delta.0, "alice").unwrap(); + approve!(app, token_x, dex, liquidity_delta.0, alice).unwrap(); + approve!(app, token_y, dex, liquidity_delta.0, alice).unwrap(); create_position!( app, @@ -175,28 +187,28 @@ fn test_remove_position() { liquidity_delta, pool_state.sqrt_price, pool_state.sqrt_price, - "alice" + alice ) .unwrap(); - let position_state = get_position!(app, dex, 1, "alice").unwrap(); + let position_state = get_position!(app, dex, 1, alice).unwrap(); // Check position assert!(position_state.lower_tick_index == incorrect_lower_tick_index); assert!(position_state.upper_tick_index == incorrect_upper_tick_index); } let amount = 1000; - mint!(app, token_x, "bob", amount, "alice").unwrap(); - let amount_x = balance_of!(app, token_x, "bob"); + mint!(app, token_x, bob, amount, alice).unwrap(); + let amount_x = balance_of!(app, token_x, bob); assert_eq!(amount_x, amount); - approve!(app, token_x, dex, amount, "bob").unwrap(); + approve!(app, token_x, dex, amount, bob).unwrap(); let pool_state_before = get_pool!(app, dex, token_x, token_y, fee_tier).unwrap(); let swap_amount = TokenAmount::new(amount); let slippage = SqrtPrice::new(MIN_SQRT_PRICE); - swap!(app, dex, pool_key, true, swap_amount, true, slippage, "bob").unwrap(); + swap!(app, dex, pool_key, true, swap_amount, true, slippage, bob).unwrap(); let pool_state_after = get_pool!(app, dex, token_x, token_y, fee_tier).unwrap(); assert_eq!( @@ -214,8 +226,8 @@ fn test_remove_position() { assert_eq!(pool_state_after.current_tick_index, -10); assert_ne!(pool_state_after.sqrt_price, pool_state_before.sqrt_price); - let amount_x = balance_of!(app, token_x, "bob"); - let amount_y = balance_of!(app, token_y, "bob"); + let amount_x = balance_of!(app, token_x, bob); + let amount_y = balance_of!(app, token_y, bob); assert_eq!(amount_x, 0); assert_eq!(amount_y, 993); @@ -224,7 +236,7 @@ fn test_remove_position() { let dex_y_before_remove = balance_of!(app, token_y, dex); // Remove position - remove_position!(app, dex, remove_position_index, "alice").unwrap(); + remove_position!(app, dex, remove_position_index, alice).unwrap(); // Load states let pool_state = get_pool!(app, dex, token_x, token_y, fee_tier).unwrap(); @@ -260,21 +272,23 @@ fn test_remove_position() { #[test] fn test_position_within_current_tick() { + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; + let max_tick_test = 177_450; // for tickSpacing 4 let min_tick_test = -max_tick_test; let init_tick = -23028; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); + let dex = create_dex!(app, Percentage::new(0), alice); let initial_balance = 100_000_000; - let (token_x, token_y) = create_tokens!(app, initial_balance, initial_balance); + let (token_x, token_y) = create_tokens!(app, initial_balance, initial_balance, alice); let fee_tier = FeeTier::new(Percentage::from_scale(2, 4), 4).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); create_pool!( app, @@ -284,12 +298,12 @@ fn test_position_within_current_tick() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); - approve!(app, token_x, dex, initial_balance, "alice").unwrap(); - approve!(app, token_y, dex, initial_balance, "alice").unwrap(); + approve!(app, token_x, dex, initial_balance, alice).unwrap(); + approve!(app, token_y, dex, initial_balance, alice).unwrap(); let pool_key = PoolKey::new(token_x.to_string(), token_y.to_string(), fee_tier).unwrap(); let lower_tick_index = min_tick_test + 10; @@ -308,17 +322,17 @@ fn test_position_within_current_tick() { liquidity_delta, pool_state.sqrt_price, SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); // Load states - let position_state = get_position!(app, dex, 0, "alice").unwrap(); + let position_state = get_position!(app, dex, 0, alice).unwrap(); let pool_state = get_pool!(app, dex, token_x, token_y, fee_tier).unwrap(); let lower_tick = get_tick!(app, dex, pool_key, lower_tick_index).unwrap(); let upper_tick = get_tick!(app, dex, pool_key, upper_tick_index).unwrap(); - let alice_x = balance_of!(app, token_x, "alice"); - let alice_y = balance_of!(app, token_y, "alice"); + let alice_x = balance_of!(app, token_x, alice); + let alice_y = balance_of!(app, token_y, alice); let dex_x = balance_of!(app, token_x, dex); let dex_y = balance_of!(app, token_y, dex); @@ -357,17 +371,20 @@ fn test_position_within_current_tick() { #[test] fn test_position_below_current_tick() { + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; + let init_tick = -23028; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); + + let dex = create_dex!(app, Percentage::new(0), alice); let initial_balance = 10_000_000_000; - let (token_x, token_y) = create_tokens!(app, initial_balance, initial_balance); + let (token_x, token_y) = create_tokens!(app, initial_balance, initial_balance, alice); let fee_tier = FeeTier::new(Percentage::from_scale(2, 4), 4).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); create_pool!( app, @@ -377,12 +394,12 @@ fn test_position_below_current_tick() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); - approve!(app, token_x, dex, initial_balance, "alice").unwrap(); - approve!(app, token_y, dex, initial_balance, "alice").unwrap(); + approve!(app, token_x, dex, initial_balance, alice).unwrap(); + approve!(app, token_y, dex, initial_balance, alice).unwrap(); let pool_key = PoolKey::new(token_x.to_string(), token_y.to_string(), fee_tier).unwrap(); let lower_tick_index = -46080; @@ -401,17 +418,17 @@ fn test_position_below_current_tick() { liquidity_delta, pool_state_before.sqrt_price, SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); // Load states - let position_state = get_position!(app, dex, 0, "alice").unwrap(); + let position_state = get_position!(app, dex, 0, alice).unwrap(); let pool_state = get_pool!(app, dex, token_x, token_y, fee_tier).unwrap(); let lower_tick = get_tick!(app, dex, pool_key, lower_tick_index).unwrap(); let upper_tick = get_tick!(app, dex, pool_key, upper_tick_index).unwrap(); - let alice_x = balance_of!(app, token_x, "alice"); - let alice_y = balance_of!(app, token_y, "alice"); + let alice_x = balance_of!(app, token_x, alice); + let alice_y = balance_of!(app, token_y, alice); let dex_x = balance_of!(app, token_x, dex); let dex_y = balance_of!(app, token_y, dex); @@ -451,18 +468,20 @@ fn test_position_below_current_tick() { #[test] fn test_position_above_current_tick() { + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; + let init_tick = -23028; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); + let dex = create_dex!(app, Percentage::new(0), alice); let initial_balance = 10_000_000_000; - let (token_x, token_y) = create_tokens!(app, initial_balance, initial_balance); + let (token_x, token_y) = create_tokens!(app, initial_balance, initial_balance, alice); let fee_tier = FeeTier::new(Percentage::from_scale(2, 4), 4).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); create_pool!( app, @@ -472,12 +491,12 @@ fn test_position_above_current_tick() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); - approve!(app, token_x, dex, initial_balance, "alice").unwrap(); - approve!(app, token_y, dex, initial_balance, "alice").unwrap(); + approve!(app, token_x, dex, initial_balance, alice).unwrap(); + approve!(app, token_y, dex, initial_balance, alice).unwrap(); let pool_key = PoolKey::new(token_x.to_string(), token_y.to_string(), fee_tier).unwrap(); let lower_tick_index = -22980; @@ -495,17 +514,17 @@ fn test_position_above_current_tick() { liquidity_delta, pool_state.sqrt_price, SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); // Load states - let position_state = get_position!(app, dex, 0, "alice").unwrap(); + let position_state = get_position!(app, dex, 0, alice).unwrap(); let pool_state = get_pool!(app, dex, token_x, token_y, fee_tier).unwrap(); let lower_tick = get_tick!(app, dex, pool_key, lower_tick_index).unwrap(); let upper_tick = get_tick!(app, dex, pool_key, upper_tick_index).unwrap(); - let alice_x = balance_of!(app, token_x, "alice"); - let alice_y = balance_of!(app, token_y, "alice"); + let alice_x = balance_of!(app, token_x, alice); + let alice_y = balance_of!(app, token_y, alice); let dex_x = balance_of!(app, token_x, dex); let dex_y = balance_of!(app, token_y, dex); diff --git a/contracts/oraiswap-v3/src/tests/position_list.rs b/contracts/oraiswap-v3/src/tests/position_list.rs index 09d58cf..fdb3d95 100644 --- a/contracts/oraiswap-v3/src/tests/position_list.rs +++ b/contracts/oraiswap-v3/src/tests/position_list.rs @@ -1,3 +1,4 @@ +use cosmwasm_std::coins; use decimal::*; use crate::{ @@ -5,20 +6,22 @@ use crate::{ liquidity::Liquidity, percentage::Percentage, sqrt_price::{calculate_sqrt_price, SqrtPrice}, - tests::helper::{macros::*, MockApp}, + tests::helper::{macros::*, MockApp, FEE_DENOM}, FeeTier, PoolKey, }; #[test] fn test_remove_position_from_empty_list() { - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::from_scale(6, 3)); + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; + + let dex = create_dex!(app, Percentage::from_scale(6, 3), alice); let initial_amount = 10u128.pow(10); - let (token_x, token_y) = create_tokens!(app, initial_amount, initial_amount); + let (token_x, token_y) = create_tokens!(app, initial_amount, initial_amount, alice); let fee_tier = FeeTier::new(Percentage::from_scale(6, 3), 3).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let init_tick = -23028; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); @@ -30,27 +33,30 @@ fn test_remove_position_from_empty_list() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); - let error = remove_position!(app, dex, 0, "alice").unwrap_err(); + let error = remove_position!(app, dex, 0, alice).unwrap_err(); assert!(error.root_cause().to_string().contains("not found")); } #[test] fn test_add_multiple_positions() { + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; + let init_tick = -23028; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); + + let dex = create_dex!(app, Percentage::new(0), alice); let initial_balance = 10u128.pow(10); - let (token_x, token_y) = create_tokens!(app, initial_balance, initial_balance); + let (token_x, token_y) = create_tokens!(app, initial_balance, initial_balance, alice); let fee_tier = FeeTier::new(Percentage::from_scale(2, 4), 3).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); create_pool!( app, @@ -60,12 +66,12 @@ fn test_add_multiple_positions() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); - approve!(app, token_x, dex, initial_balance, "alice").unwrap(); - approve!(app, token_y, dex, initial_balance, "alice").unwrap(); + approve!(app, token_x, dex, initial_balance, alice).unwrap(); + approve!(app, token_y, dex, initial_balance, alice).unwrap(); let pool_key = PoolKey::new(token_x.to_string(), token_y.to_string(), fee_tier).unwrap(); let tick_indexes = [-9780, -42, 0, 9, 276, 32343, -50001]; @@ -83,7 +89,7 @@ fn test_add_multiple_positions() { liquidity_delta, pool_state.sqrt_price, SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); @@ -96,7 +102,7 @@ fn test_add_multiple_positions() { liquidity_delta, pool_state.sqrt_price, SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); @@ -109,7 +115,7 @@ fn test_add_multiple_positions() { liquidity_delta, pool_state.sqrt_price, SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); @@ -122,7 +128,7 @@ fn test_add_multiple_positions() { liquidity_delta, pool_state.sqrt_price, SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); } @@ -130,12 +136,12 @@ fn test_add_multiple_positions() { // Remove middle position { let position_index_to_remove = 2; - let positions_list_before = get_all_positions!(app, dex, "alice"); + let positions_list_before = get_all_positions!(app, dex, alice); let last_position = &positions_list_before[positions_list_before.len() - 1]; - remove_position!(app, dex, position_index_to_remove, "alice").unwrap(); + remove_position!(app, dex, position_index_to_remove, alice).unwrap(); - let positions_list_after = get_all_positions!(app, dex, "alice"); + let positions_list_after = get_all_positions!(app, dex, alice); let tested_position = &positions_list_after[position_index_to_remove as usize]; // Last position should be at removed index @@ -162,7 +168,7 @@ fn test_add_multiple_positions() { } // Add position in place of the removed one { - let positions_list_before = get_all_positions!(app, dex, "alice"); + let positions_list_before = get_all_positions!(app, dex, alice); create_position!( app, @@ -173,37 +179,37 @@ fn test_add_multiple_positions() { liquidity_delta, pool_state.sqrt_price, SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); - let positions_list_after = get_all_positions!(app, dex, "alice"); + let positions_list_after = get_all_positions!(app, dex, alice); assert_eq!(positions_list_before.len() + 1, positions_list_after.len()); } // Remove last position { - let last_position_index_before = get_all_positions!(app, dex, "alice").len() - 1; + let last_position_index_before = get_all_positions!(app, dex, alice).len() - 1; - remove_position!(app, dex, last_position_index_before as u32, "alice").unwrap(); + remove_position!(app, dex, last_position_index_before as u32, alice).unwrap(); - let last_position_index_after = get_all_positions!(app, dex, "alice").len() - 1; + let last_position_index_after = get_all_positions!(app, dex, alice).len() - 1; assert_eq!(last_position_index_before - 1, last_position_index_after) } // Remove all positions { - let last_position_index = get_all_positions!(app, dex, "alice").len(); + let last_position_index = get_all_positions!(app, dex, alice).len(); for i in (0..last_position_index).rev() { - remove_position!(app, dex, i as u32, "alice").unwrap(); + remove_position!(app, dex, i as u32, alice).unwrap(); } - let list_length = get_all_positions!(app, dex, "alice").len(); + let list_length = get_all_positions!(app, dex, alice).len(); assert_eq!(list_length, 0); } // Add position to cleared list { - let list_length_before = get_all_positions!(app, dex, "alice").len(); + let list_length_before = get_all_positions!(app, dex, alice).len(); create_position!( app, @@ -214,28 +220,33 @@ fn test_add_multiple_positions() { liquidity_delta, pool_state.sqrt_price, SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); - let list_length_after = get_all_positions!(app, dex, "alice").len(); + let list_length_after = get_all_positions!(app, dex, alice).len(); assert_eq!(list_length_after, list_length_before + 1); } } #[test] fn test_only_owner_can_modify_position_list() { + let (mut app, accounts) = MockApp::new(&[ + ("alice", &coins(100_000_000_000, FEE_DENOM)), + ("bob", &coins(100_000_000_000, FEE_DENOM)), + ]); + let alice = &accounts[0]; + let bob = &accounts[1]; let init_tick = -23028; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); + let dex = create_dex!(app, Percentage::new(0), alice); let initial_balance = 10u128.pow(10); - let (token_x, token_y) = create_tokens!(app, initial_balance, initial_balance); + let (token_x, token_y) = create_tokens!(app, initial_balance, initial_balance, alice); let fee_tier = FeeTier::new(Percentage::from_scale(2, 4), 3).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); create_pool!( app, @@ -245,12 +256,12 @@ fn test_only_owner_can_modify_position_list() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); - approve!(app, token_x, dex, initial_balance, "alice").unwrap(); - approve!(app, token_y, dex, initial_balance, "alice").unwrap(); + approve!(app, token_x, dex, initial_balance, alice).unwrap(); + approve!(app, token_y, dex, initial_balance, alice).unwrap(); let pool_key = PoolKey::new(token_x.to_string(), token_y.to_string(), fee_tier).unwrap(); let tick_indexes = [-9780, -42, 0, 9, 276, 32343, -50001]; @@ -268,7 +279,7 @@ fn test_only_owner_can_modify_position_list() { liquidity_delta, pool_state.sqrt_price, SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); @@ -281,7 +292,7 @@ fn test_only_owner_can_modify_position_list() { liquidity_delta, pool_state.sqrt_price, SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); @@ -294,7 +305,7 @@ fn test_only_owner_can_modify_position_list() { liquidity_delta, pool_state.sqrt_price, SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); @@ -307,7 +318,7 @@ fn test_only_owner_can_modify_position_list() { liquidity_delta, pool_state.sqrt_price, SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); } @@ -315,12 +326,12 @@ fn test_only_owner_can_modify_position_list() { // Remove middle position { let position_index_to_remove = 2; - let positions_list_before = get_all_positions!(app, dex, "alice"); + let positions_list_before = get_all_positions!(app, dex, alice); let last_position = &positions_list_before[positions_list_before.len() - 1]; - remove_position!(app, dex, position_index_to_remove, "alice").unwrap(); + remove_position!(app, dex, position_index_to_remove, alice).unwrap(); - let positions_list_after = get_all_positions!(app, dex, "alice"); + let positions_list_after = get_all_positions!(app, dex, alice); let tested_position = &positions_list_after[position_index_to_remove as usize]; // Last position should be at removed index @@ -347,7 +358,7 @@ fn test_only_owner_can_modify_position_list() { } // Add position in place of the removed one { - let positions_list_before = get_all_positions!(app, dex, "alice"); + let positions_list_before = get_all_positions!(app, dex, alice); create_position!( app, @@ -358,18 +369,18 @@ fn test_only_owner_can_modify_position_list() { liquidity_delta, pool_state.sqrt_price, SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); - let positions_list_after = get_all_positions!(app, dex, "alice"); + let positions_list_after = get_all_positions!(app, dex, alice); assert_eq!(positions_list_before.len() + 1, positions_list_after.len()); } // Remove last position { - let last_position_index_before = get_all_positions!(app, dex, "alice").len() - 1; + let last_position_index_before = get_all_positions!(app, dex, alice).len() - 1; - let unauthorized_user = "bob"; + let unauthorized_user = bob; let error = remove_position!( app, dex, @@ -383,18 +394,23 @@ fn test_only_owner_can_modify_position_list() { #[test] fn test_transfer_position_ownership() { + let (mut app, accounts) = MockApp::new(&[ + ("alice", &coins(100_000_000_000, FEE_DENOM)), + ("bob", &coins(100_000_000_000, FEE_DENOM)), + ]); + let alice = &accounts[0]; + let bob = &accounts[1]; let init_tick = -23028; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); + let dex = create_dex!(app, Percentage::new(0), alice); let initial_balance = 10u128.pow(10); - let (token_x, token_y) = create_tokens!(app, initial_balance, initial_balance); + let (token_x, token_y) = create_tokens!(app, initial_balance, initial_balance, alice); let fee_tier = FeeTier::new(Percentage::from_scale(2, 4), 3).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); create_pool!( app, @@ -404,12 +420,12 @@ fn test_transfer_position_ownership() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); - approve!(app, token_x, dex, initial_balance, "alice").unwrap(); - approve!(app, token_y, dex, initial_balance, "alice").unwrap(); + approve!(app, token_x, dex, initial_balance, alice).unwrap(); + approve!(app, token_y, dex, initial_balance, alice).unwrap(); let pool_key = PoolKey::new(token_x.to_string(), token_y.to_string(), fee_tier).unwrap(); let tick_indexes = [-9780, -42, 0, 9, 276, 32343, -50001]; @@ -425,10 +441,10 @@ fn test_transfer_position_ownership() { liquidity_delta, pool_state.sqrt_price, SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); - let list_length = get_all_positions!(app, dex, "alice").len(); + let list_length = get_all_positions!(app, dex, alice).len(); assert_eq!(list_length, 1) } @@ -444,7 +460,7 @@ fn test_transfer_position_ownership() { liquidity_delta, pool_state.sqrt_price, SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); create_position!( @@ -456,7 +472,7 @@ fn test_transfer_position_ownership() { liquidity_delta, pool_state.sqrt_price, SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); create_position!( @@ -468,25 +484,24 @@ fn test_transfer_position_ownership() { liquidity_delta, pool_state.sqrt_price, SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); } // Transfer first position { let transferred_index = 0; - let owner_list_before = get_all_positions!(app, dex, "alice"); - let recipient_list_before = get_all_positions!(app, dex, "bob"); - let removed_position = get_position!(app, dex, transferred_index, "alice").unwrap(); + let owner_list_before = get_all_positions!(app, dex, alice); + let recipient_list_before = get_all_positions!(app, dex, bob); + let removed_position = get_position!(app, dex, transferred_index, alice).unwrap(); let last_position_before = &owner_list_before[owner_list_before.len() - 1]; - transfer_position!(app, dex, transferred_index, "bob", "alice").unwrap(); + transfer_position!(app, dex, transferred_index, bob, alice).unwrap(); - let recipient_position = get_position!(app, dex, transferred_index, "bob").unwrap(); - let owner_list_after = get_all_positions!(app, dex, "alice"); - let recipient_list_after = get_all_positions!(app, dex, "bob"); - let owner_first_position_after = - get_position!(app, dex, transferred_index, "alice").unwrap(); + let recipient_position = get_position!(app, dex, transferred_index, bob).unwrap(); + let owner_list_after = get_all_positions!(app, dex, alice); + let recipient_list_after = get_all_positions!(app, dex, bob); + let owner_first_position_after = get_position!(app, dex, transferred_index, alice).unwrap(); assert_eq!(recipient_list_after.len(), recipient_list_before.len() + 1); assert_eq!(owner_list_before.len() - 1, owner_list_after.len()); @@ -501,16 +516,15 @@ fn test_transfer_position_ownership() { // Transfer middle position { let transferred_index = 1; - let owner_list_before = get_all_positions!(app, dex, "alice"); - let recipient_list_before = get_all_positions!(app, dex, "bob"); + let owner_list_before = get_all_positions!(app, dex, alice); + let recipient_list_before = get_all_positions!(app, dex, bob); let last_position_before = &owner_list_before[owner_list_before.len() - 1]; - transfer_position!(app, dex, transferred_index, "bob", "alice").unwrap(); + transfer_position!(app, dex, transferred_index, bob, alice).unwrap(); - let owner_list_after = get_all_positions!(app, dex, "alice"); - let recipient_list_after = get_all_positions!(app, dex, "bob"); - let owner_first_position_after = - get_position!(app, dex, transferred_index, "alice").unwrap(); + let owner_list_after = get_all_positions!(app, dex, alice); + let recipient_list_after = get_all_positions!(app, dex, bob); + let owner_first_position_after = get_position!(app, dex, transferred_index, alice).unwrap(); assert_eq!(recipient_list_after.len(), recipient_list_before.len() + 1); assert_eq!(owner_list_before.len() - 1, owner_list_after.len()); @@ -520,15 +534,15 @@ fn test_transfer_position_ownership() { } // Transfer last position { - let owner_list_before = get_all_positions!(app, dex, "alice"); + let owner_list_before = get_all_positions!(app, dex, alice); let transferred_index = (owner_list_before.len() - 1) as u32; - let removed_position = get_position!(app, dex, transferred_index, "alice").unwrap(); + let removed_position = get_position!(app, dex, transferred_index, alice).unwrap(); - transfer_position!(app, dex, transferred_index, "bob", "alice").unwrap(); + transfer_position!(app, dex, transferred_index, bob, alice).unwrap(); - let recipient_list_after = get_all_positions!(app, dex, "bob"); + let recipient_list_after = get_all_positions!(app, dex, bob); let recipient_position_index = (recipient_list_after.len() - 1) as u32; - let recipient_position = get_position!(app, dex, recipient_position_index, "bob").unwrap(); + let recipient_position = get_position!(app, dex, recipient_position_index, bob).unwrap(); positions_equals!(removed_position, recipient_position); } @@ -536,15 +550,15 @@ fn test_transfer_position_ownership() { // Clear position { let transferred_index = 0; - let recipient_list_before = get_all_positions!(app, dex, "bob"); - let removed_position = get_position!(app, dex, transferred_index, "alice").unwrap(); + let recipient_list_before = get_all_positions!(app, dex, bob); + let removed_position = get_position!(app, dex, transferred_index, alice).unwrap(); - transfer_position!(app, dex, transferred_index, "bob", "alice").unwrap(); + transfer_position!(app, dex, transferred_index, bob, alice).unwrap(); - let recipient_list_after = get_all_positions!(app, dex, "bob"); + let recipient_list_after = get_all_positions!(app, dex, bob); let recipient_position_index = (recipient_list_after.len() - 1) as u32; - let recipient_position = get_position!(app, dex, recipient_position_index, "bob").unwrap(); - let owner_list_after = get_all_positions!(app, dex, "alice"); + let recipient_position = get_position!(app, dex, recipient_position_index, bob).unwrap(); + let owner_list_after = get_all_positions!(app, dex, alice); assert_eq!(recipient_list_after.len(), recipient_list_before.len() + 1); assert_eq!(0, owner_list_after.len()); @@ -556,19 +570,19 @@ fn test_transfer_position_ownership() { // Get back position { let transferred_index = 0; - let owner_list_before = get_all_positions!(app, dex, "alice"); - let recipient_list_before = get_all_positions!(app, dex, "bob"); - let removed_position = get_position!(app, dex, transferred_index, "bob").unwrap(); + let owner_list_before = get_all_positions!(app, dex, alice); + let recipient_list_before = get_all_positions!(app, dex, bob); + let removed_position = get_position!(app, dex, transferred_index, bob).unwrap(); let last_position_before = &recipient_list_before[recipient_list_before.len() - 1]; - transfer_position!(app, dex, transferred_index, "alice", "bob").unwrap(); + transfer_position!(app, dex, transferred_index, alice, bob).unwrap(); - let owner_list_after = get_all_positions!(app, dex, "alice"); - let recipient_list_after = get_all_positions!(app, dex, "bob"); + let owner_list_after = get_all_positions!(app, dex, alice); + let recipient_list_after = get_all_positions!(app, dex, bob); let recipient_first_position_after = - get_position!(app, dex, transferred_index, "bob").unwrap(); + get_position!(app, dex, transferred_index, bob).unwrap(); - let owner_new_position = get_position!(app, dex, transferred_index, "alice").unwrap(); + let owner_new_position = get_position!(app, dex, transferred_index, alice).unwrap(); assert_eq!(recipient_list_after.len(), recipient_list_before.len() - 1); assert_eq!(owner_list_before.len() + 1, owner_list_after.len()); @@ -583,18 +597,24 @@ fn test_transfer_position_ownership() { #[test] fn test_only_owner_can_transfer_position() { + let (mut app, accounts) = MockApp::new(&[ + ("alice", &coins(100_000_000_000, FEE_DENOM)), + ("bob", &coins(100_000_000_000, FEE_DENOM)), + ]); + let alice = &accounts[0]; + let bob = &accounts[1]; + let init_tick = -23028; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); + let dex = create_dex!(app, Percentage::new(0), alice); let initial_balance = 10u128.pow(10); - let (token_x, token_y) = create_tokens!(app, initial_balance, initial_balance); + let (token_x, token_y) = create_tokens!(app, initial_balance, initial_balance, alice); let fee_tier = FeeTier::new(Percentage::from_scale(2, 4), 3).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); create_pool!( app, @@ -604,12 +624,12 @@ fn test_only_owner_can_transfer_position() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); - approve!(app, token_x, dex, initial_balance, "alice").unwrap(); - approve!(app, token_y, dex, initial_balance, "alice").unwrap(); + approve!(app, token_x, dex, initial_balance, alice).unwrap(); + approve!(app, token_y, dex, initial_balance, alice).unwrap(); let pool_key = PoolKey::new(token_x.to_string(), token_y.to_string(), fee_tier).unwrap(); let tick_indexes = [-9780, -42, 0, 9, 276, 32343, -50001]; @@ -625,10 +645,10 @@ fn test_only_owner_can_transfer_position() { liquidity_delta, pool_state.sqrt_price, SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); - let list_length = get_all_positions!(app, dex, "alice").len(); + let list_length = get_all_positions!(app, dex, alice).len(); assert_eq!(list_length, 1) } @@ -644,7 +664,7 @@ fn test_only_owner_can_transfer_position() { liquidity_delta, pool_state.sqrt_price, SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); create_position!( @@ -656,7 +676,7 @@ fn test_only_owner_can_transfer_position() { liquidity_delta, pool_state.sqrt_price, SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); create_position!( @@ -668,7 +688,7 @@ fn test_only_owner_can_transfer_position() { liquidity_delta, pool_state.sqrt_price, SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); } @@ -676,24 +696,27 @@ fn test_only_owner_can_transfer_position() { { let transferred_index = 0; - let error = transfer_position!(app, dex, transferred_index, "alice", "bob").unwrap_err(); + let error = transfer_position!(app, dex, transferred_index, alice, bob).unwrap_err(); assert!(error.root_cause().to_string().contains("not found")); } } #[test] fn test_multiple_positions_on_same_tick() { + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; + let init_tick = 0; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); + + let dex = create_dex!(app, Percentage::new(0), alice); let initial_balance = 100_000_000; - let (token_x, token_y) = create_tokens!(app, initial_balance, initial_balance); + let (token_x, token_y) = create_tokens!(app, initial_balance, initial_balance, alice); let fee_tier = FeeTier::new(Percentage::from_scale(2, 4), 10).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); create_pool!( app, @@ -703,12 +726,12 @@ fn test_multiple_positions_on_same_tick() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); - approve!(app, token_x, dex, initial_balance, "alice").unwrap(); - approve!(app, token_y, dex, initial_balance, "alice").unwrap(); + approve!(app, token_x, dex, initial_balance, alice).unwrap(); + approve!(app, token_y, dex, initial_balance, alice).unwrap(); let pool_key = PoolKey::new(token_x.to_string(), token_y.to_string(), fee_tier).unwrap(); // Three position on same lower and upper tick @@ -729,11 +752,11 @@ fn test_multiple_positions_on_same_tick() { liquidity_delta, pool_state.sqrt_price, SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); - let first_position = get_position!(app, dex, 0, "alice").unwrap(); + let first_position = get_position!(app, dex, 0, alice).unwrap(); create_position!( app, @@ -744,11 +767,11 @@ fn test_multiple_positions_on_same_tick() { liquidity_delta, pool_state.sqrt_price, SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); - let second_position = get_position!(app, dex, 1, "alice").unwrap(); + let second_position = get_position!(app, dex, 1, alice).unwrap(); create_position!( app, @@ -759,11 +782,11 @@ fn test_multiple_positions_on_same_tick() { liquidity_delta, pool_state.sqrt_price, SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); - let third_position = get_position!(app, dex, 2, "alice").unwrap(); + let third_position = get_position!(app, dex, 2, alice).unwrap(); assert!(first_position.lower_tick_index == second_position.lower_tick_index); assert!(first_position.upper_tick_index == second_position.upper_tick_index); @@ -833,11 +856,11 @@ fn test_multiple_positions_on_same_tick() { liquidity_delta, pool_state.sqrt_price, SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); - let first_position = get_position!(app, dex, 3, "alice").unwrap(); + let first_position = get_position!(app, dex, 3, alice).unwrap(); // Check first position assert!(first_position.pool_key == pool_key); @@ -859,11 +882,11 @@ fn test_multiple_positions_on_same_tick() { liquidity_delta, pool_state.sqrt_price, SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); - let second_position = get_position!(app, dex, 4, "alice").unwrap(); + let second_position = get_position!(app, dex, 4, alice).unwrap(); // Check second position assert!(second_position.pool_key == pool_key); @@ -884,11 +907,11 @@ fn test_multiple_positions_on_same_tick() { liquidity_delta, pool_state.sqrt_price, SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); - let third_position = get_position!(app, dex, 5, "alice").unwrap(); + let third_position = get_position!(app, dex, 5, alice).unwrap(); // Check third position assert!(third_position.pool_key == pool_key); diff --git a/contracts/oraiswap-v3/src/tests/position_slippage.rs b/contracts/oraiswap-v3/src/tests/position_slippage.rs index c7053ea..fc112f2 100644 --- a/contracts/oraiswap-v3/src/tests/position_slippage.rs +++ b/contracts/oraiswap-v3/src/tests/position_slippage.rs @@ -1,20 +1,22 @@ +use cosmwasm_std::coins; use decimal::*; use crate::{ liquidity::Liquidity, percentage::Percentage, sqrt_price::{calculate_sqrt_price, SqrtPrice}, - tests::helper::{macros::*, MockApp}, + tests::helper::{macros::*, MockApp, FEE_DENOM}, ContractError, FeeTier, PoolKey, }; #[test] fn test_position_slippage_zero_slippage_and_inside_range() { - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::from_scale(1, 2)); + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; + let dex = create_dex!(app, Percentage::from_scale(1, 2), alice); - let (token_x, token_y) = create_tokens!(app, 10u128.pow(23)); - let pool_key = init_slippage_pool_with_liquidity!(app, dex, token_x, token_y); + let (token_x, token_y) = create_tokens!(app, 10u128.pow(23), alice); + let pool_key = init_slippage_pool_with_liquidity!(app, dex, token_x, token_y, alice); let pool = get_pool!(app, dex, token_x, token_y, pool_key.fee_tier).unwrap(); @@ -32,7 +34,7 @@ fn test_position_slippage_zero_slippage_and_inside_range() { liquidity_delta, known_price, known_price, - "alice" + alice ) .unwrap(); } @@ -53,7 +55,7 @@ fn test_position_slippage_zero_slippage_and_inside_range() { liquidity_delta, limit_lower, limit_upper, - "alice" + alice ) .unwrap(); } @@ -61,10 +63,11 @@ fn test_position_slippage_zero_slippage_and_inside_range() { #[test] fn test_position_slippage_below_range() { - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::from_scale(1, 2)); - let (token_x, token_y) = create_tokens!(app, 10u128.pow(23)); - let pool_key = init_slippage_pool_with_liquidity!(app, dex, token_x, token_y); + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; + let dex = create_dex!(app, Percentage::from_scale(1, 2), alice); + let (token_x, token_y) = create_tokens!(app, 10u128.pow(23), alice); + let pool_key = init_slippage_pool_with_liquidity!(app, dex, token_x, token_y, alice); get_pool!(app, dex, token_x, token_y, pool_key.fee_tier).unwrap(); @@ -81,21 +84,23 @@ fn test_position_slippage_below_range() { liquidity_delta, limit_lower, limit_upper, - "alice" + alice ) .unwrap_err(); - assert_eq!( - error.root_cause().to_string(), - ContractError::PriceLimitReached {}.to_string() - ); + assert!(error + .root_cause() + .to_string() + .contains(&ContractError::PriceLimitReached {}.to_string())); } #[test] fn test_position_slippage_above_range() { - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::from_scale(1, 2)); - let (token_x, token_y) = create_tokens!(app, 10u128.pow(23)); - let pool_key = init_slippage_pool_with_liquidity!(app, dex, token_x, token_y); + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; + + let dex = create_dex!(app, Percentage::from_scale(1, 2), alice); + let (token_x, token_y) = create_tokens!(app, 10u128.pow(23), alice); + let pool_key = init_slippage_pool_with_liquidity!(app, dex, token_x, token_y, alice); get_pool!(app, dex, token_x, token_y, pool_key.fee_tier).unwrap(); @@ -112,11 +117,11 @@ fn test_position_slippage_above_range() { liquidity_delta, limit_lower, limit_upper, - "alice" + alice ) .unwrap_err(); - assert_eq!( - error.root_cause().to_string(), - ContractError::PriceLimitReached {}.to_string() - ); + assert!(error + .root_cause() + .to_string() + .contains(&ContractError::PriceLimitReached {}.to_string())); } diff --git a/contracts/oraiswap-v3/src/tests/protocol_fee.rs b/contracts/oraiswap-v3/src/tests/protocol_fee.rs index ea443db..2c978cb 100644 --- a/contracts/oraiswap-v3/src/tests/protocol_fee.rs +++ b/contracts/oraiswap-v3/src/tests/protocol_fee.rs @@ -1,28 +1,34 @@ +use cosmwasm_std::coins; use decimal::*; use crate::{ percentage::Percentage, - tests::helper::{macros::*, MockApp}, + tests::helper::{macros::*, MockApp, FEE_DENOM}, token_amount::TokenAmount, ContractError, FeeTier, PoolKey, }; #[test] fn test_protocol_fee() { - let mut app = MockApp::new(&[("alice", &[])]); - - let (dex, token_x, token_y) = init_dex_and_tokens!(app); - init_basic_pool!(app, dex, token_x, token_y); - init_basic_position!(app, dex, token_x, token_y); - init_basic_swap!(app, dex, token_x, token_y); + let (mut app, accounts) = MockApp::new(&[ + ("alice", &coins(100_000_000_000, FEE_DENOM)), + ("bob", &coins(100_000_000_000, FEE_DENOM)), + ]); + let alice = &accounts[0]; + let bob = &accounts[1]; + + let (dex, token_x, token_y) = init_dex_and_tokens!(app, alice); + init_basic_pool!(app, dex, token_x, token_y, alice); + init_basic_position!(app, dex, token_x, token_y, alice); + init_basic_swap!(app, dex, token_x, token_y, alice, bob); let fee_tier = FeeTier::new(Percentage::from_scale(6, 3), 10).unwrap(); let pool_key = PoolKey::new(token_x.to_string(), token_y.to_string(), fee_tier).unwrap(); - withdraw_protocol_fee!(app, dex, pool_key, "alice").unwrap(); + withdraw_protocol_fee!(app, dex, pool_key, alice).unwrap(); - let amount_x = balance_of!(app, token_x, "alice"); - let amount_y = balance_of!(app, token_y, "alice"); + let amount_x = balance_of!(app, token_x, alice); + let amount_y = balance_of!(app, token_y, alice); assert_eq!(amount_x, 9999999501); assert_eq!(amount_y, 9999999000); @@ -44,11 +50,16 @@ fn test_protocol_fee() { #[test] fn test_protocol_fee_not_admin() { - let mut app = MockApp::new(&[("alice", &[])]); - let (dex, token_x, token_y) = init_dex_and_tokens!(app); - init_basic_pool!(app, dex, token_x, token_y); - init_basic_position!(app, dex, token_x, token_y); - init_basic_swap!(app, dex, token_x, token_y); + let (mut app, accounts) = MockApp::new(&[ + ("alice", &coins(100_000_000_000, FEE_DENOM)), + ("bob", &coins(100_000_000_000, FEE_DENOM)), + ]); + let alice = &accounts[0]; + let bob = &accounts[1]; + let (dex, token_x, token_y) = init_dex_and_tokens!(app, alice); + init_basic_pool!(app, dex, token_x, token_y, alice); + init_basic_position!(app, dex, token_x, token_y, alice); + init_basic_swap!(app, dex, token_x, token_y, alice, bob); let pool_key = PoolKey::new( token_x.to_string(), @@ -60,31 +71,36 @@ fn test_protocol_fee_not_admin() { ) .unwrap(); - let error = withdraw_protocol_fee!(app, dex, pool_key, "bob").unwrap_err(); - assert_eq!( - error.root_cause().to_string(), - ContractError::Unauthorized {}.to_string() - ); + let error = withdraw_protocol_fee!(app, dex, pool_key, bob).unwrap_err(); + assert!(error + .root_cause() + .to_string() + .contains(&ContractError::Unauthorized {}.to_string())); } #[test] fn test_withdraw_fee_not_deployer() { - let mut app = MockApp::new(&[("alice", &[])]); - let (dex, token_x, token_y) = init_dex_and_tokens!(app); - init_basic_pool!(app, dex, token_x, token_y); - init_basic_position!(app, dex, token_x, token_y); - init_basic_swap!(app, dex, token_x, token_y); - - let user_address = "bob"; + let (mut app, accounts) = MockApp::new(&[ + ("alice", &coins(100_000_000_000, FEE_DENOM)), + ("bob", &coins(100_000_000_000, FEE_DENOM)), + ]); + let alice = &accounts[0]; + let bob = &accounts[1]; + let (dex, token_x, token_y) = init_dex_and_tokens!(app, alice); + init_basic_pool!(app, dex, token_x, token_y, alice); + init_basic_position!(app, dex, token_x, token_y, alice); + init_basic_swap!(app, dex, token_x, token_y, alice, bob); + + let user_address = bob; let fee_tier = FeeTier::new(Percentage::from_scale(6, 3), 10).unwrap(); let pool_key = PoolKey::new(token_x.to_string(), token_y.to_string(), fee_tier).unwrap(); - change_fee_receiver!(app, dex, pool_key, user_address, "alice").unwrap(); + change_fee_receiver!(app, dex, pool_key, user_address, alice).unwrap(); let pool = get_pool!(app, dex, token_x, token_y, fee_tier).unwrap(); - assert_eq!(pool.fee_receiver, user_address); + assert_eq!(pool.fee_receiver.as_str(), user_address); - withdraw_protocol_fee!(app, dex, pool_key, "bob").unwrap(); + withdraw_protocol_fee!(app, dex, pool_key, bob).unwrap(); let amount_x = balance_of!(app, token_x, user_address); let amount_y = balance_of!(app, token_y, user_address); diff --git a/contracts/oraiswap-v3/src/tests/remove_fee_tier.rs b/contracts/oraiswap-v3/src/tests/remove_fee_tier.rs index a7c622c..876ded7 100644 --- a/contracts/oraiswap-v3/src/tests/remove_fee_tier.rs +++ b/contracts/oraiswap-v3/src/tests/remove_fee_tier.rs @@ -1,23 +1,26 @@ +use cosmwasm_std::coins; use decimal::*; use crate::{ percentage::Percentage, - tests::helper::{macros::*, MockApp}, + tests::helper::{macros::*, MockApp, FEE_DENOM}, ContractError, FeeTier, }; #[test] fn test_remove_fee_tier() { - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; + + let dex = create_dex!(app, Percentage::new(0), alice); let fee_tier = FeeTier::new(Percentage::from_scale(2, 4), 1).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let fee_tier = FeeTier::new(Percentage::from_scale(2, 4), 2).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); - remove_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + remove_fee_tier!(app, dex, fee_tier, alice).unwrap(); let exist = fee_tier_exist!( app, dex, @@ -28,35 +31,42 @@ fn test_remove_fee_tier() { #[test] fn test_remove_not_existing_fee_tier() { - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; + + let dex = create_dex!(app, Percentage::new(0), alice); let fee_tier = FeeTier::new(Percentage::from_scale(2, 4), 1).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let fee_tier = FeeTier::new(Percentage::from_scale(2, 4), 2).unwrap(); - let error = remove_fee_tier!(app, dex, fee_tier, "alice").unwrap_err(); + let error = remove_fee_tier!(app, dex, fee_tier, alice).unwrap_err(); - assert_eq!( - error.root_cause().to_string(), - ContractError::FeeTierNotFound {}.to_string() - ); + assert!(error + .root_cause() + .to_string() + .contains(&ContractError::FeeTierNotFound {}.to_string())); } #[test] fn test_remove_fee_tier_not_admin() { - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::new(0)); + let (mut app, accounts) = MockApp::new(&[ + ("alice", &coins(100_000_000_000, FEE_DENOM)), + ("bob", &coins(100_000_000_000, FEE_DENOM)), + ]); + let alice = &accounts[0]; + let bob = &accounts[1]; + let dex = create_dex!(app, Percentage::new(0), alice); let fee_tier = FeeTier::new(Percentage::from_scale(2, 4), 1).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let fee_tier = FeeTier::new(Percentage::from_scale(2, 4), 2).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); - let error = remove_fee_tier!(app, dex, fee_tier, "bob").unwrap_err(); - assert_eq!( - error.root_cause().to_string(), - ContractError::Unauthorized {}.to_string() - ); + let error = remove_fee_tier!(app, dex, fee_tier, bob).unwrap_err(); + assert!(error + .root_cause() + .to_string() + .contains(&ContractError::Unauthorized {}.to_string())); } diff --git a/contracts/oraiswap-v3/src/tests/slippage.rs b/contracts/oraiswap-v3/src/tests/slippage.rs index 22eb60b..56e94ae 100644 --- a/contracts/oraiswap-v3/src/tests/slippage.rs +++ b/contracts/oraiswap-v3/src/tests/slippage.rs @@ -1,26 +1,29 @@ +use cosmwasm_std::coins; use decimal::*; use crate::{ liquidity::Liquidity, percentage::Percentage, sqrt_price::{calculate_sqrt_price, SqrtPrice}, - tests::helper::{macros::*, MockApp}, + tests::helper::{macros::*, MockApp, FEE_DENOM}, token_amount::TokenAmount, ContractError, FeeTier, PoolKey, MAX_SQRT_PRICE, }; #[test] fn test_basic_slippage() { - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::from_scale(1, 2)); + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; + + let dex = create_dex!(app, Percentage::from_scale(1, 2), alice); let mint_amount = 10u128.pow(23); - let (token_x, token_y) = create_tokens!(app, mint_amount, mint_amount); + let (token_x, token_y) = create_tokens!(app, mint_amount, mint_amount, alice); - let pool_key = init_slippage_pool_with_liquidity!(app, dex, token_x, token_y); + let pool_key = init_slippage_pool_with_liquidity!(app, dex, token_x, token_y, alice); let amount = 10u128.pow(8); let swap_amount = TokenAmount::new(amount); - approve!(app, token_x, dex, amount, "alice").unwrap(); + approve!(app, token_x, dex, amount, alice).unwrap(); let target_sqrt_price = SqrtPrice::new(1009940000000000000000001); swap!( @@ -31,7 +34,7 @@ fn test_basic_slippage() { swap_amount, true, target_sqrt_price, - "alice" + alice ) .unwrap(); let expected_sqrt_price = SqrtPrice::new(1009940000000000000000000); @@ -42,14 +45,15 @@ fn test_basic_slippage() { #[test] fn test_swap_close_to_limit() { - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::from_scale(1, 2)); + let (mut app, accounts) = MockApp::new(&[("alice", &coins(100_000_000_000, FEE_DENOM))]); + let alice = &accounts[0]; + let dex = create_dex!(app, Percentage::from_scale(1, 2), alice); let mint_amount = 10u128.pow(23); - let (token_x, token_y) = create_tokens!(app, mint_amount, mint_amount); - let pool_key = init_slippage_pool_with_liquidity!(app, dex, token_x, token_y); + let (token_x, token_y) = create_tokens!(app, mint_amount, mint_amount, alice); + let pool_key = init_slippage_pool_with_liquidity!(app, dex, token_x, token_y, alice); let amount = 10u128.pow(8); let swap_amount = TokenAmount::new(amount); - approve!(app, token_x, dex, amount, "alice").unwrap(); + approve!(app, token_x, dex, amount, alice).unwrap(); let target_sqrt_price = SqrtPrice::new(MAX_SQRT_PRICE); let quoted_target_sqrt_price = quote!( @@ -74,24 +78,29 @@ fn test_swap_close_to_limit() { swap_amount, true, target_sqrt_price, - "alice" + alice ) .unwrap_err(); - assert_eq!( - error.root_cause().to_string(), - ContractError::PriceLimitReached {}.to_string() - ); + assert!(error + .root_cause() + .to_string() + .contains(&ContractError::PriceLimitReached {}.to_string())); } #[test] fn test_swap_exact_limit() { - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, Percentage::from_scale(1, 2)); + let (mut app, accounts) = MockApp::new(&[ + ("alice", &coins(100_000_000_000, FEE_DENOM)), + ("bob", &coins(100_000_000_000, FEE_DENOM)), + ]); + let alice = &accounts[0]; + let bob = &accounts[1]; + let dex = create_dex!(app, Percentage::from_scale(1, 2), alice); let initial_amount = 10u128.pow(10); - let (token_x, token_y) = create_tokens!(app, initial_amount, initial_amount); - init_basic_pool!(app, dex, token_x, token_y); - init_basic_position!(app, dex, token_x, token_y); + let (token_x, token_y) = create_tokens!(app, initial_amount, initial_amount, alice); + init_basic_pool!(app, dex, token_x, token_y, alice); + init_basic_position!(app, dex, token_x, token_y, alice); let fee_tier = FeeTier::new(Percentage::from_scale(6, 3), 10).unwrap(); @@ -99,11 +108,11 @@ fn test_swap_exact_limit() { let amount = 1000; - mint!(app, token_x, "bob", amount, "alice").unwrap(); - let amount_x = balance_of!(app, token_x, "bob"); + mint!(app, token_x, bob, amount, alice).unwrap(); + let amount_x = balance_of!(app, token_x, bob); assert_eq!(amount_x, amount); - approve!(app, token_x, dex, amount, "bob").unwrap(); + approve!(app, token_x, dex, amount, bob).unwrap(); let swap_amount = TokenAmount::new(amount); - swap_exact_limit!(app, dex, pool_key, true, swap_amount, true, "bob"); + swap_exact_limit!(app, dex, pool_key, true, swap_amount, true, bob); } diff --git a/contracts/oraiswap-v3/src/tests/swap.rs b/contracts/oraiswap-v3/src/tests/swap.rs index 3edfe52..ac64f13 100644 --- a/contracts/oraiswap-v3/src/tests/swap.rs +++ b/contracts/oraiswap-v3/src/tests/swap.rs @@ -1,3 +1,4 @@ +use cosmwasm_std::coins; use decimal::{Decimal, Factories}; use crate::{ @@ -5,7 +6,7 @@ use crate::{ liquidity::Liquidity, percentage::Percentage, sqrt_price::{calculate_sqrt_price, SqrtPrice}, - tests::helper::macros::*, + tests::helper::{macros::*, FEE_DENOM}, token_amount::TokenAmount, ContractError, FeeTier, PoolKey, MAX_SQRT_PRICE, MIN_SQRT_PRICE, }; @@ -14,17 +15,23 @@ use super::helper::MockApp; #[test] fn test_swap_x_to_y() { + let (mut app, accounts) = MockApp::new(&[ + ("alice", &coins(100_000_000_000, FEE_DENOM)), + ("bob", &coins(100_000_000_000, FEE_DENOM)), + ]); + let alice = &accounts[0]; + let bob = &accounts[1]; let protocol_fee = Percentage::from_scale(6, 3); - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, protocol_fee); + + let dex = create_dex!(app, protocol_fee, alice); let fee_tier = FeeTier::new(protocol_fee, 10).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let init_tick = 0; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); let initial_amount = 10u128.pow(10); - let (token_x, token_y) = create_tokens!(app, initial_amount, initial_amount); + let (token_x, token_y) = create_tokens!(app, initial_amount, initial_amount, alice); create_pool!( app, @@ -34,12 +41,12 @@ fn test_swap_x_to_y() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); - approve!(app, token_x, dex, initial_amount, "alice").unwrap(); - approve!(app, token_y, dex, initial_amount, "alice").unwrap(); + approve!(app, token_x, dex, initial_amount, alice).unwrap(); + approve!(app, token_y, dex, initial_amount, alice).unwrap(); let pool_key = PoolKey::new(token_x.to_string(), token_y.to_string(), fee_tier).unwrap(); @@ -58,7 +65,7 @@ fn test_swap_x_to_y() { liquidity_delta, SqrtPrice::new(0), SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); @@ -71,7 +78,7 @@ fn test_swap_x_to_y() { liquidity_delta, SqrtPrice::new(0), SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); @@ -81,8 +88,8 @@ fn test_swap_x_to_y() { let amount = 1000; let swap_amount = TokenAmount(amount); - mint!(app, token_x, "bob", amount, "alice").unwrap(); - approve!(app, token_x, dex, amount, "bob").unwrap(); + mint!(app, token_x, bob, amount, alice).unwrap(); + approve!(app, token_x, dex, amount, bob).unwrap(); let slippage = SqrtPrice::new(MIN_SQRT_PRICE); let target_sqrt_price = quote!(app, dex, pool_key, true, swap_amount, true, slippage) @@ -100,7 +107,7 @@ fn test_swap_x_to_y() { swap_amount, true, target_sqrt_price, - "bob" + bob ) .unwrap(); @@ -112,8 +119,8 @@ fn test_swap_x_to_y() { let lower_tick_bit = is_tick_initialized!(app, dex, pool_key, lower_tick_index); let middle_tick_bit = is_tick_initialized!(app, dex, pool_key, middle_tick_index); let upper_tick_bit = is_tick_initialized!(app, dex, pool_key, upper_tick_index); - let bob_x = balance_of!(app, token_x, "bob"); - let bob_y = balance_of!(app, token_y, "bob"); + let bob_x = balance_of!(app, token_x, bob); + let bob_y = balance_of!(app, token_y, bob); let dex_x = balance_of!(app, token_x, dex); let dex_y = balance_of!(app, token_y, dex); let delta_dex_y = before_dex_y - dex_y; @@ -153,14 +160,20 @@ fn test_swap_x_to_y() { #[test] fn test_swap_y_to_x() { + let (mut app, accounts) = MockApp::new(&[ + ("alice", &coins(100_000_000_000, FEE_DENOM)), + ("bob", &coins(100_000_000_000, FEE_DENOM)), + ]); + let alice = &accounts[0]; + let bob = &accounts[1]; let protocol_fee = Percentage::from_scale(6, 3); - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, protocol_fee); + + let dex = create_dex!(app, protocol_fee, alice); let initial_amount = 10u128.pow(10); - let (token_x, token_y) = create_tokens!(app, initial_amount, initial_amount); + let (token_x, token_y) = create_tokens!(app, initial_amount, initial_amount, alice); let fee_tier = FeeTier::new(protocol_fee, 10).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let init_tick = 0; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); @@ -173,12 +186,12 @@ fn test_swap_y_to_x() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); - approve!(app, token_x, dex, initial_amount, "alice").unwrap(); - approve!(app, token_y, dex, initial_amount, "alice").unwrap(); + approve!(app, token_x, dex, initial_amount, alice).unwrap(); + approve!(app, token_y, dex, initial_amount, alice).unwrap(); let pool_key = PoolKey::new(token_x.to_string(), token_y.to_string(), fee_tier).unwrap(); @@ -197,7 +210,7 @@ fn test_swap_y_to_x() { liquidity_delta, SqrtPrice::new(0), SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); @@ -210,7 +223,7 @@ fn test_swap_y_to_x() { liquidity_delta, SqrtPrice::new(0), SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); @@ -220,8 +233,8 @@ fn test_swap_y_to_x() { let amount = 1000; let swap_amount = TokenAmount(amount); - mint!(app, token_y, "bob", amount, "alice").unwrap(); - approve!(app, token_y, dex, amount, "bob").unwrap(); + mint!(app, token_y, bob, amount, alice).unwrap(); + approve!(app, token_y, dex, amount, bob).unwrap(); let target_sqrt_price = SqrtPrice::new(MAX_SQRT_PRICE); @@ -248,7 +261,7 @@ fn test_swap_y_to_x() { swap_amount, true, target_sqrt_price, - "bob" + bob ) .unwrap(); @@ -260,8 +273,8 @@ fn test_swap_y_to_x() { let lower_tick_bit = is_tick_initialized!(app, dex, pool_key, lower_tick_index); let middle_tick_bit = is_tick_initialized!(app, dex, pool_key, middle_tick_index); let upper_tick_bit = is_tick_initialized!(app, dex, pool_key, upper_tick_index); - let bob_x = balance_of!(app, token_x, "bob"); - let bob_y = balance_of!(app, token_y, "bob"); + let bob_x = balance_of!(app, token_x, bob); + let bob_y = balance_of!(app, token_y, bob); let dex_x = balance_of!(app, token_x, dex); let dex_y = balance_of!(app, token_y, dex); let delta_dex_x = before_dex_x - dex_x; @@ -301,16 +314,22 @@ fn test_swap_y_to_x() { #[test] fn test_swap_not_enough_liquidity_token_x() { + let (mut app, accounts) = MockApp::new(&[ + ("alice", &coins(100_000_000_000, FEE_DENOM)), + ("bob", &coins(100_000_000_000, FEE_DENOM)), + ]); + let alice = &accounts[0]; + let bob = &accounts[1]; let protocol_fee: Percentage = Percentage::from_scale(6, 3); - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, protocol_fee); + + let dex = create_dex!(app, protocol_fee, alice); let initial_amount = 10u128.pow(10); - let (token_x, token_y) = create_tokens!(app, initial_amount, initial_amount); + let (token_x, token_y) = create_tokens!(app, initial_amount, initial_amount, alice); let fee_tier = FeeTier::new(protocol_fee, 10).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let init_tick = 0; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); @@ -322,12 +341,12 @@ fn test_swap_not_enough_liquidity_token_x() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); - approve!(app, token_x, dex, initial_amount, "alice").unwrap(); - approve!(app, token_y, dex, initial_amount, "alice").unwrap(); + approve!(app, token_x, dex, initial_amount, alice).unwrap(); + approve!(app, token_y, dex, initial_amount, alice).unwrap(); let pool_key = PoolKey::new(token_x.to_string(), token_y.to_string(), fee_tier).unwrap(); @@ -346,7 +365,7 @@ fn test_swap_not_enough_liquidity_token_x() { liquidity_delta, SqrtPrice::new(0), SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); @@ -359,7 +378,7 @@ fn test_swap_not_enough_liquidity_token_x() { liquidity_delta, SqrtPrice::new(0), SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); @@ -368,8 +387,8 @@ fn test_swap_not_enough_liquidity_token_x() { let amount = 1000; let swap_amount = TokenAmount(amount); - mint!(app, token_x, "bob", amount, "alice").unwrap(); - approve!(app, token_x, dex, amount, "bob").unwrap(); + mint!(app, token_x, bob, amount, alice).unwrap(); + approve!(app, token_x, dex, amount, bob).unwrap(); let target_sqrt_price = SqrtPrice::new(MIN_SQRT_PRICE); @@ -381,27 +400,33 @@ fn test_swap_not_enough_liquidity_token_x() { swap_amount, true, target_sqrt_price, - "bob" + bob ) .unwrap_err(); - assert_eq!( - error.root_cause().to_string(), - ContractError::TickLimitReached {}.to_string() - ); + assert!(error + .root_cause() + .to_string() + .contains(&ContractError::TickLimitReached {}.to_string())); } #[test] fn test_swap_not_enough_liquidity_token_y() { + let (mut app, accounts) = MockApp::new(&[ + ("alice", &coins(100_000_000_000, FEE_DENOM)), + ("bob", &coins(100_000_000_000, FEE_DENOM)), + ]); + let alice = &accounts[0]; + let bob = &accounts[1]; let protocol_fee = Percentage::from_scale(6, 3); - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, protocol_fee); + + let dex = create_dex!(app, protocol_fee, alice); let initial_amount = 10u128.pow(10); - let (token_x, token_y) = create_tokens!(app, initial_amount, initial_amount); + let (token_x, token_y) = create_tokens!(app, initial_amount, initial_amount, alice); let fee_tier = FeeTier::new(protocol_fee, 10).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let init_tick = 0; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); @@ -413,12 +438,12 @@ fn test_swap_not_enough_liquidity_token_y() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); - approve!(app, token_x, dex, initial_amount, "alice").unwrap(); - approve!(app, token_y, dex, initial_amount, "alice").unwrap(); + approve!(app, token_x, dex, initial_amount, alice).unwrap(); + approve!(app, token_y, dex, initial_amount, alice).unwrap(); let pool_key = PoolKey::new(token_x.to_string(), token_y.to_string(), fee_tier).unwrap(); @@ -437,7 +462,7 @@ fn test_swap_not_enough_liquidity_token_y() { liquidity_delta, SqrtPrice::new(0), SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); @@ -450,7 +475,7 @@ fn test_swap_not_enough_liquidity_token_y() { liquidity_delta, SqrtPrice::new(0), SqrtPrice::max_instance(), - "alice" + alice ) .unwrap(); @@ -460,24 +485,14 @@ fn test_swap_not_enough_liquidity_token_y() { let amount = 1000; let swap_amount = TokenAmount(amount); - mint!(app, token_y, "bob", amount, "alice").unwrap(); - approve!(app, token_y, dex, amount, "bob").unwrap(); + mint!(app, token_y, bob, amount, alice).unwrap(); + approve!(app, token_y, dex, amount, bob).unwrap(); let slippage = SqrtPrice::new(MAX_SQRT_PRICE); - let error = swap!( - app, - dex, - pool_key, - false, - swap_amount, - true, - slippage, - "bob" - ) - .unwrap_err(); - assert_eq!( - error.root_cause().to_string(), - ContractError::TickLimitReached {}.to_string() - ); + let error = swap!(app, dex, pool_key, false, swap_amount, true, slippage, bob).unwrap_err(); + assert!(error + .root_cause() + .to_string() + .contains(&ContractError::TickLimitReached {}.to_string())); } diff --git a/contracts/oraiswap-v3/src/tests/swap_route.rs b/contracts/oraiswap-v3/src/tests/swap_route.rs index 7cacab0..0000b40 100644 --- a/contracts/oraiswap-v3/src/tests/swap_route.rs +++ b/contracts/oraiswap-v3/src/tests/swap_route.rs @@ -1,3 +1,4 @@ +use cosmwasm_std::coins; use decimal::*; use crate::{ @@ -5,33 +6,39 @@ use crate::{ liquidity::Liquidity, percentage::Percentage, sqrt_price::calculate_sqrt_price, - tests::helper::{macros::*, MockApp}, + tests::helper::{macros::*, MockApp, FEE_DENOM}, token_amount::TokenAmount, FeeTier, PoolKey, }; #[test] fn swap_route() { + let (mut app, accounts) = MockApp::new(&[ + ("alice", &coins(100_000_000_000, FEE_DENOM)), + ("bob", &coins(100_000_000_000, FEE_DENOM)), + ]); + let alice = &accounts[0]; + let bob = &accounts[1]; let protocol_fee = Percentage::from_scale(6, 3); let initial_amount = 10u128.pow(10); - let mut app = MockApp::new(&[]); - let dex = create_dex!(app, protocol_fee); + + let dex = create_dex!(app, protocol_fee, alice); let (token_x, token_y, token_z) = - create_3_tokens!(app, initial_amount, initial_amount, initial_amount); + create_3_tokens!(app, initial_amount, initial_amount, initial_amount, alice); - approve!(app, token_x, dex, initial_amount, "alice").unwrap(); - approve!(app, token_y, dex, initial_amount, "alice").unwrap(); - approve!(app, token_z, dex, initial_amount, "alice").unwrap(); + approve!(app, token_x, dex, initial_amount, alice).unwrap(); + approve!(app, token_y, dex, initial_amount, alice).unwrap(); + approve!(app, token_z, dex, initial_amount, alice).unwrap(); let amount = 1000; - mint!(app, token_x, "bob", amount, "alice").unwrap(); - approve!(app, token_x, dex, amount, "bob").unwrap(); - approve!(app, token_y, dex, initial_amount, "bob").unwrap(); + mint!(app, token_x, bob, amount, alice).unwrap(); + approve!(app, token_x, dex, amount, bob).unwrap(); + approve!(app, token_y, dex, initial_amount, bob).unwrap(); let fee_tier = FeeTier::new(protocol_fee, 1).unwrap(); - add_fee_tier!(app, dex, fee_tier, "alice").unwrap(); + add_fee_tier!(app, dex, fee_tier, alice).unwrap(); let init_tick = 0; let init_sqrt_price = calculate_sqrt_price(init_tick).unwrap(); @@ -43,7 +50,7 @@ fn swap_route() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); @@ -55,7 +62,7 @@ fn swap_route() { fee_tier, init_sqrt_price, init_tick, - "alice" + alice ) .unwrap(); @@ -76,7 +83,7 @@ fn swap_route() { liquidity_delta, slippage_limit_lower, slippage_limit_upper, - "alice" + alice ) .unwrap(); @@ -92,7 +99,7 @@ fn swap_route() { liquidity_delta, slippage_limit_lower, slippage_limit_upper, - "alice" + alice ) .unwrap(); @@ -118,13 +125,13 @@ fn swap_route() { expected_token_amount, slippage, swaps.clone(), - "bob" + bob ) .unwrap(); - let bob_amount_x = balance_of!(app, token_x, "bob"); - let bob_amount_y = balance_of!(app, token_y, "bob"); - let bob_amount_z = balance_of!(app, token_z, "bob"); + let bob_amount_x = balance_of!(app, token_x, bob); + let bob_amount_y = balance_of!(app, token_y, bob); + let bob_amount_z = balance_of!(app, token_z, bob); assert_eq!(bob_amount_x, 0); assert_eq!(bob_amount_y, 0); @@ -138,16 +145,16 @@ fn swap_route() { assert_eq!(pool_2_after.fee_protocol_token_x, TokenAmount(1)); assert_eq!(pool_2_after.fee_protocol_token_y, TokenAmount(0)); - let alice_amount_x_before = balance_of!(app, token_x, "alice"); - let alice_amount_y_before = balance_of!(app, token_y, "alice"); - let alice_amount_z_before = balance_of!(app, token_z, "alice"); + let alice_amount_x_before = balance_of!(app, token_x, alice); + let alice_amount_y_before = balance_of!(app, token_y, alice); + let alice_amount_z_before = balance_of!(app, token_z, alice); - claim_fee!(app, dex, 0, "alice").unwrap(); - claim_fee!(app, dex, 1, "alice").unwrap(); + claim_fee!(app, dex, 0, alice).unwrap(); + claim_fee!(app, dex, 1, alice).unwrap(); - let alice_amount_x_after = balance_of!(app, token_x, "alice"); - let alice_amount_y_after = balance_of!(app, token_y, "alice"); - let alice_amount_z_after = balance_of!(app, token_z, "alice"); + let alice_amount_x_after = balance_of!(app, token_x, alice); + let alice_amount_y_after = balance_of!(app, token_y, alice); + let alice_amount_z_after = balance_of!(app, token_z, alice); assert_eq!(alice_amount_x_after - alice_amount_x_before, 4); assert_eq!(alice_amount_y_after - alice_amount_y_before, 4);