Skip to content

Commit

Permalink
chore: fix queries
Browse files Browse the repository at this point in the history
  • Loading branch information
kerber0x committed Jun 10, 2024
1 parent 1c6ec9d commit 788be30
Showing 1 changed file with 24 additions and 39 deletions.
63 changes: 24 additions & 39 deletions contracts/alliance-hub/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -31,41 +31,40 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
fn get_config(deps: Deps) -> StdResult<Binary> {
let cfg = CONFIG.load(deps.storage)?;

to_binary(&cfg)
to_json_binary(&cfg)
}

fn get_validators(deps: Deps) -> StdResult<Binary> {
let validators = VALIDATORS.load(deps.storage)?;

to_binary(&validators)
to_json_binary(&validators)
}

fn get_whitelisted_assets(deps: Deps) -> StdResult<Binary> {
let whitelist = WHITELIST.range(deps.storage, None, None, Order::Ascending);
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<Binary> {
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<Binary> {
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,
})
Expand All @@ -74,17 +73,16 @@ fn get_staked_balance(deps: Deps, asset_query: AssetQuery) -> StdResult<Binary>
fn get_pending_rewards(deps: Deps, asset_query: AssetQuery) -> StdResult<Binary> {
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),
Expand All @@ -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<Binary> {
Expand All @@ -123,42 +119,31 @@ 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::<StdResult<Vec<PendingRewardsRes>>>();

to_binary(&all_pending_rewards?)
to_json_binary(&all_pending_rewards?)
}

fn get_total_staked_balances(deps: Deps) -> StdResult<Binary> {
let total_staked_balances: StdResult<Vec<StakedBalanceRes>> = TOTAL_BALANCES
.range(deps.storage, None, None, Order::Ascending)
.map(|total_balance| -> StdResult<StakedBalanceRes> {
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?)
}

0 comments on commit 788be30

Please sign in to comment.