Skip to content

Commit 7346ae1

Browse files
authored
Merge pull request #2167 from CosmWasm/chipshort/transfer-msg-builder
Implement TransferMsgBuilder
2 parents 3443f9a + d373f78 commit 7346ae1

File tree

6 files changed

+344
-13
lines changed

6 files changed

+344
-13
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ and this project adheres to
5151
`ibc_source_callback` and `ibc_destination_callback`, as well as the
5252
`IbcCallbackRequest` type. ([#2025])
5353
- cosmwasm-vm: Add support for the two new IBC Callbacks entrypoints. ([#2025])
54+
- cosmwasm-std: Add `TransferMsgBuilder` to more easily create an
55+
`IbcMsg::Transfer` with different kinds of memo values, including IBC
56+
Callbacks memo values. ([#2167])
5457

5558
[#1983]: https://github.com/CosmWasm/cosmwasm/pull/1983
5659
[#2025]: https://github.com/CosmWasm/cosmwasm/pull/2025
@@ -67,6 +70,7 @@ and this project adheres to
6770
[#2124]: https://github.com/CosmWasm/cosmwasm/pull/2124
6871
[#2129]: https://github.com/CosmWasm/cosmwasm/pull/2129
6972
[#2166]: https://github.com/CosmWasm/cosmwasm/pull/2166
73+
[#2167]: https://github.com/CosmWasm/cosmwasm/pull/2167
7074

7175
### Changed
7276

contracts/ibc-callbacks/src/contract.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use cosmwasm_std::{
2-
entry_point, to_json_binary, to_json_string, Binary, Deps, DepsMut, Empty, Env,
3-
IbcBasicResponse, IbcCallbackRequest, IbcDestinationCallbackMsg, IbcDstCallback, IbcMsg,
4-
IbcSourceCallbackMsg, IbcSrcCallback, IbcTimeout, MessageInfo, Response, StdError, StdResult,
2+
entry_point, to_json_binary, Binary, Deps, DepsMut, Empty, Env, IbcBasicResponse,
3+
IbcDestinationCallbackMsg, IbcDstCallback, IbcSourceCallbackMsg, IbcSrcCallback, IbcTimeout,
4+
MessageInfo, Response, StdError, StdResult, TransferMsgBuilder,
55
};
66

77
use crate::msg::{CallbackType, ExecuteMsg, QueryMsg};
@@ -71,16 +71,19 @@ fn execute_transfer(
7171
}
7272
};
7373

74-
let transfer_msg = IbcMsg::Transfer {
75-
to_address,
76-
timeout: IbcTimeout::with_timestamp(env.block.time.plus_seconds(timeout_seconds as u64)),
74+
let builder = TransferMsgBuilder::new(
7775
channel_id,
78-
amount: coin.clone(),
79-
memo: Some(to_json_string(&match callback_type {
80-
CallbackType::Both => IbcCallbackRequest::both(src_callback, dst_callback),
81-
CallbackType::Src => IbcCallbackRequest::source(src_callback),
82-
CallbackType::Dst => IbcCallbackRequest::destination(dst_callback),
83-
})?),
76+
to_address.clone(),
77+
coin.clone(),
78+
IbcTimeout::with_timestamp(env.block.time.plus_seconds(timeout_seconds as u64)),
79+
);
80+
let transfer_msg = match callback_type {
81+
CallbackType::Both => builder
82+
.with_src_callback(src_callback)
83+
.with_dst_callback(dst_callback)
84+
.build(),
85+
CallbackType::Src => builder.with_src_callback(src_callback).build(),
86+
CallbackType::Dst => builder.with_dst_callback(dst_callback).build(),
8487
};
8588

8689
Ok(Response::new()

packages/std/src/ibc.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ use crate::{to_json_binary, Binary};
1313
use crate::{Addr, Timestamp};
1414

1515
mod callbacks;
16+
mod transfer_msg_builder;
17+
1618
pub use callbacks::*;
19+
pub use transfer_msg_builder::*;
1720

1821
/// These are messages in the IBC lifecycle. Only usable by IBC-enabled contracts
1922
/// (contracts that directly speak the IBC protocol via 6 entry points)

packages/std/src/ibc/callbacks.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,34 @@ use crate::{Addr, IbcAcknowledgement, IbcPacket, Uint64};
1515
///
1616
/// # Example
1717
///
18+
/// Using [`TransferMsgBuilder`](crate::TransferMsgBuilder):
1819
/// ```rust
1920
/// use cosmwasm_std::{
20-
/// to_json_string, Coin, IbcCallbackRequest, IbcMsg, IbcSrcCallback, IbcTimeout, Response,
21+
/// to_json_string, Coin, IbcCallbackRequest, TransferMsgBuilder, IbcSrcCallback, IbcTimeout, Response,
2122
/// Timestamp,
2223
/// };
24+
/// # use cosmwasm_std::testing::mock_env;
25+
/// # let env = mock_env();
26+
///
27+
/// let _msg = TransferMsgBuilder::new(
28+
/// "channel-0".to_string(),
29+
/// "cosmos1example".to_string(),
30+
/// Coin::new(10u32, "ucoin"),
31+
/// Timestamp::from_seconds(12345),
32+
/// )
33+
/// .with_src_callback(IbcSrcCallback {
34+
/// address: env.contract.address,
35+
/// gas_limit: None,
36+
/// })
37+
/// .build();
38+
/// ```
2339
///
40+
/// Manual serialization:
41+
/// ```rust
42+
/// use cosmwasm_std::{
43+
/// to_json_string, Coin, IbcCallbackRequest, IbcMsg, IbcSrcCallback, IbcTimeout, Response,
44+
/// Timestamp,
45+
/// };
2446
/// # use cosmwasm_std::testing::mock_env;
2547
/// # let env = mock_env();
2648
///

0 commit comments

Comments
 (0)