Skip to content

Commit

Permalink
cosmrs: Coin type changes (#235)
Browse files Browse the repository at this point in the history
- Implemented `serde` traits for `Denom`
- Changed internal `Coin` representation
- Getter for `Decimal`'s internal value
- Removed unused `Decimal`
  • Loading branch information
jstuczyn authored Jul 23, 2022
1 parent 2ed647e commit 1a7a1b0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 66 deletions.
33 changes: 30 additions & 3 deletions cosmrs/src/base.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Base functionality.
use crate::{proto, Decimal, Error, ErrorReport, Result};
use crate::{proto, Error, ErrorReport, Result};
use eyre::WrapErr;
use serde::{de, de::Error as _, ser, Deserialize, Serialize};
use std::{fmt, str::FromStr};
Expand Down Expand Up @@ -128,13 +128,17 @@ impl Serialize for AccountId {
}

/// Coin defines a token with a denomination and an amount.
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct Coin {
/// Denomination
pub denom: Denom,

/// Amount
pub amount: Decimal,
// represent coin amount as an u128, which theoretically supports lower maximum value than
// cosmos-sdk's `Int` that has a maximum value of 2^256 - 1. (https://github.com/cosmos/cosmos-sdk/blob/v0.45.4/types/int.go#L72-L74=)
// But I would argue this is sufficient for the current realistic use cases and is less cumbersome to use than the `Decimal`
// (which should have been used for a `DecCoin`)
pub amount: u128,
}

impl TryFrom<proto::cosmos::base::v1beta1::Coin> for Coin {
Expand Down Expand Up @@ -171,6 +175,15 @@ impl From<&Coin> for proto::cosmos::base::v1beta1::Coin {
}
}

impl fmt::Display for Coin {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// Follow the same formatting without the space between amount and denom as
// Cosmwasm in their Coin, which is furthermore consistent with the cosmos-sdk:
// https://github.com/cosmos/cosmos-sdk/blob/v0.42.4/types/coin.go#L643-L645
write!(f, "{}{}", self.amount, self.denom)
}
}

/// Denomination.
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct Denom(String);
Expand Down Expand Up @@ -202,6 +215,20 @@ impl FromStr for Denom {
}
}

impl<'de> Deserialize<'de> for Denom {
fn deserialize<D: de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
String::deserialize(deserializer)?
.parse()
.map_err(D::Error::custom)
}
}

impl Serialize for Denom {
fn serialize<S: ser::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.0.serialize(serializer)
}
}

#[cfg(test)]
mod tests {
use super::{AccountId, Denom};
Expand Down
59 changes: 0 additions & 59 deletions cosmrs/src/decimal.rs

This file was deleted.

2 changes: 0 additions & 2 deletions cosmrs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@ pub mod cosmwasm;
pub mod dev;

mod base;
mod decimal;
mod error;
mod prost_ext;

pub use crate::{
base::{AccountId, Coin, Denom},
decimal::Decimal,
error::{Error, Result},
tx::Tx,
};
Expand Down
2 changes: 1 addition & 1 deletion cosmrs/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
//! // We'll be doing a simple send transaction.
//! // First we'll create a "Coin" amount to be sent, in this case 1 million uatoms.
//! let amount = Coin {
//! amount: 1_000_000u64.into(),
//! amount: 1_000_000u128,
//! denom: "uatom".parse()?,
//! };
//!
Expand Down
3 changes: 2 additions & 1 deletion cosmrs/src/tx/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
use super::Gas;
use crate::{prost_ext::ParseOptional, proto, AccountId, Coin, ErrorReport, Result};
use serde::{Deserialize, Serialize};

/// Fee includes the amount of coins paid in fees and the maximum gas to be
/// used by the transaction.
///
/// The ratio yields an effective “gasprice”, which must be above some minimum
/// to be accepted into the mempool.
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, PartialOrd, Ord, Serialize)]
pub struct Fee {
/// Amount of coins to be paid as a fee.
pub amount: Vec<Coin>,
Expand Down

0 comments on commit 1a7a1b0

Please sign in to comment.