Skip to content

Commit

Permalink
Merge pull request #62 from balancednetwork/bugfix/native-deposit-fee…
Browse files Browse the repository at this point in the history
…-query

fix: Fix fee query related to native token deposits
  • Loading branch information
AntonAndell authored Apr 8, 2024
2 parents 628805f + 8b0e91b commit 0dc3649
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 21 deletions.
34 changes: 22 additions & 12 deletions contracts/core-contracts/cw-asset-manager/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -645,6 +650,7 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
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)?),
}
}

Expand Down Expand Up @@ -678,6 +684,10 @@ mod query {
icon_nid,
})
}

pub fn query_limit(deps: Deps, asset: String) -> StdResult<RateLimit> {
RATE_LIMITS.load(deps.storage, asset)
}
}

#[cfg(test)]
Expand Down
4 changes: 3 additions & 1 deletion contracts/cw-common/src/asset_manager_msg.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -60,6 +60,8 @@ pub enum QueryMsg {
GetConfiguration {},
#[returns(NetIdResponse)]
GetNetIds {},
#[returns(RateLimit)]
GetLimit { asset: String },
}

#[cw_serde]
Expand Down
9 changes: 6 additions & 3 deletions contracts/cw-common/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn verify_protocol(
Err(ContractError::Unauthorized {})
}

pub fn get_protocols(deps: &DepsMut, xcall_manager: Addr) -> Result<ProtocolConfig, ContractError> {
pub fn get_protocols(deps: &Deps, xcall_manager: Addr) -> Result<ProtocolConfig, ContractError> {
let query_msg = GetProtocols {};
let query = QueryRequest::Wasm(WasmQuery::Smart {
contract_addr: xcall_manager.to_string(),
Expand All @@ -43,7 +43,7 @@ pub fn get_protocols(deps: &DepsMut, xcall_manager: Addr) -> Result<ProtocolConf
}

pub fn get_fee(
deps: &DepsMut,
deps: &Deps,
xcall: Addr,
nid: NetId,
rollback: bool,
Expand Down Expand Up @@ -82,7 +82,10 @@ pub fn balance_of(deps: &Deps, token: String, owner: String) -> Result<u128, Con
msg: to_binary(&query_msg).map_err(ContractError::Std)?,
});

deps.querier.query(&query).map_err(ContractError::Std)
let balance_response: cw20::BalanceResponse =
deps.querier.query(&query).map_err(ContractError::Std)?;
let balance_u128 = balance_response.balance.u128();
Ok(balance_u128)
}

pub fn bank_balance_of(deps: &Deps, token: String, owner: String) -> Result<u128, ContractError> {
Expand Down
18 changes: 14 additions & 4 deletions contracts/cw-common/src/rate_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,31 @@ impl RateLimited for RateLimit {
amount: u128,
is_denom: bool,
) -> Result<RateLimit, ContractError> {
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();
Expand Down
2 changes: 1 addition & 1 deletion contracts/token-contracts/cw-hub-bnusd/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 0dc3649

Please sign in to comment.