-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cosmos-tx: MsgSend support w\ integration test #71
Merged
tony-iqlusion
merged 2 commits into
main
from
tony-iqlusion/cosmos-tx-msg-send-and-integration-test
Apr 9, 2021
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,73 @@ | ||
//! Bank module support | ||
//! | ||
//! <https://docs.cosmos.network/master/modules/bank/> | ||
|
||
use crate::{ | ||
tx::{Msg, MsgType}, | ||
AccountId, Coin, 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()?, | ||
to_address: proto.to_address.parse()?, | ||
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,171 @@ | ||
//! Base functionality. | ||
|
||
use crate::{Decimal, Error, Result}; | ||
use cosmos_sdk_proto::cosmos; | ||
use std::{ | ||
convert::{TryFrom, TryInto}, | ||
fmt, | ||
str::FromStr, | ||
}; | ||
use subtle_encoding::bech32; | ||
|
||
/// Account identifiers | ||
#[derive(Clone, Eq, PartialEq, PartialOrd, Ord)] | ||
pub struct AccountId { | ||
/// Account ID encoded as Bech32 | ||
bech32: String, | ||
|
||
/// Length of the human-readable prefix of the address | ||
hrp_length: usize, | ||
} | ||
|
||
impl AccountId { | ||
/// Create an [`AccountId`] with the given human-readable prefix and | ||
/// public key hash. | ||
pub fn new(prefix: &str, bytes: [u8; tendermint::account::LENGTH]) -> Result<Self> { | ||
let id = bech32::encode(prefix, &bytes); | ||
|
||
// TODO(tarcieri): ensure this is the proper validation for an account prefix | ||
if prefix.chars().all(|c| matches!(c, 'a'..='z')) { | ||
Ok(Self { | ||
bech32: id, | ||
hrp_length: prefix.len(), | ||
}) | ||
} else { | ||
Err(Error::AccountId { id }.into()) | ||
} | ||
} | ||
|
||
/// Get the human-readable prefix of this account. | ||
pub fn prefix(&self) -> &str { | ||
&self.bech32[..self.hrp_length] | ||
} | ||
|
||
/// Decode an account ID from Bech32 to an inner byte value. | ||
pub fn to_bytes(&self) -> [u8; tendermint::account::LENGTH] { | ||
bech32::decode(&self.bech32) | ||
.ok() | ||
.and_then(|result| result.1.try_into().ok()) | ||
.expect("malformed Bech32 AccountId") | ||
} | ||
} | ||
|
||
impl AsRef<str> for AccountId { | ||
fn as_ref(&self) -> &str { | ||
&self.bech32 | ||
} | ||
} | ||
|
||
impl fmt::Debug for AccountId { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_tuple("AccountId").field(&self.as_ref()).finish() | ||
} | ||
} | ||
|
||
impl fmt::Display for AccountId { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.write_str(self.as_ref()) | ||
} | ||
} | ||
|
||
impl FromStr for AccountId { | ||
type Err = eyre::Report; | ||
|
||
fn from_str(s: &str) -> Result<Self> { | ||
let (hrp, bytes) = bech32::decode(s)?; | ||
|
||
if bytes.len() == tendermint::account::LENGTH { | ||
Ok(Self { | ||
bech32: s.to_owned(), | ||
hrp_length: hrp.len(), | ||
}) | ||
} else { | ||
Err(Error::AccountId { id: s.to_owned() }.into()) | ||
} | ||
} | ||
} | ||
|
||
impl From<AccountId> for tendermint::account::Id { | ||
fn from(id: AccountId) -> tendermint::account::Id { | ||
tendermint::account::Id::from(&id) | ||
} | ||
} | ||
|
||
impl From<&AccountId> for tendermint::account::Id { | ||
fn from(id: &AccountId) -> tendermint::account::Id { | ||
tendermint::account::Id::new(id.to_bytes()) | ||
} | ||
} | ||
|
||
/// 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 fmt::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 { name: s.to_owned() }.into()) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
big thumbs on on local types for these things. 👍🏻