From 8b0e91befd1ca969f78c4c07b94763f51a458eeb Mon Sep 17 00:00:00 2001 From: AntonAndell Date: Fri, 5 Apr 2024 16:21:05 +0200 Subject: [PATCH] fix: Fix fee query related to native token deposits Fee query now uses correct NID also general cleanup and code flow improvments --- .../cw-asset-manager/src/contract.rs | 34 ++++++++++++------- contracts/cw-common/src/asset_manager_msg.rs | 4 ++- contracts/cw-common/src/helpers.rs | 9 +++-- contracts/cw-common/src/rate_limit.rs | 18 +++++++--- .../cw-hub-bnusd/src/contract.rs | 2 +- 5 files changed, 46 insertions(+), 21 deletions(-) diff --git a/contracts/core-contracts/cw-asset-manager/src/contract.rs b/contracts/core-contracts/cw-asset-manager/src/contract.rs index f643ed6..33624a8 100644 --- a/contracts/core-contracts/cw-asset-manager/src/contract.rs +++ b/contracts/core-contracts/cw-asset-manager/src/contract.rs @@ -112,14 +112,11 @@ pub fn execute( let token = info.funds.iter().find(|c| c.denom == denom).unwrap(); ensure!(!token.amount.is_zero(), ContractError::InvalidAmount); + let icon_nid = ICON_NET_ID.load(deps.storage)?; + let (native, token) = + exec::calculate_denom_funds(&deps, &info, token.clone(), denom.clone(), icon_nid)?; + let nid = NID.load(deps.storage)?; - let (native, token) = exec::calculate_denom_funds( - &deps, - &info, - token.clone(), - denom.clone(), - nid.clone(), - )?; let depositor = NetworkAddress::new(nid.as_str(), info.sender.as_str()); ensure!( @@ -357,7 +354,8 @@ mod exec { let source_xcall = SOURCE_XCALL.load(deps.storage)?; //create xcall msg for dispatching send call - let protocol_config = get_protocols(&deps, X_CALL_MANAGER.load(deps.storage)?).unwrap(); + let protocol_config = + get_protocols(&deps.as_ref(), X_CALL_MANAGER.load(deps.storage)?).unwrap(); let xcall_message = XCallMsg::SendCallMessage { to: dest_am.to_string().parse()?, data: xcall_data.rlp_bytes().to_vec(), @@ -611,10 +609,17 @@ mod exec { } let xcall = SOURCE_XCALL.load(deps.storage)?; - let protocol_config = get_protocols(deps, X_CALL_MANAGER.load(deps.storage)?).unwrap(); - let fee: Uint128 = get_fee(deps, xcall, nid, true, Some(protocol_config.sources)) - .unwrap() - .into(); + let protocol_config = + get_protocols(&deps.as_ref(), X_CALL_MANAGER.load(deps.storage)?).unwrap(); + let fee: Uint128 = get_fee( + &deps.as_ref(), + xcall, + nid, + true, + Some(protocol_config.sources), + ) + .unwrap() + .into(); ensure!(token.amount > fee, ContractError::InvalidAmount); let new_token = Coin { denom: token.denom.clone(), @@ -645,6 +650,7 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult { QueryMsg::GetOwner {} => to_binary(&query::query_get_owner(deps)?), QueryMsg::GetConfiguration {} => to_binary(&query::query_config(deps)?), QueryMsg::GetNetIds {} => to_binary(&query::query_nid(deps)?), + QueryMsg::GetLimit { asset } => to_binary(&query::query_limit(deps, asset)?), } } @@ -678,6 +684,10 @@ mod query { icon_nid, }) } + + pub fn query_limit(deps: Deps, asset: String) -> StdResult { + RATE_LIMITS.load(deps.storage, asset) + } } #[cfg(test)] diff --git a/contracts/cw-common/src/asset_manager_msg.rs b/contracts/cw-common/src/asset_manager_msg.rs index fa8572b..7884f36 100644 --- a/contracts/cw-common/src/asset_manager_msg.rs +++ b/contracts/cw-common/src/asset_manager_msg.rs @@ -1,6 +1,6 @@ +use crate::rate_limit::RateLimit; use cosmwasm_schema::{cw_serde, QueryResponses}; use cosmwasm_std::{Addr, Uint128}; - #[cw_serde] pub struct InstantiateMsg { pub source_xcall: String, @@ -60,6 +60,8 @@ pub enum QueryMsg { GetConfiguration {}, #[returns(NetIdResponse)] GetNetIds {}, + #[returns(RateLimit)] + GetLimit { asset: String }, } #[cw_serde] diff --git a/contracts/cw-common/src/helpers.rs b/contracts/cw-common/src/helpers.rs index 998af32..8a28df8 100644 --- a/contracts/cw-common/src/helpers.rs +++ b/contracts/cw-common/src/helpers.rs @@ -32,7 +32,7 @@ pub fn verify_protocol( Err(ContractError::Unauthorized {}) } -pub fn get_protocols(deps: &DepsMut, xcall_manager: Addr) -> Result { +pub fn get_protocols(deps: &Deps, xcall_manager: Addr) -> Result { let query_msg = GetProtocols {}; let query = QueryRequest::Wasm(WasmQuery::Smart { contract_addr: xcall_manager.to_string(), @@ -43,7 +43,7 @@ pub fn get_protocols(deps: &DepsMut, xcall_manager: Addr) -> Result Result Result { diff --git a/contracts/cw-common/src/rate_limit.rs b/contracts/cw-common/src/rate_limit.rs index 9b6d95e..a58fad0 100644 --- a/contracts/cw-common/src/rate_limit.rs +++ b/contracts/cw-common/src/rate_limit.rs @@ -32,21 +32,31 @@ impl RateLimited for RateLimit { amount: u128, is_denom: bool, ) -> Result { + if self.period == 0 { + return Ok(self.clone()); + } + let balance = match is_denom { true => bank_balance_of(&deps.as_ref(), token, env.contract.address.to_string())?, false => balance_of(&deps.as_ref(), token, env.contract.address.to_string())?, }; - if self.period == 0 { - return Ok(self.clone()); - } - let current_time = env.block.time.seconds(); let max_limit = balance .checked_mul(self.percentage.into()) .unwrap() .checked_div(POINTS) .unwrap(); + + if self.current_limit == 0 { + return Ok(RateLimit { + percentage: self.percentage, + period: self.period, + last_update: current_time, + current_limit: max_limit, + }); + } + // The maximum amount that can be withdraw in one period let max_withdraw = balance.checked_sub(max_limit).unwrap(); let time_diff = current_time.checked_sub(self.last_update).unwrap(); diff --git a/contracts/token-contracts/cw-hub-bnusd/src/contract.rs b/contracts/token-contracts/cw-hub-bnusd/src/contract.rs index 2be51b9..b61b520 100644 --- a/contracts/token-contracts/cw-hub-bnusd/src/contract.rs +++ b/contracts/token-contracts/cw-hub-bnusd/src/contract.rs @@ -322,7 +322,7 @@ mod execute { }; let hub_token_address = NetworkAddress::new(&hub_net.to_string(), hub_address.as_ref()); - let cfg = get_protocols(&deps, X_CALL_MANAGER.load(deps.storage)?).unwrap(); + let cfg = get_protocols(&deps.as_ref(), X_CALL_MANAGER.load(deps.storage)?).unwrap(); let call_message = XCallMsg::SendCallMessage { to: hub_token_address, data: encode(&call_data).to_vec(),