Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add token supply query #17

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ homepage = "https://cosmwasm.com"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
default = ["iterator", "staking"]
default = ["iterator", "staking", "cosmwasm_1_1"]
iterator = ["cosmwasm-std/iterator"]
stargate = ["cosmwasm-std/stargate"]
staking = ["cosmwasm-std/staking"]
backtrace = ["anyhow/backtrace"]
cosmwasm_1_1 = ["cosmwasm-std/cosmwasm_1_1"]

[dependencies]
cw-utils = "1.0"
Expand Down
37 changes: 35 additions & 2 deletions src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use schemars::JsonSchema;

use cosmwasm_std::{
coin, to_binary, Addr, AllBalanceResponse, Api, BalanceResponse, BankMsg, BankQuery, Binary,
BlockInfo, Coin, Event, Querier, Storage,
BlockInfo, Coin, Event, Querier, Storage, Order, Uint128,
};
use cw_storage_plus::Map;
use cw_utils::NativeBalance;
use serde::{Serialize, Deserialize};

use crate::app::CosmosRouter;
use crate::executor::AppResponse;
Expand All @@ -27,6 +28,16 @@ pub enum BankSudo {
},
}

// TODO: remove this when I figure out how non_exhaustive works
#[cfg(feature = "cosmwasm_1_1")]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct SupplyResponse {
/// Always returns a Coin with the requested denom.
/// This will be of zero amount if the denom does not exist.
pub amount: Coin,
}

Comment on lines +31 to +40
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cosmwasm-std version should be bumped and then this can be removed. Instead the SupplyResponse from cosmwasm-std should be used (using either the Default impl or the recently introduced constructor)

pub trait Bank: Module<ExecT = BankMsg, QueryT = BankQuery, SudoT = BankSudo> {}

#[derive(Default)]
Expand All @@ -48,6 +59,7 @@ impl BankKeeper {
self.set_balance(&mut bank_storage, account, amount)
}

// this is an "admin" function to let us adjust bank accounts
fn set_balance(
&self,
bank_storage: &mut dyn Storage,
Expand All @@ -61,12 +73,28 @@ impl BankKeeper {
.map_err(Into::into)
}

// this is an "admin" function to let us adjust bank accounts
fn get_balance(&self, bank_storage: &dyn Storage, account: &Addr) -> AnyResult<Vec<Coin>> {
let val = BALANCES.may_load(bank_storage, account)?;
Ok(val.unwrap_or_default().into_vec())
}

fn get_supply(&self, bank_storage: &dyn Storage, denom: String) -> AnyResult<Coin> {
let supply: Uint128 = BALANCES
.range(bank_storage, None, None, Order::Ascending)
.into_iter()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to call into_iter() here, it's a noop

.map(|a| a.unwrap().1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a big fan of this unwrap, the error should be bubbled up

.fold(Uint128::zero(), |accum, item| {
let mut subtotal = Uint128::zero();
for coin in item.into_vec() {
if coin.denom == denom {
subtotal = subtotal + coin.amount;
}
}
accum + subtotal
});
Ok(coin(supply.into(), denom))
}

fn send(
&self,
bank_storage: &mut dyn Storage,
Expand Down Expand Up @@ -205,6 +233,11 @@ impl Module for BankKeeper {
let res = BalanceResponse { amount };
Ok(to_binary(&res)?)
}
BankQuery::Supply { denom } => {
let amount = self.get_supply(&bank_storage, denom)?;
let res = SupplyResponse { amount };
Ok(to_binary(&res)?)
}
q => bail!("Unsupported bank query: {:?}", q),
}
}
Expand Down