Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jl/add swap enum and refund action - STALE #25

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 56 additions & 45 deletions contracts/entry-point/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ use cosmwasm_std::{
use cw_utils::one_coin;
use skip::{
coins::Coins,
entry_point::{Affiliate, ExecuteMsg, PostSwapAction},
entry_point::{Action, Affiliate, ExecuteMsg},
ibc::{ExecuteMsg as IbcTransferExecuteMsg, IbcInfo, IbcTransfer},
swap::{
ExecuteMsg as SwapExecuteMsg, QueryMsg as SwapQueryMsg, SwapExactCoinIn, SwapExactCoinOut,
ExecuteMsg as SwapExecuteMsg, QueryMsg as SwapQueryMsg, Swap, SwapExactCoinIn,
SwapExactCoinOut,
},
};

Expand All @@ -27,10 +28,10 @@ pub fn execute_swap_and_action(
env: Env,
info: MessageInfo,
fee_swap: Option<SwapExactCoinOut>,
user_swap: SwapExactCoinIn,
user_swap: Swap,
min_coin: Coin,
timeout_timestamp: u64,
post_swap_action: PostSwapAction,
post_swap_action: Action,
affiliates: Vec<Affiliate>,
) -> ContractResult<Response> {
// Create a response object to return
Expand All @@ -47,43 +48,54 @@ pub fn execute_swap_and_action(
// Error if there is not exactly one coin sent to the contract
let mut remaining_coin_received = one_coin(&info)?;

// Get the ibc_info from the post swap action if the post swap action
// is an IBC transfer, otherwise set it to None
let ibc_fees = match &post_swap_action {
PostSwapAction::IbcTransfer { ibc_info } => ibc_info.fee.clone().try_into()?,
_ => Coins::new(),
};

// Process the fee swap if it exists
if let Some(fee_swap) = fee_swap {
// Create the fee swap message
// NOTE: this call mutates the user swap coin by subtracting the fee swap in amount
let fee_swap_msg = verify_and_create_fee_swap_msg(
&deps,
fee_swap,
&mut remaining_coin_received,
&ibc_fees,
)?;
// TODO: Remove panic and handle SwapExactCoinOut later once implemented
match user_swap {
Swap::SwapExactCoinIn(user_swap) => {
// Get the ibc_info from the post swap action if the post swap action
// is an IBC transfer, otherwise set it to None
let ibc_fees = match &post_swap_action {
Action::IbcTransfer { ibc_info } => ibc_info.fee.clone().try_into()?,
_ => Coins::new(),
};

// Process the fee swap if it exists
if let Some(fee_swap) = fee_swap {
// Create the fee swap message
// NOTE: this call mutates the user swap coin by subtracting the fee swap in amount
let fee_swap_msg = verify_and_create_fee_swap_msg(
&deps,
fee_swap,
&mut remaining_coin_received,
&ibc_fees,
)?;

// Add the fee swap message to the response
response = response
.add_message(fee_swap_msg)
.add_attribute("action", "dispatch_fee_swap");
} else {
// Deduct the amount of the remaining received coin's denomination that matches
// with the IBC fees from the remaining coin received amount
remaining_coin_received.amount = remaining_coin_received
.amount
.checked_sub(ibc_fees.get_amount(&remaining_coin_received.denom))?;
}

// Add the fee swap message to the response
response = response
.add_message(fee_swap_msg)
.add_attribute("action", "dispatch_fee_swap");
} else {
// Deduct the amount of the remaining received coin's denomination that matches
// with the IBC fees from the remaining coin received amount
remaining_coin_received.amount = remaining_coin_received
.amount
.checked_sub(ibc_fees.get_amount(&remaining_coin_received.denom))?;
}
// Create the user swap message
let user_swap_msg = verify_and_create_user_swap_msg(
&deps,
user_swap,
remaining_coin_received,
&min_coin.denom,
)?;

// Create the user swap message
let user_swap_msg = verify_and_create_user_swap_msg(
&deps,
user_swap,
remaining_coin_received,
&min_coin.denom,
)?;
// Add the user swap message to the response
response = response
.add_message(user_swap_msg)
.add_attribute("action", "dispatch_user_swap");
}
Swap::SwapExactCoinOut(_) => panic!("SwapExactCoinOut not allowed for user swap"),
};

// Create the transfer message
let post_swap_action_msg = WasmMsg::Execute {
Expand All @@ -99,9 +111,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
Expand All @@ -112,7 +123,7 @@ pub fn execute_post_swap_action(
info: MessageInfo,
min_coin: Coin,
timeout_timestamp: u64,
post_swap_action: PostSwapAction,
post_swap_action: Action,
affiliates: Vec<Affiliate>,
) -> ContractResult<Response> {
// Enforce the caller is the contract itself
Expand Down Expand Up @@ -176,7 +187,7 @@ pub fn execute_post_swap_action(
}

match post_swap_action {
PostSwapAction::BankSend { to_address } => {
Action::BankSend { to_address } => {
// Create the bank send message
let bank_send_msg =
verify_and_create_bank_send_msg(deps, to_address, transfer_out_coin)?;
Expand All @@ -186,7 +197,7 @@ pub fn execute_post_swap_action(
.add_message(bank_send_msg)
.add_attribute("action", "dispatch_post_swap_bank_send");
}
PostSwapAction::IbcTransfer { ibc_info } => {
Action::IbcTransfer { ibc_info } => {
// Enforce min out w/ ibc fees and create the IBC Transfer adapter contract call message
let ibc_transfer_adapter_msg = verify_and_create_ibc_transfer_adapter_msg(
deps,
Expand All @@ -201,7 +212,7 @@ pub fn execute_post_swap_action(
.add_message(ibc_transfer_adapter_msg)
.add_attribute("action", "dispatch_post_swap_ibc_transfer");
}
PostSwapAction::ContractCall {
Action::ContractCall {
contract_address,
msg,
} => {
Expand Down
32 changes: 16 additions & 16 deletions contracts/entry-point/tests/test_execute_post_swap_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use cosmwasm_std::{
SubMsg, Timestamp, Uint128, WasmMsg,
};
use skip::{
entry_point::{Affiliate, ExecuteMsg, PostSwapAction},
entry_point::{Action, Affiliate, ExecuteMsg},
ibc::{ExecuteMsg as IbcTransferExecuteMsg, IbcFee, IbcInfo},
};
use skip_swap_entry_point::{
Expand Down Expand Up @@ -46,7 +46,7 @@ Expect Error
struct Params {
caller: String,
min_coin: Coin,
post_swap_action: PostSwapAction,
post_swap_action: Action,
affiliates: Vec<Affiliate>,
expected_messages: Vec<SubMsg>,
expected_error: Option<ContractError>,
Expand All @@ -57,7 +57,7 @@ struct Params {
Params {
caller: "entry_point".to_string(),
min_coin: Coin::new(1_000_000, "osmo"),
post_swap_action: PostSwapAction::BankSend {
post_swap_action: Action::BankSend {
to_address: "cosmos1xv9tklw7d82sezh9haa573wufgy59vmwe6xxe5".to_string(),
},
affiliates: vec![],
Expand All @@ -78,7 +78,7 @@ struct Params {
Params {
caller: "entry_point".to_string(),
min_coin: Coin::new(1_000_000, "osmo"),
post_swap_action: PostSwapAction::IbcTransfer {
post_swap_action: Action::IbcTransfer {
ibc_info: IbcInfo {
source_channel: "channel-0".to_string(),
receiver: "receiver".to_string(),
Expand Down Expand Up @@ -127,7 +127,7 @@ struct Params {
Params {
caller: "entry_point".to_string(),
min_coin: Coin::new(1_000_000, "osmo"),
post_swap_action: PostSwapAction::ContractCall {
post_swap_action: Action::ContractCall {
contract_address: "contract_call".to_string(),
msg: to_binary(&"contract_call_msg").unwrap(),
},
Expand All @@ -151,7 +151,7 @@ struct Params {
Params {
caller: "entry_point".to_string(),
min_coin: Coin::new(1_000_000, "osmo"),
post_swap_action: PostSwapAction::IbcTransfer {
post_swap_action: Action::IbcTransfer {
ibc_info: IbcInfo {
source_channel: "channel-0".to_string(),
receiver: "receiver".to_string(),
Expand Down Expand Up @@ -203,7 +203,7 @@ struct Params {
Params {
caller: "entry_point".to_string(),
min_coin: Coin::new(800_000, "untrn"),
post_swap_action: PostSwapAction::IbcTransfer {
post_swap_action: Action::IbcTransfer {
ibc_info: IbcInfo {
source_channel: "channel-0".to_string(),
receiver: "receiver".to_string(),
Expand Down Expand Up @@ -252,7 +252,7 @@ struct Params {
Params {
caller: "entry_point".to_string(),
min_coin: Coin::new(900_000, "osmo"),
post_swap_action: PostSwapAction::BankSend {
post_swap_action: Action::BankSend {
to_address: "swapper".to_string(),
},
affiliates: vec![Affiliate {
Expand Down Expand Up @@ -288,7 +288,7 @@ struct Params {
Params {
caller: "entry_point".to_string(),
min_coin: Coin::new(900_000, "osmo"),
post_swap_action: PostSwapAction::ContractCall {
post_swap_action: Action::ContractCall {
contract_address: "contract_call".to_string(),
msg: to_binary(&"contract_call_msg").unwrap(),
},
Expand Down Expand Up @@ -326,7 +326,7 @@ struct Params {
Params {
caller: "entry_point".to_string(),
min_coin: Coin::new(900_000, "osmo"),
post_swap_action: PostSwapAction::IbcTransfer {
post_swap_action: Action::IbcTransfer {
ibc_info: IbcInfo {
source_channel: "channel-0".to_string(),
receiver: "receiver".to_string(),
Expand Down Expand Up @@ -388,7 +388,7 @@ struct Params {
Params {
caller: "entry_point".to_string(),
min_coin: Coin::new(700_000, "untrn"),
post_swap_action: PostSwapAction::IbcTransfer {
post_swap_action: Action::IbcTransfer {
ibc_info: IbcInfo {
source_channel: "channel-0".to_string(),
receiver: "receiver".to_string(),
Expand Down Expand Up @@ -450,7 +450,7 @@ struct Params {
Params {
caller: "entry_point".to_string(),
min_coin: Coin::new(950_000, "osmo"),
post_swap_action: PostSwapAction::IbcTransfer {
post_swap_action: Action::IbcTransfer {
ibc_info: IbcInfo {
source_channel: "channel-0".to_string(),
receiver: "receiver".to_string(),
Expand All @@ -475,7 +475,7 @@ struct Params {
Params {
caller: "entry_point".to_string(),
min_coin: Coin::new(900_000, "untrn"),
post_swap_action: PostSwapAction::IbcTransfer {
post_swap_action: Action::IbcTransfer {
ibc_info: IbcInfo {
source_channel: "channel-0".to_string(),
receiver: "receiver".to_string(),
Expand All @@ -497,7 +497,7 @@ struct Params {
Params {
caller: "entry_point".to_string(),
min_coin: Coin::new(1_100_000, "untrn"),
post_swap_action: PostSwapAction::BankSend {
post_swap_action: Action::BankSend {
to_address: "swapper".to_string(),
},
affiliates: vec![],
Expand All @@ -509,7 +509,7 @@ struct Params {
Params {
caller: "unauthorized".to_string(),
min_coin: Coin::new(1_100_000, "untrn"),
post_swap_action: PostSwapAction::BankSend {
post_swap_action: Action::BankSend {
to_address: "swapper".to_string(),
},
affiliates: vec![],
Expand All @@ -521,7 +521,7 @@ struct Params {
Params {
caller: "entry_point".to_string(),
min_coin: Coin::new(900_000, "untrn"),
post_swap_action: PostSwapAction::ContractCall {
post_swap_action: Action::ContractCall {
contract_address: "entry_point".to_string(),
msg: to_binary(&"contract_call_msg").unwrap(),
},
Expand Down
Loading