Skip to content
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

Add bank::MsgMultiSend support #237

Merged
merged 4 commits into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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