From a5e4c49050ed76cbc5636ea94adf77827a653ba6 Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Fri, 1 Dec 2023 10:23:58 +0700 Subject: [PATCH] refactor: remove add supported tool exec Msg --- src/contract.rs | 168 +++++++-------------------------------- src/helpers.rs | 3 +- src/integration_tests.rs | 14 ---- src/msg.rs | 22 +---- 4 files changed, 32 insertions(+), 175 deletions(-) diff --git a/src/contract.rs b/src/contract.rs index 4211ece..551ac6c 100644 --- a/src/contract.rs +++ b/src/contract.rs @@ -12,7 +12,6 @@ use astroport::asset::AssetInfo; use astroport::pair::{self}; use cosmwasm_std::{Addr, Order, StdError, WasmMsg}; -use self::query::*; // version info for migration info const CONTRACT_NAME: &str = "crates.io:lq-express-sm"; const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -44,11 +43,6 @@ pub fn execute( ) -> Result { match msg { ExecuteMsg::Astro { pair_address } => execute::astro_exec(deps, info, pair_address), - ExecuteMsg::AddSupportedPool { - pool_address, - token_1, - token_2, - } => execute::add_supported_pool(deps, info, pool_address, token_1, token_2), ExecuteMsg::MySwap { pool_address } => execute::swap(deps, info, pool_address), } } @@ -100,14 +94,10 @@ fn handle_swap_reply(_deps: DepsMut, _env: Env, msg: Reply) -> StdResult StdResult { - match msg { - QueryMsg::GetPair { pool_address } => to_json_binary(&query_pair(deps, env, pool_address)?), - QueryMsg::GetPoolAddr { token_1, token_2 } => { - to_json_binary(&query_pool_addr(deps, env, token_1, token_2)?) - } - } +pub fn query(_deps: Deps, _env: Env, msg: QueryMsg) -> StdResult { + match msg {} } -pub mod query { - use crate::contract::*; - use crate::msg::{GetPairResponse, GetPoolAddrResponse}; - use crate::state::POOL_INFO; - use cosmwasm_std::StdResult; - - pub fn query_pair(deps: Deps, _env: Env, pool_address: String) -> StdResult { - let pair: Vec<_> = POOL_INFO - .idx - .address - .prefix(pool_address) - .range(deps.storage, None, None, Order::Ascending) - .flatten() - .collect(); - if pair.is_empty() { - return Err(StdError::GenericErr { - msg: "pool address not found".to_string(), - }); - } - let (token_1, token_2): (String, String) = pair - .first() - .iter() - .map(|pair| (pair.1.token_1.clone(), pair.1.token_2.clone())) - .unzip(); - let resp = GetPairResponse { token_1, token_2 }; - Ok(resp) - } - pub fn query_pool_addr( - deps: Deps, - _env: Env, - token_1: String, - token_2: String, - ) -> StdResult { - let token_1 = token_1.to_lowercase(); - let token_2 = token_2.to_lowercase(); - - let pools = POOL_INFO - .idx - .pair - .prefix((token_1, token_2)) - .range(deps.storage, None, None, Order::Ascending) - .flatten() - .collect::>(); - - if pools.is_empty() { - return Err(StdError::GenericErr { - msg: "No pool exist for this pair yet".to_string(), - }); - } - let pool_addresses = pools - .iter() - .map(|pool_info| pool_info.1.address.to_string().clone()) - .collect::>(); - Ok(GetPoolAddrResponse { pool_addresses }) - } -} - -#[cfg(test)] -mod tests { - use crate::msg::GetPairResponse; - - use super::*; - use cosmwasm_std::Addr; - use cw_multi_test::{App, ContractWrapper, Executor}; - - #[test] - fn test_add_pool() { - let mut app = App::default(); - let owner = Addr::unchecked("owner"); - let code = ContractWrapper::new(execute, instantiate, query); - let code_id = app.store_code(Box::new(code)); - - let addr = app - .instantiate_contract( - code_id, - owner.clone(), - &InstantiateMsg {}, - &[], - "Contract", - None, - ) - .unwrap(); - let msg = ExecuteMsg::AddSupportedPool { - pool_address: "pool1".to_string(), - token_1: "inj".to_string(), - token_2: "atom".to_string(), - }; - app.execute_contract(owner.clone(), addr.clone(), &msg, &[]) - .unwrap(); - - app.update_block(|b| b.height += 1); - - let resp: GetPairResponse = app - .wrap() - .query_wasm_smart( - addr.clone(), - &QueryMsg::GetPair { - pool_address: "pool1".to_string(), - }, - ) - .unwrap(); - assert_eq!( - resp, - GetPairResponse { - token_1: "inj".to_string(), - token_2: "atom".to_string() - } - ) - } -} +// pub mod query { +// use crate::contract::*; +// use crate::msg::{GetPairResponse, GetPoolAddrResponse}; +// use crate::state::POOL_INFO; +// use cosmwasm_std::StdResult; +// } + +// #[cfg(test)] +// mod tests { +// use crate::msg::GetPairResponse; + +// use super::*; +// use cosmwasm_std::Addr; +// use cw_multi_test::{App, ContractWrapper, Executor}; +// } diff --git a/src/helpers.rs b/src/helpers.rs index b3045a5..3f89a13 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -1,9 +1,8 @@ -use astroport::{pair::PoolResponse, querier}; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use crate::msg::ExecuteMsg; -use cosmwasm_std::{to_json_binary, Addr, CosmosMsg, QuerierWrapper, StdResult, WasmMsg}; +use cosmwasm_std::{to_json_binary, Addr, CosmosMsg, StdResult, WasmMsg}; /// CwTemplateContract is a wrapper around Addr that provides a lot of helpers /// for working with this. diff --git a/src/integration_tests.rs b/src/integration_tests.rs index 23510f8..a4720b8 100644 --- a/src/integration_tests.rs +++ b/src/integration_tests.rs @@ -2,7 +2,6 @@ #[cfg(test)] mod tests { use crate::helpers::CwTemplateContract; - use crate::msg::ExecuteMsg; use crate::msg::InstantiateMsg; use astroport::asset::{Asset, AssetInfo, PairInfo}; use astroport::factory::{InstantiateMsg as FactoryInitMsg, PairConfig, PairType}; @@ -319,19 +318,6 @@ mod tests { let ttt_amount = Uint128::new(1_000_000_000000); let inj_offer = Uint128::new(1_000000); - // Add supported pool - app.execute_contract( - owner.clone(), - my_contract.addr(), - &ExecuteMsg::AddSupportedPool { - pool_address: pair_contract.addr().to_string(), - token_1: "inj".into(), - token_2: "ttt".into(), - }, - &[], - ) - .unwrap(); - let (msg, coins) = provide_liquidity_msg(ttt_amount, inj_amount, None, None, &token_contract); diff --git a/src/msg.rs b/src/msg.rs index f1f7d40..3e8289b 100644 --- a/src/msg.rs +++ b/src/msg.rs @@ -1,4 +1,3 @@ -use astroport::asset::AssetInfo; use cosmwasm_schema::{cw_serde, QueryResponses}; use cosmwasm_std::Addr; @@ -7,28 +6,13 @@ pub struct InstantiateMsg {} #[cw_serde] pub enum ExecuteMsg { - Astro { - pair_address: String, - }, - AddSupportedPool { - pool_address: String, - token_1: String, - token_2: String, - }, - MySwap { - pool_address: Addr, - }, + Astro { pair_address: String }, + MySwap { pool_address: Addr }, } #[cw_serde] #[derive(QueryResponses)] -pub enum QueryMsg { - // GetCount returns the current count as a json-encoded number - #[returns(GetPairResponse)] - GetPair { pool_address: String }, - #[returns(GetPoolAddrResponse)] - GetPoolAddr { token_1: String, token_2: String }, -} +pub enum QueryMsg {} //We define a custom struct for each query response #[cw_serde]