From 539a941ad4d6c5623db4958b4c9fbc84675bc46f Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 23 Jun 2021 14:27:38 +0200 Subject: [PATCH 1/6] Remove AssetRaw variants, tfi compiles --- packages/tfi/schema/asset.json | 24 +---- packages/tfi/schema/asset_info.json | 24 +---- packages/tfi/schema/pair_info.json | 24 +---- packages/tfi/src/asset.rs | 160 +++++----------------------- packages/tfi/src/testing.rs | 34 ++---- 5 files changed, 46 insertions(+), 220 deletions(-) diff --git a/packages/tfi/schema/asset.json b/packages/tfi/schema/asset.json index b686aa7..5041520 100644 --- a/packages/tfi/schema/asset.json +++ b/packages/tfi/schema/asset.json @@ -29,15 +29,7 @@ ], "properties": { "token": { - "type": "object", - "required": [ - "contract_addr" - ], - "properties": { - "contract_addr": { - "$ref": "#/definitions/Addr" - } - } + "$ref": "#/definitions/Addr" } }, "additionalProperties": false @@ -45,19 +37,11 @@ { "type": "object", "required": [ - "native_token" + "native" ], "properties": { - "native_token": { - "type": "object", - "required": [ - "denom" - ], - "properties": { - "denom": { - "type": "string" - } - } + "native": { + "type": "string" } }, "additionalProperties": false diff --git a/packages/tfi/schema/asset_info.json b/packages/tfi/schema/asset_info.json index 4ac5d0e..1ae63d0 100644 --- a/packages/tfi/schema/asset_info.json +++ b/packages/tfi/schema/asset_info.json @@ -10,15 +10,7 @@ ], "properties": { "token": { - "type": "object", - "required": [ - "contract_addr" - ], - "properties": { - "contract_addr": { - "$ref": "#/definitions/Addr" - } - } + "$ref": "#/definitions/Addr" } }, "additionalProperties": false @@ -26,19 +18,11 @@ { "type": "object", "required": [ - "native_token" + "native" ], "properties": { - "native_token": { - "type": "object", - "required": [ - "denom" - ], - "properties": { - "denom": { - "type": "string" - } - } + "native": { + "type": "string" } }, "additionalProperties": false diff --git a/packages/tfi/schema/pair_info.json b/packages/tfi/schema/pair_info.json index e9b4770..56a81b3 100644 --- a/packages/tfi/schema/pair_info.json +++ b/packages/tfi/schema/pair_info.json @@ -38,15 +38,7 @@ ], "properties": { "token": { - "type": "object", - "required": [ - "contract_addr" - ], - "properties": { - "contract_addr": { - "$ref": "#/definitions/Addr" - } - } + "$ref": "#/definitions/Addr" } }, "additionalProperties": false @@ -54,19 +46,11 @@ { "type": "object", "required": [ - "native_token" + "native" ], "properties": { - "native_token": { - "type": "object", - "required": [ - "denom" - ], - "properties": { - "denom": { - "type": "string" - } - } + "native": { + "type": "string" } }, "additionalProperties": false diff --git a/packages/tfi/src/asset.rs b/packages/tfi/src/asset.rs index 94d2a72..d23ce1b 100644 --- a/packages/tfi/src/asset.rs +++ b/packages/tfi/src/asset.rs @@ -4,8 +4,8 @@ use std::fmt; use crate::querier::{query_balance, query_token_balance}; use cosmwasm_std::{ - to_binary, Addr, Api, BankMsg, CanonicalAddr, Coin, CosmosMsg, MessageInfo, QuerierWrapper, - StdError, StdResult, Uint128, WasmMsg, + to_binary, Addr, BankMsg, Coin, CosmosMsg, MessageInfo, QuerierWrapper, StdError, StdResult, + Uint128, WasmMsg, }; use cw20::Cw20ExecuteMsg; @@ -30,7 +30,7 @@ impl Asset { let amount = self.amount; match &self.info { - AssetInfo::Token { contract_addr } => Ok(CosmosMsg::Wasm(WasmMsg::Execute { + AssetInfo::Token(contract_addr) => Ok(CosmosMsg::Wasm(WasmMsg::Execute { contract_addr: contract_addr.to_string(), msg: to_binary(&Cw20ExecuteMsg::Transfer { recipient: recipient.to_string(), @@ -38,7 +38,7 @@ impl Asset { })?, send: vec![], })), - AssetInfo::NativeToken { .. } => Ok(CosmosMsg::Bank(BankMsg::Send { + AssetInfo::Native(_) => Ok(CosmosMsg::Bank(BankMsg::Send { to_address: recipient.to_string(), amount: vec![self.deduct_tax()?], })), @@ -48,7 +48,7 @@ impl Asset { // TODO: rename this pub fn deduct_tax(&self) -> StdResult { let amount = self.amount; - if let AssetInfo::NativeToken { denom } = &self.info { + if let AssetInfo::Native(denom) = &self.info { Ok(Coin { denom: denom.clone(), amount, @@ -59,7 +59,7 @@ impl Asset { } pub fn assert_sent_native_token_balance(&self, message_info: &MessageInfo) -> StdResult<()> { - if let AssetInfo::NativeToken { denom } = &self.info { + if let AssetInfo::Native(denom) = &self.info { match message_info.funds.iter().find(|x| x.denom == *denom) { Some(coin) => { if self.amount == coin.amount { @@ -80,20 +80,6 @@ impl Asset { Ok(()) } } - - pub fn to_raw(&self, api: &dyn Api) -> StdResult { - Ok(AssetRaw { - info: match &self.info { - AssetInfo::NativeToken { denom } => AssetInfoRaw::NativeToken { - denom: denom.to_string(), - }, - AssetInfo::Token { contract_addr } => AssetInfoRaw::Token { - contract_addr: api.addr_canonicalize(contract_addr.as_str())?, - }, - }, - amount: self.amount, - }) - } } /// AssetInfo contract_addr is usually passed from the cw20 hook @@ -101,131 +87,56 @@ impl Asset { #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] #[serde(rename_all = "snake_case")] pub enum AssetInfo { - Token { contract_addr: Addr }, - NativeToken { denom: String }, + Token(Addr), + Native(String), } impl fmt::Display for AssetInfo { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - AssetInfo::NativeToken { denom } => write!(f, "{}", denom), - AssetInfo::Token { contract_addr } => write!(f, "{}", contract_addr), + AssetInfo::Native(denom) => write!(f, "{}", denom), + AssetInfo::Token(contract_addr) => write!(f, "{}", contract_addr), } } } impl AssetInfo { - pub fn to_raw(&self, api: &dyn Api) -> StdResult { + pub fn as_bytes(&self) -> &[u8] { match self { - AssetInfo::NativeToken { denom } => Ok(AssetInfoRaw::NativeToken { - denom: denom.to_string(), - }), - AssetInfo::Token { contract_addr } => Ok(AssetInfoRaw::Token { - contract_addr: api.addr_canonicalize(contract_addr.as_str())?, - }), + AssetInfo::Native(denom) => denom.as_bytes(), + AssetInfo::Token(contract_addr) => contract_addr.as_str().as_bytes(), } } pub fn is_native_token(&self) -> bool { match self { - AssetInfo::NativeToken { .. } => true, - AssetInfo::Token { .. } => false, + AssetInfo::Native(_) => true, + AssetInfo::Token(_) => false, } } pub fn query_pool(&self, querier: &QuerierWrapper, pool_addr: Addr) -> StdResult { match self { - AssetInfo::Token { contract_addr, .. } => { + AssetInfo::Token(contract_addr) => { query_token_balance(querier, contract_addr.clone(), pool_addr) } - AssetInfo::NativeToken { denom, .. } => { - query_balance(querier, pool_addr, denom.to_string()) - } + AssetInfo::Native(denom) => query_balance(querier, pool_addr, denom.to_string()), } } pub fn equal(&self, asset: &AssetInfo) -> bool { match self { - AssetInfo::Token { contract_addr, .. } => { - let self_contract_addr = contract_addr; - match asset { - AssetInfo::Token { contract_addr, .. } => self_contract_addr == contract_addr, - AssetInfo::NativeToken { .. } => false, - } - } - AssetInfo::NativeToken { denom, .. } => { - let self_denom = denom; - match asset { - AssetInfo::Token { .. } => false, - AssetInfo::NativeToken { denom, .. } => self_denom == denom, - } - } - } - } -} - -#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] -pub struct AssetRaw { - pub info: AssetInfoRaw, - pub amount: Uint128, -} - -impl AssetRaw { - pub fn to_normal(&self, api: &dyn Api) -> StdResult { - Ok(Asset { - info: match &self.info { - AssetInfoRaw::NativeToken { denom } => AssetInfo::NativeToken { - denom: denom.to_string(), - }, - AssetInfoRaw::Token { contract_addr } => AssetInfo::Token { - contract_addr: api.addr_humanize(&contract_addr)?, - }, - }, - amount: self.amount, - }) - } -} - -#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] -pub enum AssetInfoRaw { - Token { contract_addr: CanonicalAddr }, - NativeToken { denom: String }, -} - -impl AssetInfoRaw { - pub fn to_normal(&self, api: &dyn Api) -> StdResult { - match self { - AssetInfoRaw::NativeToken { denom } => Ok(AssetInfo::NativeToken { - denom: denom.to_string(), - }), - AssetInfoRaw::Token { contract_addr } => Ok(AssetInfo::Token { - contract_addr: api.addr_humanize(&contract_addr)?, - }), - } - } - - pub fn as_bytes(&self) -> &[u8] { - match self { - AssetInfoRaw::NativeToken { denom } => denom.as_bytes(), - AssetInfoRaw::Token { contract_addr } => contract_addr.as_slice(), - } - } - - pub fn equal(&self, asset: &AssetInfoRaw) -> bool { - match self { - AssetInfoRaw::Token { contract_addr, .. } => { + AssetInfo::Token(contract_addr) => { let self_contract_addr = contract_addr; match asset { - AssetInfoRaw::Token { contract_addr, .. } => { - self_contract_addr == contract_addr - } - AssetInfoRaw::NativeToken { .. } => false, + AssetInfo::Token(contract_addr) => self_contract_addr == contract_addr, + AssetInfo::Native(_) => false, } } - AssetInfoRaw::NativeToken { denom, .. } => { + AssetInfo::Native(denom) => { let self_denom = denom; match asset { - AssetInfoRaw::Token { .. } => false, - AssetInfoRaw::NativeToken { denom, .. } => self_denom == denom, + AssetInfo::Token(_) => false, + AssetInfo::Native(denom) => self_denom == denom, } } } @@ -240,33 +151,14 @@ pub struct PairInfo { pub liquidity_token: Addr, } -#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] -pub struct PairInfoRaw { - pub asset_infos: [AssetInfoRaw; 2], - pub contract_addr: CanonicalAddr, - pub liquidity_token: CanonicalAddr, -} - -impl PairInfoRaw { - pub fn to_normal(&self, api: &dyn Api) -> StdResult { - Ok(PairInfo { - liquidity_token: api.addr_humanize(&self.liquidity_token)?, - contract_addr: api.addr_humanize(&self.contract_addr)?, - asset_infos: [ - self.asset_infos[0].to_normal(api)?, - self.asset_infos[1].to_normal(api)?, - ], - }) - } - +impl PairInfo { pub fn query_pools( &self, querier: &QuerierWrapper, - api: &dyn Api, contract_addr: Addr, ) -> StdResult<[Asset; 2]> { - let info_0: AssetInfo = self.asset_infos[0].to_normal(api)?; - let info_1: AssetInfo = self.asset_infos[1].to_normal(api)?; + let info_0 = self.asset_infos[0].clone(); + let info_1 = self.asset_infos[1].clone(); Ok([ Asset { amount: info_0.query_pool(querier, contract_addr.clone())?, diff --git a/packages/tfi/src/testing.rs b/packages/tfi/src/testing.rs index 1415d0d..59e9a18 100644 --- a/packages/tfi/src/testing.rs +++ b/packages/tfi/src/testing.rs @@ -94,31 +94,17 @@ fn supply_querier() { #[test] fn test_asset_info() { - let token_info: AssetInfo = AssetInfo::Token { - contract_addr: Addr::unchecked("asset0000"), - }; - let native_token_info: AssetInfo = AssetInfo::NativeToken { - denom: "uusd".to_string(), - }; + let token_info: AssetInfo = AssetInfo::Token(Addr::unchecked("asset0000")); + let native_token_info: AssetInfo = AssetInfo::Native("uusd".to_string()); assert_eq!(false, token_info.equal(&native_token_info)); - assert_eq!( - false, - token_info.equal(&AssetInfo::Token { - contract_addr: Addr::unchecked("asset0001"), - }) - ); + assert_ne!(token_info, AssetInfo::Token(Addr::unchecked("asset0001")),); - assert_eq!( - true, - token_info.equal(&AssetInfo::Token { - contract_addr: Addr::unchecked("asset0000"), - }) - ); + assert_eq!(token_info, AssetInfo::Token(Addr::unchecked("asset0000")),); - assert_eq!(true, native_token_info.is_native_token()); - assert_eq!(false, token_info.is_native_token()); + assert!(native_token_info.is_native_token()); + assert!(!token_info.is_native_token()); let mut deps = mock_dependencies(&[Coin { denom: "uusd".to_string(), @@ -167,16 +153,12 @@ fn test_asset() { let token_asset = Asset { amount: Uint128(123123u128), - info: AssetInfo::Token { - contract_addr: Addr::unchecked("asset0000"), - }, + info: AssetInfo::Token(Addr::unchecked("asset0000")), }; let native_token_asset = Asset { amount: Uint128(123123u128), - info: AssetInfo::NativeToken { - denom: "uusd".to_string(), - }, + info: AssetInfo::Native("uusd".to_string()), }; assert_eq!( native_token_asset.deduct_tax().unwrap(), From e7abc1ae387ca480e2f92a353f03f6fcc58dd676 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 23 Jun 2021 14:39:52 +0200 Subject: [PATCH 2/6] tfi-pair compiles --- contracts/tfi-pair/schema/execute_msg.json | 24 +---- .../tfi-pair/schema/instantiate_msg.json | 24 +---- contracts/tfi-pair/schema/pair_info.json | 24 +---- contracts/tfi-pair/schema/pool_response.json | 24 +---- contracts/tfi-pair/schema/query_msg.json | 24 +---- contracts/tfi-pair/src/contract.rs | 100 +++++++----------- contracts/tfi-pair/src/state.rs | 48 +-------- contracts/tfi-pair/src/testing.rs | 36 +++---- 8 files changed, 81 insertions(+), 223 deletions(-) diff --git a/contracts/tfi-pair/schema/execute_msg.json b/contracts/tfi-pair/schema/execute_msg.json index 6bab6d4..df0453b 100644 --- a/contracts/tfi-pair/schema/execute_msg.json +++ b/contracts/tfi-pair/schema/execute_msg.json @@ -128,15 +128,7 @@ ], "properties": { "token": { - "type": "object", - "required": [ - "contract_addr" - ], - "properties": { - "contract_addr": { - "$ref": "#/definitions/Addr" - } - } + "$ref": "#/definitions/Addr" } }, "additionalProperties": false @@ -144,19 +136,11 @@ { "type": "object", "required": [ - "native_token" + "native" ], "properties": { - "native_token": { - "type": "object", - "required": [ - "denom" - ], - "properties": { - "denom": { - "type": "string" - } - } + "native": { + "type": "string" } }, "additionalProperties": false diff --git a/contracts/tfi-pair/schema/instantiate_msg.json b/contracts/tfi-pair/schema/instantiate_msg.json index f6b4c6c..6b8b887 100644 --- a/contracts/tfi-pair/schema/instantiate_msg.json +++ b/contracts/tfi-pair/schema/instantiate_msg.json @@ -38,15 +38,7 @@ ], "properties": { "token": { - "type": "object", - "required": [ - "contract_addr" - ], - "properties": { - "contract_addr": { - "$ref": "#/definitions/Addr" - } - } + "$ref": "#/definitions/Addr" } }, "additionalProperties": false @@ -54,19 +46,11 @@ { "type": "object", "required": [ - "native_token" + "native" ], "properties": { - "native_token": { - "type": "object", - "required": [ - "denom" - ], - "properties": { - "denom": { - "type": "string" - } - } + "native": { + "type": "string" } }, "additionalProperties": false diff --git a/contracts/tfi-pair/schema/pair_info.json b/contracts/tfi-pair/schema/pair_info.json index e9b4770..56a81b3 100644 --- a/contracts/tfi-pair/schema/pair_info.json +++ b/contracts/tfi-pair/schema/pair_info.json @@ -38,15 +38,7 @@ ], "properties": { "token": { - "type": "object", - "required": [ - "contract_addr" - ], - "properties": { - "contract_addr": { - "$ref": "#/definitions/Addr" - } - } + "$ref": "#/definitions/Addr" } }, "additionalProperties": false @@ -54,19 +46,11 @@ { "type": "object", "required": [ - "native_token" + "native" ], "properties": { - "native_token": { - "type": "object", - "required": [ - "denom" - ], - "properties": { - "denom": { - "type": "string" - } - } + "native": { + "type": "string" } }, "additionalProperties": false diff --git a/contracts/tfi-pair/schema/pool_response.json b/contracts/tfi-pair/schema/pool_response.json index 8110438..729bc3e 100644 --- a/contracts/tfi-pair/schema/pool_response.json +++ b/contracts/tfi-pair/schema/pool_response.json @@ -49,15 +49,7 @@ ], "properties": { "token": { - "type": "object", - "required": [ - "contract_addr" - ], - "properties": { - "contract_addr": { - "$ref": "#/definitions/Addr" - } - } + "$ref": "#/definitions/Addr" } }, "additionalProperties": false @@ -65,19 +57,11 @@ { "type": "object", "required": [ - "native_token" + "native" ], "properties": { - "native_token": { - "type": "object", - "required": [ - "denom" - ], - "properties": { - "denom": { - "type": "string" - } - } + "native": { + "type": "string" } }, "additionalProperties": false diff --git a/contracts/tfi-pair/schema/query_msg.json b/contracts/tfi-pair/schema/query_msg.json index 4896cf8..34eb0c1 100644 --- a/contracts/tfi-pair/schema/query_msg.json +++ b/contracts/tfi-pair/schema/query_msg.json @@ -97,15 +97,7 @@ ], "properties": { "token": { - "type": "object", - "required": [ - "contract_addr" - ], - "properties": { - "contract_addr": { - "$ref": "#/definitions/Addr" - } - } + "$ref": "#/definitions/Addr" } }, "additionalProperties": false @@ -113,19 +105,11 @@ { "type": "object", "required": [ - "native_token" + "native" ], "properties": { - "native_token": { - "type": "object", - "required": [ - "denom" - ], - "properties": { - "denom": { - "type": "string" - } - } + "native": { + "type": "string" } }, "additionalProperties": false diff --git a/contracts/tfi-pair/src/contract.rs b/contracts/tfi-pair/src/contract.rs index 7aff05c..db4c4b1 100644 --- a/contracts/tfi-pair/src/contract.rs +++ b/contracts/tfi-pair/src/contract.rs @@ -7,16 +7,15 @@ use crate::state::PAIR_INFO; use cosmwasm_std::entry_point; use cosmwasm_std::{ - attr, from_binary, to_binary, Addr, Binary, CanonicalAddr, Coin, CosmosMsg, Decimal, Deps, - DepsMut, Env, MessageInfo, Reply, ReplyOn, Response, StdError, StdResult, SubMsg, Uint128, - WasmMsg, + attr, from_binary, to_binary, Addr, Binary, Coin, CosmosMsg, Decimal, Deps, DepsMut, Env, + MessageInfo, Reply, ReplyOn, Response, StdError, StdResult, SubMsg, Uint128, WasmMsg, }; use cw20::{Cw20ExecuteMsg, Cw20ReceiveMsg, MinterResponse}; use integer_sqrt::IntegerSquareRoot; use protobuf::Message; use std::str::FromStr; -use tfi::asset::{Asset, AssetInfo, PairInfo, PairInfoRaw}; +use tfi::asset::{Asset, AssetInfo, PairInfo}; use tfi::pair::{ Cw20HookMsg, ExecuteMsg, InstantiateMsg, MigrateMsg, PoolResponse, QueryMsg, ReverseSimulationResponse, SimulationResponse, @@ -33,13 +32,11 @@ pub fn instantiate( _info: MessageInfo, msg: InstantiateMsg, ) -> StdResult { - let pair_info: &PairInfoRaw = &PairInfoRaw { - contract_addr: deps.api.addr_canonicalize(env.contract.address.as_str())?, - liquidity_token: CanonicalAddr::from(vec![]), - asset_infos: [ - msg.asset_infos[0].to_raw(deps.api)?, - msg.asset_infos[1].to_raw(deps.api)?, - ], + let pair_info: &PairInfo = &PairInfo { + contract_addr: env.contract.address.clone(), + // TODO: fix this + liquidity_token: Addr::unchecked(""), + asset_infos: msg.asset_infos, }; PAIR_INFO.save(deps.storage, &pair_info)?; @@ -133,11 +130,11 @@ pub fn receive_cw20( }) => { // only asset contract can execute this message let mut authorized: bool = false; - let config: PairInfoRaw = PAIR_INFO.load(deps.storage)?; + let config: PairInfo = PAIR_INFO.load(deps.storage)?; let pools: [Asset; 2] = - config.query_pools(&deps.querier, deps.api, env.contract.address.clone())?; + config.query_pools(&deps.querier, env.contract.address.clone())?; for pool in pools.iter() { - if let AssetInfo::Token { contract_addr, .. } = &pool.info { + if let AssetInfo::Token(contract_addr) = &pool.info { if contract_addr == &info.sender { authorized = true; } @@ -160,7 +157,7 @@ pub fn receive_cw20( info, Addr::unchecked(cw20_msg.sender), Asset { - info: AssetInfo::Token { contract_addr }, + info: AssetInfo::Token(contract_addr), amount: cw20_msg.amount, }, belief_price, @@ -169,8 +166,8 @@ pub fn receive_cw20( ) } Ok(Cw20HookMsg::WithdrawLiquidity {}) => { - let config: PairInfoRaw = PAIR_INFO.load(deps.storage)?; - if deps.api.addr_canonicalize(info.sender.as_str())? != config.liquidity_token { + let config: PairInfo = PAIR_INFO.load(deps.storage)?; + if info.sender != config.liquidity_token { return Err(ContractError::Unauthorized {}); } @@ -189,19 +186,17 @@ pub fn reply(deps: DepsMut, _env: Env, msg: Reply) -> StdResult { Message::parse_from_bytes(data.as_slice()).map_err(|_| { StdError::parse_err("MsgInstantiateContractResponse", "failed to parse data") })?; - let liquidity_token = res.get_contract_address(); + let liquidity_token = Addr::unchecked(res.get_contract_address()); + let attributes = vec![attr("liquidity_token_addr", liquidity_token.as_str())]; - let api = deps.api; PAIR_INFO.update(deps.storage, |mut meta| -> StdResult<_> { - meta.liquidity_token = api.addr_canonicalize(liquidity_token)?; + meta.liquidity_token = liquidity_token; Ok(meta) })?; Ok(Response { - messages: vec![], - submessages: vec![], - attributes: vec![attr("liquidity_token_addr", liquidity_token)], - data: None, + attributes, + ..Response::default() }) } @@ -217,9 +212,9 @@ pub fn provide_liquidity( asset.assert_sent_native_token_balance(&info)?; } - let pair_info: PairInfoRaw = PAIR_INFO.load(deps.storage)?; + let pair_info: PairInfo = PAIR_INFO.load(deps.storage)?; let mut pools: [Asset; 2] = - pair_info.query_pools(&deps.querier, deps.api, env.contract.address.clone())?; + pair_info.query_pools(&deps.querier, env.contract.address.clone())?; let deposits: [Uint128; 2] = [ assets .iter() @@ -236,7 +231,7 @@ pub fn provide_liquidity( let mut messages: Vec = vec![]; for (i, pool) in pools.iter_mut().enumerate() { // If the pool is token contract, then we need to execute TransferFrom msg to receive funds - if let AssetInfo::Token { contract_addr, .. } = &pool.info { + if let AssetInfo::Token(contract_addr) = &pool.info { messages.push(CosmosMsg::Wasm(WasmMsg::Execute { contract_addr: contract_addr.to_string(), msg: to_binary(&Cw20ExecuteMsg::TransferFrom { @@ -256,8 +251,7 @@ pub fn provide_liquidity( // assert slippage tolerance assert_slippage_tolerance(&slippage_tolerance, &deposits, &pools)?; - let liquidity_token = deps.api.addr_humanize(&pair_info.liquidity_token)?; - let total_share = query_supply(&deps.querier, liquidity_token)?; + let total_share = query_supply(&deps.querier, pair_info.liquidity_token.clone())?; let share = if total_share == Uint128::zero() { // Initial share = collateral amount Uint128((deposits[0].u128() * deposits[1].u128()).integer_sqrt()) @@ -275,10 +269,7 @@ pub fn provide_liquidity( // mint LP token to sender messages.push(CosmosMsg::Wasm(WasmMsg::Execute { - contract_addr: deps - .api - .addr_humanize(&pair_info.liquidity_token)? - .to_string(), + contract_addr: pair_info.liquidity_token.into(), msg: to_binary(&Cw20ExecuteMsg::Mint { recipient: info.sender.to_string(), amount: share, @@ -304,11 +295,10 @@ pub fn withdraw_liquidity( sender: Addr, amount: Uint128, ) -> Result { - let pair_info: PairInfoRaw = PAIR_INFO.load(deps.storage)?; - let liquidity_addr: Addr = deps.api.addr_humanize(&pair_info.liquidity_token)?; + let pair_info: PairInfo = PAIR_INFO.load(deps.storage)?; - let pools: [Asset; 2] = pair_info.query_pools(&deps.querier, deps.api, env.contract.address)?; - let total_share: Uint128 = query_supply(&deps.querier, liquidity_addr)?; + let pools: [Asset; 2] = pair_info.query_pools(&deps.querier, env.contract.address)?; + let total_share: Uint128 = query_supply(&deps.querier, pair_info.liquidity_token.clone())?; let share_ratio: Decimal = Decimal::from_ratio(amount, total_share); let refund_assets: Vec = pools @@ -327,10 +317,7 @@ pub fn withdraw_liquidity( refund_assets[1].clone().into_msg(sender)?, // burn liquidity token CosmosMsg::Wasm(WasmMsg::Execute { - contract_addr: deps - .api - .addr_humanize(&pair_info.liquidity_token)? - .to_string(), + contract_addr: pair_info.liquidity_token.into(), msg: to_binary(&Cw20ExecuteMsg::Burn { amount })?, send: vec![], }), @@ -362,9 +349,9 @@ pub fn swap( ) -> Result { offer_asset.assert_sent_native_token_balance(&info)?; - let pair_info: PairInfoRaw = PAIR_INFO.load(deps.storage)?; + let pair_info: PairInfo = PAIR_INFO.load(deps.storage)?; - let pools: [Asset; 2] = pair_info.query_pools(&deps.querier, deps.api, env.contract.address)?; + let pools: [Asset; 2] = pair_info.query_pools(&deps.querier, env.contract.address)?; let offer_pool: Asset; let ask_pool: Asset; @@ -439,20 +426,15 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> Result Result { - let pair_info: PairInfoRaw = PAIR_INFO.load(deps.storage)?; - let pair_info = pair_info.to_normal(deps.api)?; - + let pair_info: PairInfo = PAIR_INFO.load(deps.storage)?; Ok(pair_info) } pub fn query_pool(deps: Deps) -> Result { - let pair_info: PairInfoRaw = PAIR_INFO.load(deps.storage)?; - let contract_addr = deps.api.addr_humanize(&pair_info.contract_addr)?; - let assets: [Asset; 2] = pair_info.query_pools(&deps.querier, deps.api, contract_addr)?; - let total_share: Uint128 = query_supply( - &deps.querier, - deps.api.addr_humanize(&pair_info.liquidity_token)?, - )?; + let pair_info: PairInfo = PAIR_INFO.load(deps.storage)?; + let assets: [Asset; 2] = + pair_info.query_pools(&deps.querier, pair_info.contract_addr.clone())?; + let total_share: Uint128 = query_supply(&deps.querier, pair_info.liquidity_token)?; let resp = PoolResponse { assets, @@ -466,10 +448,10 @@ pub fn query_simulation( deps: Deps, offer_asset: Asset, ) -> Result { - let pair_info: PairInfoRaw = PAIR_INFO.load(deps.storage)?; + let pair_info: PairInfo = PAIR_INFO.load(deps.storage)?; - let contract_addr = deps.api.addr_humanize(&pair_info.contract_addr)?; - let pools: [Asset; 2] = pair_info.query_pools(&deps.querier, deps.api, contract_addr)?; + let pools: [Asset; 2] = + pair_info.query_pools(&deps.querier, pair_info.contract_addr.clone())?; let offer_pool: Asset; let ask_pool: Asset; @@ -497,10 +479,10 @@ pub fn query_reverse_simulation( deps: Deps, ask_asset: Asset, ) -> Result { - let pair_info: PairInfoRaw = PAIR_INFO.load(deps.storage)?; + let pair_info: PairInfo = PAIR_INFO.load(deps.storage)?; - let contract_addr = deps.api.addr_humanize(&pair_info.contract_addr)?; - let pools: [Asset; 2] = pair_info.query_pools(&deps.querier, deps.api, contract_addr)?; + let pools: [Asset; 2] = + pair_info.query_pools(&deps.querier, pair_info.contract_addr.clone())?; let offer_pool: Asset; let ask_pool: Asset; diff --git a/contracts/tfi-pair/src/state.rs b/contracts/tfi-pair/src/state.rs index 3b2f5e1..a3d7411 100644 --- a/contracts/tfi-pair/src/state.rs +++ b/contracts/tfi-pair/src/state.rs @@ -1,49 +1,5 @@ use cw_storage_plus::Item; -use tfi::asset::PairInfoRaw; +use tfi::asset::PairInfo; // put the length bytes at the first for compatibility with legacy singleton store -pub const PAIR_INFO: Item = Item::new("\u{0}\u{9}pair_info"); - -#[cfg(test)] -mod test { - use super::*; - - use cosmwasm_std::testing::mock_dependencies; - use cosmwasm_std::{Api, StdResult, Storage}; - use cosmwasm_storage::{singleton, singleton_read}; - use tfi::asset::AssetInfoRaw; - const KEY_PAIR_INFO: &[u8] = b"pair_info"; - - pub fn store_pair_info(storage: &mut dyn Storage, config: &PairInfoRaw) -> StdResult<()> { - singleton(storage, KEY_PAIR_INFO).save(config) - } - pub fn read_pair_info(storage: &dyn Storage) -> StdResult { - singleton_read(storage, KEY_PAIR_INFO).load() - } - - #[test] - fn legacy_compatibility() { - let mut deps = mock_dependencies(&[]); - store_pair_info( - &mut deps.storage, - &PairInfoRaw { - asset_infos: [ - AssetInfoRaw::NativeToken { - denom: "uusd".to_string(), - }, - AssetInfoRaw::Token { - contract_addr: deps.api.addr_canonicalize("token0000").unwrap(), - }, - ], - contract_addr: deps.api.addr_canonicalize("pair0000").unwrap(), - liquidity_token: deps.api.addr_canonicalize("liquidity0000").unwrap(), - }, - ) - .unwrap(); - - assert_eq!( - PAIR_INFO.load(&deps.storage).unwrap(), - read_pair_info(&deps.storage).unwrap() - ); - } -} +pub const PAIR_INFO: Item = Item::new("pair_info"); diff --git a/contracts/tfi-pair/src/testing.rs b/contracts/tfi-pair/src/testing.rs index c0f7754..e757686 100644 --- a/contracts/tfi-pair/src/testing.rs +++ b/contracts/tfi-pair/src/testing.rs @@ -25,7 +25,7 @@ fn proper_initialization() { let msg = InstantiateMsg { asset_infos: [ - AssetInfo::NativeToken { + AssetInfo::Native { denom: "uusd".to_string(), }, AssetInfo::Token { @@ -88,7 +88,7 @@ fn proper_initialization() { assert_eq!( pair_info.asset_infos, [ - AssetInfo::NativeToken { + AssetInfo::Native { denom: "uusd".to_string(), }, AssetInfo::Token { @@ -115,7 +115,7 @@ fn provide_liquidity() { let msg = InstantiateMsg { asset_infos: [ - AssetInfo::NativeToken { + AssetInfo::Native { denom: "uusd".to_string(), }, AssetInfo::Token { @@ -156,7 +156,7 @@ fn provide_liquidity() { amount: Uint128::from(100u128), }, Asset { - info: AssetInfo::NativeToken { + info: AssetInfo::Native { denom: "uusd".to_string(), }, amount: Uint128::from(100u128), @@ -232,7 +232,7 @@ fn provide_liquidity() { amount: Uint128::from(100u128), }, Asset { - info: AssetInfo::NativeToken { + info: AssetInfo::Native { denom: "uusd".to_string(), }, amount: Uint128::from(200u128), @@ -290,7 +290,7 @@ fn provide_liquidity() { amount: Uint128::from(100u128), }, Asset { - info: AssetInfo::NativeToken { + info: AssetInfo::Native { denom: "uusd".to_string(), }, amount: Uint128::from(50u128), @@ -346,7 +346,7 @@ fn provide_liquidity() { amount: Uint128::from(98u128), }, Asset { - info: AssetInfo::NativeToken { + info: AssetInfo::Native { denom: "uusd".to_string(), }, amount: Uint128::from(100u128), @@ -388,7 +388,7 @@ fn provide_liquidity() { amount: Uint128::from(100u128), }, Asset { - info: AssetInfo::NativeToken { + info: AssetInfo::Native { denom: "uusd".to_string(), }, amount: Uint128::from(98u128), @@ -430,7 +430,7 @@ fn provide_liquidity() { amount: Uint128::from(99u128), }, Asset { - info: AssetInfo::NativeToken { + info: AssetInfo::Native { denom: "uusd".to_string(), }, amount: Uint128::from(100u128), @@ -468,7 +468,7 @@ fn provide_liquidity() { amount: Uint128::from(100u128), }, Asset { - info: AssetInfo::NativeToken { + info: AssetInfo::Native { denom: "uusd".to_string(), }, amount: Uint128::from(99u128), @@ -508,7 +508,7 @@ fn withdraw_liquidity() { let msg = InstantiateMsg { asset_infos: [ - AssetInfo::NativeToken { + AssetInfo::Native { denom: "uusd".to_string(), }, AssetInfo::Token { @@ -625,7 +625,7 @@ fn try_native_to_token() { let msg = InstantiateMsg { asset_infos: [ - AssetInfo::NativeToken { + AssetInfo::Native { denom: "uusd".to_string(), }, AssetInfo::Token { @@ -659,7 +659,7 @@ fn try_native_to_token() { // normal swap let msg = ExecuteMsg::Swap { offer_asset: Asset { - info: AssetInfo::NativeToken { + info: AssetInfo::Native { denom: "uusd".to_string(), }, amount: offer_amount, @@ -702,7 +702,7 @@ fn try_native_to_token() { let simulation_res: SimulationResponse = query_simulation( deps.as_ref(), Asset { - info: AssetInfo::NativeToken { + info: AssetInfo::Native { denom: "uusd".to_string(), }, amount: offer_amount, @@ -800,7 +800,7 @@ fn try_token_to_native() { let msg = InstantiateMsg { asset_infos: [ - AssetInfo::NativeToken { + AssetInfo::Native { denom: "uusd".to_string(), }, AssetInfo::Token { @@ -910,7 +910,7 @@ fn try_token_to_native() { deps.as_ref(), Asset { amount: expected_return_amount, - info: AssetInfo::NativeToken { + info: AssetInfo::Native { denom: "uusd".to_string(), }, }, @@ -1042,7 +1042,7 @@ fn test_query_pool() { let msg = InstantiateMsg { asset_infos: [ - AssetInfo::NativeToken { + AssetInfo::Native { denom: "uusd".to_string(), }, AssetInfo::Token { @@ -1079,7 +1079,7 @@ fn test_query_pool() { res.assets, [ Asset { - info: AssetInfo::NativeToken { + info: AssetInfo::Native { denom: "uusd".to_string(), }, amount: asset_0_amount From ef708b0a39a23a83bbe13379ba47e2f4754eb6b8 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 23 Jun 2021 14:42:28 +0200 Subject: [PATCH 3/6] Fix tfi-pair tests --- contracts/tfi-pair/src/testing.rs | 144 ++++++++---------------------- 1 file changed, 36 insertions(+), 108 deletions(-) diff --git a/contracts/tfi-pair/src/testing.rs b/contracts/tfi-pair/src/testing.rs index e757686..531aabe 100644 --- a/contracts/tfi-pair/src/testing.rs +++ b/contracts/tfi-pair/src/testing.rs @@ -25,12 +25,8 @@ fn proper_initialization() { let msg = InstantiateMsg { asset_infos: [ - AssetInfo::Native { - denom: "uusd".to_string(), - }, - AssetInfo::Token { - contract_addr: Addr::unchecked("asset0000"), - }, + AssetInfo::Native("uusd".to_string()), + AssetInfo::Token(Addr::unchecked("asset0000")), ], token_code_id: 10u64, }; @@ -88,12 +84,8 @@ fn proper_initialization() { assert_eq!( pair_info.asset_infos, [ - AssetInfo::Native { - denom: "uusd".to_string(), - }, - AssetInfo::Token { - contract_addr: Addr::unchecked("asset0000") - } + AssetInfo::Native("uusd".to_string()), + AssetInfo::Token(Addr::unchecked("asset0000")), ] ); } @@ -115,12 +107,8 @@ fn provide_liquidity() { let msg = InstantiateMsg { asset_infos: [ - AssetInfo::Native { - denom: "uusd".to_string(), - }, - AssetInfo::Token { - contract_addr: Addr::unchecked("asset0000"), - }, + AssetInfo::Native("uusd".to_string()), + AssetInfo::Token(Addr::unchecked("asset0000")), ], token_code_id: 10u64, }; @@ -150,15 +138,11 @@ fn provide_liquidity() { let msg = ExecuteMsg::ProvideLiquidity { assets: [ Asset { - info: AssetInfo::Token { - contract_addr: Addr::unchecked("asset0000".to_string()), - }, + info: AssetInfo::Token(Addr::unchecked("asset0000".to_string())), amount: Uint128::from(100u128), }, Asset { - info: AssetInfo::Native { - denom: "uusd".to_string(), - }, + info: AssetInfo::Native("uusd".to_string()), amount: Uint128::from(100u128), }, ], @@ -226,15 +210,11 @@ fn provide_liquidity() { let msg = ExecuteMsg::ProvideLiquidity { assets: [ Asset { - info: AssetInfo::Token { - contract_addr: Addr::unchecked("asset0000".to_string()), - }, + info: AssetInfo::Token(Addr::unchecked("asset0000".to_string())), amount: Uint128::from(100u128), }, Asset { - info: AssetInfo::Native { - denom: "uusd".to_string(), - }, + info: AssetInfo::Native("uusd".to_string()), amount: Uint128::from(200u128), }, ], @@ -284,15 +264,11 @@ fn provide_liquidity() { let msg = ExecuteMsg::ProvideLiquidity { assets: [ Asset { - info: AssetInfo::Token { - contract_addr: Addr::unchecked("asset0000".to_string()), - }, + info: AssetInfo::Token(Addr::unchecked("asset0000".to_string())), amount: Uint128::from(100u128), }, Asset { - info: AssetInfo::Native { - denom: "uusd".to_string(), - }, + info: AssetInfo::Native("uusd".to_string()), amount: Uint128::from(50u128), }, ], @@ -340,15 +316,11 @@ fn provide_liquidity() { let msg = ExecuteMsg::ProvideLiquidity { assets: [ Asset { - info: AssetInfo::Token { - contract_addr: Addr::unchecked("asset0000".to_string()), - }, + info: AssetInfo::Token(Addr::unchecked("asset0000".to_string())), amount: Uint128::from(98u128), }, Asset { - info: AssetInfo::Native { - denom: "uusd".to_string(), - }, + info: AssetInfo::Native("uusd".to_string()), amount: Uint128::from(100u128), }, ], @@ -382,15 +354,11 @@ fn provide_liquidity() { let msg = ExecuteMsg::ProvideLiquidity { assets: [ Asset { - info: AssetInfo::Token { - contract_addr: Addr::unchecked("asset0000".to_string()), - }, + info: AssetInfo::Token(Addr::unchecked("asset0000".to_string())), amount: Uint128::from(100u128), }, Asset { - info: AssetInfo::Native { - denom: "uusd".to_string(), - }, + info: AssetInfo::Native("uusd".to_string()), amount: Uint128::from(98u128), }, ], @@ -424,15 +392,11 @@ fn provide_liquidity() { let msg = ExecuteMsg::ProvideLiquidity { assets: [ Asset { - info: AssetInfo::Token { - contract_addr: Addr::unchecked("asset0000".to_string()), - }, + info: AssetInfo::Token(Addr::unchecked("asset0000".to_string())), amount: Uint128::from(99u128), }, Asset { - info: AssetInfo::Native { - denom: "uusd".to_string(), - }, + info: AssetInfo::Native("uusd".to_string()), amount: Uint128::from(100u128), }, ], @@ -462,15 +426,11 @@ fn provide_liquidity() { let msg = ExecuteMsg::ProvideLiquidity { assets: [ Asset { - info: AssetInfo::Token { - contract_addr: Addr::unchecked("asset0000".to_string()), - }, + info: AssetInfo::Token(Addr::unchecked("asset0000".to_string())), amount: Uint128::from(100u128), }, Asset { - info: AssetInfo::Native { - denom: "uusd".to_string(), - }, + info: AssetInfo::Native("uusd".to_string()), amount: Uint128::from(99u128), }, ], @@ -508,12 +468,8 @@ fn withdraw_liquidity() { let msg = InstantiateMsg { asset_infos: [ - AssetInfo::Native { - denom: "uusd".to_string(), - }, - AssetInfo::Token { - contract_addr: Addr::unchecked("asset0000"), - }, + AssetInfo::Native("uusd".to_string()), + AssetInfo::Token(Addr::unchecked("asset0000")), ], token_code_id: 10u64, }; @@ -625,12 +581,8 @@ fn try_native_to_token() { let msg = InstantiateMsg { asset_infos: [ - AssetInfo::Native { - denom: "uusd".to_string(), - }, - AssetInfo::Token { - contract_addr: Addr::unchecked("asset0000"), - }, + AssetInfo::Native("uusd".to_string()), + AssetInfo::Token(Addr::unchecked("asset0000")), ], token_code_id: 10u64, }; @@ -659,9 +611,7 @@ fn try_native_to_token() { // normal swap let msg = ExecuteMsg::Swap { offer_asset: Asset { - info: AssetInfo::Native { - denom: "uusd".to_string(), - }, + info: AssetInfo::Native("uusd".to_string()), amount: offer_amount, }, belief_price: None, @@ -702,9 +652,7 @@ fn try_native_to_token() { let simulation_res: SimulationResponse = query_simulation( deps.as_ref(), Asset { - info: AssetInfo::Native { - denom: "uusd".to_string(), - }, + info: AssetInfo::Native("uusd".to_string()), amount: offer_amount, }, ) @@ -717,9 +665,7 @@ fn try_native_to_token() { let reverse_simulation_res: ReverseSimulationResponse = query_reverse_simulation( deps.as_ref(), Asset { - info: AssetInfo::Token { - contract_addr: Addr::unchecked("asset0000"), - }, + info: AssetInfo::Token(Addr::unchecked("asset0000")), amount: expected_return_amount, }, ) @@ -800,12 +746,8 @@ fn try_token_to_native() { let msg = InstantiateMsg { asset_infos: [ - AssetInfo::Native { - denom: "uusd".to_string(), - }, - AssetInfo::Token { - contract_addr: Addr::unchecked("asset0000"), - }, + AssetInfo::Native("uusd".to_string()), + AssetInfo::Token(Addr::unchecked("asset0000")), ], token_code_id: 10u64, }; @@ -834,9 +776,7 @@ fn try_token_to_native() { // unauthorized access; can not execute swap directly for token swap let msg = ExecuteMsg::Swap { offer_asset: Asset { - info: AssetInfo::Token { - contract_addr: Addr::unchecked("asset0000"), - }, + info: AssetInfo::Token(Addr::unchecked("asset0000")), amount: offer_amount, }, belief_price: None, @@ -895,9 +835,7 @@ fn try_token_to_native() { deps.as_ref(), Asset { amount: offer_amount, - info: AssetInfo::Token { - contract_addr: Addr::unchecked("asset0000"), - }, + info: AssetInfo::Token(Addr::unchecked("asset0000")), }, ) .unwrap(); @@ -910,9 +848,7 @@ fn try_token_to_native() { deps.as_ref(), Asset { amount: expected_return_amount, - info: AssetInfo::Native { - denom: "uusd".to_string(), - }, + info: AssetInfo::Native("uusd".to_string()), }, ) .unwrap(); @@ -1042,12 +978,8 @@ fn test_query_pool() { let msg = InstantiateMsg { asset_infos: [ - AssetInfo::Native { - denom: "uusd".to_string(), - }, - AssetInfo::Token { - contract_addr: Addr::unchecked("asset0000"), - }, + AssetInfo::Native("uusd".to_string()), + AssetInfo::Token(Addr::unchecked("asset0000")), ], token_code_id: 10u64, }; @@ -1079,15 +1011,11 @@ fn test_query_pool() { res.assets, [ Asset { - info: AssetInfo::Native { - denom: "uusd".to_string(), - }, + info: AssetInfo::Native("uusd".to_string()), amount: asset_0_amount }, Asset { - info: AssetInfo::Token { - contract_addr: Addr::unchecked("asset0000"), - }, + info: AssetInfo::Token(Addr::unchecked("asset0000")), amount: asset_1_amount } ] From 3d485b0c3ddad4bb7df76dcb5e945b8f7098fee9 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 23 Jun 2021 15:03:47 +0200 Subject: [PATCH 4/6] Update tfi-factory --- contracts/tfi-factory/schema/execute_msg.json | 24 +-- contracts/tfi-factory/schema/pair_info.json | 24 +-- .../tfi-factory/schema/pairs_response.json | 24 +-- contracts/tfi-factory/schema/query_msg.json | 24 +-- contracts/tfi-factory/src/contract.rs | 57 +++---- contracts/tfi-factory/src/mock_querier.rs | 26 +-- contracts/tfi-factory/src/querier.rs | 8 +- contracts/tfi-factory/src/state.rs | 158 ++---------------- contracts/tfi-factory/src/testing.rs | 42 ++--- 9 files changed, 69 insertions(+), 318 deletions(-) diff --git a/contracts/tfi-factory/schema/execute_msg.json b/contracts/tfi-factory/schema/execute_msg.json index a5a3ee2..e084cc1 100644 --- a/contracts/tfi-factory/schema/execute_msg.json +++ b/contracts/tfi-factory/schema/execute_msg.json @@ -82,15 +82,7 @@ ], "properties": { "token": { - "type": "object", - "required": [ - "contract_addr" - ], - "properties": { - "contract_addr": { - "$ref": "#/definitions/Addr" - } - } + "$ref": "#/definitions/Addr" } }, "additionalProperties": false @@ -98,19 +90,11 @@ { "type": "object", "required": [ - "native_token" + "native" ], "properties": { - "native_token": { - "type": "object", - "required": [ - "denom" - ], - "properties": { - "denom": { - "type": "string" - } - } + "native": { + "type": "string" } }, "additionalProperties": false diff --git a/contracts/tfi-factory/schema/pair_info.json b/contracts/tfi-factory/schema/pair_info.json index e9b4770..56a81b3 100644 --- a/contracts/tfi-factory/schema/pair_info.json +++ b/contracts/tfi-factory/schema/pair_info.json @@ -38,15 +38,7 @@ ], "properties": { "token": { - "type": "object", - "required": [ - "contract_addr" - ], - "properties": { - "contract_addr": { - "$ref": "#/definitions/Addr" - } - } + "$ref": "#/definitions/Addr" } }, "additionalProperties": false @@ -54,19 +46,11 @@ { "type": "object", "required": [ - "native_token" + "native" ], "properties": { - "native_token": { - "type": "object", - "required": [ - "denom" - ], - "properties": { - "denom": { - "type": "string" - } - } + "native": { + "type": "string" } }, "additionalProperties": false diff --git a/contracts/tfi-factory/schema/pairs_response.json b/contracts/tfi-factory/schema/pairs_response.json index de7ff76..fb0bef0 100644 --- a/contracts/tfi-factory/schema/pairs_response.json +++ b/contracts/tfi-factory/schema/pairs_response.json @@ -28,15 +28,7 @@ ], "properties": { "token": { - "type": "object", - "required": [ - "contract_addr" - ], - "properties": { - "contract_addr": { - "$ref": "#/definitions/Addr" - } - } + "$ref": "#/definitions/Addr" } }, "additionalProperties": false @@ -44,19 +36,11 @@ { "type": "object", "required": [ - "native_token" + "native" ], "properties": { - "native_token": { - "type": "object", - "required": [ - "denom" - ], - "properties": { - "denom": { - "type": "string" - } - } + "native": { + "type": "string" } }, "additionalProperties": false diff --git a/contracts/tfi-factory/schema/query_msg.json b/contracts/tfi-factory/schema/query_msg.json index 8fdbf1d..dfdb261 100644 --- a/contracts/tfi-factory/schema/query_msg.json +++ b/contracts/tfi-factory/schema/query_msg.json @@ -88,15 +88,7 @@ ], "properties": { "token": { - "type": "object", - "required": [ - "contract_addr" - ], - "properties": { - "contract_addr": { - "$ref": "#/definitions/Addr" - } - } + "$ref": "#/definitions/Addr" } }, "additionalProperties": false @@ -104,19 +96,11 @@ { "type": "object", "required": [ - "native_token" + "native" ], "properties": { - "native_token": { - "type": "object", - "required": [ - "denom" - ], - "properties": { - "denom": { - "type": "string" - } - } + "native": { + "type": "string" } }, "additionalProperties": false diff --git a/contracts/tfi-factory/src/contract.rs b/contracts/tfi-factory/src/contract.rs index e2a503d..72c005b 100644 --- a/contracts/tfi-factory/src/contract.rs +++ b/contracts/tfi-factory/src/contract.rs @@ -1,8 +1,8 @@ #[cfg(not(feature = "library"))] use cosmwasm_std::entry_point; use cosmwasm_std::{ - attr, to_binary, Addr, Binary, Deps, DepsMut, Env, MessageInfo, Reply, ReplyOn, Response, - StdError, StdResult, SubMsg, WasmMsg, + attr, to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Reply, ReplyOn, Response, StdError, + StdResult, SubMsg, WasmMsg, }; use crate::querier::query_liquidity_token; @@ -10,7 +10,7 @@ use crate::response::MsgInstantiateContractResponse; use crate::state::{pair_key, read_pairs, Config, TmpPairInfo, CONFIG, PAIRS, TMP_PAIR_INFO}; use protobuf::Message; -use tfi::asset::{AssetInfo, PairInfo, PairInfoRaw}; +use tfi::asset::{AssetInfo, PairInfo}; use tfi::factory::{ ConfigResponse, ExecuteMsg, InstantiateMsg, MigrateMsg, PairsResponse, QueryMsg, }; @@ -24,7 +24,7 @@ pub fn instantiate( msg: InstantiateMsg, ) -> StdResult { let config = Config { - owner: deps.api.addr_canonicalize(info.sender.as_str())?, + owner: info.sender, token_code_id: msg.token_code_id, pair_code_id: msg.pair_code_id, }; @@ -63,15 +63,14 @@ pub fn execute_update_config( let mut config: Config = CONFIG.load(deps.storage)?; // permission check - if deps.api.addr_canonicalize(info.sender.as_str())? != config.owner { + if info.sender != config.owner { return Err(StdError::generic_err("unauthorized")); } if let Some(owner) = owner { // validate address format - let _ = deps.api.addr_validate(&owner)?; - - config.owner = deps.api.addr_canonicalize(&owner)?; + let owner = deps.api.addr_validate(&owner)?; + config.owner = owner; } if let Some(token_code_id) = token_code_id { @@ -100,12 +99,8 @@ pub fn execute_create_pair( asset_infos: [AssetInfo; 2], ) -> StdResult { let config: Config = CONFIG.load(deps.storage)?; - let raw_infos = [ - asset_infos[0].to_raw(deps.api)?, - asset_infos[1].to_raw(deps.api)?, - ]; - let pair_key = pair_key(&raw_infos); + let pair_key = pair_key(&asset_infos); if let Ok(Some(_)) = PAIRS.may_load(deps.storage, &pair_key) { return Err(StdError::generic_err("Pair already exists")); } @@ -114,7 +109,7 @@ pub fn execute_create_pair( deps.storage, &TmpPairInfo { pair_key, - asset_infos: raw_infos, + asset_infos: asset_infos.clone(), }, )?; @@ -154,15 +149,15 @@ pub fn reply(deps: DepsMut, _env: Env, msg: Reply) -> StdResult { StdError::parse_err("MsgInstantiateContractResponse", "failed to parse data") })?; - let pair_contract = res.get_contract_address(); - let liquidity_token = query_liquidity_token(deps.as_ref(), Addr::unchecked(pair_contract))?; + let pair_contract = deps.api.addr_validate(res.get_contract_address())?; + let liquidity_token = query_liquidity_token(deps.as_ref(), pair_contract.clone())?; PAIRS.save( deps.storage, &tmp_pair_info.pair_key, - &PairInfoRaw { - liquidity_token: deps.api.addr_canonicalize(liquidity_token.as_str())?, - contract_addr: deps.api.addr_canonicalize(pair_contract)?, + &PairInfo { + liquidity_token: liquidity_token.clone(), + contract_addr: pair_contract.clone(), asset_infos: tmp_pair_info.asset_infos, }, )?; @@ -171,7 +166,7 @@ pub fn reply(deps: DepsMut, _env: Env, msg: Reply) -> StdResult { messages: vec![], submessages: vec![], attributes: vec![ - attr("pair_contract_addr", pair_contract), + attr("pair_contract_addr", pair_contract.as_str()), attr("liquidity_token_addr", liquidity_token.as_str()), ], data: None, @@ -192,7 +187,7 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult { pub fn query_config(deps: Deps) -> StdResult { let state: Config = CONFIG.load(deps.storage)?; let resp = ConfigResponse { - owner: deps.api.addr_humanize(&state.owner)?.to_string(), + owner: state.owner.into(), token_code_id: state.token_code_id, pair_code_id: state.pair_code_id, }; @@ -201,12 +196,9 @@ pub fn query_config(deps: Deps) -> StdResult { } pub fn query_pair(deps: Deps, asset_infos: [AssetInfo; 2]) -> StdResult { - let pair_key = pair_key(&[ - asset_infos[0].to_raw(deps.api)?, - asset_infos[1].to_raw(deps.api)?, - ]); - let pair_info: PairInfoRaw = PAIRS.load(deps.storage, &pair_key)?; - pair_info.to_normal(deps.api) + let pair_key = pair_key(&asset_infos); + let pair_info: PairInfo = PAIRS.load(deps.storage, &pair_key)?; + Ok(pair_info) } pub fn query_pairs( @@ -214,16 +206,7 @@ pub fn query_pairs( start_after: Option<[AssetInfo; 2]>, limit: Option, ) -> StdResult { - let start_after = if let Some(start_after) = start_after { - Some([ - start_after[0].to_raw(deps.api)?, - start_after[1].to_raw(deps.api)?, - ]) - } else { - None - }; - - let pairs: Vec = read_pairs(deps.storage, deps.api, start_after, limit)?; + let pairs: Vec = read_pairs(deps.storage, start_after, limit)?; let resp = PairsResponse { pairs }; Ok(resp) diff --git a/contracts/tfi-factory/src/mock_querier.rs b/contracts/tfi-factory/src/mock_querier.rs index 364fb83..06bec1a 100644 --- a/contracts/tfi-factory/src/mock_querier.rs +++ b/contracts/tfi-factory/src/mock_querier.rs @@ -1,11 +1,10 @@ use cosmwasm_std::testing::{MockApi, MockQuerier, MockStorage, MOCK_CONTRACT_ADDR}; use cosmwasm_std::{ - from_slice, to_binary, Api, Coin, ContractResult, Empty, OwnedDeps, Querier, QuerierResult, + from_slice, to_binary, Coin, ContractResult, Empty, OwnedDeps, Querier, QuerierResult, QueryRequest, SystemError, SystemResult, WasmQuery, }; -use cosmwasm_storage::to_length_prefixed; use std::collections::HashMap; -use tfi::asset::{AssetInfoRaw, PairInfo, PairInfoRaw}; +use tfi::asset::{AssetInfo, PairInfo}; /// mock_dependencies is a drop-in replacement for cosmwasm_std::testing::mock_dependencies /// this uses our CustomQuerier. @@ -69,7 +68,7 @@ impl WasmMockQuerier { match &request { QueryRequest::Wasm(WasmQuery::Raw { contract_addr, key }) => { let key: &[u8] = key.as_slice(); - let prefix_pair_info = to_length_prefixed(b"pair_info").to_vec(); + let prefix_pair_info = b"pair_info".to_vec(); if key.to_vec() == prefix_pair_info { let pair_info: PairInfo = match self.tfi_pair_querier.pairs.get(contract_addr) { @@ -82,21 +81,12 @@ impl WasmMockQuerier { } }; - let api: MockApi = MockApi::default(); - SystemResult::Ok(ContractResult::from(to_binary(&PairInfoRaw { - contract_addr: api - .addr_canonicalize(pair_info.contract_addr.as_str()) - .unwrap(), - liquidity_token: api - .addr_canonicalize(pair_info.liquidity_token.as_str()) - .unwrap(), + SystemResult::Ok(ContractResult::from(to_binary(&PairInfo { + contract_addr: pair_info.contract_addr.clone(), + liquidity_token: pair_info.liquidity_token, asset_infos: [ - AssetInfoRaw::NativeToken { - denom: "uusd".to_string(), - }, - AssetInfoRaw::NativeToken { - denom: "uusd".to_string(), - }, + AssetInfo::Native("uusd".to_string()), + AssetInfo::Native("uusd".to_string()), ], }))) } else { diff --git a/contracts/tfi-factory/src/querier.rs b/contracts/tfi-factory/src/querier.rs index b5805d4..90adcf1 100644 --- a/contracts/tfi-factory/src/querier.rs +++ b/contracts/tfi-factory/src/querier.rs @@ -1,12 +1,12 @@ use cosmwasm_std::{Addr, Binary, Deps, QueryRequest, StdResult, WasmQuery}; -use tfi::asset::PairInfoRaw; +use tfi::asset::PairInfo; pub fn query_liquidity_token(deps: Deps, contract_addr: Addr) -> StdResult { // load pair_info form the pair contract - let pair_info: PairInfoRaw = deps.querier.query(&QueryRequest::Wasm(WasmQuery::Raw { + let pair_info: PairInfo = deps.querier.query(&QueryRequest::Wasm(WasmQuery::Raw { contract_addr: contract_addr.to_string(), - key: Binary::from("\u{0}\u{9}pair_info".as_bytes()), + key: Binary::from("pair_info".as_bytes()), }))?; - deps.api.addr_humanize(&pair_info.liquidity_token) + Ok(pair_info.liquidity_token) } diff --git a/contracts/tfi-factory/src/state.rs b/contracts/tfi-factory/src/state.rs index 5b96f5c..fab9c3f 100644 --- a/contracts/tfi-factory/src/state.rs +++ b/contracts/tfi-factory/src/state.rs @@ -1,30 +1,29 @@ use schemars::JsonSchema; use serde::{Deserialize, Serialize}; -use cosmwasm_std::{Api, CanonicalAddr, Order, StdResult, Storage}; +use cosmwasm_std::{Addr, Order, StdResult, Storage}; use cw_storage_plus::{Bound, Item, Map}; -use tfi::asset::{AssetInfoRaw, PairInfo, PairInfoRaw}; +use tfi::asset::{AssetInfo, PairInfo}; #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] pub struct Config { - pub owner: CanonicalAddr, + pub owner: Addr, pub pair_code_id: u64, pub token_code_id: u64, } -// put the length bytes at the first for compatibility with legacy singleton store -pub const CONFIG: Item = Item::new("\u{0}\u{6}config"); +pub const CONFIG: Item = Item::new("config"); #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] pub struct TmpPairInfo { pub pair_key: Vec, - pub asset_infos: [AssetInfoRaw; 2], + pub asset_infos: [AssetInfo; 2], } pub const TMP_PAIR_INFO: Item = Item::new("tmp_pair_info"); -pub const PAIRS: Map<&[u8], PairInfoRaw> = Map::new("pair_info"); +pub const PAIRS: Map<&[u8], PairInfo> = Map::new("pair_info"); -pub fn pair_key(asset_infos: &[AssetInfoRaw; 2]) -> Vec { +pub fn pair_key(asset_infos: &[AssetInfo; 2]) -> Vec { let mut asset_infos = asset_infos.to_vec(); asset_infos.sort_by(|a, b| a.as_bytes().cmp(&b.as_bytes())); @@ -36,8 +35,7 @@ const MAX_LIMIT: u32 = 30; const DEFAULT_LIMIT: u32 = 10; pub fn read_pairs( storage: &dyn Storage, - api: &dyn Api, - start_after: Option<[AssetInfoRaw; 2]>, + start_after: Option<[AssetInfo; 2]>, limit: Option, ) -> StdResult> { let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize; @@ -46,15 +44,12 @@ pub fn read_pairs( PAIRS .range(storage, start, None, Order::Ascending) .take(limit) - .map(|item| { - let (_, v) = item?; - v.to_normal(api) - }) - .collect::>>() + .map(|item| Ok(item?.1)) + .collect() } // this will set the first key after the provided key, by appending a 1 byte -fn calc_range_start(start_after: Option<[AssetInfoRaw; 2]>) -> Option> { +fn calc_range_start(start_after: Option<[AssetInfo; 2]>) -> Option> { start_after.map(|asset_infos| { let mut asset_infos = asset_infos.to_vec(); asset_infos.sort_by(|a, b| a.as_bytes().cmp(&b.as_bytes())); @@ -67,134 +62,3 @@ fn calc_range_start(start_after: Option<[AssetInfoRaw; 2]>) -> Option> { v }) } - -#[cfg(test)] -mod test { - use super::*; - - use cosmwasm_std::testing::mock_dependencies; - use cosmwasm_std::{Api, StdResult, Storage}; - use cosmwasm_storage::{ - bucket, bucket_read, singleton, singleton_read, Bucket, ReadonlyBucket, - }; - const KEY_CONFIG: &[u8] = b"config"; - - pub fn store_config(storage: &mut dyn Storage, config: &Config) -> StdResult<()> { - singleton(storage, KEY_CONFIG).save(config) - } - pub fn read_config(storage: &dyn Storage) -> StdResult { - singleton_read(storage, KEY_CONFIG).load() - } - - #[test] - fn config_legacy_compatibility() { - let mut deps = mock_dependencies(&[]); - store_config( - &mut deps.storage, - &Config { - owner: deps.api.addr_canonicalize("owner0000").unwrap(), - pair_code_id: 1, - token_code_id: 1, - }, - ) - .unwrap(); - - assert_eq!( - CONFIG.load(&deps.storage).unwrap(), - read_config(&deps.storage).unwrap() - ); - } - - const PREFIX_PAIR_INFO: &[u8] = b"pair_info"; - pub fn store_pair(storage: &mut dyn Storage, data: &PairInfoRaw) -> StdResult<()> { - let mut asset_infos = data.asset_infos.clone().to_vec(); - asset_infos.sort_by(|a, b| a.as_bytes().cmp(&b.as_bytes())); - - let mut pair_bucket: Bucket = bucket(storage, PREFIX_PAIR_INFO); - pair_bucket.save( - &[asset_infos[0].as_bytes(), asset_infos[1].as_bytes()].concat(), - &data, - ) - } - pub fn read_pair( - storage: &dyn Storage, - asset_infos: &[AssetInfoRaw; 2], - ) -> StdResult { - let mut asset_infos = asset_infos.clone().to_vec(); - asset_infos.sort_by(|a, b| a.as_bytes().cmp(&b.as_bytes())); - - let pair_bucket: ReadonlyBucket = bucket_read(storage, PREFIX_PAIR_INFO); - pair_bucket.load(&[asset_infos[0].as_bytes(), asset_infos[1].as_bytes()].concat()) - } - - pub fn legacy_read_pairs( - storage: &dyn Storage, - api: &dyn Api, - start_after: Option<[AssetInfoRaw; 2]>, - limit: Option, - ) -> StdResult> { - let pair_bucket: ReadonlyBucket = bucket_read(storage, PREFIX_PAIR_INFO); - let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize; - let start = calc_range_start(start_after); - pair_bucket - .range(start.as_deref(), None, Order::Ascending) - .take(limit) - .map(|item| { - let (_, v) = item?; - v.to_normal(api) - }) - .collect() - } - - #[test] - fn pair_info_legacy_compatibility() { - let mut deps = mock_dependencies(&[]); - let pair_info = PairInfoRaw { - asset_infos: [ - AssetInfoRaw::NativeToken { - denom: "uusd".to_string(), - }, - AssetInfoRaw::Token { - contract_addr: deps.api.addr_canonicalize("token0000").unwrap(), - }, - ], - contract_addr: deps.api.addr_canonicalize("pair0000").unwrap(), - liquidity_token: deps.api.addr_canonicalize("liquidity0000").unwrap(), - }; - - let pair_info2 = PairInfoRaw { - asset_infos: [ - AssetInfoRaw::NativeToken { - denom: "uusd".to_string(), - }, - AssetInfoRaw::Token { - contract_addr: deps.api.addr_canonicalize("token0001").unwrap(), - }, - ], - contract_addr: deps.api.addr_canonicalize("pair0001").unwrap(), - liquidity_token: deps.api.addr_canonicalize("liquidity0001").unwrap(), - }; - - store_pair(&mut deps.storage, &pair_info).unwrap(); - store_pair(&mut deps.storage, &pair_info2).unwrap(); - - assert_eq!( - PAIRS - .load(&deps.storage, &pair_key(&pair_info.asset_infos)) - .unwrap(), - read_pair(&deps.storage, &pair_info.asset_infos).unwrap() - ); - - assert_eq!( - PAIRS - .load(&deps.storage, &pair_key(&pair_info2.asset_infos)) - .unwrap(), - read_pair(&deps.storage, &pair_info2.asset_infos).unwrap() - ); - - assert_eq!( - read_pairs(&deps.storage, &deps.api, None, None), - legacy_read_pairs(&deps.storage, &deps.api, None, None), - ); - } -} diff --git a/contracts/tfi-factory/src/testing.rs b/contracts/tfi-factory/src/testing.rs index d2a789f..322a566 100644 --- a/contracts/tfi-factory/src/testing.rs +++ b/contracts/tfi-factory/src/testing.rs @@ -116,12 +116,8 @@ fn create_pair() { let _res = instantiate(deps.as_mut(), env, info, msg).unwrap(); let asset_infos = [ - AssetInfo::Token { - contract_addr: Addr::unchecked("asset0000"), - }, - AssetInfo::Token { - contract_addr: Addr::unchecked("asset0001"), - }, + AssetInfo::Token(Addr::unchecked("asset0000")), + AssetInfo::Token(Addr::unchecked("asset0001")), ]; let msg = ExecuteMsg::CreatePair { @@ -159,16 +155,11 @@ fn create_pair() { },] ); - let raw_infos = [ - asset_infos[0].to_raw(deps.as_ref().api).unwrap(), - asset_infos[1].to_raw(deps.as_ref().api).unwrap(), - ]; - assert_eq!( TMP_PAIR_INFO.load(&deps.storage).unwrap(), TmpPairInfo { - asset_infos: raw_infos.clone(), - pair_key: pair_key(&raw_infos), + asset_infos: asset_infos.clone(), + pair_key: pair_key(&asset_infos), } ); } @@ -178,25 +169,16 @@ fn reply_test() { let mut deps = mock_dependencies(&[]); let asset_infos = [ - AssetInfo::Token { - contract_addr: Addr::unchecked("asset0000"), - }, - AssetInfo::Token { - contract_addr: Addr::unchecked("asset0001"), - }, - ]; - - let raw_infos = [ - asset_infos[0].to_raw(deps.as_ref().api).unwrap(), - asset_infos[1].to_raw(deps.as_ref().api).unwrap(), + AssetInfo::Token(Addr::unchecked("asset0000")), + AssetInfo::Token(Addr::unchecked("asset0001")), ]; - let pair_key = pair_key(&raw_infos); + let pair_key = pair_key(&asset_infos); TMP_PAIR_INFO .save( &mut deps.storage, &TmpPairInfo { - asset_infos: raw_infos, + asset_infos: asset_infos.clone(), pair_key, }, ) @@ -215,12 +197,8 @@ fn reply_test() { &"pair0000".to_string(), &PairInfo { asset_infos: [ - AssetInfo::NativeToken { - denom: "uusd".to_string(), - }, - AssetInfo::NativeToken { - denom: "uusd".to_string(), - }, + AssetInfo::Native("uusd".to_string()), + AssetInfo::Native("uusd".to_string()), ], contract_addr: Addr::unchecked("pair0000"), liquidity_token: Addr::unchecked("liquidity0000"), From f201e7259d175100a61caf2205097b753383689a Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 23 Jun 2021 15:06:44 +0200 Subject: [PATCH 5/6] Update tfi-router --- .../tfi-router/schema/cw20_hook_msg.json | 24 ++----- contracts/tfi-router/schema/execute_msg.json | 24 ++----- contracts/tfi-router/schema/query_msg.json | 24 ++----- contracts/tfi-router/src/contract.rs | 64 +++++-------------- contracts/tfi-router/src/operations.rs | 10 ++- 5 files changed, 32 insertions(+), 114 deletions(-) diff --git a/contracts/tfi-router/schema/cw20_hook_msg.json b/contracts/tfi-router/schema/cw20_hook_msg.json index 73f2625..8ad1166 100644 --- a/contracts/tfi-router/schema/cw20_hook_msg.json +++ b/contracts/tfi-router/schema/cw20_hook_msg.json @@ -57,15 +57,7 @@ ], "properties": { "token": { - "type": "object", - "required": [ - "contract_addr" - ], - "properties": { - "contract_addr": { - "$ref": "#/definitions/Addr" - } - } + "$ref": "#/definitions/Addr" } }, "additionalProperties": false @@ -73,19 +65,11 @@ { "type": "object", "required": [ - "native_token" + "native" ], "properties": { - "native_token": { - "type": "object", - "required": [ - "denom" - ], - "properties": { - "denom": { - "type": "string" - } - } + "native": { + "type": "string" } }, "additionalProperties": false diff --git a/contracts/tfi-router/schema/execute_msg.json b/contracts/tfi-router/schema/execute_msg.json index cb4c19c..b077e15 100644 --- a/contracts/tfi-router/schema/execute_msg.json +++ b/contracts/tfi-router/schema/execute_msg.json @@ -130,15 +130,7 @@ ], "properties": { "token": { - "type": "object", - "required": [ - "contract_addr" - ], - "properties": { - "contract_addr": { - "$ref": "#/definitions/Addr" - } - } + "$ref": "#/definitions/Addr" } }, "additionalProperties": false @@ -146,19 +138,11 @@ { "type": "object", "required": [ - "native_token" + "native" ], "properties": { - "native_token": { - "type": "object", - "required": [ - "denom" - ], - "properties": { - "denom": { - "type": "string" - } - } + "native": { + "type": "string" } }, "additionalProperties": false diff --git a/contracts/tfi-router/schema/query_msg.json b/contracts/tfi-router/schema/query_msg.json index 15b61b0..3090467 100644 --- a/contracts/tfi-router/schema/query_msg.json +++ b/contracts/tfi-router/schema/query_msg.json @@ -57,15 +57,7 @@ ], "properties": { "token": { - "type": "object", - "required": [ - "contract_addr" - ], - "properties": { - "contract_addr": { - "$ref": "#/definitions/Addr" - } - } + "$ref": "#/definitions/Addr" } }, "additionalProperties": false @@ -73,19 +65,11 @@ { "type": "object", "required": [ - "native_token" + "native" ], "properties": { - "native_token": { - "type": "object", - "required": [ - "denom" - ], - "properties": { - "denom": { - "type": "string" - } - } + "native": { + "type": "string" } }, "additionalProperties": false diff --git a/contracts/tfi-router/src/contract.rs b/contracts/tfi-router/src/contract.rs index a7baa61..ec55286 100644 --- a/contracts/tfi-router/src/contract.rs +++ b/contracts/tfi-router/src/contract.rs @@ -285,20 +285,12 @@ fn test_invalid_operations() { true, assert_operations(&[ SwapOperation { - offer_asset_info: AssetInfo::NativeToken { - denom: "ukrw".to_string(), - }, - ask_asset_info: AssetInfo::Token { - contract_addr: Addr::unchecked("asset0001"), - }, + offer_asset_info: AssetInfo::Native("ukrw".to_string()), + ask_asset_info: AssetInfo::Token(Addr::unchecked("asset0001")), }, SwapOperation { - offer_asset_info: AssetInfo::Token { - contract_addr: Addr::unchecked("asset0001"), - }, - ask_asset_info: AssetInfo::NativeToken { - denom: "uluna".to_string(), - }, + offer_asset_info: AssetInfo::Token(Addr::unchecked("asset0001")), + ask_asset_info: AssetInfo::Native("uluna".to_string()), } ]) .is_ok() @@ -309,28 +301,16 @@ fn test_invalid_operations() { true, assert_operations(&[ SwapOperation { - offer_asset_info: AssetInfo::NativeToken { - denom: "ukrw".to_string(), - }, - ask_asset_info: AssetInfo::Token { - contract_addr: Addr::unchecked("asset0001"), - }, + offer_asset_info: AssetInfo::Native("ukrw".to_string()), + ask_asset_info: AssetInfo::Token(Addr::unchecked("asset0001")), }, SwapOperation { - offer_asset_info: AssetInfo::Token { - contract_addr: Addr::unchecked("asset0001"), - }, - ask_asset_info: AssetInfo::NativeToken { - denom: "uluna".to_string(), - }, + offer_asset_info: AssetInfo::Token(Addr::unchecked("asset0001")), + ask_asset_info: AssetInfo::Native("uluna".to_string()), }, SwapOperation { - offer_asset_info: AssetInfo::NativeToken { - denom: "uluna".to_string(), - }, - ask_asset_info: AssetInfo::Token { - contract_addr: Addr::unchecked("asset0002"), - }, + offer_asset_info: AssetInfo::Native("uluna".to_string()), + ask_asset_info: AssetInfo::Token(Addr::unchecked("asset0002")), }, ]) .is_ok() @@ -341,28 +321,16 @@ fn test_invalid_operations() { true, assert_operations(&[ SwapOperation { - offer_asset_info: AssetInfo::NativeToken { - denom: "ukrw".to_string(), - }, - ask_asset_info: AssetInfo::Token { - contract_addr: Addr::unchecked("asset0001"), - }, + offer_asset_info: AssetInfo::Native("ukrw".to_string()), + ask_asset_info: AssetInfo::Token(Addr::unchecked("asset0001")), }, SwapOperation { - offer_asset_info: AssetInfo::Token { - contract_addr: Addr::unchecked("asset0001"), - }, - ask_asset_info: AssetInfo::NativeToken { - denom: "uaud".to_string(), - }, + offer_asset_info: AssetInfo::Token(Addr::unchecked("asset0001")), + ask_asset_info: AssetInfo::Native("uaud".to_string()), }, SwapOperation { - offer_asset_info: AssetInfo::NativeToken { - denom: "uluna".to_string(), - }, - ask_asset_info: AssetInfo::Token { - contract_addr: Addr::unchecked("asset0002"), - }, + offer_asset_info: AssetInfo::Native("uluna".to_string()), + ask_asset_info: AssetInfo::Token(Addr::unchecked("asset0002")), }, ]) .is_err() diff --git a/contracts/tfi-router/src/operations.rs b/contracts/tfi-router/src/operations.rs index 560d1d2..04717d5 100644 --- a/contracts/tfi-router/src/operations.rs +++ b/contracts/tfi-router/src/operations.rs @@ -37,10 +37,8 @@ pub fn execute_swap_operation( )?; let amount = match offer_asset_info.clone() { - AssetInfo::NativeToken { denom } => { - query_balance(&deps.querier, env.contract.address, denom)? - } - AssetInfo::Token { contract_addr } => { + AssetInfo::Native(denom) => query_balance(&deps.querier, env.contract.address, denom)?, + AssetInfo::Token(contract_addr) => { query_token_balance(&deps.querier, contract_addr, env.contract.address)? } }; @@ -71,7 +69,7 @@ pub fn asset_into_swap_msg( to: Option, ) -> StdResult { match offer_asset.info.clone() { - AssetInfo::NativeToken { denom } => { + AssetInfo::Native(denom) => { let amount = offer_asset.amount; Ok(CosmosMsg::Wasm(WasmMsg::Execute { @@ -88,7 +86,7 @@ pub fn asset_into_swap_msg( })?, })) } - AssetInfo::Token { contract_addr } => Ok(CosmosMsg::Wasm(WasmMsg::Execute { + AssetInfo::Token(contract_addr) => Ok(CosmosMsg::Wasm(WasmMsg::Execute { contract_addr: contract_addr.to_string(), send: vec![], msg: to_binary(&Cw20ExecuteMsg::Send { From e0a03e30e976e4dc7561c1fa961b630315831c70 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 23 Jun 2021 15:16:11 +0200 Subject: [PATCH 6/6] Validate address where appropriate --- contracts/tfi-pair/src/contract.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/contracts/tfi-pair/src/contract.rs b/contracts/tfi-pair/src/contract.rs index db4c4b1..ec7e7e9 100644 --- a/contracts/tfi-pair/src/contract.rs +++ b/contracts/tfi-pair/src/contract.rs @@ -25,6 +25,7 @@ use tfi::token::InstantiateMsg as TokenInstantiateMsg; /// Commission rate == 0.3% const COMMISSION_RATE: &str = "0.003"; + #[cfg_attr(not(feature = "library"), entry_point)] pub fn instantiate( deps: DepsMut, @@ -34,7 +35,7 @@ pub fn instantiate( ) -> StdResult { let pair_info: &PairInfo = &PairInfo { contract_addr: env.contract.address.clone(), - // TODO: fix this + // ugly placeholder, but we set this in the callback liquidity_token: Addr::unchecked(""), asset_infos: msg.asset_infos, }; @@ -151,11 +152,12 @@ pub fn receive_cw20( None }; + let api = deps.api; swap( deps, env, info, - Addr::unchecked(cw20_msg.sender), + api.addr_validate(&cw20_msg.sender)?, Asset { info: AssetInfo::Token(contract_addr), amount: cw20_msg.amount,