Skip to content

Commit

Permalink
Remove cw20 support
Browse files Browse the repository at this point in the history
  • Loading branch information
lubkoll committed Jun 24, 2024
1 parent 464d0e3 commit 5c9584c
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 88 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ cw-controllers = "1.0.1"
cw-dex = "0.5.3"
cw-storage-plus = "1.0.1"
cw2 = "1.0.1"
cw20 = "1.0.1"
thiserror = "1.0.38"
apollo-utils = "0.1.0"
cw-dex-osmosis = { version = "0.1.0", optional = true }
Expand Down
59 changes: 5 additions & 54 deletions src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@ use apollo_cw_asset::{Asset, AssetInfo, AssetInfoUnchecked};
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
from_json, to_json_binary, Addr, Binary, CosmosMsg, Deps, DepsMut, Env, Event, MessageInfo,
Order, Response, StdError, StdResult, Uint128,
to_json_binary, Addr, Binary, CosmosMsg, Deps, DepsMut, Env, Event, MessageInfo, Order,
Response, StdError, StdResult, Uint128,
};
use cw2::set_contract_version;
use cw20::Cw20ReceiveMsg;

use crate::error::ContractError;
use crate::helpers::receive_asset;
use crate::msg::{
BestPathForPairResponse, CallbackMsg, Cw20HookMsg, ExecuteMsg, InstantiateMsg, MigrateMsg,
QueryMsg,
BestPathForPairResponse, CallbackMsg, ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg,
};
use crate::operations::{SwapOperation, SwapOperationsList, SwapOperationsListUnchecked};
use crate::state::{ADMIN, PATHS};
Expand Down Expand Up @@ -41,7 +38,6 @@ pub fn execute(
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::Receive(msg) => receive_cw20(deps, env, info, msg),
ExecuteMsg::ExecuteSwapOperations {
operations,
offer_amount,
Expand All @@ -52,7 +48,6 @@ pub fn execute(
execute_swap_operations(
deps,
env,
info.clone(),
info.sender,
operations,
offer_amount,
Expand Down Expand Up @@ -104,40 +99,10 @@ pub fn execute(
}
}

pub fn receive_cw20(
deps: DepsMut,
env: Env,
info: MessageInfo,
cw20_msg: Cw20ReceiveMsg,
) -> Result<Response, ContractError> {
let sender = deps.api.addr_validate(&cw20_msg.sender)?;

match from_json(&cw20_msg.msg)? {
Cw20HookMsg::ExecuteSwapOperations {
operations,
minimum_receive,
to,
} => {
let operations = operations.check(deps.as_ref())?;
execute_swap_operations(
deps,
env,
info,
sender,
operations,
None,
minimum_receive,
to,
)
}
}
}

#[allow(clippy::too_many_arguments)]
pub fn execute_swap_operations(
deps: DepsMut,
env: Env,
info: MessageInfo,
sender: Addr,
operations: SwapOperationsList,
offer_amount: Option<Uint128>,
Expand All @@ -150,24 +115,10 @@ pub fn execute_swap_operations(
let target_asset_info = operations.to();
let offer_asset_info = operations.from();

// 1. Validate sent asset. We only do this if the passed in optional
// `offer_amount` 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![];
if let Some(offer_amount) = offer_amount {
msgs.extend(receive_asset(
&info,
&env,
&Asset::new(offer_asset_info.clone(), offer_amount),
)?);
};

// 2. Loop and execute swap operations
// Loop and execute swap operations
let mut msgs: Vec<CosmosMsg> = operations.into_execute_msgs(&env, recipient.clone())?;

// 3. Assert min receive
// Assert min receive
if let Some(minimum_receive) = minimum_receive {
let recipient_balance =
target_asset_info.query_balance(&deps.querier, recipient.clone())?;
Expand Down
32 changes: 2 additions & 30 deletions src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::vec;

use apollo_cw_asset::{Asset, AssetInfo, AssetList};
use apollo_cw_asset::{Asset, AssetInfo};
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{
to_json_binary, Addr, Api, Coin, CosmosMsg, Env, MessageInfo, QuerierWrapper, QueryRequest,
to_json_binary, Addr, Api, Coin, CosmosMsg, MessageInfo, QuerierWrapper, QueryRequest,
StdError, StdResult, Uint128, WasmMsg, WasmQuery,
};

Expand Down Expand Up @@ -178,31 +178,3 @@ pub fn assert_native_token_received(info: &MessageInfo, asset: &Asset) -> StdRes
}
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)
})
}
2 changes: 0 additions & 2 deletions src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use apollo_cw_asset::{Asset, AssetInfo, AssetInfoUnchecked};
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{wasm_execute, Addr, CosmosMsg, Empty, Env, Uint128};
use cw20::Cw20ReceiveMsg;

use crate::operations::{SwapOperation, SwapOperationsListUnchecked};
use crate::ContractError;
Expand All @@ -10,7 +9,6 @@ pub type InstantiateMsg = Empty;

#[cw_serde]
pub enum ExecuteMsg {
Receive(Cw20ReceiveMsg),
ExecuteSwapOperations {
operations: SwapOperationsListUnchecked,
/// Optional because we only need the information if the user wants to
Expand Down

0 comments on commit 5c9584c

Please sign in to comment.