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 support for MsgSend on THORChain #51

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Changes from 1 commit
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
52 changes: 45 additions & 7 deletions packages/cosmos/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,54 @@ impl From<MsgUpdateAdmin> for TxMessage {

impl From<MsgSend> for TxMessage {
fn from(msg: MsgSend) -> Self {
TxMessage::new(
"/cosmos.bank.v1beta1.MsgSend",
msg.encode_to_vec(),
format!(
"{} sending {} to {}",
// Very hacky approach to sending the alternative MsgSend
// on THORChain. For simplicity, we simply look for an appropriate
// HRP prefix on the destination address.
if msg.to_address.starts_with("sthor1") {
#[derive(::prost::Message)]
struct ThorMsgSend {
/// Source of funds
#[prost(bytes, tag = "1")]
from_address: Vec<u8>,
/// Destination of funds
#[prost(bytes, tag = "2")]
to_address: Vec<u8>,
/// Funds to be sent
#[prost(message, repeated, tag = "3")]
amount: ::prost::alloc::vec::Vec<cosmos_sdk_proto::cosmos::base::v1beta1::Coin>,
}
let description = format!(
"{} sending (via THORChain message) {} to {}",
msg.from_address,
PrettyCoins(msg.amount.as_slice()),
msg.to_address,
),
)
);
fn get_acc_address(s: &str) -> Vec<u8> {
// This code will panic if an invalid address is provided.
// We could fix that, but given how corner a case this is,
// I'm not interested in changing the overall API to accommodate
// it.
bech32::decode(s).unwrap().1
Copy link
Member

@psibi psibi Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should instead use expect here in case we run into the corner case and it might help us to figure out where the issue is ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, update pushed

}
let thormsg = ThorMsgSend {
from_address: get_acc_address(&msg.from_address),
to_address: get_acc_address(&msg.to_address),
amount: msg.amount,
};

TxMessage::new("/types.MsgSend", thormsg.encode_to_vec(), description)
} else {
TxMessage::new(
"/cosmos.bank.v1beta1.MsgSend",
msg.encode_to_vec(),
format!(
"{} sending {} to {}",
msg.from_address,
PrettyCoins(msg.amount.as_slice()),
msg.to_address,
),
)
}
}
}

Expand Down
Loading