From adcab3f42dc4b8c121d1beef332f29cdf4181ba2 Mon Sep 17 00:00:00 2001 From: AntonAndell Date: Thu, 2 Nov 2023 15:10:52 +0100 Subject: [PATCH 1/6] feat: add native withdraws to assetManager for archway --- .../cw-asset-manager/Cargo.toml | 2 + .../cw-asset-manager/src/contract.rs | 175 +++++++++++++++++- .../cw-asset-manager/src/external.rs | 54 ++++++ .../cw-asset-manager/src/helpers.rs | 25 +++ .../cw-asset-manager/src/lib.rs | 1 + .../cw-asset-manager/src/state.rs | 3 + contracts/cw-common/src/asset_manager_msg.rs | 8 + contracts/cw-common/src/xcall_data_types.rs | 18 ++ 8 files changed, 284 insertions(+), 2 deletions(-) create mode 100644 contracts/core-contracts/cw-asset-manager/src/external.rs diff --git a/contracts/core-contracts/cw-asset-manager/Cargo.toml b/contracts/core-contracts/cw-asset-manager/Cargo.toml index 62c3a3b..6f5ddeb 100644 --- a/contracts/core-contracts/cw-asset-manager/Cargo.toml +++ b/contracts/core-contracts/cw-asset-manager/Cargo.toml @@ -20,6 +20,8 @@ crate-type = ["cdylib", "rlib"] backtraces = ["cosmwasm-std/backtraces"] # use library feature to disable all instantiate/execute/query exports library = [] +default = ["archway"] +archway = [] [package.metadata.scripts] optimize = """docker run --rm -v "$(pwd)":/code \ diff --git a/contracts/core-contracts/cw-asset-manager/src/contract.rs b/contracts/core-contracts/cw-asset-manager/src/contract.rs index ca2fee2..a16f5be 100644 --- a/contracts/core-contracts/cw-asset-manager/src/contract.rs +++ b/contracts/core-contracts/cw-asset-manager/src/contract.rs @@ -8,7 +8,7 @@ use cosmwasm_std::{ use cw2::set_contract_version; use cw20::{AllowanceResponse, Cw20ExecuteMsg, Cw20QueryMsg}; -use cw_common::asset_manager_msg::{ExecuteMsg, InstantiateMsg, QueryMsg}; +use cw_common::asset_manager_msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg}; use cw_common::network_address::IconAddressValidation; use cw_common::network_address::NetworkAddress; use cw_common::x_call_msg::{GetNetworkAddress, XCallMsg}; @@ -57,7 +57,14 @@ pub fn execute( ExecuteMsg::HandleCallMessage { from, data } => { exec::handle_xcall_msg(deps, env, info, from, data) } - + ExecuteMsg::ConfigureNative { + native_token_address, + native_token_manager, + } => { + let owner = OWNER.load(deps.storage).map_err(ContractError::Std)?; + ensure_eq!(owner, info.sender, ContractError::OnlyOwner); + exec::setup_native_token(deps, native_token_address, native_token_manager) + } ExecuteMsg::Deposit { token_address, amount, @@ -160,6 +167,25 @@ mod exec { Ok(Response::default()) } + pub fn setup_native_token( + deps: DepsMut, + native_token_address: String, + native_token_manager: String, + ) -> Result { + let token_addr = deps + .api + .addr_validate(&native_token_address) + .map_err(ContractError::Std)?; + let token_manager_addr = deps + .api + .addr_validate(&native_token_manager) + .map_err(ContractError::Std)?; + NATIVE_TOKEN_ADDRESS.save(deps.storage, &token_addr)?; + NATIVE_TOKEN_MANAGER.save(deps.storage, &token_manager_addr)?; + + Ok(Response::default()) + } + #[allow(clippy::too_many_arguments)] pub fn deposit_cw20_tokens( deps: DepsMut, @@ -307,6 +333,19 @@ mod exec { transfer_tokens(deps, account, token_address, amount)? } + + DecodedStruct::WithdrawNativeTo(data_struct) => { + let icon_am = ICON_ASSET_MANAGER.load(deps.storage)?; + if from != icon_am.to_string() { + return Err(ContractError::OnlyIconAssetManager {}); + } + + let token_address = data_struct.token_address; + let account = data_struct.user_address; + let amount = Uint128::from(data_struct.amount); + + swap_to_native(deps, account, token_address, amount)? + } }; Ok(res) @@ -341,6 +380,68 @@ mod exec { }; Ok(Response::new().add_submessage(sub_msg)) } + + #[cfg(feature = "archway")] + fn swap_to_native( + deps: DepsMut, + account: String, + token_address: String, + amount: Uint128, + ) -> Result { + use crate::external::{ConfigResponse, Cw20HookMsg, StakingQueryMsg}; + + deps.api.addr_validate(&account)?; + deps.api.addr_validate(&token_address)?; + let query_msg = &StakingQueryMsg::ConfigInfo {}; + let manager = NATIVE_TOKEN_MANAGER.load(deps.storage)?; + let query_resp: ConfigResponse = deps + .querier + .query_wasm_smart::(manager.clone(), &query_msg)?; + let swap_contract = query_resp.swap_contract_addr; + + let hook = &Cw20HookMsg::Swap { + belief_price: None, + max_spread: None, + to: Some(account.clone()), + }; + let transfer_msg = &Cw20ExecuteMsg::Send { + contract: swap_contract.clone(), + amount, + msg: to_binary(hook)?, + }; + + let execute_msg: CosmosMsg = CosmosMsg::Wasm(WasmMsg::Execute { + contract_addr: token_address.clone(), + msg: to_binary(transfer_msg)?, + funds: vec![], + }); + + let sub_msg = SubMsg { + id: SUCCESS_REPLY_MSG, + msg: execute_msg, + gas_limit: None, + reply_on: cosmwasm_std::ReplyOn::Never, + }; + Ok(Response::new().add_submessage(sub_msg)) + } + + #[cfg(not(any(feature = "archway")))] + fn swap_to_native( + deps: DepsMut, + account: String, + token_address: String, + amount: Uint128, + ) -> Result { + transfer_tokens(deps, account, token_address, amount) + } +} + +#[cfg_attr(not(feature = "library"), entry_point)] +pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result { + set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION) + .map_err(ContractError::Std)?; + + Ok(Response::default().add_attribute("migrate", "successful")) } #[cfg_attr(not(feature = "library"), entry_point)] @@ -640,6 +741,76 @@ mod tests { assert!(result.is_err()); } + #[cfg(feature = "archway")] + #[test] + fn test_withdraw_native_archway() { + use cw_common::xcall_data_types::WithdrawNativeTo; + + use crate::external::ConfigResponse; + + let (mut deps, env, info, _) = test_setup(); + let mocked_xcall_info = mock_info("xcall", &[]); + + let staking = "staking"; + let swap = "swap"; + let token = "token1"; + let account = "account1"; + + deps.querier.update_wasm(|r: &WasmQuery| match r { + WasmQuery::Smart { + contract_addr: _, + msg: _, + } => SystemResult::Ok(ContractResult::Ok( + to_binary(&ConfigResponse { + admin: "".to_string(), + pause_admin: "".to_string(), + bond_denom: "".to_string(), + liquid_token_addr: "".to_string(), + swap_contract_addr: swap.to_string(), + treasury_contract_addr: "".to_string(), + team_wallet_addr: "".to_string(), + commission_percentage: 1, + team_percentage: 1, + liquidity_percentage: 1, + delegations: vec![], + contract_state: false, + }) + .unwrap(), + )), + _ => todo!(), + }); + + let resp = execute( + deps.as_mut(), + env.clone(), + info, + ExecuteMsg::ConfigureNative { + native_token_address: token.to_string(), + native_token_manager: staking.to_string(), + }, + ); + assert!(resp.is_ok()); + + let am_nw = "0x01.icon/cxc2d01de5013778d71d99f985e4e2ff3a9b48a66c"; + let withdraw_msg = WithdrawNativeTo { + token_address: token.to_string(), + amount: 1000, + user_address: account.to_string(), + }; + + let exe_msg = ExecuteMsg::HandleCallMessage { + from: am_nw.to_string(), + data: withdraw_msg.rlp_bytes().to_vec(), + }; + let resp = execute( + deps.as_mut(), + env.clone(), + mocked_xcall_info.clone(), + exe_msg, + ); + assert!(resp.is_ok()); + } + #[test] fn test_configure_network() { //verify configuration updates from owner side diff --git a/contracts/core-contracts/cw-asset-manager/src/external.rs b/contracts/core-contracts/cw-asset-manager/src/external.rs new file mode 100644 index 0000000..47c2bc3 --- /dev/null +++ b/contracts/core-contracts/cw-asset-manager/src/external.rs @@ -0,0 +1,54 @@ +use cosmwasm_schema::cw_serde; +use cosmwasm_std::Decimal; +use schemars::JsonSchema; +use serde::{Deserialize, Serialize}; + +#[cw_serde] +pub enum StakingQueryMsg { + ConfigInfo {}, +} + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] +pub struct ConfigResponse { + /// Should be multi-sig, is able to update the config + pub admin: String, + /// Should be single-sig, is able to pause the contract + pub pause_admin: String, + /// This is the denomination we can stake (and only one we accept for payments) + pub bond_denom: String, + /// Liquid token address + pub liquid_token_addr: String, + /// Swap contract address + pub swap_contract_addr: String, + /// Liquid Treasury contract address + pub treasury_contract_addr: String, + /// Team wallet address + pub team_wallet_addr: String, + /// percentage of commission taken off of staking rewards + pub commission_percentage: u16, + /// percentage of rewards for the team + pub team_percentage: u16, + /// percentage of rewards for the liquidity providers + pub liquidity_percentage: u16, + /// Delegations preferences for a whitelist of validators, each validator has a delegation percentage + pub delegations: Vec, + /// contract state (active/paused) + pub contract_state: bool, +} + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] +pub struct DelegationPercentage { + pub validator: String, + pub percentage: u16, +} + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] +#[serde(rename_all = "snake_case")] +pub enum Cw20HookMsg { + /// Sell a given amount of asset + Swap { + belief_price: Option, + max_spread: Option, + to: Option, + }, +} diff --git a/contracts/core-contracts/cw-asset-manager/src/helpers.rs b/contracts/core-contracts/cw-asset-manager/src/helpers.rs index 04d00bd..fea7fa9 100644 --- a/contracts/core-contracts/cw-asset-manager/src/helpers.rs +++ b/contracts/core-contracts/cw-asset-manager/src/helpers.rs @@ -8,6 +8,7 @@ use crate::error::ContractError; #[derive(Debug)] pub enum DecodedStruct { WithdrawTo(WithdrawTo), + WithdrawNativeTo(WithdrawTo), DepositRevert(DepositRevert), } @@ -45,6 +46,30 @@ pub fn decode_encoded_bytes(data: &[u8]) -> Result<(&str, DecodedStruct), Contra Ok(("WithdrawTo", DecodedStruct::WithdrawTo(withdraw_to))) } + "WithdrawNativeTo" => { + if rlp.item_count()? != 4 { + return Err(DecoderError::RlpInvalidLength.into()); + } + + // Extract the fields + let token: String = rlp.val_at(1)?; + let user_address: String = rlp.val_at(2)?; + let amount: u128 = rlp.val_at(3)?; + + // Create a new WithdrawTo instance + let withdraw_to = WithdrawTo { + token_address: token, + user_address, + amount, + }; + + // Return the decoded struct as an OK variant + Ok(( + "WithdrawNativeTo", + DecodedStruct::WithdrawNativeTo(withdraw_to), + )) + } + "DepositRevert" => { if rlp.item_count()? != 4 { return Err(DecoderError::RlpInvalidLength.into()); diff --git a/contracts/core-contracts/cw-asset-manager/src/lib.rs b/contracts/core-contracts/cw-asset-manager/src/lib.rs index 39e6d15..fa201cd 100644 --- a/contracts/core-contracts/cw-asset-manager/src/lib.rs +++ b/contracts/core-contracts/cw-asset-manager/src/lib.rs @@ -1,6 +1,7 @@ pub mod constants; pub mod contract; mod error; +pub mod external; pub mod helpers; pub mod state; pub use crate::error::ContractError; diff --git a/contracts/core-contracts/cw-asset-manager/src/state.rs b/contracts/core-contracts/cw-asset-manager/src/state.rs index c369f24..3621369 100644 --- a/contracts/core-contracts/cw-asset-manager/src/state.rs +++ b/contracts/core-contracts/cw-asset-manager/src/state.rs @@ -12,3 +12,6 @@ pub const NID: Item = Item::new("network_id"); pub const ICON_ASSET_MANAGER: Item = Item::new("icon_asset_manager_network_address"); pub const ICON_NET_ID: Item = Item::new("icon_asset_manager_network_id"); + +pub const NATIVE_TOKEN_ADDRESS: Item = Item::new("native_token_address"); +pub const NATIVE_TOKEN_MANAGER: Item = Item::new("native_token_manager"); diff --git a/contracts/cw-common/src/asset_manager_msg.rs b/contracts/cw-common/src/asset_manager_msg.rs index 93fcdd9..8468d07 100644 --- a/contracts/cw-common/src/asset_manager_msg.rs +++ b/contracts/cw-common/src/asset_manager_msg.rs @@ -25,6 +25,11 @@ pub enum ExecuteMsg { destination_asset_manager: String, }, + ConfigureNative { + native_token_address: String, + native_token_manager: String, + }, + HandleCallMessage { from: String, data: Vec, @@ -58,3 +63,6 @@ pub struct NetIdResponse { pub x_call_nid: String, //NetID pub icon_nid: String, //NetID } + +#[cw_serde] +pub struct MigrateMsg {} diff --git a/contracts/cw-common/src/xcall_data_types.rs b/contracts/cw-common/src/xcall_data_types.rs index 67004ea..6502276 100644 --- a/contracts/cw-common/src/xcall_data_types.rs +++ b/contracts/cw-common/src/xcall_data_types.rs @@ -27,6 +27,13 @@ pub struct WithdrawTo { pub amount: u128, } +#[cw_serde] +pub struct WithdrawNativeTo { + pub token_address: String, + pub user_address: String, + pub amount: u128, +} + //for testing impl Encodable for Deposit { //specify the encoding logic for struct's fields so that rlp_bytes() can alo use @@ -65,6 +72,17 @@ impl Encodable for WithdrawTo { } } +impl Encodable for WithdrawNativeTo { + fn rlp_append(&self, s: &mut RlpStream) { + let method = "WithdrawNativeTo".to_string(); + s.begin_list(4) + .append(&method) + .append(&self.token_address) + .append(&self.user_address) + .append(&self.amount); + } +} + #[cfg(test)] mod tests { use super::*; From bca10dd23e31a57b758a5791a17f783563e583b8 Mon Sep 17 00:00:00 2001 From: AntonAndell Date: Mon, 6 Nov 2023 09:33:18 +0100 Subject: [PATCH 2/6] fix/43 remove reply logic --- .../cw-hub-bnusd/src/constants.rs | 2 -- .../cw-hub-bnusd/src/contract.rs | 22 +++---------------- .../token-contracts/cw-hub-bnusd/src/error.rs | 2 -- .../cw-hub-bnusd/tests/setup.rs | 4 ++-- 4 files changed, 5 insertions(+), 25 deletions(-) diff --git a/contracts/token-contracts/cw-hub-bnusd/src/constants.rs b/contracts/token-contracts/cw-hub-bnusd/src/constants.rs index 475f938..8c37fd6 100644 --- a/contracts/token-contracts/cw-hub-bnusd/src/constants.rs +++ b/contracts/token-contracts/cw-hub-bnusd/src/constants.rs @@ -1,8 +1,6 @@ -//Constants for Reply messages use cosmwasm_std::Uint128; -pub const REPLY_MSG_SUCCESS: u64 = 1; pub const X_CROSS_TRANSFER: &str = "xCrossTransfer"; pub const X_CROSS_TRANSFER_REVERT: &str = "xCrossTransferRevert"; pub const TOKEN_NAME: &str = "Balanced Dollar"; diff --git a/contracts/token-contracts/cw-hub-bnusd/src/contract.rs b/contracts/token-contracts/cw-hub-bnusd/src/contract.rs index fe03e8b..211312f 100644 --- a/contracts/token-contracts/cw-hub-bnusd/src/contract.rs +++ b/contracts/token-contracts/cw-hub-bnusd/src/contract.rs @@ -1,7 +1,7 @@ use std::str::FromStr; use crate::constants::{ - REPLY_MSG_SUCCESS, TOKEN_DECIMALS, TOKEN_NAME, TOKEN_SYMBOL, TOKEN_TOTAL_SUPPLY, + TOKEN_DECIMALS, TOKEN_NAME, TOKEN_SYMBOL, TOKEN_TOTAL_SUPPLY, X_CROSS_TRANSFER, X_CROSS_TRANSFER_REVERT, }; use crate::error::ContractError; @@ -13,8 +13,7 @@ use cw_common::network_address::IconAddressValidation; #[cfg(not(feature = "library"))] use cosmwasm_std::entry_point; use cosmwasm_std::{ - to_binary, Addr, Binary, Deps, DepsMut, Empty, Env, MessageInfo, QueryRequest, Reply, Response, - StdError, StdResult, WasmQuery, + to_binary, Addr, Binary, Deps, DepsMut, Empty, Env, MessageInfo, QueryRequest, Response, StdResult, WasmQuery, }; use cw2::set_contract_version; @@ -162,13 +161,6 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult { } } -#[cfg_attr(not(feature = "library"), entry_point)] -pub fn reply(deps: DepsMut, env: Env, msg: Reply) -> Result { - match msg.id { - REPLY_MSG_SUCCESS => reply_msg_success(deps, env, msg), - _ => Err(ContractError::InvalidReply), - } -} #[cfg_attr(not(feature = "library"), entry_point)] pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result { @@ -178,14 +170,6 @@ pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result Result { - match msg.result { - cosmwasm_std::SubMsgResult::Ok(_) => Ok(Response::default()), - cosmwasm_std::SubMsgResult::Err(error) => { - Err(StdError::GenericErr { msg: error }).map_err(Into::::into) - } - } -} mod execute { use std::str::from_utf8; @@ -310,7 +294,7 @@ mod execute { funds, }); - let sub_message = SubMsg::reply_always(wasm_execute_message, REPLY_MSG_SUCCESS); + let sub_message = SubMsg::new(wasm_execute_message); debug_println!("this is {:?}", info.sender); debug_println!("burn from {:?}", sub_message); diff --git a/contracts/token-contracts/cw-hub-bnusd/src/error.rs b/contracts/token-contracts/cw-hub-bnusd/src/error.rs index 87498e0..64355de 100644 --- a/contracts/token-contracts/cw-hub-bnusd/src/error.rs +++ b/contracts/token-contracts/cw-hub-bnusd/src/error.rs @@ -22,8 +22,6 @@ pub enum ContractError { OnlyHub, #[error("Invalid Method")] InvalidMethod, - #[error("Invalid Reply")] - InvalidReply, #[error("Issue in Minting of Token")] MintError, #[error("Issue in Burning of Token")] diff --git a/contracts/token-contracts/cw-hub-bnusd/tests/setup.rs b/contracts/token-contracts/cw-hub-bnusd/tests/setup.rs index eef9216..8a00574 100644 --- a/contracts/token-contracts/cw-hub-bnusd/tests/setup.rs +++ b/contracts/token-contracts/cw-hub-bnusd/tests/setup.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use std::str::FromStr; use cw_common::x_call_msg::XCallMsg as XCallExecuteMsg; -use cw_hub_bnusd::contract::{execute, instantiate, query, reply}; +use cw_hub_bnusd::contract::{execute, instantiate, query}; use cw_multi_test::App; use cw_multi_test::{Contract, ContractWrapper, Executor}; use cw_xcall_ibc_connection::{ @@ -103,7 +103,7 @@ pub fn ibc_mock_core_setup() -> Box> { ) } pub fn hub_token_contract_setup() -> Box> { - Box::new(ContractWrapper::new(execute, instantiate, query).with_reply(reply)) + Box::new(ContractWrapper::new(execute, instantiate, query)) } pub fn x_call_connection_setup() -> Box> { From 67861490902e44ed1417092d391d2594a2590ee2 Mon Sep 17 00:00:00 2001 From: AntonAndell Date: Mon, 6 Nov 2023 09:39:44 +0100 Subject: [PATCH 3/6] fix/44 check amount is larger than zero in cross transfer --- contracts/token-contracts/cw-hub-bnusd/src/contract.rs | 4 +++- contracts/token-contracts/cw-hub-bnusd/src/error.rs | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/contracts/token-contracts/cw-hub-bnusd/src/contract.rs b/contracts/token-contracts/cw-hub-bnusd/src/contract.rs index 211312f..60ce385 100644 --- a/contracts/token-contracts/cw-hub-bnusd/src/contract.rs +++ b/contracts/token-contracts/cw-hub-bnusd/src/contract.rs @@ -175,7 +175,7 @@ mod execute { use std::str::from_utf8; use bytes::BytesMut; - use cosmwasm_std::{to_binary, Addr, CosmosMsg, SubMsg}; + use cosmwasm_std::{to_binary, Addr, CosmosMsg, SubMsg, ensure}; use cw_common::network_address::NetId; use cw_ibc_rlp_lib::rlp::{decode, encode}; use debug_print::debug_println; @@ -257,6 +257,8 @@ mod execute { if !to.validate_foreign_addresses() { return Err(ContractError::InvalidNetworkAddress); } + ensure!(amount > 0, ContractError::InvalidAmount); + let funds = info.funds.clone(); let nid = NID.load(deps.storage)?; let hub_net: NetId = DESTINATION_TOKEN_NET.load(deps.storage)?; diff --git a/contracts/token-contracts/cw-hub-bnusd/src/error.rs b/contracts/token-contracts/cw-hub-bnusd/src/error.rs index 64355de..8a84a37 100644 --- a/contracts/token-contracts/cw-hub-bnusd/src/error.rs +++ b/contracts/token-contracts/cw-hub-bnusd/src/error.rs @@ -16,6 +16,8 @@ pub enum ContractError { WrongNetwork, #[error("Invalid to Address")] InvalidToAddress, + #[error("Token amount can't be zero")] + InvalidAmount, #[error("OnlyCallService")] OnlyCallService, #[error("OnlyHub")] From 8472f6f51306b7d5f71d00c2489d427f4fb5957e Mon Sep 17 00:00:00 2001 From: AntonAndell Date: Mon, 6 Nov 2023 13:02:16 +0100 Subject: [PATCH 4/6] apply formatting --- .../token-contracts/cw-hub-bnusd/src/constants.rs | 1 - .../token-contracts/cw-hub-bnusd/src/contract.rs | 11 +++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/contracts/token-contracts/cw-hub-bnusd/src/constants.rs b/contracts/token-contracts/cw-hub-bnusd/src/constants.rs index 8c37fd6..a205778 100644 --- a/contracts/token-contracts/cw-hub-bnusd/src/constants.rs +++ b/contracts/token-contracts/cw-hub-bnusd/src/constants.rs @@ -1,4 +1,3 @@ - use cosmwasm_std::Uint128; pub const X_CROSS_TRANSFER: &str = "xCrossTransfer"; diff --git a/contracts/token-contracts/cw-hub-bnusd/src/contract.rs b/contracts/token-contracts/cw-hub-bnusd/src/contract.rs index 60ce385..c1699c4 100644 --- a/contracts/token-contracts/cw-hub-bnusd/src/contract.rs +++ b/contracts/token-contracts/cw-hub-bnusd/src/contract.rs @@ -1,8 +1,8 @@ use std::str::FromStr; use crate::constants::{ - TOKEN_DECIMALS, TOKEN_NAME, TOKEN_SYMBOL, TOKEN_TOTAL_SUPPLY, - X_CROSS_TRANSFER, X_CROSS_TRANSFER_REVERT, + TOKEN_DECIMALS, TOKEN_NAME, TOKEN_SYMBOL, TOKEN_TOTAL_SUPPLY, X_CROSS_TRANSFER, + X_CROSS_TRANSFER_REVERT, }; use crate::error::ContractError; use crate::state::{ @@ -13,7 +13,8 @@ use cw_common::network_address::IconAddressValidation; #[cfg(not(feature = "library"))] use cosmwasm_std::entry_point; use cosmwasm_std::{ - to_binary, Addr, Binary, Deps, DepsMut, Empty, Env, MessageInfo, QueryRequest, Response, StdResult, WasmQuery, + to_binary, Addr, Binary, Deps, DepsMut, Empty, Env, MessageInfo, QueryRequest, Response, + StdResult, WasmQuery, }; use cw2::set_contract_version; @@ -161,7 +162,6 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult { } } - #[cfg_attr(not(feature = "library"), entry_point)] pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result { set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION) @@ -170,12 +170,11 @@ pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result Date: Mon, 6 Nov 2023 13:55:50 +0100 Subject: [PATCH 5/6] fix: 50 seperate query to a seperate function --- .../cw-asset-manager/src/contract.rs | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/contracts/core-contracts/cw-asset-manager/src/contract.rs b/contracts/core-contracts/cw-asset-manager/src/contract.rs index a16f5be..34164a2 100644 --- a/contracts/core-contracts/cw-asset-manager/src/contract.rs +++ b/contracts/core-contracts/cw-asset-manager/src/contract.rs @@ -121,6 +121,19 @@ mod exec { use super::*; + fn query_network_address( + deps: &DepsMut, + x_call_addr: &Addr, + ) -> Result { + let query_msg = GetNetworkAddress {}; + let query = QueryRequest::Wasm(WasmQuery::Smart { + contract_addr: x_call_addr.to_string(), + msg: to_binary(&query_msg).map_err(ContractError::Std)?, + }); + + deps.querier.query(&query).map_err(ContractError::Std) + } + pub fn setup( deps: DepsMut, source_xcall: String, @@ -132,14 +145,7 @@ mod exec { .addr_validate(&source_xcall) .map_err(ContractError::Std)?; - // query network address of xcall - let query_msg = GetNetworkAddress {}; - let query = QueryRequest::Wasm(WasmQuery::Smart { - contract_addr: x_call_addr.to_string(), - msg: to_binary(&query_msg).map_err(ContractError::Std)?, - }); - - let xcall_network_address: NetworkAddress = deps.querier.query(&query)?; + let xcall_network_address: NetworkAddress = query_network_address(&deps, &x_call_addr)?; if xcall_network_address.to_string().is_empty() { return Err(ContractError::XAddressNotFound); From 8dc622292de0d38e858c98c262ad770fae39cd52 Mon Sep 17 00:00:00 2001 From: AntonAndell Date: Mon, 6 Nov 2023 13:58:42 +0100 Subject: [PATCH 6/6] fix: 51 remove unnecessary address validation --- contracts/core-contracts/cw-asset-manager/src/contract.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/contracts/core-contracts/cw-asset-manager/src/contract.rs b/contracts/core-contracts/cw-asset-manager/src/contract.rs index 34164a2..945d320 100644 --- a/contracts/core-contracts/cw-asset-manager/src/contract.rs +++ b/contracts/core-contracts/cw-asset-manager/src/contract.rs @@ -304,8 +304,7 @@ mod exec { from: String, data: Vec, ) -> Result { - let xcall = SOURCE_XCALL.load(deps.storage)?; - let x_call_addr = deps.api.addr_validate(xcall.as_ref())?; + let x_call_addr = SOURCE_XCALL.load(deps.storage)?; let x_network = X_CALL_NETWORK_ADDRESS.load(deps.storage)?; if info.sender != x_call_addr {