From 8d26e48a5b301beaca0e6ddd8ec53da033809864 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Mon, 24 Jan 2022 15:43:10 +0000 Subject: [PATCH] Introduced RPC query for total token supply (#1053) * Introduced RPC query for total token supply * Cargo fmt --- .../src/nymd/cosmwasm_client/client.rs | 39 +++++++++++++++++++ .../validator-client/src/nymd/mod.rs | 7 ++++ 2 files changed, 46 insertions(+) diff --git a/common/client-libs/validator-client/src/nymd/cosmwasm_client/client.rs b/common/client-libs/validator-client/src/nymd/cosmwasm_client/client.rs index 1d07729ce7..eb7ccc3f05 100644 --- a/common/client-libs/validator-client/src/nymd/cosmwasm_client/client.rs +++ b/common/client-libs/validator-client/src/nymd/cosmwasm_client/client.rs @@ -13,6 +13,7 @@ use cosmrs::proto::cosmos::auth::v1beta1::{ }; use cosmrs::proto::cosmos::bank::v1beta1::{ QueryAllBalancesRequest, QueryAllBalancesResponse, QueryBalanceRequest, QueryBalanceResponse, + QueryTotalSupplyRequest, QueryTotalSupplyResponse, }; use cosmrs::proto::cosmos::tx::v1beta1::{ SimulateRequest, SimulateResponse as ProtoSimulateResponse, @@ -27,6 +28,7 @@ use cosmrs::tendermint::abci::Code as AbciCode; use cosmrs::tendermint::abci::Transaction; use cosmrs::tendermint::{abci, block, chain}; use cosmrs::{tx, AccountId, Coin, Denom, Tx}; +use cosmwasm_std::Coin as CosmWasmCoin; use prost::Message; use serde::{Deserialize, Serialize}; use std::convert::{TryFrom, TryInto}; @@ -162,6 +164,43 @@ pub trait CosmWasmClient: rpc::Client { .map_err(|_| NymdError::SerializationError("Coins".to_owned())) } + // this is annoyingly and inconsistently returning `Vec` rather than + // Vec, since cosmrs::Coin can't deal with IBC denoms. + // Presumably after https://github.com/cosmos/cosmos-rust/issues/173 is resolved, + // the code could be adjusted + async fn get_total_supply(&self) -> Result, NymdError> { + let path = Some("/cosmos.bank.v1beta1.Query/TotalSupply".parse().unwrap()); + + let mut supply = Vec::new(); + let mut pagination = None; + + loop { + let req = QueryTotalSupplyRequest { pagination }; + + let mut res = self + .make_abci_query::<_, QueryTotalSupplyResponse>(path.clone(), req) + .await?; + + supply.append(&mut res.supply); + if let Some(pagination_info) = res.pagination { + pagination = Some(create_pagination(pagination_info.next_key)) + } else { + break; + } + } + + supply + .into_iter() + .map(|coin| { + coin.amount.parse().map(|amount| CosmWasmCoin { + denom: coin.denom, + amount, + }) + }) + .collect::>() + .map_err(|_| NymdError::SerializationError("Coins".to_owned())) + } + async fn get_tx(&self, id: tx::Hash) -> Result { Ok(self.tx(id, false).await?) } diff --git a/common/client-libs/validator-client/src/nymd/mod.rs b/common/client-libs/validator-client/src/nymd/mod.rs index d5c86ef28e..a7c428f251 100644 --- a/common/client-libs/validator-client/src/nymd/mod.rs +++ b/common/client-libs/validator-client/src/nymd/mod.rs @@ -269,6 +269,13 @@ impl NymdClient { self.get_balance(address, self.denom()?).await } + pub async fn get_total_supply(&self) -> Result, NymdError> + where + C: CosmWasmClient + Sync, + { + self.client.get_total_supply().await + } + pub async fn get_contract_settings(&self) -> Result where C: CosmWasmClient + Sync,