Skip to content

Commit

Permalink
Factory, Multihop, Phoenix-utils: fixes to comply with the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gangov committed Oct 31, 2023
1 parent 24b6806 commit 80a10aa
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 15 deletions.
1 change: 1 addition & 0 deletions contracts/factory/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
2 changes: 2 additions & 0 deletions contracts/factory/src/tests/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions contracts/factory/src/tests/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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,
};
Expand All @@ -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,
};
Expand All @@ -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,
};
Expand Down Expand Up @@ -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,
};
Expand All @@ -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,
};
Expand All @@ -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,
};
Expand Down
43 changes: 34 additions & 9 deletions contracts/multihop/src/contract.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<Swap>, amount: i128);
fn swap(
env: Env,
recipient: Address,
referral: Option<Referral>,
operations: Vec<Swap>,
amount: i128,
);

fn simulate_swap(env: Env, operations: Vec<Swap>, amount: i128) -> SimulateSwapResponse;

Expand Down Expand Up @@ -51,7 +58,13 @@ impl MultihopTrait for Multihop {
.publish(("initialize", "Multihop factory with admin: "), admin);
}

fn swap(env: Env, recipient: Address, operations: Vec<Swap>, amount: i128) {
fn swap(
env: Env,
recipient: Address,
referral: Option<Referral>,
operations: Vec<Swap>,
amount: i128,
) {
if operations.is_empty() {
panic!("Multihop: Swap: operations is empty!");
}
Expand All @@ -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::<i64>,
&Some(5000i64),
);
if let Some(referral) = referral.clone() {
next_offer_amount = lp_client.swap(
&recipient,
&Some(referral),
&op.offer_asset,
&next_offer_amount,
&None::<i64>,
&Some(5000i64),
);
} else {
next_offer_amount = lp_client.swap(
&recipient,
&None,
&op.offer_asset,
&next_offer_amount,
&None::<i64>,
&Some(5000i64),
);
}
});
}

Expand Down
9 changes: 9 additions & 0 deletions contracts/multihop/src/storage.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
1 change: 1 addition & 0 deletions contracts/multihop/src/tests/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
12 changes: 6 additions & 6 deletions contracts/multihop/src/tests/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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);
}
1 change: 1 addition & 0 deletions packages/phoenix/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down

0 comments on commit 80a10aa

Please sign in to comment.