Skip to content

Commit

Permalink
cosmrs: add bank::MsgMultiSend support (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrit authored Jun 2, 2022
1 parent bae0a9b commit 5b52a8c
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 0 deletions.
142 changes: 142 additions & 0 deletions cosmrs/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,145 @@ impl From<&MsgSend> for proto::cosmos::bank::v1beta1::MsgSend {
}
}
}

/// MsgMultiSend represents an arbitrary multi-in, multi-out send message.
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct MsgMultiSend {
/// Sender account/amount pairs.
pub inputs: Vec<MultiSendIo>,

/// Recipient account/amount pairs.
pub outputs: Vec<MultiSendIo>,
}

impl Msg for MsgMultiSend {
type Proto = proto::cosmos::bank::v1beta1::MsgMultiSend;
}

impl TryFrom<proto::cosmos::bank::v1beta1::MsgMultiSend> for MsgMultiSend {
type Error = ErrorReport;

fn try_from(proto: proto::cosmos::bank::v1beta1::MsgMultiSend) -> Result<MsgMultiSend> {
MsgMultiSend::try_from(&proto)
}
}

impl TryFrom<&proto::cosmos::bank::v1beta1::MsgMultiSend> for MsgMultiSend {
type Error = ErrorReport;

fn try_from(proto: &proto::cosmos::bank::v1beta1::MsgMultiSend) -> Result<MsgMultiSend> {
Ok(MsgMultiSend {
inputs: proto
.inputs
.iter()
.map(TryFrom::try_from)
.collect::<Result<_, _>>()?,
outputs: proto
.outputs
.iter()
.map(TryFrom::try_from)
.collect::<Result<_, _>>()?,
})
}
}

impl From<MsgMultiSend> for proto::cosmos::bank::v1beta1::MsgMultiSend {
fn from(coin: MsgMultiSend) -> proto::cosmos::bank::v1beta1::MsgMultiSend {
proto::cosmos::bank::v1beta1::MsgMultiSend::from(&coin)
}
}

impl From<&MsgMultiSend> for proto::cosmos::bank::v1beta1::MsgMultiSend {
fn from(msg: &MsgMultiSend) -> proto::cosmos::bank::v1beta1::MsgMultiSend {
proto::cosmos::bank::v1beta1::MsgMultiSend {
inputs: msg.inputs.iter().map(Into::into).collect(),
outputs: msg.outputs.iter().map(Into::into).collect(),
}
}
}

/// Represents a MultiSend Input or Output
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct MultiSendIo {
/// The address that `coins` will be sent to/from
pub address: AccountId,

/// The coins to send to/from `address`
pub coins: Vec<Coin>,
}

impl TryFrom<proto::cosmos::bank::v1beta1::Input> for MultiSendIo {
type Error = ErrorReport;

fn try_from(proto: proto::cosmos::bank::v1beta1::Input) -> Result<MultiSendIo> {
MultiSendIo::try_from(&proto)
}
}

impl TryFrom<&proto::cosmos::bank::v1beta1::Input> for MultiSendIo {
type Error = ErrorReport;

fn try_from(proto: &proto::cosmos::bank::v1beta1::Input) -> Result<MultiSendIo> {
Ok(MultiSendIo {
address: proto.address.parse()?,
coins: proto
.coins
.iter()
.map(TryFrom::try_from)
.collect::<Result<_, _>>()?,
})
}
}

impl TryFrom<proto::cosmos::bank::v1beta1::Output> for MultiSendIo {
type Error = ErrorReport;

fn try_from(proto: proto::cosmos::bank::v1beta1::Output) -> Result<MultiSendIo> {
MultiSendIo::try_from(&proto)
}
}

impl TryFrom<&proto::cosmos::bank::v1beta1::Output> for MultiSendIo {
type Error = ErrorReport;

fn try_from(proto: &proto::cosmos::bank::v1beta1::Output) -> Result<MultiSendIo> {
Ok(MultiSendIo {
address: proto.address.parse()?,
coins: proto
.coins
.iter()
.map(TryFrom::try_from)
.collect::<Result<_, _>>()?,
})
}
}

impl From<MultiSendIo> for proto::cosmos::bank::v1beta1::Output {
fn from(output: MultiSendIo) -> proto::cosmos::bank::v1beta1::Output {
proto::cosmos::bank::v1beta1::Output::from(&output)
}
}

impl From<&MultiSendIo> for proto::cosmos::bank::v1beta1::Output {
fn from(output: &MultiSendIo) -> proto::cosmos::bank::v1beta1::Output {
proto::cosmos::bank::v1beta1::Output {
address: output.address.to_string(),
coins: output.coins.iter().map(Into::into).collect(),
}
}
}

impl From<MultiSendIo> for proto::cosmos::bank::v1beta1::Input {
fn from(input: MultiSendIo) -> proto::cosmos::bank::v1beta1::Input {
proto::cosmos::bank::v1beta1::Input::from(&input)
}
}

impl From<&MultiSendIo> for proto::cosmos::bank::v1beta1::Input {
fn from(input: &MultiSendIo) -> proto::cosmos::bank::v1beta1::Input {
proto::cosmos::bank::v1beta1::Input {
address: input.address.to_string(),
coins: input.coins.iter().map(Into::into).collect(),
}
}
}
4 changes: 4 additions & 0 deletions cosmrs/src/tx/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ impl MsgProto for proto::cosmos::bank::v1beta1::MsgSend {
const TYPE_URL: &'static str = "/cosmos.bank.v1beta1.MsgSend";
}

impl MsgProto for proto::cosmos::bank::v1beta1::MsgMultiSend {
const TYPE_URL: &'static str = "/cosmos.bank.v1beta1.MsgMultiSend";
}

impl MsgProto for proto::cosmos::distribution::v1beta1::MsgSetWithdrawAddress {
const TYPE_URL: &'static str = "/cosmos.distribution.v1beta1.MsgSetWithdrawAddress";
}
Expand Down

0 comments on commit 5b52a8c

Please sign in to comment.