Skip to content

Commit

Permalink
feat: Improve TransferV2 API
Browse files Browse the repository at this point in the history
  • Loading branch information
kulikthebird committed Feb 7, 2025
1 parent f8f75dc commit 8e8a6ef
Show file tree
Hide file tree
Showing 4 changed files with 264 additions and 160 deletions.
4 changes: 3 additions & 1 deletion contracts/ibc-callbacks/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@ fn execute_transfer(
}
ChannelVersion::V2 => {
let builder = TransferMsgBuilderV2::new(
channel_id,
to_address.clone(),
info.funds.into_iter().map(Into::into).collect(),
)
.with_direct_transfer(
channel_id,
IbcTimeout::with_timestamp(env.block.time.plus_seconds(timeout_seconds as u64)),
);
match callback_type {
Expand Down
34 changes: 25 additions & 9 deletions packages/go-gen/tests/cosmwasm_std__IbcMsg.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,16 @@ type TransferMsg struct {
ToAddress string `json:"to_address"`
}
type TransferV2Msg struct {
// Existing channel to send the tokens over.
ChannelID string `json:"channel_id"`
Forwarding *Forwarding `json:"forwarding,omitempty"`
// An optional memo. See the blog post ["Moving Beyond Simple Token Transfers"](https://medium.com/the-interchain-foundation/moving-beyond-simple-token-transfers-d42b2b1dc29b) for more information.
//
// There is no difference between setting this to `None` or an empty string.
Memo string `json:"memo,omitempty"`
// when packet times out, measured on remote chain.
Timeout IBCTimeout `json:"timeout"`
// Address on the remote chain to receive these tokens.
ToAddress string `json:"to_address"`
// MsgTransfer in v2 version supports multiple coins.
Tokens Array[Coin] `json:"tokens"`
// The transfer can be of type: * Direct, * Forwarding, * Forwarding with unwind flag set.
TransferType TransferV2Type `json:"transfer_type"`
}
type SendPacketMsg struct {
ChannelID string `json:"channel_id"`
Expand Down Expand Up @@ -102,10 +99,6 @@ type Coin struct {
Amount string `json:"amount"`
Denom string `json:"denom"`
}
type Forwarding struct {
Hops Array[Hop] `json:"hops"`
Unwind bool `json:"unwind"`
}
type Hop struct {
ChannelID string `json:"channel_id"`
PortID string `json:"port_id"`
Expand All @@ -131,4 +124,27 @@ type IBCTimeoutBlock struct {
Height uint64 `json:"height"`
// the version that the client is currently on (e.g. after resetting the chain this could increment 1 as height drops to 0)
Revision uint64 `json:"revision"`
}
type DirectType struct {
// Existing channel to send the tokens over.
ChannelID string `json:"channel_id"`
// When packet times out, measured on remote chain.
IBCTimeout IBCTimeout `json:"ibc_timeout"`
}
type MultiHopType struct {
// Existing channel to send the tokens over.
ChannelID string `json:"channel_id"`
Hops Array[Hop] `json:"hops"`
// When packet times out, measured on remote chain. TimestampHeight is not supported in ibc-go Transfer V2.
Timeout Uint64 `json:"timeout"`
}
type UnwindingType struct {
Hops Array[Hop] `json:"hops"`
// When packet times out, measured on remote chain. TimestampHeight is not supported in ibc-go Transfer V2.
Timeout Uint64 `json:"timeout"`
}
type TransferV2Type struct {
Direct *DirectType `json:"direct,omitempty"`
MultiHop *MultiHopType `json:"multi_hop,omitempty"`
Unwinding *UnwindingType `json:"unwinding,omitempty"`
}
46 changes: 34 additions & 12 deletions packages/std/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,35 @@ pub struct Hop {

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct Forwarding {
pub unwind: bool,
pub hops: Vec<Hop>,
pub enum TransferV2Type {
Direct {
/// Existing channel to send the tokens over.
channel_id: String,
/// When packet times out, measured on remote chain.
ibc_timeout: IbcTimeout,
},
MultiHop {
/// Existing channel to send the tokens over.
channel_id: String,
// A struct containing the list of next hops,
// determining where the tokens must be forwarded next.
// More info can be found in the `MsgTransfer` IBC docs:
// https://ibc.cosmos.network/main/apps/transfer/messages/
hops: Vec<Hop>,
/// When packet times out, measured on remote chain.
/// TimestampHeight is not supported in ibc-go Transfer V2.
timeout: Timestamp,
},
Unwinding {
// A struct containing the list of next hops,
// determining where the tokens must be forwarded next.
// More info can be found in the `MsgTransfer` IBC docs:
// https://ibc.cosmos.network/main/apps/transfer/messages/
hops: Vec<Hop>,
/// When packet times out, measured on remote chain.
/// TimestampHeight is not supported in ibc-go Transfer V2.
timeout: Timestamp,
},
}

/// These are messages in the IBC lifecycle. Only usable by IBC-enabled contracts
Expand Down Expand Up @@ -77,25 +103,21 @@ pub enum IbcMsg {
/// module to.
#[cfg(feature = "cosmwasm_3_0")]
TransferV2 {
/// Existing channel to send the tokens over.
channel_id: String,
/// The transfer can be of type:
/// * Direct,
/// * Forwarding,
/// * Forwarding with unwind flag set.
transfer_type: TransferV2Type,
/// Address on the remote chain to receive these tokens.
to_address: String,
/// MsgTransfer in v2 version supports multiple coins.
tokens: Vec<Coin>,
/// when packet times out, measured on remote chain.
timeout: IbcTimeout,
/// An optional memo. See the blog post
/// ["Moving Beyond Simple Token Transfers"](https://medium.com/the-interchain-foundation/moving-beyond-simple-token-transfers-d42b2b1dc29b)
/// for more information.
///
/// There is no difference between setting this to `None` or an empty string.
memo: Option<String>,
// A struct containing the list of next hops,
// determining where the tokens must be forwarded next.
// More info can be found in the `MsgTransfer` IBC docs:
// https://ibc.cosmos.network/main/apps/transfer/messages/
forwarding: Option<Forwarding>,
},
/// Sends an IBC packet with given data over the existing channel.
/// Data should be encoded in a format defined by the channel version,
Expand Down
Loading

0 comments on commit 8e8a6ef

Please sign in to comment.