Skip to content

Commit

Permalink
Adds BankQuery::Supply support
Browse files Browse the repository at this point in the history
Builds off: #17
  • Loading branch information
Jake Hartnell committed Aug 17, 2023
1 parent 9d000f6 commit 942bcb6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
27 changes: 24 additions & 3 deletions src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use schemars::JsonSchema;

use cosmwasm_std::{
coin, to_binary, Addr, AllBalanceResponse, Api, BalanceResponse, BankMsg, BankQuery, Binary,
BlockInfo, Coin, Event, Querier, Storage,
BlockInfo, Coin, Event, Order, Querier, Storage, SupplyResponse, Uint128,
};
use cw_storage_plus::Map;
use cw_utils::NativeBalance;
Expand All @@ -18,7 +18,6 @@ const BALANCES: Map<&Addr, NativeBalance> = Map::new("balances");

pub const NAMESPACE_BANK: &[u8] = b"bank";

// WIP
#[derive(Clone, std::fmt::Debug, PartialEq, Eq, JsonSchema)]
pub enum BankSudo {
Mint {
Expand Down Expand Up @@ -48,6 +47,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 +61,27 @@ 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)
.map(|a| a.unwrap().1)
.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 +220,12 @@ impl Module for BankKeeper {
let res = BalanceResponse { amount };
Ok(to_binary(&res)?)
}
BankQuery::Supply { denom } => {
let amount = self.get_supply(&bank_storage, denom)?;
let mut res = SupplyResponse::default();
res.amount = amount;
Ok(to_binary(&res)?)
}
q => bail!("Unsupported bank query: {:?}", q),
}
}
Expand Down

0 comments on commit 942bcb6

Please sign in to comment.