Skip to content

Commit a1b922e

Browse files
author
Tomasz Kulik
committed
chore: Update after review
1 parent 39179ac commit a1b922e

File tree

8 files changed

+99
-49
lines changed

8 files changed

+99
-49
lines changed

contracts/ibc-callbacks/schema/ibc-callbacks.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@
4343
},
4444
"channel_version": {
4545
"description": "IBC channel version",
46-
"type": "string"
46+
"allOf": [
47+
{
48+
"$ref": "#/definitions/ChannelVersion"
49+
}
50+
]
4751
},
4852
"timeout_seconds": {
4953
"description": "The amount of seconds from now the transfer should timeout at",
@@ -87,6 +91,13 @@
8791
]
8892
}
8993
]
94+
},
95+
"ChannelVersion": {
96+
"type": "string",
97+
"enum": [
98+
"v1",
99+
"v2"
100+
]
90101
}
91102
}
92103
},

contracts/ibc-callbacks/schema/raw/execute.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@
3232
},
3333
"channel_version": {
3434
"description": "IBC channel version",
35-
"type": "string"
35+
"allOf": [
36+
{
37+
"$ref": "#/definitions/ChannelVersion"
38+
}
39+
]
3640
},
3741
"timeout_seconds": {
3842
"description": "The amount of seconds from now the transfer should timeout at",
@@ -76,6 +80,13 @@
7680
]
7781
}
7882
]
83+
},
84+
"ChannelVersion": {
85+
"type": "string",
86+
"enum": [
87+
"v1",
88+
"v2"
89+
]
7990
}
8091
}
8192
}

contracts/ibc-callbacks/src/contract.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use cosmwasm_std::{
44
MessageInfo, Response, StdError, StdResult, TransferMsgBuilder, TransferMsgBuilderV2,
55
};
66

7-
use crate::msg::{CallbackType, ExecuteMsg, QueryMsg};
7+
use crate::msg::{CallbackType, ChannelVersion, ExecuteMsg, QueryMsg};
88
use crate::state::{load_stats, save_stats, CallbackStats};
99

