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

⚡️ Derive user swap coin in from remaining coin received only #31

Merged
Merged
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
10 changes: 0 additions & 10 deletions contracts/entry-point/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,6 @@ pub enum ContractError {
#[error("Fee Swap Coin Out Greater Than IBC Fee")]
FeeSwapCoinOutGreaterThanIbcFee,

/////////////////
/// USER SWAP ///
/////////////////

#[error("User Swap Coin In Denom Differs From Coin Sent To Contract")]
UserSwapCoinInDenomMismatch,

#[error("User Swap Coin In Amount Is Not Equal To The Remaining Coin Received")]
UserSwapCoinInNotEqualToRemainingReceived,

////////////////////////
/// POST SWAP ACTION ///
////////////////////////
Expand Down
25 changes: 2 additions & 23 deletions contracts/entry-point/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,31 +365,10 @@ fn verify_and_create_user_swap_msg(
remaining_coin_received: Coin,
min_coin_denom: &str,
) -> ContractResult<WasmMsg> {
// Set the user swap coin in to the remaining coin received if it is not provided
// Otherwise, use the provided user swap coin in, erroring if it doesn't pass validation
let user_swap_coin_in = match user_swap.coin_in.clone() {
Some(coin_in) => {
// Verify the coin_in denom is the same as the remaining coin received denom
if coin_in.denom != remaining_coin_received.denom {
return Err(ContractError::UserSwapCoinInDenomMismatch);
}

// Error if the coin_in amount is not the same as the remaining coin received amount
// If it's greater than it is attempting to swap more than is allowed
// If it's less than it would leave funds on the contract
if coin_in.amount != remaining_coin_received.amount {
return Err(ContractError::UserSwapCoinInNotEqualToRemainingReceived);
}

coin_in
}
None => remaining_coin_received,
};

// Validate swap operations
validate_swap_operations(
&user_swap.operations,
&user_swap_coin_in.denom,
&remaining_coin_received.denom,
min_coin_denom,
)?;

Expand All @@ -404,7 +383,7 @@ fn verify_and_create_user_swap_msg(
let user_swap_msg = WasmMsg::Execute {
contract_addr: user_swap_adapter_contract_address.to_string(),
msg: to_binary(&user_swap_msg_args)?,
funds: vec![user_swap_coin_in],
funds: vec![remaining_coin_received],
};

Ok(user_swap_msg)
Expand Down
135 changes: 2 additions & 133 deletions contracts/entry-point/tests/test_execute_swap_and_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ Expect Error
- Fee Swap Coin Out Greater Than Ibc Fee Requires

// User Swap
- User Swap Specified Coin More Than Remaining Coin Sent To Contract After Fee Swap
- User Swap Denom In Is Not The Same As Coin Sent To Contract
- User Swap Denom In Is Not The Same As First Swap Operation Denom In
- User Swap First Swap Operation Denom In Is Not The Same As Remaining Coin Received Denom
- User Swap Last Swap Operation Denom Out Is Not The Same As Min Coin Out Denom

// Invalid Coins Sent To Contract
Expand Down Expand Up @@ -69,7 +67,6 @@ struct Params {
fee_swap: None,
user_swap: SwapExactCoinIn {
swap_venue_name: "swap_venue_name".to_string(),
coin_in: None,
operations: vec![
SwapOperation {
pool: "pool".to_string(),
Expand Down Expand Up @@ -146,7 +143,6 @@ struct Params {
),
user_swap: SwapExactCoinIn {
swap_venue_name: "swap_venue_name".to_string(),
coin_in: None,
operations: vec![
SwapOperation {
pool: "pool_2".to_string(),
Expand Down Expand Up @@ -264,7 +260,6 @@ struct Params {
),
user_swap: SwapExactCoinIn {
swap_venue_name: "swap_venue_name".to_string(),
coin_in: Some(Coin::new(800_000, "osmo")),
operations: vec![
SwapOperation {
pool: "pool_2".to_string(),
Expand Down Expand Up @@ -361,93 +356,6 @@ struct Params {
expected_error: None,
};
"Fee Swap And User Swap Using Specified Coin")]
#[test_case(
Params {
info_funds: vec![
Coin::new(1_000_000, "osmo"),
],
fee_swap: Some(
SwapExactCoinOut {
swap_venue_name: "swap_venue_name".to_string(),
coin_out: Coin::new(200_000, "untrn"),
operations: vec![
SwapOperation {
pool: "pool".to_string(),
denom_in: "osmo".to_string(),
denom_out: "untrn".to_string(),
}
],
refund_address: None,
}
),
user_swap: SwapExactCoinIn {
swap_venue_name: "swap_venue_name".to_string(),
coin_in: Some(Coin::new(900_000, "osmo")),
operations: vec![
SwapOperation {
pool: "pool_2".to_string(),
denom_in: "osmo".to_string(),
denom_out: "uatom".to_string(),
}
],
},
min_coin: Coin::new(100_000, "uatom"),
timeout_timestamp: 101,
post_swap_action: Action::IbcTransfer {
ibc_info: IbcInfo {
source_channel: "channel-0".to_string(),
receiver: "receiver".to_string(),
memo: "".to_string(),
fee: IbcFee {
recv_fee: vec![],
ack_fee: vec![Coin::new(100_000, "untrn")],
timeout_fee: vec![Coin::new(100_000, "untrn")],
},
recover_address: "cosmos1xv9tklw7d82sezh9haa573wufgy59vmwe6xxe5"
.to_string(),
},
},
expected_messages: vec![],
expected_error: Some(ContractError::UserSwapCoinInNotEqualToRemainingReceived),
};
"User Swap Specified Coin More Than Remaining Coin Sent To Contract After Fee Swap - Expect Error")]
#[test_case(
Params {
info_funds: vec![
Coin::new(1_000_000, "untrn"),
],
fee_swap: None,
user_swap: SwapExactCoinIn {
swap_venue_name: "swap_venue_name".to_string(),
coin_in: Some(Coin::new(799_999, "untrn")),
operations: vec![
SwapOperation {
pool: "pool_2".to_string(),
denom_in: "untrn".to_string(),
denom_out: "uatom".to_string(),
}
],
},
min_coin: Coin::new(100_000, "uatom"),
timeout_timestamp: 101,
post_swap_action: Action::IbcTransfer {
ibc_info: IbcInfo {
source_channel: "channel-0".to_string(),
receiver: "receiver".to_string(),
memo: "".to_string(),
fee: IbcFee {
recv_fee: vec![],
ack_fee: vec![Coin::new(100_000, "untrn")],
timeout_fee: vec![Coin::new(100_000, "untrn")],
},
recover_address: "cosmos1xv9tklw7d82sezh9haa573wufgy59vmwe6xxe5"
.to_string(),
},
},
expected_messages: vec![],
expected_error: Some(ContractError::UserSwapCoinInNotEqualToRemainingReceived),
};
"User Swap Specified Coin Less Than Remaining Coin Left To Swap - Expect Error")]
#[test_case(
Params {
info_funds: vec![
Expand All @@ -469,7 +377,6 @@ struct Params {
),
user_swap: SwapExactCoinIn {
swap_venue_name: "swap_venue_name".to_string(),
coin_in: Some(Coin::new(100_000, "osmo")),
operations: vec![
SwapOperation {
pool: "pool_2".to_string(),
Expand Down Expand Up @@ -523,7 +430,6 @@ struct Params {
),
user_swap: SwapExactCoinIn {
swap_venue_name: "swap_venue_name".to_string(),
coin_in: Some(Coin::new(900_000, "osmo")),
operations: vec![
SwapOperation {
pool: "pool_2".to_string(),
Expand Down Expand Up @@ -573,7 +479,6 @@ struct Params {
),
user_swap: SwapExactCoinIn {
swap_venue_name: "swap_venue_name".to_string(),
coin_in: Some(Coin::new(900_000, "osmo")),
operations: vec![
SwapOperation {
pool: "pool_2".to_string(),
Expand Down Expand Up @@ -623,7 +528,6 @@ struct Params {
),
user_swap: SwapExactCoinIn {
swap_venue_name: "swap_venue_name".to_string(),
coin_in: Some(Coin::new(900_000, "osmo")),
operations: vec![
SwapOperation {
pool: "pool_2".to_string(),
Expand Down Expand Up @@ -673,7 +577,6 @@ struct Params {
),
user_swap: SwapExactCoinIn {
swap_venue_name: "swap_venue_name".to_string(),
coin_in: Some(Coin::new(799_999, "osmo")),
operations: vec![
SwapOperation {
pool: "pool_2".to_string(),
Expand Down Expand Up @@ -702,32 +605,6 @@ struct Params {
expected_error: Some(ContractError::FeeSwapCoinOutGreaterThanIbcFee),
};
"Fee Swap Coin Out Greater Than Ibc Fee Requires")]
#[test_case(
Params {
info_funds: vec![
Coin::new(1_000_000, "uatom"),
],
fee_swap: None,
user_swap: SwapExactCoinIn {
swap_venue_name: "swap_venue_name".to_string(),
coin_in: Some(Coin::new(900_000, "osmo")),
operations: vec![
SwapOperation {
pool: "pool_2".to_string(),
denom_in: "osmo".to_string(),
denom_out: "uatom".to_string(),
}
],
},
min_coin: Coin::new(100_000, "uatom"),
timeout_timestamp: 101,
post_swap_action: Action::BankSend {
to_address: "to_address".to_string(),
},
expected_messages: vec![],
expected_error: Some(ContractError::UserSwapCoinInDenomMismatch),
};
"User Swap Denom In Is Not The Same As Coin Sent To Contract - Expect Error")]
#[test_case(
Params {
info_funds: vec![
Expand All @@ -736,7 +613,6 @@ struct Params {
fee_swap: None,
user_swap: SwapExactCoinIn {
swap_venue_name: "swap_venue_name".to_string(),
coin_in: Some(Coin::new(1_000_000, "osmo")),
operations: vec![
SwapOperation {
pool: "pool_2".to_string(),
Expand All @@ -753,7 +629,7 @@ struct Params {
expected_messages: vec![],
expected_error: Some(ContractError::Skip(SwapOperationsCoinInDenomMismatch)),
};
"User Swap Denom In Is Not The Same As First Swap Operation Denom In - Expect Error")]
"User Swap First Swap Operation Denom In Is Not The Same As Remaining Coin Received Denom - Expect Error")]
#[test_case(
Params {
info_funds: vec![
Expand All @@ -762,7 +638,6 @@ struct Params {
fee_swap: None,
user_swap: SwapExactCoinIn {
swap_venue_name: "swap_venue_name".to_string(),
coin_in: Some(Coin::new(1_000_000, "osmo")),
operations: vec![
SwapOperation {
pool: "pool_2".to_string(),
Expand Down Expand Up @@ -801,7 +676,6 @@ struct Params {
),
user_swap: SwapExactCoinIn {
swap_venue_name: "swap_venue_name".to_string(),
coin_in: None,
operations: vec![
SwapOperation {
pool: "pool_2".to_string(),
Expand All @@ -825,7 +699,6 @@ struct Params {
fee_swap: None,
user_swap: SwapExactCoinIn {
swap_venue_name: "swap_venue_name".to_string(),
coin_in: None,
operations: vec![
SwapOperation {
pool: "pool".to_string(),
Expand All @@ -852,7 +725,6 @@ struct Params {
fee_swap: None,
user_swap: SwapExactCoinIn {
swap_venue_name: "swap_venue_name".to_string(),
coin_in: None,
operations: vec![
SwapOperation {
pool: "pool".to_string(),
Expand All @@ -878,7 +750,6 @@ struct Params {
fee_swap: None,
user_swap: SwapExactCoinIn {
swap_venue_name: "swap_venue_name".to_string(),
coin_in: None,
operations: vec![],
},
min_coin: Coin::new(1_000_000, "osmo"),
Expand All @@ -905,7 +776,6 @@ struct Params {
),
user_swap: SwapExactCoinIn {
swap_venue_name: "swap_venue_name".to_string(),
coin_in: Some(Coin::new(900_000, "osmo")),
operations: vec![
SwapOperation {
pool: "pool_2".to_string(),
Expand Down Expand Up @@ -942,7 +812,6 @@ struct Params {
fee_swap: None,
user_swap: SwapExactCoinIn {
swap_venue_name: "swap_venue_name".to_string(),
coin_in: None,
operations: vec![],
},
min_coin: Coin::new(1_000_000, "osmo"),
Expand Down
5 changes: 2 additions & 3 deletions packages/skip/src/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,11 @@ pub struct SwapExactCoinOut {
pub refund_address: Option<String>,
}

// Swap object that swaps the given coin in when present. When not present,
// swaps the remaining coin recevied from the contract call minus fee swap (if present)
// Swap object that swaps the remaining coin recevied
// from the contract call minus fee swap (if present)
#[cw_serde]
pub struct SwapExactCoinIn {
pub swap_venue_name: String,
pub coin_in: Option<Coin>,
pub operations: Vec<SwapOperation>,
}

Expand Down