Skip to content

Commit

Permalink
Add staked balances query for easier frontend integration
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeHartnell committed Dec 28, 2023
1 parent 25774f0 commit 546403a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
13 changes: 11 additions & 2 deletions contracts/provider/native-staking-proxy/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cosmwasm_std::WasmMsg::Execute;
use cosmwasm_std::{
coin, ensure_eq, to_binary, Coin, DistributionMsg, GovMsg, Response, StakingMsg, VoteOption,
WeightedVoteOption,
coin, ensure_eq, to_binary, Coin, Delegation, DistributionMsg, GovMsg, Response, StakingMsg,
VoteOption, WeightedVoteOption,
};
use cw2::set_contract_version;
use cw_storage_plus::Item;
Expand Down Expand Up @@ -332,6 +332,15 @@ impl NativeStakingProxyContract<'_> {
fn config(&self, ctx: QueryCtx) -> Result<ConfigResponse, ContractError> {
Ok(self.config.load(ctx.deps.storage)?)
}

#[msg(query)]
fn staked_balances(&self, ctx: QueryCtx) -> Result<Vec<Delegation>, ContractError> {
let balances = ctx
.deps
.querier
.query_all_delegations(ctx.env.contract.address)?;
Ok(balances)
}
}

// Some unit tests, due to mt limitations / unsupported msgs
Expand Down
15 changes: 13 additions & 2 deletions contracts/provider/native-staking-proxy/src/multitest.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Result as AnyResult;

use cosmwasm_std::testing::mock_env;
use cosmwasm_std::{coin, coins, to_binary, Addr, Decimal, Validator};
use cosmwasm_std::{coin, coins, to_binary, Addr, Decimal, Delegation, Validator};

use cw_multi_test::{App as MtApp, StakingInfo, StakingSudo, SudoMsg};

Expand Down Expand Up @@ -194,10 +194,21 @@ fn staking() {
let delegation = app
.app()
.wrap()
.query_delegation(staking_proxy.contract_addr, validator.to_owned())
.query_delegation(staking_proxy.contract_addr.clone(), validator.to_owned())
.unwrap()
.unwrap();
assert_eq!(delegation.amount, coin(120, OSMO));

// Test staked balances query
let balances = staking_proxy.staked_balances().unwrap();
assert_eq!(
balances,
vec![Delegation {
delegator: Addr::unchecked(proxy_addr),
validator: validator.to_string(),
amount: coin(120, OSMO),
}]
);
}

#[test]
Expand Down

0 comments on commit 546403a

Please sign in to comment.