Compare commits

...

5 Commits

Author SHA1 Message Date
Jon Häggblad d729081996 primitives: add vec conversion functions 2022-03-29 10:49:35 +02:00
Jon Häggblad 2ce1c8833f primitives: add note about more coin tests 2022-03-29 10:49:33 +02:00
Jon Häggblad 635ca745d9 primitives: move Coin type top its own file 2022-03-29 10:49:31 +02:00
Jon Häggblad 4ef7fac377 primitives: conversions 2022-03-29 10:49:28 +02:00
Jon Häggblad c1d136bd54 primitives: initial creation of files 2022-03-29 10:49:22 +02:00
10 changed files with 153 additions and 3 deletions
Generated
+10
View File
@@ -3683,6 +3683,15 @@ dependencies = [
"uint",
]
[[package]]
name = "primitives"
version = "0.1.0"
dependencies = [
"cosmrs",
"cosmwasm-std",
"thiserror",
]
[[package]]
name = "proc-macro-crate"
version = "1.1.3"
@@ -5999,6 +6008,7 @@ dependencies = [
"log",
"mixnet-contract-common",
"network-defaults",
"primitives",
"prost",
"reqwest",
"serde",
+1
View File
@@ -44,6 +44,7 @@ members = [
"common/nymsphinx/params",
"common/nymsphinx/types",
"common/pemstore",
"common/primitives",
"common/socks5/proxy-helpers",
"common/socks5/requests",
"common/topology",
@@ -21,6 +21,7 @@ url = { version = "2.2", features = ["serde"] }
coconut-interface = { path = "../../coconut-interface" }
network-defaults = { path = "../../network-defaults" }
primitives = { path = "../../primitives" }
validator-api-requests = { path = "../../../validator-api/validator-api-requests" }
# required for nymd-client
@@ -369,14 +369,14 @@ pub trait SigningCosmWasmClient: CosmWasmClient {
&self,
sender_address: &AccountId,
recipient_address: &AccountId,
amount: Vec<Coin>,
amount: Vec<primitives::Coin>,
fee: Fee,
memo: impl Into<String> + Send + 'static,
) -> Result<broadcast::tx_commit::Response, NymdError> {
let send_msg = MsgSend {
from_address: sender_address.clone(),
to_address: recipient_address.clone(),
amount,
amount: primitives::coin::try_into(amount)?,
}
.to_any()
.map_err(|_| NymdError::SerializationError("MsgSend".to_owned()))?;
@@ -108,6 +108,9 @@ pub enum NymdError {
#[error("Abci query failed with code {0} - {1}")]
AbciError(u32, abci::Log),
#[error("Failed to handle primitives")]
PrimitivesError(#[from] primitives::PrimitivesError)
}
impl NymdError {
@@ -606,7 +606,7 @@ impl<C> NymdClient<C> {
pub async fn send(
&self,
recipient: &AccountId,
amount: Vec<CosmosCoin>,
amount: Vec<primitives::Coin>,
memo: impl Into<String> + Send + 'static,
) -> Result<broadcast::tx_commit::Response, NymdError>
where
+9
View File
@@ -0,0 +1,9 @@
[package]
name = "primitives"
version = "0.1.0"
edition = "2021"
[dependencies]
cosmrs = { version = "0.4.1", features = ["rpc", "bip32", "cosmwasm"] }
cosmwasm-std = "1.0.0-beta3"
thiserror = "1.0"
+100
View File
@@ -0,0 +1,100 @@
// Copyright 2022 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use crate::error::PrimitivesError;
use core::fmt;
use cosmwasm_std::Uint128;
use std::str::FromStr;
/// Common Coin type for the backend.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Coin {
pub amount: Uint128,
pub denom: String,
}
impl Coin {
pub fn new(amount: u128, denom: impl Into<String>) -> Coin {
Coin {
amount: Uint128::new(amount),
denom: denom.into(),
}
}
}
impl fmt::Display for Coin {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}{}", self.amount, self.denom)
}
}
impl TryFrom<cosmrs::Coin> for Coin {
type Error = PrimitivesError;
fn try_from(cosmos_coin: cosmrs::Coin) -> Result<Self, Self::Error> {
Ok(Self {
amount: cosmos_coin.amount.to_string().as_str().try_into()?,
denom: cosmos_coin.denom.to_string(),
})
}
}
impl TryFrom<Coin> for cosmrs::Coin {
type Error = PrimitivesError;
fn try_from(coin: Coin) -> Result<Self, Self::Error> {
Ok(Self {
denom: cosmrs::Denom::from_str(&coin.denom)?,
amount: cosmrs::Decimal::from_str(&coin.amount.to_string())?,
})
}
}
pub fn try_into(coins: Vec<Coin>) -> Result<Vec<cosmrs::Coin>, PrimitivesError> {
coins.into_iter().map(TryInto::try_into).collect::<Result<Vec<_>, _>>()
}
pub fn into(coins: Vec<Coin>) -> Vec<cosmwasm_std::Coin> {
coins.into_iter().map(Into::into).collect()
}
impl From<cosmwasm_std::Coin> for Coin {
fn from(cosmwasm_coin: cosmwasm_std::Coin) -> Self {
Self {
amount: cosmwasm_coin.amount,
denom: cosmwasm_coin.denom,
}
}
}
impl From<Coin> for cosmwasm_std::Coin {
fn from(coin: Coin) -> Self {
Self {
amount: coin.amount,
denom: coin.denom,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn convert_to_and_from_cosmwasm_coin() {
let coin = Coin::new(42, "ucoin");
let cosmwasm_coin: cosmwasm_std::Coin = coin.clone().into();
assert_eq!(coin, Coin::from(cosmwasm_coin));
}
#[test]
fn convert_to_and_from_cosmos_coin() {
let coin = Coin::new(42, "ucoin");
let cosmos_coin: cosmrs::Coin = coin.clone().try_into().unwrap();
assert_eq!(coin, Coin::try_from(cosmos_coin).unwrap());
}
// WIP(JON): more tests
// Especially converting from cosmrs::Coin
}
+18
View File
@@ -0,0 +1,18 @@
// Copyright 2022 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use thiserror::Error;
#[derive(Debug, Error)]
pub enum PrimitivesError {
#[error("{source}")]
CosmwasmError {
#[from]
source: cosmwasm_std::StdError,
},
#[error("{source}")]
CosmrsError {
#[from]
source: cosmrs::ErrorReport,
},
}
+8
View File
@@ -0,0 +1,8 @@
// Copyright 2022 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
pub mod coin;
mod error;
pub use error::PrimitivesError;
pub use coin::Coin;