From 80a10aa354b3f536dd960bcbf8880cfef105bd32 Mon Sep 17 00:00:00 2001 From: Kaloyan Gangov Date: Tue, 31 Oct 2023 20:28:49 +0200 Subject: [PATCH] Factory, Multihop, Phoenix-utils: fixes to comply with the tests --- contracts/factory/src/contract.rs | 1 + contracts/factory/src/tests/config.rs | 2 ++ contracts/factory/src/tests/queries.rs | 7 +++++ contracts/multihop/src/contract.rs | 43 ++++++++++++++++++++------ contracts/multihop/src/storage.rs | 9 ++++++ contracts/multihop/src/tests/setup.rs | 1 + contracts/multihop/src/tests/swap.rs | 12 +++---- packages/phoenix/src/utils.rs | 1 + 8 files changed, 61 insertions(+), 15 deletions(-) diff --git a/contracts/factory/src/contract.rs b/contracts/factory/src/contract.rs index f12168ea..d7ccfb47 100644 --- a/contracts/factory/src/contract.rs +++ b/contracts/factory/src/contract.rs @@ -84,6 +84,7 @@ impl FactoryTrait for Factory { lp_init_info.fee_recipient, lp_init_info.max_allowed_slippage_bps, lp_init_info.max_allowed_spread_bps, + lp_init_info.max_referral_bps, lp_init_info.token_init_info.clone(), lp_init_info.stake_init_info, ) diff --git a/contracts/factory/src/tests/config.rs b/contracts/factory/src/tests/config.rs index a2ceb591..d9a1a3f0 100644 --- a/contracts/factory/src/tests/config.rs +++ b/contracts/factory/src/tests/config.rs @@ -79,6 +79,7 @@ fn factory_successfully_inits_lp() { max_allowed_spread_bps: 500, share_token_decimals: 7, swap_fee_bps: 0, + max_referral_bps: 5_000, token_init_info: token_init_info.clone(), stake_init_info, }; @@ -96,6 +97,7 @@ fn factory_successfully_inits_lp() { fee_recipient: user, max_allowed_slippage_bps: 5_000, max_allowed_spread_bps: 500, + max_referral_bps: 5_000, pool_type: lp_contract::PairType::Xyk, share_token: share_token_address, stake_contract: stake_token_address, diff --git a/contracts/factory/src/tests/queries.rs b/contracts/factory/src/tests/queries.rs index 74404c8f..97869cf4 100644 --- a/contracts/factory/src/tests/queries.rs +++ b/contracts/factory/src/tests/queries.rs @@ -28,6 +28,7 @@ pub struct LiquidityPoolConfig { pub max_allowed_slippage_bps: i64, /// The maximum amount of spread (in bps) that is tolerated during swap pub max_allowed_spread_bps: i64, + pub max_referral_bps: i64, } #[test] @@ -106,6 +107,7 @@ fn test_deploy_multiple_liquidity_pools() { max_allowed_spread_bps: 500, share_token_decimals: 7, swap_fee_bps: 0, + max_referral_bps: 5_000, token_init_info: first_token_init_info.clone(), stake_init_info: first_stake_init_info, }; @@ -118,6 +120,7 @@ fn test_deploy_multiple_liquidity_pools() { max_allowed_spread_bps: 400, share_token_decimals: 6, swap_fee_bps: 0, + max_referral_bps: 5_000, token_init_info: second_token_init_info, stake_init_info: second_stake_init_info, }; @@ -130,6 +133,7 @@ fn test_deploy_multiple_liquidity_pools() { max_allowed_spread_bps: 400, share_token_decimals: 6, swap_fee_bps: 0, + max_referral_bps: 5_000, token_init_info: third_token_init_info, stake_init_info: third_stake_init_info, }; @@ -300,6 +304,7 @@ fn test_queries_by_tuple() { max_allowed_spread_bps: 500, share_token_decimals: 7, swap_fee_bps: 0, + max_referral_bps: 5_000, token_init_info: first_token_init_info.clone(), stake_init_info: first_stake_init_info, }; @@ -312,6 +317,7 @@ fn test_queries_by_tuple() { max_allowed_spread_bps: 400, share_token_decimals: 6, swap_fee_bps: 0, + max_referral_bps: 5_000, token_init_info: second_token_init_info, stake_init_info: second_stake_init_info, }; @@ -324,6 +330,7 @@ fn test_queries_by_tuple() { max_allowed_spread_bps: 400, share_token_decimals: 6, swap_fee_bps: 0, + max_referral_bps: 5_000, token_init_info: third_token_init_info, stake_init_info: third_stake_init_info, }; diff --git a/contracts/multihop/src/contract.rs b/contracts/multihop/src/contract.rs index 099bede2..b994c44d 100644 --- a/contracts/multihop/src/contract.rs +++ b/contracts/multihop/src/contract.rs @@ -1,5 +1,6 @@ use soroban_sdk::{contract, contractimpl, contractmeta, Address, Env, Vec}; +use crate::lp_contract::Referral; use crate::storage::{ get_factory, is_initialized, save_factory, set_initialized, DataKey, SimulateReverseSwapResponse, SimulateSwapResponse, Swap, @@ -19,7 +20,13 @@ pub struct Multihop; pub trait MultihopTrait { fn initialize(env: Env, admin: Address, factory: Address); - fn swap(env: Env, recipient: Address, operations: Vec, amount: i128); + fn swap( + env: Env, + recipient: Address, + referral: Option, + operations: Vec, + amount: i128, + ); fn simulate_swap(env: Env, operations: Vec, amount: i128) -> SimulateSwapResponse; @@ -51,7 +58,13 @@ impl MultihopTrait for Multihop { .publish(("initialize", "Multihop factory with admin: "), admin); } - fn swap(env: Env, recipient: Address, operations: Vec, amount: i128) { + fn swap( + env: Env, + recipient: Address, + referral: Option, + operations: Vec, + amount: i128, + ) { if operations.is_empty() { panic!("Multihop: Swap: operations is empty!"); } @@ -70,13 +83,25 @@ impl MultihopTrait for Multihop { .query_for_pool_by_token_pair(&op.clone().offer_asset, &op.ask_asset.clone()); let lp_client = lp_contract::Client::new(&env, &liquidity_pool_addr); - next_offer_amount = lp_client.swap( - &recipient, - &op.offer_asset, - &next_offer_amount, - &None::, - &Some(5000i64), - ); + if let Some(referral) = referral.clone() { + next_offer_amount = lp_client.swap( + &recipient, + &Some(referral), + &op.offer_asset, + &next_offer_amount, + &None::, + &Some(5000i64), + ); + } else { + next_offer_amount = lp_client.swap( + &recipient, + &None, + &op.offer_asset, + &next_offer_amount, + &None::, + &Some(5000i64), + ); + } }); } diff --git a/contracts/multihop/src/storage.rs b/contracts/multihop/src/storage.rs index 257f5127..1ded4cc2 100644 --- a/contracts/multihop/src/storage.rs +++ b/contracts/multihop/src/storage.rs @@ -1,5 +1,14 @@ use soroban_sdk::{contracttype, Address, Env}; +#[contracttype] +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Referral { + /// Address of the referral + pub address: Address, + /// fee in bps, later parsed to percentage + pub fee: i64, +} + #[contracttype] #[derive(Clone, Debug, Eq, PartialEq)] pub struct Swap { diff --git a/contracts/multihop/src/tests/setup.rs b/contracts/multihop/src/tests/setup.rs index 6c86b1f0..50e38bc7 100644 --- a/contracts/multihop/src/tests/setup.rs +++ b/contracts/multihop/src/tests/setup.rs @@ -132,6 +132,7 @@ pub fn deploy_and_initialize_lp( max_allowed_spread_bps: 500, share_token_decimals: 7, swap_fee_bps: fees.unwrap_or(0i64), + max_referral_bps: 5_000, token_init_info, stake_init_info, }; diff --git a/contracts/multihop/src/tests/swap.rs b/contracts/multihop/src/tests/swap.rs index a8c599cf..5ff987e1 100644 --- a/contracts/multihop/src/tests/swap.rs +++ b/contracts/multihop/src/tests/swap.rs @@ -76,7 +76,7 @@ fn swap_three_equal_pools_no_fees() { let operations = vec![&env, swap1, swap2, swap3]; - multihop.swap(&recipient, &operations, &50i128); + multihop.swap(&recipient, &None, &operations, &50i128); // 5. check if it goes according to plan assert_eq!(token1.balance(&recipient), 0i128); @@ -122,7 +122,7 @@ fn swap_single_pool_no_fees() { let operations = vec![&env, swap1]; - multihop.swap(&recipient, &operations, &1_000); + multihop.swap(&recipient, &None, &operations, &1_000); // 5. check if it goes according to plan assert_eq!(token1.balance(&recipient), 4_000i128); // -1_000 token0 @@ -168,7 +168,7 @@ fn swap_single_pool_with_fees() { let operations = vec![&env, swap1]; - multihop.swap(&recipient, &operations, &300i128); + multihop.swap(&recipient, &None, &operations, &300i128); // 5. check if it goes according to plan // 1000 tokens initially @@ -249,7 +249,7 @@ fn swap_three_different_pools_no_fees() { let operations = vec![&env, swap1, swap2, swap3]; - multihop.swap(&recipient, &operations, &5_000i128); + multihop.swap(&recipient, &None, &operations, &5_000i128); // 5. check if it goes according to plan assert_eq!(token1.balance(&recipient), 0i128); @@ -329,7 +329,7 @@ fn swap_three_different_pools_with_fees() { let operations = vec![&env, swap1, swap2, swap3]; - multihop.swap(&recipient, &operations, &10_000i128); + multihop.swap(&recipient, &None, &operations, &10_000i128); // we start swapping 10_000 tokens @@ -373,5 +373,5 @@ fn swap_panics_with_no_operations() { let swap_vec = vec![&env]; - multihop.swap(&recipient, &swap_vec, &50i128); + multihop.swap(&recipient, &None, &swap_vec, &50i128); } diff --git a/packages/phoenix/src/utils.rs b/packages/phoenix/src/utils.rs index 6a974696..9f65e82c 100644 --- a/packages/phoenix/src/utils.rs +++ b/packages/phoenix/src/utils.rs @@ -50,6 +50,7 @@ pub struct LiquidityPoolInitInfo { pub fee_recipient: Address, pub max_allowed_slippage_bps: i64, pub max_allowed_spread_bps: i64, + pub max_referral_bps: i64, pub token_init_info: TokenInitInfo, pub stake_init_info: StakeInitInfo, }