From 9909f9f6fdbf6a5636f2573ffd45b102b01dcc27 Mon Sep 17 00:00:00 2001 From: Pacman Date: Thu, 17 Nov 2022 18:14:50 +0100 Subject: [PATCH] refactor: import asset helpers from apollo_utils --- Cargo.lock | 45 +++++++++++++++++++++++---------------------- Cargo.toml | 1 + src/contract.rs | 18 ++++++++---------- src/helpers.rs | 47 +++-------------------------------------------- 4 files changed, 35 insertions(+), 76 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4c3e473..e33aaae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -22,7 +22,7 @@ checksum = "216261ddc8289130e551ddcd5ce8a064710c0d064a4d2895c67151c92b5443f6" [[package]] name = "apollo-utils" version = "0.1.0" -source = "git+https://github.com/apollodao/apollo-utils.git#f042bb9b9bce31dc365e80db82d2ab6ad4356ff7" +source = "git+https://github.com/apollodao/apollo-utils.git#50643e9e738a2ccbfb709948247ba5820d15b2ab" dependencies = [ "cosmwasm-std", "cw-asset", @@ -120,11 +120,11 @@ checksum = "cec318a675afcb6a1ea1d4340e2d377e56e47c266f28043ceccbf4412ddfdd3b" [[package]] name = "cosmwasm-crypto" -version = "1.1.5" +version = "1.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28376836c7677e1ea6d6656a754582e88b91e544ce22fae42956d5fe5549a958" +checksum = "b1fc6d95cc171e56882d3e90689be1b38fecd31a4e6981129fc4973409de18b9" dependencies = [ - "digest 0.10.5", + "digest 0.10.6", "ed25519-zebra", "k256", "rand_core 0.6.4", @@ -133,18 +133,18 @@ dependencies = [ [[package]] name = "cosmwasm-derive" -version = "1.1.5" +version = "1.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8eb69f4f7a8a4bce68c8fbd3646238fede1e77056e4ea31c5b6bfc37b709eec3" +checksum = "a552716cf87ad173cd6b593fd72bf24fab490e0e5420aa75a227d6817d06b5df" dependencies = [ "syn", ] [[package]] name = "cosmwasm-schema" -version = "1.1.5" +version = "1.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a227cfeb9a7152b26a354b1c990e930e962f75fd68f57ab5ae2ef888c8524292" +checksum = "98c025d629589ca5d43fb8ff06decc387a4d01ead14f6b505300777fc6f01e1d" dependencies = [ "cosmwasm-schema-derive", "schemars", @@ -155,9 +155,9 @@ dependencies = [ [[package]] name = "cosmwasm-schema-derive" -version = "1.1.5" +version = "1.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3626cb42eef870de67f791e873711255325224d86f281bf628c42abd295f3a14" +checksum = "6f09a65d22861a24a1b30eab6aa6759333723629bc2cb568cd22569745e1dcb2" dependencies = [ "proc-macro2", "quote", @@ -166,9 +166,9 @@ dependencies = [ [[package]] name = "cosmwasm-std" -version = "1.1.5" +version = "1.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46bf9157d060abbc55152aeadcace799d03dc630575daa66604079a1206cb060" +checksum = "1240e76655f4b858b3fb94befbf48e253e92c6640d9d674a1491d15f10bc4d72" dependencies = [ "base64", "cosmwasm-crypto", @@ -185,9 +185,9 @@ dependencies = [ [[package]] name = "cosmwasm-storage" -version = "1.1.5" +version = "1.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b61fcfef87d15af0263e2e4d792af80355929674a3b4e29ffb3c898ec6e25852" +checksum = "b1e4ffcc2d75f4957ec4e7e9f059b45c5de4edfac8f1ec41df5a42b962ccb6b3" dependencies = [ "cosmwasm-std", "serde", @@ -246,7 +246,7 @@ dependencies = [ [[package]] name = "cw-asset" version = "0.3.0" -source = "git+https://github.com/apollodao/cw-asset.git#f2701959a48ecae0620d492de6debd2442460a41" +source = "git+https://github.com/apollodao/cw-asset.git#0f4eebc2f10688958c82dc219859dc204cf4076b" dependencies = [ "astroport", "cosmwasm-std", @@ -288,7 +288,7 @@ dependencies = [ [[package]] name = "cw-dex" version = "0.0.1" -source = "git+https://github.com/apollodao/cw-dex#f3855e5f999d678c1e950d9b64b829f8140b70c5" +source = "git+https://github.com/apollodao/cw-dex#0072c4eb0ffd1d06044ab6b8522b020490235834" dependencies = [ "apollo-utils", "astroport", @@ -320,6 +320,7 @@ dependencies = [ name = "cw-dex-router" version = "0.1.0" dependencies = [ + "apollo-utils", "cosmwasm-schema", "cosmwasm-std", "cosmwasm-storage", @@ -656,9 +657,9 @@ dependencies = [ [[package]] name = "digest" -version = "0.10.5" +version = "0.10.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adfbc57365a37acbd2ebf2b64d7e69bb766e2fea813521ed536f5d0520dcf86c" +checksum = "8168378f4e5023e7218c89c891c0fd8ecdb5e5e4f18cb78f38cf245dd021e76f" dependencies = [ "block-buffer 0.10.3", "crypto-common", @@ -713,7 +714,7 @@ dependencies = [ "base16ct", "crypto-bigint", "der", - "digest 0.10.5", + "digest 0.10.6", "ff", "generic-array", "group", @@ -793,7 +794,7 @@ version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" dependencies = [ - "digest 0.10.5", + "digest 0.10.6", ] [[package]] @@ -1161,7 +1162,7 @@ checksum = "82e6b795fe2e3b1e845bafcb27aa35405c4d47cdfc92af5fc8d3002f76cebdc0" dependencies = [ "cfg-if", "cpufeatures", - "digest 0.10.5", + "digest 0.10.6", ] [[package]] @@ -1170,7 +1171,7 @@ version = "1.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "74233d3b3b2f6d4b006dc19dee745e73e2a6bfb6f93607cd3b02bd5b00797d7c" dependencies = [ - "digest 0.10.5", + "digest 0.10.6", "rand_core 0.6.4", ] diff --git a/Cargo.toml b/Cargo.toml index f680e44..d1648ee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/contract.rs b/src/contract.rs index 678893b..600d979 100644 --- a/src/contract.rs +++ b/src/contract.rs @@ -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}; @@ -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 = vec![]; + let mut msgs: Vec = 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 @@ -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 @@ -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)] diff --git a/src/helpers.rs b/src/helpers.rs index 31c9b5c..2ab104d 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -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::{ @@ -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> { - 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> { - assets.into_iter().try_fold(vec![], |mut msgs, asset| { - msgs.append(&mut receive_asset(info, env, asset)?); - Ok(msgs) - }) -}