From fa6f6935dc74d8ad37a2520fa0418e451d49267f Mon Sep 17 00:00:00 2001 From: Jeremy Liu <31809888+NotJeremyLiu@users.noreply.github.com> Date: Wed, 26 Jul 2023 18:06:55 -0400 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Replace=20custom=20impl=20?= =?UTF-8?q?of=20Coins=20with=20cw=5Fstd=20Coins?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contracts/entry-point/src/execute.rs | 14 ++-- .../osmosis/ibc-transfer/src/contract.rs | 5 +- packages/skip/src/coins.rs | 74 ------------------- packages/skip/src/ibc.rs | 18 ++++- packages/skip/src/lib.rs | 1 - 5 files changed, 26 insertions(+), 86 deletions(-) delete mode 100644 packages/skip/src/coins.rs diff --git a/contracts/entry-point/src/execute.rs b/contracts/entry-point/src/execute.rs index 63e39959..5cd5a8cf 100644 --- a/contracts/entry-point/src/execute.rs +++ b/contracts/entry-point/src/execute.rs @@ -3,11 +3,11 @@ use crate::{ state::{BLOCKED_CONTRACT_ADDRESSES, IBC_TRANSFER_CONTRACT_ADDRESS, SWAP_VENUE_MAP}, }; use cosmwasm_std::{ - to_binary, Addr, BankMsg, Binary, Coin, DepsMut, Env, MessageInfo, Response, Uint128, WasmMsg, + to_binary, Addr, BankMsg, Binary, Coin, Coins, DepsMut, Env, MessageInfo, Response, Uint128, + WasmMsg, }; use cw_utils::one_coin; use skip::{ - coins::Coins, entry_point::{Action, Affiliate, ExecuteMsg}, ibc::{ExecuteMsg as IbcTransferExecuteMsg, IbcInfo, IbcTransfer}, swap::{ @@ -52,7 +52,7 @@ pub fn execute_swap_and_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(), + _ => Coins::default(), }; // Process the fee swap if it exists @@ -75,7 +75,7 @@ pub fn execute_swap_and_action( // 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))?; + .checked_sub(ibc_fees.amount_of(&remaining_coin_received.denom))?; } // Create the user swap message @@ -288,7 +288,7 @@ fn verify_and_create_ibc_transfer_adapter_msg( // Get the amount of the IBC fee payment that matches // the denom of the ibc transfer out coin. // If there is no denom match, then default to zero. - let transfer_out_coin_ibc_fee_amount = ibc_fees_map.get_amount(&min_coin.denom); + let transfer_out_coin_ibc_fee_amount = ibc_fees_map.amount_of(&min_coin.denom); // Subtract the IBC fee amount from the transfer out coin transfer_out_coin.amount = transfer_out_coin @@ -305,7 +305,7 @@ fn verify_and_create_ibc_transfer_adapter_msg( // (which is the transfer out coin plus the IBC fee amounts) // using a map for convenience, and then converting to a vector of coins let mut ibc_msg_funds_map = ibc_fees_map; - ibc_msg_funds_map.add_coin(&transfer_out_coin)?; + ibc_msg_funds_map.add(transfer_out_coin.clone())?; // Convert the map to a vector of coins let ibc_msg_funds: Vec = ibc_msg_funds_map.into(); @@ -410,7 +410,7 @@ fn verify_and_create_fee_swap_msg( )?; // Verify the fee swap coin out amount less than or equal to the ibc fee amount - if fee_swap.coin_out.amount > ibc_fees.get_amount(&fee_swap.coin_out.denom) { + if fee_swap.coin_out.amount > ibc_fees.amount_of(&fee_swap.coin_out.denom) { return Err(ContractError::FeeSwapCoinOutGreaterThanIbcFee); } diff --git a/contracts/networks/osmosis/ibc-transfer/src/contract.rs b/contracts/networks/osmosis/ibc-transfer/src/contract.rs index c0930d39..48027264 100644 --- a/contracts/networks/osmosis/ibc-transfer/src/contract.rs +++ b/contracts/networks/osmosis/ibc-transfer/src/contract.rs @@ -6,14 +6,13 @@ use crate::{ }, }; use cosmwasm_std::{ - entry_point, to_binary, BankMsg, Binary, Coin, CosmosMsg, Deps, DepsMut, Env, MessageInfo, - Reply, Response, SubMsg, SubMsgResult, + entry_point, to_binary, BankMsg, Binary, Coin, Coins, CosmosMsg, Deps, DepsMut, Env, + MessageInfo, Reply, Response, SubMsg, SubMsgResult, }; use ibc_proto::ibc::applications::transfer::v1::{MsgTransfer, MsgTransferResponse}; use prost::Message; use serde_cw_value::Value; use skip::{ - coins::Coins, ibc::{ AckID, ExecuteMsg, IbcFee, IbcInfo, IbcLifecycleComplete, InstantiateMsg, OsmosisQueryMsg as QueryMsg, diff --git a/packages/skip/src/coins.rs b/packages/skip/src/coins.rs deleted file mode 100644 index 3aeb7e6d..00000000 --- a/packages/skip/src/coins.rs +++ /dev/null @@ -1,74 +0,0 @@ -use crate::ibc::IbcFee; -use cosmwasm_std::{Coin, OverflowError, Uint128}; -use std::collections::BTreeMap; - -// Coins is a struct that wraps a BTreeMap of String denom to Uint128 total amount -pub struct Coins(BTreeMap); - -// Implement add coin and get amount methods for Coins struct -impl Coins { - // Create a new Coins struct - pub fn new() -> Self { - Coins(BTreeMap::new()) - } - - // Takes a coin and adds it to the Coins map - pub fn add_coin(&mut self, coin: &Coin) -> Result<(), OverflowError> { - // Do not allow zero amount coin to create a new entry - if coin.amount.is_zero() { - return Ok(()); - } - - let amount = self - .0 - .entry(coin.denom.clone()) - .or_insert_with(Uint128::zero); - *amount = amount.checked_add(coin.amount)?; - - Ok(()) - } - - // Given a denom, returns the total amount of that denom in the Coins struct - // or returns 0 if the denom is not in the Coins struct. - pub fn get_amount(&self, denom: &str) -> Uint128 { - self.0.get(denom).cloned().unwrap_or_default() - } - - // Returns true if Coins map is empty - pub fn is_empty(&self) -> bool { - self.0.is_empty() - } -} - -impl Default for Coins { - fn default() -> Self { - Self::new() - } -} - -// Converts a Coins struct to a Vec -impl From for Vec { - fn from(coins: Coins) -> Self { - coins - .0 - .into_iter() - .map(|(denom, amount)| Coin { denom, amount }) - .collect() - } -} - -// Converts an IbcFee struct to a Coins struct -impl TryFrom for Coins { - type Error = OverflowError; - - fn try_from(ibc_fee: IbcFee) -> Result { - let mut ibc_fees = Coins(BTreeMap::new()); - - [ibc_fee.recv_fee, ibc_fee.ack_fee, ibc_fee.timeout_fee] - .iter() - .flatten() - .try_for_each(|coin| ibc_fees.add_coin(coin))?; - - Ok(ibc_fees) - } -} diff --git a/packages/skip/src/ibc.rs b/packages/skip/src/ibc.rs index d0c7c8c1..6f6875f0 100644 --- a/packages/skip/src/ibc.rs +++ b/packages/skip/src/ibc.rs @@ -1,6 +1,6 @@ use crate::proto_coin::ProtoCoin; use cosmwasm_schema::{cw_serde, QueryResponses}; -use cosmwasm_std::Coin; +use cosmwasm_std::{Coin, Coins, StdError}; use neutron_proto::neutron::feerefunder::Fee as NeutronFee; use std::convert::From; @@ -84,6 +84,22 @@ impl From for NeutronFee { } } +// Converts an IbcFee struct to a Coins struct +impl TryFrom for Coins { + type Error = StdError; + + fn try_from(ibc_fee: IbcFee) -> Result { + let mut ibc_fees = Coins::default(); + + [ibc_fee.recv_fee, ibc_fee.ack_fee, ibc_fee.timeout_fee] + .into_iter() + .flatten() + .try_for_each(|coin| ibc_fees.add(coin))?; + + Ok(ibc_fees) + } +} + #[cw_serde] pub struct IbcInfo { pub source_channel: String, diff --git a/packages/skip/src/lib.rs b/packages/skip/src/lib.rs index cdc245cf..fadebe8e 100644 --- a/packages/skip/src/lib.rs +++ b/packages/skip/src/lib.rs @@ -1,4 +1,3 @@ -pub mod coins; pub mod entry_point; pub mod error; pub mod ibc;