1010
#[entry_point]
@@ -55,7 +55,7 @@ fn execute_transfer(
5555
channel_id: String,
5656
timeout_seconds: u32,
5757
callback_type: CallbackType,
58-
channel_version: String,
58+
channel_version: ChannelVersion,
5959
) -> StdResult<Response> {
6060
let src_callback = IbcSrcCallback {
6161
address: env.contract.address,
@@ -66,8 +66,8 @@ fn execute_transfer(
6666
gas_limit: None,
6767
};
6868

69-
let transfer_msg = match channel_version.as_str() {
70-
"V1" => {
69+
let transfer_msg = match channel_version {
70+
ChannelVersion::V1 => {
7171
let coin = match &*info.funds {
7272
[coin] if !coin.amount.is_zero() => coin,
7373
_ => {
@@ -91,7 +91,7 @@ fn execute_transfer(
9191
CallbackType::Dst => builder.with_dst_callback(dst_callback).build(),
9292
}
9393
}
94-
"V2" => {
94+
ChannelVersion::V2 => {
9595
let builder = TransferMsgBuilderV2::new(
9696
channel_id,
9797
to_address.clone(),
@@ -107,11 +107,6 @@ fn execute_transfer(
107107
CallbackType::Dst => builder.with_dst_callback(dst_callback).build(),
108108
}
109109
}
110-
_ => {
111-
return Err(StdError::generic_err(
112-
"Must specify \"V1\" or \"V2\" channel version",
113-
))
114-
}
115110
};
116111

117112
Ok(Response::new()

contracts/ibc-callbacks/src/msg.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ pub enum QueryMsg {
88
CallbackStats {},
99
}
1010

11+
#[cw_serde]
12+
pub enum ChannelVersion {
13+
V1,
14+
V2,
15+
}
16+
1117
#[cw_serde]
1218
pub enum ExecuteMsg {
1319
Transfer {
@@ -21,7 +27,7 @@ pub enum ExecuteMsg {
2127
#[serde(default)]
2228
callback_type: CallbackType,
2329
/// IBC channel version
24-
channel_version: String,
30+
channel_version: ChannelVersion,
2531
},
2632
}
2733

packages/go-gen/tests/cosmwasm_std__IbcMsg.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ type TransferV2Msg struct {
2121
// 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.
2222
//
2323
// There is no difference between setting this to `None` or an empty string.
24-
//
25-
// This field is only supported on chains with CosmWasm >= 2.0 and silently ignored on older chains. If you need support for both 1.x and 2.x chain with the same codebase, it is recommended to use `CosmosMsg::Stargate` with a custom MsgTransfer protobuf encoder instead.
2624
Memo string `json:"memo,omitempty"`
2725
// when packet times out, measured on remote chain
2826
Timeout IBCTimeout `json:"timeout"`

packages/std/src/ibc.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
use core::cmp::{Ord, Ordering, PartialOrd};
55
use schemars::JsonSchema;
66
use serde::{Deserialize, Serialize};
7-
use std::vec;
87

98
use crate::coin::Coin;
109
use crate::prelude::*;
@@ -84,12 +83,6 @@ pub enum IbcMsg {
8483
/// for more information.
8584
///
8685
/// There is no difference between setting this to `None` or an empty string.
87-
///
88-
/// This field is only supported on chains with CosmWasm >= 2.0 and silently
89-
/// ignored on older chains.
90-
/// If you need support for both 1.x and 2.x chain with the same codebase,
91-
/// it is recommended to use `CosmosMsg::Stargate` with a custom MsgTransfer
92-
/// protobuf encoder instead.
9386
memo: Option<String>,
9487
// A struct containing the list of next hops,
9588
// determining where the tokens must be forwarded next.

packages/std/src/ibc/transfer_msg_builder.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@ pub struct EmptyMemo;
99
#[derive(Clone, Debug, PartialEq, Eq)]
1010
#[non_exhaustive]
1111
pub struct WithMemo {
12-
pub memo: String,
12+
pub(crate) memo: String,
1313
}
1414
#[derive(Clone, Debug, PartialEq, Eq)]
1515
#[non_exhaustive]
1616
pub struct WithSrcCallback {
17-
pub src_callback: IbcSrcCallback,
17+
pub(crate) src_callback: IbcSrcCallback,
1818
}
1919
#[derive(Clone, Debug, PartialEq, Eq)]
2020
#[non_exhaustive]
2121
pub struct WithDstCallback {
22-
pub dst_callback: IbcDstCallback,
22+
pub(crate) dst_callback: IbcDstCallback,
2323
}
2424
#[derive(Clone, Debug, PartialEq, Eq)]
2525
#[non_exhaustive]
2626
pub struct WithCallbacks {
27-
pub src_callback: IbcSrcCallback,
28-
pub dst_callback: IbcDstCallback,
27+
pub(crate) src_callback: IbcSrcCallback,
28+
pub(crate) dst_callback: IbcDstCallback,
2929
}
3030

3131
pub trait MemoSource {

packages/std/src/ibc/transfer_msg_builder_v2.rs

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,52 @@ use super::{
44
EmptyMemo, Hop, MemoSource, WithCallbacks, WithDstCallback, WithMemo, WithSrcCallback,
55
};
66

7-
impl<M: MemoSource> TransferMsgBuilderV2<M> {
7+
impl<M: MemoSource, F: Into<Vec<Hop>>> TransferMsgBuilderV2<M, F> {
88
pub fn build(self) -> IbcMsg {
99
IbcMsg::TransferV2 {
1010
channel_id: self.channel_id,
1111
to_address: self.to_address,
1212
tokens: self.tokens,
1313
timeout: self.timeout,
1414
memo: self.memo.into_memo(),
15-
forwarding: self.forwarding,
15+
forwarding: self.forwarding.into(),
1616
}
1717
}
1818
}
1919

2020
#[derive(Clone, Debug, PartialEq, Eq)]
21-
pub struct TransferMsgBuilderV2<MemoData> {
21+
pub struct TransferMsgBuilderV2<MemoData, ForwardingData> {
2222
channel_id: String,
2323
to_address: String,
2424
tokens: Vec<Coin>,
2525
timeout: IbcTimeout,
2626
memo: MemoData,
27-
forwarding: Vec<Hop>,
27+
forwarding: ForwardingData,
2828
}
2929

3030
#[derive(Clone, Debug, PartialEq, Eq)]
3131
#[non_exhaustive]
32-
pub struct WithForwarding;
32+
pub struct WithoutForwarding;
33+
34+
#[derive(Clone, Debug, PartialEq, Eq)]
35+
#[non_exhaustive]
36+
pub struct WithForwarding {
37+
pub(crate) forwarding: Vec<Hop>,
38+
}
3339

34-
impl MemoSource for WithForwarding {
35-
fn into_memo(self) -> Option<String> {
36-
None
40+
impl From<WithoutForwarding> for Vec<Hop> {
41+
fn from(_val: WithoutForwarding) -> Self {
42+
vec![]
3743
}
3844
}
3945

40-
impl TransferMsgBuilderV2<EmptyMemo> {
46+
impl From<WithForwarding> for Vec<Hop> {
47+
fn from(val: WithForwarding) -> Self {
48+
val.forwarding
49+
}
50+
}
51+
52+
impl TransferMsgBuilderV2<EmptyMemo, WithoutForwarding> {
4153
/// Creates a new transfer message with the given parameters and no memo.
4254
pub fn new(
4355
channel_id: impl Into<String>,
@@ -51,12 +63,15 @@ impl TransferMsgBuilderV2<EmptyMemo> {
5163
tokens,
5264
timeout: timeout.into(),
5365
memo: EmptyMemo,
54-
forwarding: vec![],
66+
forwarding: WithoutForwarding,
5567
}
5668
}
5769

5870
/// Adds a memo text to the transfer message.
59-
pub fn with_memo(self, memo: impl Into<String>) -> TransferMsgBuilderV2<WithMemo> {
71+
pub fn with_memo(
72+
self,
73+
memo: impl Into<String>,
74+
) -> TransferMsgBuilderV2<WithMemo, WithoutForwarding> {
6075
TransferMsgBuilderV2 {
6176
channel_id: self.channel_id,
6277
to_address: self.to_address,
@@ -74,7 +89,7 @@ impl TransferMsgBuilderV2<EmptyMemo> {
7489
pub fn with_src_callback(
7590
self,
7691
src_callback: IbcSrcCallback,
77-
) -> TransferMsgBuilderV2<WithSrcCallback> {
92+
) -> TransferMsgBuilderV2<WithSrcCallback, WithoutForwarding> {
7893
TransferMsgBuilderV2 {
7994
channel_id: self.channel_id,
8095
to_address: self.to_address,
@@ -92,7 +107,7 @@ impl TransferMsgBuilderV2<EmptyMemo> {
92107
pub fn with_dst_callback(
93108
self,
94109
dst_callback: IbcDstCallback,
95-
) -> TransferMsgBuilderV2<WithDstCallback> {
110+
) -> TransferMsgBuilderV2<WithDstCallback, WithoutForwarding> {
96111
TransferMsgBuilderV2 {
97112
channel_id: self.channel_id,
98113
to_address: self.to_address,
@@ -105,29 +120,32 @@ impl TransferMsgBuilderV2<EmptyMemo> {
105120

106121
/// Adds forwarding data.
107122
/// It is worth to notice that the builder does not allow to add forwarding data along with
108-
/// callbacks. It is discouraged in the IBC docs:
109-
/// https://github.com/cosmos/ibc-go/blob/main/docs/docs/04-middleware/02-callbacks/01-overview.md#known-limitations
110-
pub fn with_forwarding(self, forwarding: Vec<Hop>) -> TransferMsgBuilderV2<WithForwarding> {
123+
/// source callback. It is discouraged in the IBC docs:
124+
/// https://ibc.cosmos.network/v9/middleware/callbacks/overview/#known-limitations
125+
pub fn with_forwarding(
126+
self,
127+
forwarding: Vec<Hop>,
128+
) -> TransferMsgBuilderV2<EmptyMemo, WithForwarding> {
111129
TransferMsgBuilderV2 {
112130
channel_id: self.channel_id,
113131
to_address: self.to_address,
114132
tokens: self.tokens,
115133
timeout: self.timeout,
116-
memo: WithForwarding,
117-
forwarding,
134+
memo: self.memo,
135+
forwarding: WithForwarding { forwarding },
118136
}
119137
}
120138
}
121139

122-
impl TransferMsgBuilderV2<WithSrcCallback> {
140+
impl TransferMsgBuilderV2<WithSrcCallback, WithoutForwarding> {
123141
/// Adds an IBC destination callback entry to the memo field.
124142
/// Use this if you want to receive IBC callbacks on the destination chain.
125143
///
126144
/// For more info check out [`crate::IbcDestinationCallbackMsg`].
127145
pub fn with_dst_callback(
128146
self,
129147
dst_callback: IbcDstCallback,
130-
) -> TransferMsgBuilderV2<WithCallbacks> {
148+
) -> TransferMsgBuilderV2<WithCallbacks, WithoutForwarding> {
131149
TransferMsgBuilderV2 {
132150
channel_id: self.channel_id,
133151
to_address: self.to_address,
@@ -142,15 +160,15 @@ impl TransferMsgBuilderV2<WithSrcCallback> {
142160
}
143161
}
144162

145-
impl TransferMsgBuilderV2<WithDstCallback> {
163+
impl TransferMsgBuilderV2<WithDstCallback, WithoutForwarding> {
146164
/// Adds an IBC source callback entry to the memo field.
147165
/// Use this if you want to receive IBC callbacks on the source chain.
148166
///
149167
/// For more info check out [`crate::IbcSourceCallbackMsg`].
150168
pub fn with_src_callback(
151169
self,
152170
src_callback: IbcSrcCallback,
153-
) -> TransferMsgBuilderV2<WithCallbacks> {
171+
) -> TransferMsgBuilderV2<WithCallbacks, WithoutForwarding> {
154172
TransferMsgBuilderV2 {
155173
channel_id: self.channel_id,
156174
to_address: self.to_address,
@@ -163,6 +181,24 @@ impl TransferMsgBuilderV2<WithDstCallback> {
163181
forwarding: self.forwarding,
164182
}
165183
}
184+
185+
/// Adds forwarding data.
186+
/// It is worth to notice that the builder does not allow to add forwarding data along with
187+
/// source callback. It is discouraged in the IBC docs:
188+
/// https://ibc.cosmos.network/v9/middleware/callbacks/overview/#known-limitations
189+
pub fn with_forwarding(
190+
self,
191+
forwarding: Vec<Hop>,
192+
) -> TransferMsgBuilderV2<WithDstCallback, WithForwarding> {
193+
TransferMsgBuilderV2 {
194+
channel_id: self.channel_id,
195+
to_address: self.to_address,
196+
tokens: self.tokens,
197+
timeout: self.timeout,
198+
memo: self.memo,
199+
forwarding: WithForwarding { forwarding },
200+
}
201+
}
166202
}
167203

168204
#[cfg(test)]

0 commit comments

Comments
 (0)