-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] cosmos-tx: MsgSend support w\ integration test
Basic support for `MsgSend` with end-to-end integration test. Adds the following: - Traits for simplifying Protobuf serialization: - `prost_ext::MessageExt` for Protobuf encoding - `msg::MsgType` and `msg::MsgProto` for decoding/encoding `Msg` types as Protobuf `Any` messages. - Domain types which model the following: - `Coin`: amount to send and a denom - `Denom`: name of a particular denomination - `Fee`: transaction fees - `MsgSend`: transaction message for performing a simple send Additionally includes an end-to-end test which uses Docker to spawn a single-node `gaia` and send a transaction.
- Loading branch information
1 parent
4d3db43
commit 3f30113
Showing
14 changed files
with
590 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
//! Bank module support | ||
//! | ||
//! <https://docs.cosmos.network/master/modules/bank/> | ||
|
||
use crate::{AccountId, Coin, Error, Msg, MsgType, Result}; | ||
use cosmos_sdk_proto::cosmos; | ||
use std::convert::{TryFrom, TryInto}; | ||
|
||
/// MsgSend represents a message to send coins from one account to another. | ||
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] | ||
pub struct MsgSend { | ||
/// Sender's address. | ||
pub from_address: AccountId, | ||
|
||
/// Recipient's address. | ||
pub to_address: AccountId, | ||
|
||
/// Amount to send | ||
pub amount: Vec<Coin>, | ||
} | ||
|
||
impl MsgType for MsgSend { | ||
fn from_msg(msg: &Msg) -> Result<Self> { | ||
cosmos::bank::v1beta1::MsgSend::from_msg(msg).and_then(TryInto::try_into) | ||
} | ||
|
||
fn to_msg(&self) -> Result<Msg> { | ||
cosmos::bank::v1beta1::MsgSend::from(self).to_msg() | ||
} | ||
} | ||
|
||
impl TryFrom<cosmos::bank::v1beta1::MsgSend> for MsgSend { | ||
type Error = eyre::Report; | ||
|
||
fn try_from(proto: cosmos::bank::v1beta1::MsgSend) -> Result<MsgSend> { | ||
MsgSend::try_from(&proto) | ||
} | ||
} | ||
|
||
impl TryFrom<&cosmos::bank::v1beta1::MsgSend> for MsgSend { | ||
type Error = eyre::Report; | ||
|
||
fn try_from(proto: &cosmos::bank::v1beta1::MsgSend) -> Result<MsgSend> { | ||
Ok(MsgSend { | ||
from_address: proto.from_address.parse().map_err(|_| Error::Account)?, | ||
to_address: proto.to_address.parse().map_err(|_| Error::Account)?, | ||
amount: proto | ||
.amount | ||
.iter() | ||
.map(TryFrom::try_from) | ||
.collect::<Result<_, _>>()?, | ||
}) | ||
} | ||
} | ||
|
||
impl From<MsgSend> for cosmos::bank::v1beta1::MsgSend { | ||
fn from(coin: MsgSend) -> cosmos::bank::v1beta1::MsgSend { | ||
cosmos::bank::v1beta1::MsgSend::from(&coin) | ||
} | ||
} | ||
|
||
impl From<&MsgSend> for cosmos::bank::v1beta1::MsgSend { | ||
fn from(msg: &MsgSend) -> cosmos::bank::v1beta1::MsgSend { | ||
cosmos::bank::v1beta1::MsgSend { | ||
from_address: msg.from_address.to_string(), | ||
to_address: msg.to_address.to_string(), | ||
amount: msg.amount.iter().map(Into::into).collect(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
//! Base functionality. | ||
|
||
use crate::{Decimal, Error, Result}; | ||
use cosmos_sdk_proto::cosmos; | ||
use std::{ | ||
convert::TryFrom, | ||
fmt::{self, Display}, | ||
str::FromStr, | ||
}; | ||
|
||
/// Coin defines a token with a denomination and an amount. | ||
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] | ||
pub struct Coin { | ||
/// Denomination | ||
pub denom: Denom, | ||
|
||
/// Amount | ||
pub amount: Decimal, | ||
} | ||
|
||
impl TryFrom<cosmos::base::v1beta1::Coin> for Coin { | ||
type Error = eyre::Report; | ||
|
||
fn try_from(proto: cosmos::base::v1beta1::Coin) -> Result<Coin> { | ||
Coin::try_from(&proto) | ||
} | ||
} | ||
|
||
impl TryFrom<&cosmos::base::v1beta1::Coin> for Coin { | ||
type Error = eyre::Report; | ||
|
||
fn try_from(proto: &cosmos::base::v1beta1::Coin) -> Result<Coin> { | ||
Ok(Coin { | ||
denom: proto.denom.parse()?, | ||
amount: proto.amount.parse()?, | ||
}) | ||
} | ||
} | ||
|
||
impl From<Coin> for cosmos::base::v1beta1::Coin { | ||
fn from(coin: Coin) -> cosmos::base::v1beta1::Coin { | ||
cosmos::base::v1beta1::Coin::from(&coin) | ||
} | ||
} | ||
|
||
impl From<&Coin> for cosmos::base::v1beta1::Coin { | ||
fn from(coin: &Coin) -> cosmos::base::v1beta1::Coin { | ||
cosmos::base::v1beta1::Coin { | ||
denom: coin.denom.to_string(), | ||
amount: coin.amount.to_string(), | ||
} | ||
} | ||
} | ||
|
||
/// Denomination. | ||
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] | ||
pub struct Denom(String); | ||
|
||
impl AsRef<str> for Denom { | ||
fn as_ref(&self) -> &str { | ||
self.0.as_ref() | ||
} | ||
} | ||
|
||
impl Display for Denom { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.write_str(self.as_ref()) | ||
} | ||
} | ||
|
||
impl FromStr for Denom { | ||
type Err = eyre::Report; | ||
|
||
fn from_str(s: &str) -> Result<Self> { | ||
// TODO(tarcieri): ensure this is the proper validation for a denom name | ||
if s.chars().all(|c| matches!(c, 'a'..='z')) { | ||
Ok(Denom(s.to_owned())) | ||
} else { | ||
Err(Error::Denom.into()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,35 @@ | ||
//! Transaction builder and signer for Cosmos-based blockchains | ||
|
||
#![cfg_attr(docsrs, feature(doc_cfg))] | ||
#![doc( | ||
html_logo_url = "https://raw.githubusercontent.com/cosmos/cosmos-rust/main/.images/cosmos.png", | ||
html_root_url = "https://docs.rs/cosmos-sdk-proto/0.2.0-pre" | ||
)] | ||
#![forbid(unsafe_code)] | ||
#![warn(trivial_casts, trivial_numeric_casts, unused_import_braces)] | ||
|
||
pub mod bank; | ||
pub mod tx; | ||
|
||
mod base; | ||
mod builder; | ||
mod decimal; | ||
mod error; | ||
mod msg; | ||
mod prost_ext; | ||
mod signing_key; | ||
|
||
pub use crate::{ | ||
builder::Builder, decimal::Decimal, error::Error, msg::Msg, signing_key::SigningKey, | ||
base::{Coin, Denom}, | ||
builder::Builder, | ||
decimal::Decimal, | ||
error::{Error, Result}, | ||
msg::{Msg, MsgType}, | ||
signing_key::SigningKey, | ||
tx::Fee, | ||
}; | ||
pub use k256::ecdsa::{Signature, VerifyingKey}; | ||
pub use tendermint::PublicKey; | ||
pub use tendermint::{self, account::Id as AccountId, PublicKey}; | ||
|
||
#[cfg(feature = "rpc")] | ||
pub use tendermint_rpc as rpc; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.