Skip to content

Commit

Permalink
feat: support decoding ibc tx details in sdk (#1174)
Browse files Browse the repository at this point in the history
  • Loading branch information
emccorson authored and mateuszjasiuk committed Oct 30, 2024
1 parent eb1c685 commit 481fe9a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 6 deletions.
2 changes: 2 additions & 0 deletions packages/sdk/src/tx/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,8 @@ export class Tx {
return deserialize(data, TransferMsgValue);
case TxType.RevealPK:
return deserialize(data, RevealPkMsgValue);
case TxType.IBCTransfer:
return deserialize(data, IbcTransferMsgValue);
default:
throw "Unsupported Tx type!";
}
Expand Down
3 changes: 2 additions & 1 deletion packages/shared/lib/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use namada_sdk::state::Key;
use namada_sdk::token;
use namada_sdk::tx::{
TX_BOND_WASM, TX_CLAIM_REWARDS_WASM, TX_REDELEGATE_WASM, TX_REVEAL_PK, TX_TRANSFER_WASM,
TX_UNBOND_WASM, TX_VOTE_PROPOSAL, TX_WITHDRAW_WASM,
TX_UNBOND_WASM, TX_VOTE_PROPOSAL, TX_WITHDRAW_WASM, TX_IBC_WASM,
};
use namada_sdk::uint::I256;
use namada_sdk::wallet::DatedKeypair;
Expand Down Expand Up @@ -720,6 +720,7 @@ impl Query {
TX_CLAIM_REWARDS_WASM.to_string(),
TX_REVEAL_PK.to_string(),
TX_VOTE_PROPOSAL.to_string(),
TX_IBC_WASM.to_string(),
]
}

Expand Down
28 changes: 28 additions & 0 deletions packages/shared/lib/src/sdk/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,34 @@ pub struct IbcTransferMsg {
shielding_data: Option<Vec<u8>>,
}

impl IbcTransferMsg {
pub fn new(
source: String,
receiver: String,
token: String,
amount: String,
port_id: String,
channel_id: String,
timeout_height: Option<u64>,
timeout_sec_offset: Option<u64>,
memo: Option<String>,
shielding_data: Option<Vec<u8>>,
) -> IbcTransferMsg {
IbcTransferMsg {
source,
receiver,
token,
amount,
port_id,
channel_id,
timeout_height,
timeout_sec_offset,
memo,
shielding_data,
}
}
}

/// Maps serialized tx_msg into IbcTransferTx args.
///
/// # Arguments
Expand Down
34 changes: 30 additions & 4 deletions packages/shared/lib/src/sdk/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
use namada_sdk::borsh::BorshSerializeExt;
use namada_sdk::token::Transfer;
use serde::Serialize;

use namada_sdk::governance::VoteProposalData;
use namada_sdk::tx::data::pos::{Bond, ClaimRewards, Redelegation, Unbond, Withdraw};
use namada_sdk::{
borsh::{self, BorshDeserialize},
key::common::PublicKey,
};
use namada_sdk::ibc::MsgTransfer;
use namada_sdk::ibc::core::channel::types::timeout::TimeoutHeight;
use wasm_bindgen::JsError;

use crate::sdk::{
args::{
BondMsg, ClaimRewardsMsg, RedelegateMsg, RevealPkMsg, TransferDataMsg, TransferMsg,
UnbondMsg, VoteProposalMsg, WithdrawMsg,
UnbondMsg, VoteProposalMsg, WithdrawMsg, IbcTransferMsg
},
tx::TxType,
};

#[derive(Serialize, Debug, Clone)]
#[serde(untagged)]
#[derive(Debug, Clone)]
pub enum TransactionKind {
Transfer(Transfer),
Bond(Bond),
Expand All @@ -29,6 +29,7 @@ pub enum TransactionKind {
ProposalVote(VoteProposalData),
ClaimRewards(ClaimRewards),
RevealPk(PublicKey),
IbcTransfer(MsgTransfer<Transfer>),
Unknown,
}

Expand Down Expand Up @@ -59,6 +60,9 @@ impl TransactionKind {
TxType::RevealPK => TransactionKind::RevealPk(
PublicKey::try_from_slice(data).expect("Cannot deserialize PublicKey"),
),
TxType::IBCTransfer => TransactionKind::IbcTransfer(
MsgTransfer::try_from_slice(data).expect("Cannot deserialize MsgTransfer"),
),
_ => TransactionKind::Unknown,
}
}
Expand Down Expand Up @@ -181,6 +185,28 @@ impl TransactionKind {
);
borsh::to_vec(&claim_rewards)?
}
TransactionKind::IbcTransfer(msg_transfer) => {
let MsgTransfer { message, .. } = msg_transfer;

let timeout_height = match message.timeout_height_on_b {
TimeoutHeight::At(height) => Some(height.revision_height()),
TimeoutHeight::Never => None
};

let ibc_transfer_msg = IbcTransferMsg::new(
message.packet_data.sender.to_string(),
message.packet_data.receiver.to_string(),
message.packet_data.token.denom.base_denom.to_string(),
message.packet_data.token.amount.to_string(),
message.port_id_on_a.to_string(),
message.chan_id_on_a.to_string(),
timeout_height,
None,
Some(message.packet_data.memo.to_string()),
None
);
borsh::to_vec(&ibc_transfer_msg)?
},
_ => panic!("Unsupported Tx provided, cannot serialize"),
};

Expand Down
3 changes: 2 additions & 1 deletion packages/shared/lib/src/sdk/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use namada_sdk::tx::data::compute_inner_tx_hash;
use namada_sdk::tx::either::Either;
use namada_sdk::tx::{
self, TX_BOND_WASM, TX_CLAIM_REWARDS_WASM, TX_REDELEGATE_WASM, TX_REVEAL_PK, TX_TRANSFER_WASM,
TX_UNBOND_WASM, TX_VOTE_PROPOSAL, TX_WITHDRAW_WASM,
TX_UNBOND_WASM, TX_VOTE_PROPOSAL, TX_WITHDRAW_WASM, TX_IBC_WASM,
};
use namada_sdk::uint::Uint;
use namada_sdk::{address::Address, key::common::PublicKey};
Expand Down Expand Up @@ -183,6 +183,7 @@ pub fn wasm_hash_to_tx_type(wasm_hash: &str, wasm_hashes: &Vec<WasmHash>) -> Opt
(TX_CLAIM_REWARDS_WASM.to_string(), TxType::ClaimRewards),
(TX_REVEAL_PK.to_string(), TxType::RevealPK),
(TX_VOTE_PROPOSAL.to_string(), TxType::VoteProposal),
(TX_IBC_WASM.to_string(), TxType::IBCTransfer),
]);

for wh in wasm_hashes {
Expand Down

0 comments on commit 481fe9a

Please sign in to comment.