Compare commits

...

4 Commits

Author SHA1 Message Date
Jędrzej Stuczyński 7f840b889f Merge branch 'feature/optional-contract-addresses' into temp-jstuczyn-branch 2022-01-20 17:32:57 +00:00
Jędrzej Stuczyński 5cd67b22b3 Made contract addresses for query NymdClient construction optional
Similarly as it is the case when creating SigningNymdClient
2022-01-20 17:03:16 +00:00
Jędrzej Stuczyński 25c6748ae9 Cargo fmt 2022-01-20 15:52:32 +00:00
Jędrzej Stuczyński 78d2ffa923 Introduced RPC query for total token supply 2022-01-20 15:47:46 +00:00
3 changed files with 56 additions and 10 deletions
@@ -132,14 +132,14 @@ impl Client<QueryNymdClient> {
let validator_api_client = validator_api::Client::new(config.api_url.clone()); let validator_api_client = validator_api::Client::new(config.api_url.clone());
let nymd_client = NymdClient::connect( let nymd_client = NymdClient::connect(
config.nymd_url.as_str(), config.nymd_url.as_str(),
config.mixnet_contract_address.clone().unwrap_or_else(|| { Some(config.mixnet_contract_address.clone().unwrap_or_else(|| {
cosmrs::AccountId::from_str(network_defaults::DEFAULT_MIXNET_CONTRACT_ADDRESS) cosmrs::AccountId::from_str(network_defaults::DEFAULT_MIXNET_CONTRACT_ADDRESS)
.unwrap() .unwrap()
}), })),
config.vesting_contract_address.clone().unwrap_or_else(|| { Some(config.vesting_contract_address.clone().unwrap_or_else(|| {
cosmrs::AccountId::from_str(network_defaults::DEFAULT_VESTING_CONTRACT_ADDRESS) cosmrs::AccountId::from_str(network_defaults::DEFAULT_VESTING_CONTRACT_ADDRESS)
.unwrap() .unwrap()
}), })),
)?; )?;
Ok(Client { Ok(Client {
@@ -157,8 +157,8 @@ impl Client<QueryNymdClient> {
pub fn change_nymd(&mut self, new_endpoint: Url) -> Result<(), ValidatorClientError> { pub fn change_nymd(&mut self, new_endpoint: Url) -> Result<(), ValidatorClientError> {
self.nymd = NymdClient::connect( self.nymd = NymdClient::connect(
new_endpoint.as_ref(), new_endpoint.as_ref(),
self.mixnet_contract_address.clone().unwrap(), self.mixnet_contract_address.clone(),
self.vesting_contract_address.clone().unwrap(), self.vesting_contract_address.clone(),
)?; )?;
Ok(()) Ok(())
} }
@@ -13,6 +13,7 @@ use cosmrs::proto::cosmos::auth::v1beta1::{
}; };
use cosmrs::proto::cosmos::bank::v1beta1::{ use cosmrs::proto::cosmos::bank::v1beta1::{
QueryAllBalancesRequest, QueryAllBalancesResponse, QueryBalanceRequest, QueryBalanceResponse, QueryAllBalancesRequest, QueryAllBalancesResponse, QueryBalanceRequest, QueryBalanceResponse,
QueryTotalSupplyRequest, QueryTotalSupplyResponse,
}; };
use cosmrs::proto::cosmos::tx::v1beta1::{ use cosmrs::proto::cosmos::tx::v1beta1::{
SimulateRequest, SimulateResponse as ProtoSimulateResponse, SimulateRequest, SimulateResponse as ProtoSimulateResponse,
@@ -27,6 +28,7 @@ use cosmrs::tendermint::abci::Code as AbciCode;
use cosmrs::tendermint::abci::Transaction; use cosmrs::tendermint::abci::Transaction;
use cosmrs::tendermint::{abci, block, chain}; use cosmrs::tendermint::{abci, block, chain};
use cosmrs::{tx, AccountId, Coin, Denom, Tx}; use cosmrs::{tx, AccountId, Coin, Denom, Tx};
use cosmwasm_std::Coin as CosmWasmCoin;
use prost::Message; use prost::Message;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::convert::{TryFrom, TryInto}; use std::convert::{TryFrom, TryInto};
@@ -162,6 +164,43 @@ pub trait CosmWasmClient: rpc::Client {
.map_err(|_| NymdError::SerializationError("Coins".to_owned())) .map_err(|_| NymdError::SerializationError("Coins".to_owned()))
} }
// this is annoyingly and inconsistently returning `Vec<CosmWasmCoin>` rather than
// Vec<Coin>, 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<Vec<CosmWasmCoin>, 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::<Result<_, _>>()
.map_err(|_| NymdError::SerializationError("Coins".to_owned()))
}
async fn get_tx(&self, id: tx::Hash) -> Result<TxResponse, NymdError> { async fn get_tx(&self, id: tx::Hash) -> Result<TxResponse, NymdError> {
Ok(self.tx(id, false).await?) Ok(self.tx(id, false).await?)
} }
@@ -57,16 +57,16 @@ pub struct NymdClient<C> {
impl NymdClient<QueryNymdClient> { impl NymdClient<QueryNymdClient> {
pub fn connect<U>( pub fn connect<U>(
endpoint: U, endpoint: U,
mixnet_contract_address: AccountId, mixnet_contract_address: Option<AccountId>,
vesting_contract_address: AccountId, vesting_contract_address: Option<AccountId>,
) -> Result<NymdClient<QueryNymdClient>, NymdError> ) -> Result<NymdClient<QueryNymdClient>, NymdError>
where where
U: TryInto<HttpClientUrl, Error = TendermintRpcError>, U: TryInto<HttpClientUrl, Error = TendermintRpcError>,
{ {
Ok(NymdClient { Ok(NymdClient {
client: QueryNymdClient::new(endpoint)?, client: QueryNymdClient::new(endpoint)?,
mixnet_contract_address: Some(mixnet_contract_address), mixnet_contract_address,
vesting_contract_address: Some(vesting_contract_address), vesting_contract_address,
client_address: None, client_address: None,
custom_gas_limits: Default::default(), custom_gas_limits: Default::default(),
simulated_gas_multiplier: DEFAULT_SIMULATED_GAS_MULTIPLIER, simulated_gas_multiplier: DEFAULT_SIMULATED_GAS_MULTIPLIER,
@@ -255,6 +255,13 @@ impl<C> NymdClient<C> {
self.get_balance(address, self.denom()?).await self.get_balance(address, self.denom()?).await
} }
pub async fn get_total_supply(&self) -> Result<Vec<Coin>, NymdError>
where
C: CosmWasmClient + Sync,
{
self.client.get_total_supply().await
}
pub async fn get_contract_settings(&self) -> Result<ContractStateParams, NymdError> pub async fn get_contract_settings(&self) -> Result<ContractStateParams, NymdError>
where where
C: CosmWasmClient + Sync, C: CosmWasmClient + Sync,