From d729081996a2b2c3d7914a5e06bbb3e38f4c826e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20H=C3=A4ggblad?= Date: Tue, 29 Mar 2022 10:48:59 +0200 Subject: [PATCH] primitives: add vec conversion functions --- Cargo.lock | 19 ++++++++++--------- .../client-libs/validator-client/Cargo.toml | 1 + .../nymd/cosmwasm_client/signing_client.rs | 4 ++-- .../validator-client/src/nymd/error.rs | 3 +++ .../validator-client/src/nymd/mod.rs | 2 +- common/primitives/Cargo.toml | 2 +- common/primitives/src/coin.rs | 9 +++++++++ common/primitives/src/lib.rs | 5 ++++- 8 files changed, 31 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index af2f8f482d..bde347ae3d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3039,15 +3039,6 @@ dependencies = [ "websocket-requests", ] -[[package]] -name = "nym-primitives" -version = "0.1.0" -dependencies = [ - "cosmrs", - "cosmwasm-std", - "thiserror", -] - [[package]] name = "nym-socks5-client" version = "0.12.1" @@ -3692,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" @@ -6008,6 +6008,7 @@ dependencies = [ "log", "mixnet-contract-common", "network-defaults", + "primitives", "prost", "reqwest", "serde", diff --git a/common/client-libs/validator-client/Cargo.toml b/common/client-libs/validator-client/Cargo.toml index 18322bac61..3d4907fab3 100644 --- a/common/client-libs/validator-client/Cargo.toml +++ b/common/client-libs/validator-client/Cargo.toml @@ -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 diff --git a/common/client-libs/validator-client/src/nymd/cosmwasm_client/signing_client.rs b/common/client-libs/validator-client/src/nymd/cosmwasm_client/signing_client.rs index d3163aae6e..2a435117a5 100644 --- a/common/client-libs/validator-client/src/nymd/cosmwasm_client/signing_client.rs +++ b/common/client-libs/validator-client/src/nymd/cosmwasm_client/signing_client.rs @@ -369,14 +369,14 @@ pub trait SigningCosmWasmClient: CosmWasmClient { &self, sender_address: &AccountId, recipient_address: &AccountId, - amount: Vec, + amount: Vec, fee: Fee, memo: impl Into + Send + 'static, ) -> Result { 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()))?; diff --git a/common/client-libs/validator-client/src/nymd/error.rs b/common/client-libs/validator-client/src/nymd/error.rs index bfeba9c437..870de6a906 100644 --- a/common/client-libs/validator-client/src/nymd/error.rs +++ b/common/client-libs/validator-client/src/nymd/error.rs @@ -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 { diff --git a/common/client-libs/validator-client/src/nymd/mod.rs b/common/client-libs/validator-client/src/nymd/mod.rs index 0c7f81a5ae..2e11db83b1 100644 --- a/common/client-libs/validator-client/src/nymd/mod.rs +++ b/common/client-libs/validator-client/src/nymd/mod.rs @@ -606,7 +606,7 @@ impl NymdClient { pub async fn send( &self, recipient: &AccountId, - amount: Vec, + amount: Vec, memo: impl Into + Send + 'static, ) -> Result where diff --git a/common/primitives/Cargo.toml b/common/primitives/Cargo.toml index cab74c4ab9..a94ce201b3 100644 --- a/common/primitives/Cargo.toml +++ b/common/primitives/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "nym-primitives" +name = "primitives" version = "0.1.0" edition = "2021" diff --git a/common/primitives/src/coin.rs b/common/primitives/src/coin.rs index 128f25ef82..d5bc23c7ec 100644 --- a/common/primitives/src/coin.rs +++ b/common/primitives/src/coin.rs @@ -50,6 +50,15 @@ impl TryFrom for cosmrs::Coin { } } + +pub fn try_into(coins: Vec) -> Result, PrimitivesError> { + coins.into_iter().map(TryInto::try_into).collect::, _>>() +} + +pub fn into(coins: Vec) -> Vec { + coins.into_iter().map(Into::into).collect() +} + impl From for Coin { fn from(cosmwasm_coin: cosmwasm_std::Coin) -> Self { Self { diff --git a/common/primitives/src/lib.rs b/common/primitives/src/lib.rs index e1b98dbcda..dc5147d872 100644 --- a/common/primitives/src/lib.rs +++ b/common/primitives/src/lib.rs @@ -1,5 +1,8 @@ // Copyright 2022 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 -mod coin; +pub mod coin; mod error; + +pub use error::PrimitivesError; +pub use coin::Coin; \ No newline at end of file