-
Notifications
You must be signed in to change notification settings - Fork 42
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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, | ||
} | ||
|
||
pub trait Bank: Module<ExecT = BankMsg, QueryT = BankQuery, SudoT = BankSudo> {} | ||
|
||
#[derive(Default)] | ||
|
@@ -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, | ||
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to call |
||
.map(|a| a.unwrap().1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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), | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 theSupplyResponse
fromcosmwasm-std
should be used (using either theDefault
impl or the recently introduced constructor)