Skip to content

Commit

Permalink
refactor: import asset helpers from apollo_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
pacmanifold committed Nov 17, 2022
1 parent 4cf634d commit 9909f9f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 76 deletions.
45 changes: 23 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ cw20 = "0.16"
schemars = "0.8.10"
serde = {version = "1.0.144", default-features = false, features = ["derive"]}
thiserror = {version = "1.0.31"}
apollo-utils = { git = "https://github.com/apollodao/apollo-utils.git" }

[dev-dependencies]
cw-multi-test = "0.16.0"
18 changes: 8 additions & 10 deletions src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use apollo_utils::assets::{receive_asset, receive_assets};
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
from_binary, to_binary, Addr, Binary, CosmosMsg, Deps, DepsMut, Env, MessageInfo, Order,
Response, StdResult, Uint128,
Response, StdResult, SubMsg, Uint128,
};
use cw2::set_contract_version;
use cw20::Cw20ReceiveMsg;
use cw_asset::{Asset, AssetInfo, AssetInfoUnchecked, AssetList, AssetListUnchecked};

use crate::error::ContractError;
use crate::helpers::{receive_asset, receive_assets};
use crate::msg::{CallbackMsg, Cw20HookMsg, ExecuteMsg, InstantiateMsg, QueryMsg};
use crate::operations::{SwapOperation, SwapOperationsList, SwapOperationsListUnchecked};
use crate::state::{ADMIN, PATHS};
Expand Down Expand Up @@ -161,13 +161,11 @@ pub fn execute_swap_operations(
// and in this case we do transfer from on it, given that the offer asset is
// a CW20. Otherwise we assume the caller already sent funds and in the first
// call of execute_swap_operation, we just use the whole contracts balance.
let mut msgs: Vec<CosmosMsg> = vec![];
let mut msgs: Vec<SubMsg> = vec![];
if let Some(offer_amount) = offer_amount {
msgs.extend(receive_asset(
&info,
&env,
&Asset::new(offer_asset_info, offer_amount),
)?);
msgs.extend(
receive_asset(&info, &env, &Asset::new(offer_asset_info, offer_amount))?.messages,
);
};

//2. Loop and execute swap operations
Expand Down Expand Up @@ -247,7 +245,7 @@ pub fn basket_liquidate(
let recipient = to.map_or(Ok(info.sender.clone()), |x| deps.api.addr_validate(&x))?;

// 1. Assert offer_assets are sent or do TransferFrom on Cw20s
let receive_msgs = receive_assets(&info, &env, &offer_assets)?;
let receive_res = receive_assets(&info, &env, &offer_assets)?;

// 2. Loop over offer assets and for each:
// Fetch path and call ExecuteMsg::ExecuteSwapOperations
Expand Down Expand Up @@ -278,7 +276,7 @@ pub fn basket_liquidate(
);
}

Ok(Response::new().add_messages(receive_msgs))
Ok(receive_res)
}

#[cfg_attr(not(feature = "library"), entry_point)]
Expand Down
47 changes: 3 additions & 44 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use std::vec;

use cosmwasm_schema::cw_serde;
use cw20::{Cw20Coin, Cw20ExecuteMsg};
use cw_asset::{Asset, AssetInfo, AssetInfoBase, AssetList};
use cw_asset::{AssetInfo, AssetInfoBase, AssetList};

use cosmwasm_std::{
to_binary, Addr, Api, Coin, CosmosMsg, Env, MessageInfo, QuerierWrapper, QueryRequest,
StdError, StdResult, Uint128, WasmMsg, WasmQuery,
to_binary, Addr, Api, Coin, CosmosMsg, QuerierWrapper, QueryRequest, StdResult, Uint128,
WasmMsg, WasmQuery,
};

use crate::{
Expand Down Expand Up @@ -217,44 +217,3 @@ impl CwDexRouter {
}))
}
}

/// Assert that a specific native token in the form of an `Asset` was sent to the contract.
pub fn assert_native_token_received(info: &MessageInfo, asset: &Asset) -> StdResult<()> {
let coin: Coin = asset.try_into()?;

if !info.funds.contains(&coin) {
return Err(StdError::generic_err(format!(
"Assert native token receive failed for asset: {}",
asset
)));
}
Ok(())
}

/// Calls TransferFrom on an Asset if it is a Cw20. If it is a native we just
/// assert that the native token was already sent to the contract.
pub fn receive_asset(info: &MessageInfo, env: &Env, asset: &Asset) -> StdResult<Vec<CosmosMsg>> {
match &asset.info {
AssetInfo::Cw20(_coin) => {
let msg =
asset.transfer_from_msg(info.sender.clone(), env.contract.address.to_string())?;
Ok(vec![msg])
}
AssetInfo::Native(_token) => {
//Here we just assert that the native token was sent with the contract call
assert_native_token_received(info, asset)?;
Ok(vec![])
}
}
}

pub fn receive_assets(
info: &MessageInfo,
env: &Env,
assets: &AssetList,
) -> StdResult<Vec<CosmosMsg>> {
assets.into_iter().try_fold(vec![], |mut msgs, asset| {
msgs.append(&mut receive_asset(info, env, asset)?);
Ok(msgs)
})
}

0 comments on commit 9909f9f

Please sign in to comment.