From 3f718feb41609fa459eaee377556b66b73371827 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Mon, 30 May 2022 11:22:44 +0100 Subject: [PATCH] Created nymd internal coin --- .../validator-client/src/nymd/coin.rs | 44 +++++++++++++++++++ .../validator-client/src/nymd/mod.rs | 1 + 2 files changed, 45 insertions(+) create mode 100644 common/client-libs/validator-client/src/nymd/coin.rs diff --git a/common/client-libs/validator-client/src/nymd/coin.rs b/common/client-libs/validator-client/src/nymd/coin.rs new file mode 100644 index 0000000000..375af42372 --- /dev/null +++ b/common/client-libs/validator-client/src/nymd/coin.rs @@ -0,0 +1,44 @@ +// Copyright 2022 - Nym Technologies SA +// SPDX-License-Identifier: Apache-2.0 +use std::fmt; + +pub use cosmrs::Coin as CosmosCoin; +pub use cosmwasm_std::Coin as CosmWasmCoin; + +// the reason the coin is created here as opposed to different place in the codebase is that +// eventually we want to either publish the cosmwasm client separately or commit it to +// some other project, like cosmrs. Either way, in that case we can't really have +// a dependency on an internal type +pub struct Coin { + pub amount: u128, + pub denom: String, +} + +impl fmt::Display for Coin { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}{}", self.amount, self.denom) + } +} + +impl From for CosmosCoin { + fn from(coin: Coin) -> Self { + assert!( + coin.amount <= u64::MAX as u128, + "the coin amount is higher than the maximum supported by cosmrs" + ); + + CosmosCoin { + denom: coin + .denom + .parse() + .expect("the coin should have had a valid denom!"), + amount: (coin.amount as u64).into(), + } + } +} + +impl From for CosmWasmCoin { + fn from(coin: Coin) -> Self { + CosmWasmCoin::new(coin.amount, coin.denom) + } +} diff --git a/common/client-libs/validator-client/src/nymd/mod.rs b/common/client-libs/validator-client/src/nymd/mod.rs index 03adeb8e5b..cef740c993 100644 --- a/common/client-libs/validator-client/src/nymd/mod.rs +++ b/common/client-libs/validator-client/src/nymd/mod.rs @@ -46,6 +46,7 @@ pub use cosmrs::{bip32, AccountId, Decimal, Denom}; pub use signing_client::Client as SigningNymdClient; pub use traits::{VestingQueryClient, VestingSigningClient}; +pub mod coin; pub mod cosmwasm_client; pub mod error; pub mod fee;