diff --git a/contracts/alliance-hub/src/query.rs b/contracts/alliance-hub/src/query.rs index 9cf9b6a..5b2cd5b 100644 --- a/contracts/alliance-hub/src/query.rs +++ b/contracts/alliance-hub/src/query.rs @@ -4,8 +4,8 @@ use alliance_protocol::alliance_protocol::{ }; #[cfg(not(feature = "library"))] use cosmwasm_std::entry_point; -use cosmwasm_std::{to_binary, Binary, Deps, Env, Order, StdResult, Uint128}; -use cw_asset_v2::{AssetInfo, AssetInfoKey}; +use cosmwasm_std::{to_json_binary, Binary, Deps, Env, Order, StdResult, Uint128}; +use cw_asset_v3::AssetInfo; use std::collections::HashMap; use crate::state::{ @@ -31,13 +31,13 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult { fn get_config(deps: Deps) -> StdResult { let cfg = CONFIG.load(deps.storage)?; - to_binary(&cfg) + to_json_binary(&cfg) } fn get_validators(deps: Deps) -> StdResult { let validators = VALIDATORS.load(deps.storage)?; - to_binary(&validators) + to_json_binary(&validators) } fn get_whitelisted_assets(deps: Deps) -> StdResult { @@ -45,27 +45,26 @@ fn get_whitelisted_assets(deps: Deps) -> StdResult { let mut res: WhitelistedAssetsResponse = HashMap::new(); for item in whitelist { - let (key, chain_id) = item?; - let asset = key.check(deps.api, None)?; + let (asset, chain_id) = item?; #[allow(clippy::unwrap_or_default)] res.entry(chain_id).or_insert_with(Vec::new).push(asset) } - to_binary(&res) + to_json_binary(&res) } fn get_rewards_distribution(deps: Deps) -> StdResult { let asset_rewards_distr = ASSET_REWARD_DISTRIBUTION.load(deps.storage)?; - to_binary(&asset_rewards_distr) + to_json_binary(&asset_rewards_distr) } fn get_staked_balance(deps: Deps, asset_query: AssetQuery) -> StdResult { let addr = deps.api.addr_validate(&asset_query.address)?; - let key = (addr, asset_query.asset.clone().into()); + let key = (addr, &asset_query.asset); let balance = BALANCES.load(deps.storage, key)?; - to_binary(&StakedBalanceRes { + to_json_binary(&StakedBalanceRes { asset: asset_query.asset, balance, }) @@ -74,17 +73,16 @@ fn get_staked_balance(deps: Deps, asset_query: AssetQuery) -> StdResult fn get_pending_rewards(deps: Deps, asset_query: AssetQuery) -> StdResult { let config = CONFIG.load(deps.storage)?; let addr = deps.api.addr_validate(&asset_query.address)?; - let key = (addr, AssetInfoKey::from(asset_query.asset.clone())); + let key = (addr, &asset_query.asset.clone()); let user_reward_rate = USER_ASSET_REWARD_RATE.load(deps.storage, key.clone())?; - let asset_reward_rate = - ASSET_REWARD_RATE.load(deps.storage, AssetInfoKey::from(asset_query.asset.clone()))?; + let asset_reward_rate = ASSET_REWARD_RATE.load(deps.storage, &asset_query.asset.clone())?; let user_balance = BALANCES.load(deps.storage, key.clone())?; let unclaimed_rewards = UNCLAIMED_REWARDS .load(deps.storage, key) .unwrap_or(Uint128::zero()); let pending_rewards = (asset_reward_rate - user_reward_rate) * user_balance; - to_binary(&PendingRewardsRes { + to_json_binary(&PendingRewardsRes { rewards: unclaimed_rewards + pending_rewards, staked_asset: asset_query.asset, reward_asset: AssetInfo::Native(config.reward_denom), @@ -98,22 +96,20 @@ fn get_all_staked_balances(deps: Deps, asset_query: AllStakedBalancesQuery) -> S for asset_res in whitelist { // Build the required key to recover the BALANCES - let (asset_key, _) = asset_res?; - let checked_asset_info = asset_key.check(deps.api, None)?; - let asset_info_key = AssetInfoKey::from(checked_asset_info.clone()); - let stake_key = (addr.clone(), asset_info_key); + let (asset_info, _) = asset_res?; + let stake_key = (addr.clone(), &asset_info); let balance = BALANCES .load(deps.storage, stake_key) .unwrap_or(Uint128::zero()); // Append the request res.push(StakedBalanceRes { - asset: checked_asset_info, + asset: asset_info.clone(), balance, }) } - to_binary(&res) + to_json_binary(&res) } fn get_all_pending_rewards(deps: Deps, query: AllPendingRewardsQuery) -> StdResult { @@ -123,30 +119,22 @@ fn get_all_pending_rewards(deps: Deps, query: AllPendingRewardsQuery) -> StdResu .prefix(addr.clone()) .range(deps.storage, None, None, Order::Ascending) .map(|item| { - let (asset, user_reward_rate) = item?; - let asset = asset.check(deps.api, None)?; - let asset_reward_rate = - ASSET_REWARD_RATE.load(deps.storage, AssetInfoKey::from(asset.clone()))?; - let user_balance = BALANCES.load( - deps.storage, - (addr.clone(), AssetInfoKey::from(asset.clone())), - )?; + let (asset_info, user_reward_rate) = item?; + let asset_reward_rate = ASSET_REWARD_RATE.load(deps.storage, &asset_info)?; + let user_balance = BALANCES.load(deps.storage, (addr.clone(), &asset_info))?; let unclaimed_rewards = UNCLAIMED_REWARDS - .load( - deps.storage, - (addr.clone(), AssetInfoKey::from(asset.clone())), - ) + .load(deps.storage, (addr.clone(), &asset_info)) .unwrap_or(Uint128::zero()); let pending_rewards = (asset_reward_rate - user_reward_rate) * user_balance; Ok(PendingRewardsRes { rewards: pending_rewards + unclaimed_rewards, - staked_asset: asset, + staked_asset: asset_info.clone(), reward_asset: AssetInfo::Native(config.reward_denom.to_string()), }) }) .collect::>>(); - to_binary(&all_pending_rewards?) + to_json_binary(&all_pending_rewards?) } fn get_total_staked_balances(deps: Deps) -> StdResult { @@ -154,11 +142,8 @@ fn get_total_staked_balances(deps: Deps) -> StdResult { .range(deps.storage, None, None, Order::Ascending) .map(|total_balance| -> StdResult { let (asset, balance) = total_balance?; - Ok(StakedBalanceRes { - asset: asset.check(deps.api, None)?, - balance, - }) + Ok(StakedBalanceRes { asset, balance }) }) .collect(); - to_binary(&total_staked_balances?) + to_json_binary(&total_staked_balances?) }