From 4511b2b0dd627eb0982b13e66c0ee764812561c7 Mon Sep 17 00:00:00 2001 From: Jeremy Liu <31809888+NotJeremyLiu@users.noreply.github.com> Date: Fri, 28 Jul 2023 14:34:26 -0400 Subject: [PATCH 1/5] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Add=20user=20swap=20ms?= =?UTF-8?q?g=20to=20response=20earlier=20in=20the=20logic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contracts/entry-point/src/execute.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/contracts/entry-point/src/execute.rs b/contracts/entry-point/src/execute.rs index 6d35120e..8c4d28b3 100644 --- a/contracts/entry-point/src/execute.rs +++ b/contracts/entry-point/src/execute.rs @@ -99,6 +99,11 @@ pub fn execute_swap_and_action( &min_coin.denom, )?; + // Add the user swap message to the response + response = response + .add_message(user_swap_msg) + .add_attribute("action", "dispatch_user_swap"); + // Create the transfer message let post_swap_action_msg = WasmMsg::Execute { contract_addr: env.contract.address.to_string(), @@ -113,9 +118,8 @@ pub fn execute_swap_and_action( // Add the user swap message and post swap action message to the response Ok(response - .add_message(user_swap_msg) .add_message(post_swap_action_msg) - .add_attribute("action", "dispatch_user_swap_and_post_swap_action")) + .add_attribute("action", "dispatch_post_swap_action")) } // Dispatches the post swap action From 4af447936069ad36772c84bed94610ad2e8266fc Mon Sep 17 00:00:00 2001 From: Jeremy Liu <31809888+NotJeremyLiu@users.noreply.github.com> Date: Fri, 28 Jul 2023 16:54:00 -0400 Subject: [PATCH 2/5] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Move=20affiliate=20fee?= =?UTF-8?q?=20logic=20into=20swap=5Fand=5Faction?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contracts/entry-point/src/contract.rs | 2 - contracts/entry-point/src/error.rs | 3 - contracts/entry-point/src/execute.rs | 85 +++++++++++---------------- packages/skip/src/entry_point.rs | 1 - 4 files changed, 35 insertions(+), 56 deletions(-) diff --git a/contracts/entry-point/src/contract.rs b/contracts/entry-point/src/contract.rs index 09826a26..b0157a69 100644 --- a/contracts/entry-point/src/contract.rs +++ b/contracts/entry-point/src/contract.rs @@ -107,7 +107,6 @@ pub fn execute( min_coin, timeout_timestamp, post_swap_action, - affiliates, } => execute_post_swap_action( deps, env, @@ -115,7 +114,6 @@ pub fn execute( min_coin, timeout_timestamp, post_swap_action, - affiliates, ), } } diff --git a/contracts/entry-point/src/error.rs b/contracts/entry-point/src/error.rs index 50f6fcaa..2d887319 100644 --- a/contracts/entry-point/src/error.rs +++ b/contracts/entry-point/src/error.rs @@ -54,9 +54,6 @@ pub enum ContractError { #[error("Received Less Coin From Swaps Than Minimum Coin Required")] ReceivedLessCoinFromSwapsThanMinCoin, - #[error("Transfer Out Coin Less Than Minimum Required After Affiliate Fees")] - TransferOutCoinLessThanMinAfterAffiliateFees, - #[error("Transfer Out Coin Less Than Minimum Required After IBC Fees")] TransferOutCoinLessThanMinAfterIbcFees, diff --git a/contracts/entry-point/src/execute.rs b/contracts/entry-point/src/execute.rs index 8c4d28b3..21692082 100644 --- a/contracts/entry-point/src/execute.rs +++ b/contracts/entry-point/src/execute.rs @@ -104,19 +104,45 @@ pub fn execute_swap_and_action( .add_message(user_swap_msg) .add_attribute("action", "dispatch_user_swap"); - // Create the transfer message + // If affiliates exist, create the affiliate fee messages and add them to the + // response, decreasing the transfer out coin amount by each affiliate fee amount + for affiliate in affiliates.iter() { + // Verify, calculate, and get the affiliate fee amount + let affiliate_fee_amount = verify_and_calculate_affiliate_fee_amount( + &deps, + &min_coin, + affiliate, + )?; + + // Create the affiliate fee bank send message + let affiliate_fee_msg = BankMsg::Send { + to_address: affiliate.address.clone(), + amount: vec![Coin { + denom: min_coin.denom.clone(), + amount: affiliate_fee_amount, + }], + }; + + // Add the affiliate fee message and attributes to the response + response = response + .add_message(affiliate_fee_msg) + .add_attribute("action", "dispatch_affiliate_fee_bank_send") + .add_attribute("address", &affiliate.address) + .add_attribute("amount", affiliate_fee_amount); + } + + // Create the post swap action message let post_swap_action_msg = WasmMsg::Execute { contract_addr: env.contract.address.to_string(), msg: to_binary(&ExecuteMsg::PostSwapAction { min_coin, timeout_timestamp, post_swap_action, - affiliates, })?, funds: vec![], }; - // Add the user swap message and post swap action message to the response + // Add the post swap action message to the response and return the response Ok(response .add_message(post_swap_action_msg) .add_attribute("action", "dispatch_post_swap_action")) @@ -131,7 +157,6 @@ pub fn execute_post_swap_action( min_coin: Coin, timeout_timestamp: u64, post_swap_action: Action, - affiliates: Vec, ) -> ContractResult { // Enforce the caller is the contract itself if info.sender != env.contract.address { @@ -144,55 +169,15 @@ pub fn execute_post_swap_action( // Get contract balance of min out coin immediately after swap // for fee deduction and transfer out amount enforcement - let transfer_out_coin_contract_balance_after_swaps = deps + let transfer_out_coin = deps .querier .query_balance(&env.contract.address, &min_coin.denom)?; // Error if the contract balance is less than the min out coin amount - if transfer_out_coin_contract_balance_after_swaps.amount < min_coin.amount { + if transfer_out_coin.amount < min_coin.amount { return Err(ContractError::ReceivedLessCoinFromSwapsThanMinCoin); } - // Mutable copy of the transfer out coin to subtract fees from - // to become the final transfer out coin we send to the user - let mut transfer_out_coin = transfer_out_coin_contract_balance_after_swaps.clone(); - - // If affiliates exist, create the affiliate fee messages and add them to the - // response, decreasing the transfer out coin amount by each affiliate fee amount - for affiliate in affiliates.iter() { - // Verify, calculate, and get the affiliate fee amount - let affiliate_fee_amount = verify_and_calculate_affiliate_fee_amount( - &deps, - &transfer_out_coin_contract_balance_after_swaps, - affiliate, - )?; - - // Subtract the affiliate fee from the transfer out coin - transfer_out_coin.amount = transfer_out_coin.amount.checked_sub(affiliate_fee_amount)?; - - // Create the affiliate fee bank send message - let affiliate_fee_msg = BankMsg::Send { - to_address: affiliate.address.clone(), - amount: vec![Coin { - denom: transfer_out_coin_contract_balance_after_swaps.denom.clone(), - amount: affiliate_fee_amount, - }], - }; - - // Add the affiliate fee message and logs to the response - response = response - .add_message(affiliate_fee_msg) - .add_attribute("action", "dispatch_affiliate_fee_bank_send") - .add_attribute("address", &affiliate.address) - .add_attribute("amount", affiliate_fee_amount); - } - - // If affiliates exist, then error if the transfer out coin amount - // is less than the min coin amount after affiliate fees - if !affiliates.is_empty() && transfer_out_coin.amount < min_coin.amount { - return Err(ContractError::TransferOutCoinLessThanMinAfterAffiliateFees); - } - match post_swap_action { Action::BankSend { to_address } => { // Create the bank send message @@ -342,15 +327,15 @@ fn verify_and_create_user_swap_msg( // returns the calculated affiliate fee amount. fn verify_and_calculate_affiliate_fee_amount( deps: &DepsMut, - transfer_out_coin_contract_balance_after_swaps: &Coin, + min_coin: &Coin, affiliate: &Affiliate, ) -> ContractResult { // Verify the affiliate address is valid deps.api.addr_validate(&affiliate.address)?; - // Get the affiliate fee amount by multiplying the transfer out coin amount - // immediately after the swaps by the affiliate basis points fee divided by 10000 - let affiliate_fee_amount = transfer_out_coin_contract_balance_after_swaps + // Get the affiliate fee amount by multiplying the min_coin + // amount by the affiliate basis points fee divided by 10000 + let affiliate_fee_amount = min_coin .amount .multiply_ratio(affiliate.basis_points_fee, Uint128::new(10000)); diff --git a/packages/skip/src/entry_point.rs b/packages/skip/src/entry_point.rs index 4d8d24cd..7dc99625 100644 --- a/packages/skip/src/entry_point.rs +++ b/packages/skip/src/entry_point.rs @@ -35,7 +35,6 @@ pub enum ExecuteMsg { min_coin: Coin, timeout_timestamp: u64, post_swap_action: Action, - affiliates: Vec, }, } From abbbb07a8efe887f525e0490dff5c1efa4e4a520 Mon Sep 17 00:00:00 2001 From: Jeremy Liu <31809888+NotJeremyLiu@users.noreply.github.com> Date: Fri, 28 Jul 2023 17:15:18 -0400 Subject: [PATCH 3/5] =?UTF-8?q?=F0=9F=8E=A8=20run=20make=20fmt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contracts/entry-point/src/execute.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/contracts/entry-point/src/execute.rs b/contracts/entry-point/src/execute.rs index 21692082..68f542f1 100644 --- a/contracts/entry-point/src/execute.rs +++ b/contracts/entry-point/src/execute.rs @@ -11,8 +11,8 @@ use skip::{ entry_point::{Action, Affiliate, ExecuteMsg}, ibc::{ExecuteMsg as IbcTransferExecuteMsg, IbcInfo, IbcTransfer}, swap::{ - validate_swap_operations, ExecuteMsg as SwapExecuteMsg, QueryMsg as SwapQueryMsg, - Swap, SwapExactCoinOut, + validate_swap_operations, ExecuteMsg as SwapExecuteMsg, QueryMsg as SwapQueryMsg, Swap, + SwapExactCoinOut, }, }; @@ -108,11 +108,8 @@ pub fn execute_swap_and_action( // response, decreasing the transfer out coin amount by each affiliate fee amount for affiliate in affiliates.iter() { // Verify, calculate, and get the affiliate fee amount - let affiliate_fee_amount = verify_and_calculate_affiliate_fee_amount( - &deps, - &min_coin, - affiliate, - )?; + let affiliate_fee_amount = + verify_and_calculate_affiliate_fee_amount(&deps, &min_coin, affiliate)?; // Create the affiliate fee bank send message let affiliate_fee_msg = BankMsg::Send { @@ -314,10 +311,10 @@ fn verify_and_create_user_swap_msg( }; Ok(user_swap_msg) - }, + } Swap::SwapExactCoinOut(_) => { unimplemented!() - }, + } } } From 4c5b1cbdb19420e8cea26a0cfc0904373d4f179c Mon Sep 17 00:00:00 2001 From: Jeremy Liu <31809888+NotJeremyLiu@users.noreply.github.com> Date: Fri, 28 Jul 2023 17:15:36 -0400 Subject: [PATCH 4/5] =?UTF-8?q?=F0=9F=94=A5=20remove=20affiliates=20from?= =?UTF-8?q?=20post=5Fswap=5Faction=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tests/test_execute_post_swap_action.rs | 245 +----------------- 1 file changed, 2 insertions(+), 243 deletions(-) diff --git a/contracts/entry-point/tests/test_execute_post_swap_action.rs b/contracts/entry-point/tests/test_execute_post_swap_action.rs index 4bd8b77c..3c2cfca3 100644 --- a/contracts/entry-point/tests/test_execute_post_swap_action.rs +++ b/contracts/entry-point/tests/test_execute_post_swap_action.rs @@ -2,10 +2,10 @@ use cosmwasm_std::{ testing::{mock_dependencies_with_balances, mock_env, mock_info}, to_binary, Addr, BankMsg, Coin, ReplyOn::Never, - SubMsg, Timestamp, Uint128, WasmMsg, + SubMsg, Timestamp, WasmMsg, }; use skip::{ - entry_point::{Action, Affiliate, ExecuteMsg}, + entry_point::{Action, ExecuteMsg}, ibc::{ExecuteMsg as IbcTransferExecuteMsg, IbcFee, IbcInfo}, }; use skip_swap_entry_point::{ @@ -27,15 +27,8 @@ Expect Response - Ibc Transfer w/ IBC Fees of different denom than min coin - Ibc Transfer w/ IBC Fees of same denom as min coin - // With Affiliates - - Bank Send w/ Affiliate - - Contract Call w/ Affiliate - - Ibc Transfer w/ IBC Fees of different denom than min coin w/ Affiliate - - Ibc Transfer w/ IBC Fees of same denom as min coin w/ Affiliate - Expect Error - Bank Send Timeout - - Ibc Transfer w/ Affiliates Decreasing user transfer below min coin - Ibc Transfer w/ IBC Fees Decreasing user transfer below min coin - Received Less From Swap Than Min Coin - Unauthorized Caller @@ -47,7 +40,6 @@ struct Params { caller: String, min_coin: Coin, post_swap_action: Action, - affiliates: Vec, expected_messages: Vec, expected_error: Option, } @@ -60,7 +52,6 @@ struct Params { post_swap_action: Action::BankSend { to_address: "cosmos1xv9tklw7d82sezh9haa573wufgy59vmwe6xxe5".to_string(), }, - affiliates: vec![], expected_messages: vec![SubMsg { id: 0, msg: BankMsg::Send { @@ -88,7 +79,6 @@ struct Params { .to_string(), }, }, - affiliates: vec![], expected_messages: vec![SubMsg { id: 0, msg: WasmMsg::Execute { @@ -123,7 +113,6 @@ struct Params { contract_address: "contract_call".to_string(), msg: to_binary(&"contract_call_msg").unwrap(), }, - affiliates: vec![], expected_messages: vec![SubMsg { id: 0, msg: WasmMsg::Execute { @@ -157,7 +146,6 @@ struct Params { .to_string(), }, }, - affiliates: vec![], expected_messages: vec![SubMsg { id: 0, msg: WasmMsg::Execute { @@ -209,7 +197,6 @@ struct Params { .to_string(), }, }, - affiliates: vec![], expected_messages: vec![SubMsg { id: 0, msg: WasmMsg::Execute { @@ -240,229 +227,6 @@ struct Params { expected_error: None, }; "Ibc Transfer w/ IBC Fees of same denom as min coin")] -#[test_case( - Params { - caller: "entry_point".to_string(), - min_coin: Coin::new(900_000, "osmo"), - post_swap_action: Action::BankSend { - to_address: "swapper".to_string(), - }, - affiliates: vec![Affiliate { - address: "affiliate".to_string(), - basis_points_fee: Uint128::new(1000), - }], - expected_messages: vec![ - SubMsg { - id: 0, - msg: BankMsg::Send { - to_address: "affiliate".to_string(), - amount: vec![Coin::new(100_000, "osmo")], - } - .into(), - gas_limit: None, - reply_on: Never, - }, - SubMsg { - id: 0, - msg: BankMsg::Send { - to_address: "swapper".to_string(), - amount: vec![Coin::new(900_000, "osmo")], - } - .into(), - gas_limit: None, - reply_on: Never, - }, - ], - expected_error: None, - }; - "Bank Send w/ Affiliate")] -#[test_case( - Params { - caller: "entry_point".to_string(), - min_coin: Coin::new(900_000, "osmo"), - post_swap_action: Action::ContractCall { - contract_address: "contract_call".to_string(), - msg: to_binary(&"contract_call_msg").unwrap(), - }, - affiliates: vec![Affiliate { - address: "affiliate".to_string(), - basis_points_fee: Uint128::new(1000), - }], - expected_messages: vec![ - SubMsg { - id: 0, - msg: BankMsg::Send { - to_address: "affiliate".to_string(), - amount: vec![Coin::new(100_000, "osmo")], - } - .into(), - gas_limit: None, - reply_on: Never, - }, - SubMsg { - id: 0, - msg: WasmMsg::Execute { - contract_addr: "contract_call".to_string(), - msg: to_binary(&"contract_call_msg").unwrap(), - funds: vec![Coin::new(900_000, "osmo")], - } - .into(), - gas_limit: None, - reply_on: Never, - }, - ], - expected_error: None, - }; - "Contract Call w/ Affiliate")] -#[test_case( - Params { - caller: "entry_point".to_string(), - min_coin: Coin::new(900_000, "osmo"), - post_swap_action: Action::IbcTransfer { - ibc_info: IbcInfo { - source_channel: "channel-0".to_string(), - receiver: "receiver".to_string(), - memo: "".to_string(), - fee: Some(IbcFee { - recv_fee: vec![], - ack_fee: vec![Coin::new(100_000, "untrn")], - timeout_fee: vec![Coin::new(100_000, "untrn")], - }), - recover_address: "recover".to_string(), - }, - }, - affiliates: vec![Affiliate { - address: "affiliate".to_string(), - basis_points_fee: Uint128::new(1000), - }], - expected_messages: vec![ - SubMsg { - id: 0, - msg: BankMsg::Send { - to_address: "affiliate".to_string(), - amount: vec![Coin::new(100_000, "osmo")], - } - .into(), - gas_limit: None, - reply_on: Never, - }, - SubMsg { - id: 0, - msg: WasmMsg::Execute { - contract_addr: "ibc_transfer_adapter".to_string(), - msg: to_binary(&IbcTransferExecuteMsg::IbcTransfer { - info: IbcInfo { - source_channel: "channel-0".to_string(), - receiver: "receiver".to_string(), - memo: "".to_string(), - fee: Some(IbcFee { - recv_fee: vec![], - ack_fee: vec![Coin::new(100_000, "untrn")], - timeout_fee: vec![Coin::new(100_000, "untrn")], - }), - recover_address: "recover".to_string(), - }, - coin: Coin::new(900_000, "osmo"), - timeout_timestamp: 101, - }) - .unwrap(), - funds: vec![Coin::new(900_000, "osmo"), Coin::new(200_000, "untrn")], - } - .into(), - gas_limit: None, - reply_on: Never, - }, - ], - expected_error: None, - }; - "Ibc Transfer w/ IBC Fees of different denom than min coin w/ Affiliate")] -#[test_case( - Params { - caller: "entry_point".to_string(), - min_coin: Coin::new(700_000, "untrn"), - post_swap_action: Action::IbcTransfer { - ibc_info: IbcInfo { - source_channel: "channel-0".to_string(), - receiver: "receiver".to_string(), - memo: "".to_string(), - fee: Some(IbcFee { - recv_fee: vec![], - ack_fee: vec![Coin::new(100_000, "untrn")], - timeout_fee: vec![Coin::new(100_000, "untrn")], - }), - recover_address: "recover".to_string(), - }, - }, - affiliates: vec![Affiliate { - address: "affiliate".to_string(), - basis_points_fee: Uint128::new(1000), - }], - expected_messages: vec![ - SubMsg { - id: 0, - msg: BankMsg::Send { - to_address: "affiliate".to_string(), - amount: vec![Coin::new(100_000, "untrn")], - } - .into(), - gas_limit: None, - reply_on: Never, - }, - SubMsg { - id: 0, - msg: WasmMsg::Execute { - contract_addr: "ibc_transfer_adapter".to_string(), - msg: to_binary(&IbcTransferExecuteMsg::IbcTransfer { - info: IbcInfo { - source_channel: "channel-0".to_string(), - receiver: "receiver".to_string(), - memo: "".to_string(), - fee: Some(IbcFee { - recv_fee: vec![], - ack_fee: vec![Coin::new(100_000, "untrn")], - timeout_fee: vec![Coin::new(100_000, "untrn")], - }), - recover_address: "recover".to_string(), - }, - coin: Coin::new(700_000, "untrn"), - timeout_timestamp: 101, - }) - .unwrap(), - funds: vec![Coin::new(900_000, "untrn")], - } - .into(), - gas_limit: None, - reply_on: Never, - }, - ], - expected_error: None, - }; - "Ibc Transfer w/ IBC Fees of same denom as min coin w/ Affiliate")] -#[test_case( - Params { - caller: "entry_point".to_string(), - min_coin: Coin::new(950_000, "osmo"), - post_swap_action: Action::IbcTransfer { - ibc_info: IbcInfo { - source_channel: "channel-0".to_string(), - receiver: "receiver".to_string(), - memo: "".to_string(), - fee: Some(IbcFee { - recv_fee: vec![], - ack_fee: vec![Coin::new(100_000, "untrn")], - timeout_fee: vec![Coin::new(100_000, "untrn")], - }), - recover_address: "recover".to_string(), - }, - }, - affiliates: vec![Affiliate { - address: "affiliate".to_string(), - basis_points_fee: Uint128::new(1000), - }], - expected_messages: vec![], - expected_error: Some(ContractError::TransferOutCoinLessThanMinAfterAffiliateFees), - }; - "Ibc Transfer w/ Affiliates Decreasing user transfer below min coin - Expect Error")] #[test_case( Params { caller: "entry_point".to_string(), @@ -480,7 +244,6 @@ struct Params { recover_address: "recover".to_string(), }, }, - affiliates: vec![], expected_messages: vec![], expected_error: Some(ContractError::TransferOutCoinLessThanMinAfterIbcFees), }; @@ -492,7 +255,6 @@ struct Params { post_swap_action: Action::BankSend { to_address: "swapper".to_string(), }, - affiliates: vec![], expected_messages: vec![], expected_error: Some(ContractError::ReceivedLessCoinFromSwapsThanMinCoin), }; @@ -504,7 +266,6 @@ struct Params { post_swap_action: Action::BankSend { to_address: "swapper".to_string(), }, - affiliates: vec![], expected_messages: vec![], expected_error: Some(ContractError::Unauthorized), }; @@ -517,7 +278,6 @@ struct Params { contract_address: "entry_point".to_string(), msg: to_binary(&"contract_call_msg").unwrap(), }, - affiliates: vec![], expected_messages: vec![], expected_error: Some(ContractError::ContractCallAddressBlocked), }; @@ -557,7 +317,6 @@ fn test_execute_post_swap_action(params: Params) { min_coin: params.min_coin, timeout_timestamp: 101, post_swap_action: params.post_swap_action, - affiliates: params.affiliates, }, ); From ed97940aee5c105ae0cc8ae9c6ea69d63c07649d Mon Sep 17 00:00:00 2001 From: Jeremy Liu <31809888+NotJeremyLiu@users.noreply.github.com> Date: Fri, 28 Jul 2023 17:16:01 -0400 Subject: [PATCH 5/5] =?UTF-8?q?=E2=9C=85=20Add=20affiliate=20tests=20to=20?= =?UTF-8?q?swap=5Fand=5Faction=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tests/test_execute_swap_and_action.rs | 211 +++++++++++++++++- 1 file changed, 199 insertions(+), 12 deletions(-) diff --git a/contracts/entry-point/tests/test_execute_swap_and_action.rs b/contracts/entry-point/tests/test_execute_swap_and_action.rs index 6043cc16..3e8708ff 100644 --- a/contracts/entry-point/tests/test_execute_swap_and_action.rs +++ b/contracts/entry-point/tests/test_execute_swap_and_action.rs @@ -1,8 +1,9 @@ use cosmwasm_std::{ testing::{mock_dependencies_with_balances, mock_env, mock_info}, - to_binary, Addr, Coin, ContractResult, OverflowError, OverflowOperation, QuerierResult, + to_binary, Addr, BankMsg, Coin, ContractResult, OverflowError, OverflowOperation, + QuerierResult, ReplyOn::Never, - SubMsg, SystemResult, Timestamp, WasmMsg, WasmQuery, + SubMsg, SystemResult, Timestamp, Uint128, WasmMsg, WasmQuery, }; use cw_utils::PaymentError::{MultipleDenoms, NoFunds}; use skip::{ @@ -25,6 +26,8 @@ Expect Response - User Swap With IBC Transfer With IBC Fees - User Swap With IBC Transfer Without IBC Fees - Fee Swap And User Swap With IBC Fees + - User Swap With Single Affiliate + - User Swap With Multiple Affiliates Expect Error // Fee Swap @@ -64,6 +67,7 @@ struct Params { min_coin: Coin, timeout_timestamp: u64, post_swap_action: Action, + affiliates: Vec, expected_messages: Vec, expected_error: Option, } @@ -92,6 +96,7 @@ struct Params { post_swap_action: Action::BankSend { to_address: "to_address".to_string(), }, + affiliates: vec![], expected_messages: vec![ SubMsg { id: 0, @@ -122,7 +127,6 @@ struct Params { post_swap_action: Action::BankSend { to_address: "to_address".to_string(), }, - affiliates: vec![], }).unwrap(), funds: vec![], } @@ -168,6 +172,7 @@ struct Params { .to_string(), }, }, + affiliates: vec![], expected_messages: vec![ SubMsg { id: 0, @@ -209,7 +214,6 @@ struct Params { .to_string(), }, }, - affiliates: vec![], }).unwrap(), funds: vec![], } @@ -251,6 +255,7 @@ struct Params { .to_string(), }, }, + affiliates: vec![], expected_messages: vec![ SubMsg { id: 0, @@ -288,7 +293,6 @@ struct Params { .to_string(), }, }, - affiliates: vec![], }).unwrap(), funds: vec![], } @@ -346,6 +350,7 @@ struct Params { .to_string(), }, }, + affiliates: vec![], expected_messages: vec![ SubMsg { id: 0, @@ -406,7 +411,6 @@ struct Params { .to_string(), }, }, - affiliates: vec![], }).unwrap(), funds: vec![], } @@ -418,6 +422,178 @@ struct Params { expected_error: None, }; "Fee Swap And User Swap With IBC Fees")] +#[test_case( + Params { + info_funds: vec![ + Coin::new(1_000_000, "untrn"), + ], + fee_swap: None, + user_swap: Swap::SwapExactCoinIn ( + SwapExactCoinIn{ + swap_venue_name: "swap_venue_name".to_string(), + operations: vec![ + SwapOperation { + pool: "pool".to_string(), + denom_in: "untrn".to_string(), + denom_out: "osmo".to_string(), + } + ], + } + ), + min_coin: Coin::new(1_000_000, "osmo"), + timeout_timestamp: 101, + post_swap_action: Action::BankSend { + to_address: "to_address".to_string(), + }, + affiliates: vec![Affiliate { + address: "affiliate".to_string(), + basis_points_fee: Uint128::new(1000), + }], + expected_messages: vec![ + SubMsg { + id: 0, + msg: WasmMsg::Execute { + contract_addr: "swap_venue_adapter".to_string(), + msg: to_binary(&SwapExecuteMsg::Swap { + operations: vec![ + SwapOperation { + pool: "pool".to_string(), + denom_in: "untrn".to_string(), + denom_out: "osmo".to_string(), + } + ], + }).unwrap(), + funds: vec![Coin::new(1_000_000, "untrn")], + } + .into(), + gas_limit: None, + reply_on: Never, + }, + SubMsg { + id: 0, + msg: BankMsg::Send { + to_address: "affiliate".to_string(), + amount: vec![Coin::new(100_000, "osmo")], + } + .into(), + gas_limit: None, + reply_on: Never, + }, + SubMsg { + id: 0, + msg: WasmMsg::Execute { + contract_addr: "entry_point".to_string(), + msg: to_binary(&ExecuteMsg::PostSwapAction { + min_coin: Coin::new(1_000_000, "osmo"), + timeout_timestamp: 101, + post_swap_action: Action::BankSend { + to_address: "to_address".to_string(), + }, + }).unwrap(), + funds: vec![], + } + .into(), + gas_limit: None, + reply_on: Never, + }, + ], + expected_error: None, + }; + "User Swap With Single Affiliate")] +#[test_case( + Params { + info_funds: vec![ + Coin::new(1_000_000, "untrn"), + ], + fee_swap: None, + user_swap: Swap::SwapExactCoinIn ( + SwapExactCoinIn{ + swap_venue_name: "swap_venue_name".to_string(), + operations: vec![ + SwapOperation { + pool: "pool".to_string(), + denom_in: "untrn".to_string(), + denom_out: "osmo".to_string(), + } + ], + } + ), + min_coin: Coin::new(1_000_000, "osmo"), + timeout_timestamp: 101, + post_swap_action: Action::BankSend { + to_address: "to_address".to_string(), + }, + affiliates: vec![ + Affiliate { + address: "affiliate_1".to_string(), + basis_points_fee: Uint128::new(1000), + }, + Affiliate { + address: "affiliate_2".to_string(), + basis_points_fee: Uint128::new(1000), + }, + ], + expected_messages: vec![ + SubMsg { + id: 0, + msg: WasmMsg::Execute { + contract_addr: "swap_venue_adapter".to_string(), + msg: to_binary(&SwapExecuteMsg::Swap { + operations: vec![ + SwapOperation { + pool: "pool".to_string(), + denom_in: "untrn".to_string(), + denom_out: "osmo".to_string(), + } + ], + }).unwrap(), + funds: vec![Coin::new(1_000_000, "untrn")], + } + .into(), + gas_limit: None, + reply_on: Never, + }, + SubMsg { + id: 0, + msg: BankMsg::Send { + to_address: "affiliate_1".to_string(), + amount: vec![Coin::new(100_000, "osmo")], + } + .into(), + gas_limit: None, + reply_on: Never, + }, + SubMsg { + id: 0, + msg: BankMsg::Send { + to_address: "affiliate_2".to_string(), + amount: vec![Coin::new(100_000, "osmo")], + } + .into(), + gas_limit: None, + reply_on: Never, + }, + SubMsg { + id: 0, + msg: WasmMsg::Execute { + contract_addr: "entry_point".to_string(), + msg: to_binary(&ExecuteMsg::PostSwapAction { + min_coin: Coin::new(1_000_000, "osmo"), + timeout_timestamp: 101, + post_swap_action: Action::BankSend { + to_address: "to_address".to_string(), + }, + }).unwrap(), + funds: vec![], + } + .into(), + gas_limit: None, + reply_on: Never, + }, + ], + expected_error: None, + }; + "User Swap With Multiple Affiliates")] #[test_case( Params { info_funds: vec![ @@ -464,6 +640,7 @@ struct Params { .to_string(), }, }, + affiliates: vec![], expected_messages: vec![], expected_error: Some(ContractError::Overflow(OverflowError { operation: OverflowOperation::Sub, @@ -518,6 +695,7 @@ struct Params { .to_string(), }, }, + affiliates: vec![], expected_messages: vec![], expected_error: Some(ContractError::FeeSwapCoinInDenomMismatch), }; @@ -568,6 +746,7 @@ struct Params { .to_string(), }, }, + affiliates: vec![], expected_messages: vec![], expected_error: Some(ContractError::Skip(SwapOperationsCoinInDenomMismatch)), }; @@ -618,6 +797,7 @@ struct Params { .to_string(), }, }, + affiliates: vec![], expected_messages: vec![], expected_error: Some(ContractError::Skip(SwapOperationsCoinOutDenomMismatch)), }; @@ -645,6 +825,7 @@ struct Params { post_swap_action: Action::BankSend { to_address: "to_address".to_string(), }, + affiliates: vec![], expected_messages: vec![], expected_error: Some(ContractError::Skip(SwapOperationsCoinInDenomMismatch)), }; @@ -683,6 +864,7 @@ struct Params { .to_string(), }, }, + affiliates: vec![], expected_messages: vec![], expected_error: Some(ContractError::IBCFeeDenomDiffersFromCoinReceived), }; @@ -710,6 +892,7 @@ struct Params { post_swap_action: Action::BankSend { to_address: "to_address".to_string(), }, + affiliates: vec![], expected_messages: vec![], expected_error: Some(ContractError::Skip(SwapOperationsCoinOutDenomMismatch)), }; @@ -749,6 +932,7 @@ struct Params { post_swap_action: Action::BankSend { to_address: "to_address".to_string(), }, + affiliates: vec![], expected_messages: vec![], expected_error: Some(ContractError::FeeSwapWithoutIbcTransfer), }; @@ -795,6 +979,7 @@ struct Params { .to_string(), }, }, + affiliates: vec![], expected_messages: vec![], expected_error: Some(ContractError::FeeSwapWithoutIbcFees), }; @@ -845,6 +1030,7 @@ struct Params { .to_string(), }, }, + affiliates: vec![], expected_messages: vec![], expected_error: Some(ContractError::Skip(IbcFeesNotOneCoin)), }; @@ -895,6 +1081,7 @@ struct Params { .to_string(), }, }, + affiliates: vec![], expected_messages: vec![], expected_error: Some(ContractError::Skip(IbcFeesNotOneCoin)), }; @@ -920,6 +1107,7 @@ struct Params { post_swap_action: Action::BankSend { to_address: "to_address".to_string(), }, + affiliates: vec![], expected_messages: vec![], expected_error: Some(ContractError::Payment(NoFunds{})), }; @@ -948,6 +1136,7 @@ struct Params { post_swap_action: Action::BankSend { to_address: "to_address".to_string(), }, + affiliates: vec![], expected_messages: vec![], expected_error: Some(ContractError::Payment(MultipleDenoms{})), }; @@ -969,6 +1158,7 @@ struct Params { post_swap_action: Action::BankSend { to_address: "to_address".to_string(), }, + affiliates: vec![], expected_messages: vec![], expected_error: Some(ContractError::Skip(SwapOperationsEmpty)), }; @@ -1013,6 +1203,7 @@ struct Params { .to_string(), }, }, + affiliates: vec![], expected_messages: vec![], expected_error: Some(ContractError::Skip(SwapOperationsEmpty)), }; @@ -1034,6 +1225,7 @@ struct Params { post_swap_action: Action::BankSend { to_address: "to_address".to_string(), }, + affiliates: vec![], expected_messages: vec![], expected_error: Some(ContractError::Timeout), }; @@ -1080,11 +1272,6 @@ fn test_execute_swap_and_action(params: Params) { ) .unwrap(); - // Create standardized params used across all tests - // The reason for these params to not need to be defined in each test case is because - // they have no direct impact on the test case - let affiliates: Vec = vec![]; - // Call execute_swap_and_action with the given test case params let res = skip_swap_entry_point::contract::execute( deps.as_mut(), @@ -1096,7 +1283,7 @@ fn test_execute_swap_and_action(params: Params) { min_coin: params.min_coin, timeout_timestamp: params.timeout_timestamp, post_swap_action: params.post_swap_action, - affiliates, + affiliates: params.affiliates, }, );