Skip to content

Commit

Permalink
Merge pull request #2167 from CosmWasm/chipshort/transfer-msg-builder
Browse files Browse the repository at this point in the history
Implement TransferMsgBuilder
  • Loading branch information
chipshort authored Jun 5, 2024
2 parents 3443f9a + d373f78 commit 7346ae1
Show file tree
Hide file tree
Showing 6 changed files with 344 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ and this project adheres to
`ibc_source_callback` and `ibc_destination_callback`, as well as the
`IbcCallbackRequest` type. ([#2025])
- cosmwasm-vm: Add support for the two new IBC Callbacks entrypoints. ([#2025])
- cosmwasm-std: Add `TransferMsgBuilder` to more easily create an
`IbcMsg::Transfer` with different kinds of memo values, including IBC
Callbacks memo values. ([#2167])

[#1983]: https://github.com/CosmWasm/cosmwasm/pull/1983
[#2025]: https://github.com/CosmWasm/cosmwasm/pull/2025
Expand All @@ -67,6 +70,7 @@ and this project adheres to
[#2124]: https://github.com/CosmWasm/cosmwasm/pull/2124
[#2129]: https://github.com/CosmWasm/cosmwasm/pull/2129
[#2166]: https://github.com/CosmWasm/cosmwasm/pull/2166
[#2167]: https://github.com/CosmWasm/cosmwasm/pull/2167

### Changed

Expand Down
27 changes: 15 additions & 12 deletions contracts/ibc-callbacks/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cosmwasm_std::{
entry_point, to_json_binary, to_json_string, Binary, Deps, DepsMut, Empty, Env,
IbcBasicResponse, IbcCallbackRequest, IbcDestinationCallbackMsg, IbcDstCallback, IbcMsg,
IbcSourceCallbackMsg, IbcSrcCallback, IbcTimeout, MessageInfo, Response, StdError, StdResult,
entry_point, to_json_binary, Binary, Deps, DepsMut, Empty, Env, IbcBasicResponse,
IbcDestinationCallbackMsg, IbcDstCallback, IbcSourceCallbackMsg, IbcSrcCallback, IbcTimeout,
MessageInfo, Response, StdError, StdResult, TransferMsgBuilder,
};

use crate::msg::{CallbackType, ExecuteMsg, QueryMsg};
Expand Down Expand Up @@ -71,16 +71,19 @@ fn execute_transfer(
}
};

let transfer_msg = IbcMsg::Transfer {
to_address,
timeout: IbcTimeout::with_timestamp(env.block.time.plus_seconds(timeout_seconds as u64)),
let builder = TransferMsgBuilder::new(
channel_id,
amount: coin.clone(),
memo: Some(to_json_string(&match callback_type {
CallbackType::Both => IbcCallbackRequest::both(src_callback, dst_callback),
CallbackType::Src => IbcCallbackRequest::source(src_callback),
CallbackType::Dst => IbcCallbackRequest::destination(dst_callback),
})?),
to_address.clone(),
coin.clone(),
IbcTimeout::with_timestamp(env.block.time.plus_seconds(timeout_seconds as u64)),
);
let transfer_msg = match callback_type {
CallbackType::Both => builder
.with_src_callback(src_callback)
.with_dst_callback(dst_callback)
.build(),
CallbackType::Src => builder.with_src_callback(src_callback).build(),
CallbackType::Dst => builder.with_dst_callback(dst_callback).build(),
};

Ok(Response::new()
Expand Down
3 changes: 3 additions & 0 deletions packages/std/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ use crate::{to_json_binary, Binary};
use crate::{Addr, Timestamp};

mod callbacks;
mod transfer_msg_builder;

pub use callbacks::*;
pub use transfer_msg_builder::*;

/// These are messages in the IBC lifecycle. Only usable by IBC-enabled contracts
/// (contracts that directly speak the IBC protocol via 6 entry points)
Expand Down
24 changes: 23 additions & 1 deletion packages/std/src/ibc/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,34 @@ use crate::{Addr, IbcAcknowledgement, IbcPacket, Uint64};
///
/// # Example
///
/// Using [`TransferMsgBuilder`](crate::TransferMsgBuilder):
/// ```rust
/// use cosmwasm_std::{
/// to_json_string, Coin, IbcCallbackRequest, IbcMsg, IbcSrcCallback, IbcTimeout, Response,
/// to_json_string, Coin, IbcCallbackRequest, TransferMsgBuilder, IbcSrcCallback, IbcTimeout, Response,
/// Timestamp,
/// };
/// # use cosmwasm_std::testing::mock_env;
/// # let env = mock_env();
///
/// let _msg = TransferMsgBuilder::new(
/// "channel-0".to_string(),
/// "cosmos1example".to_string(),
/// Coin::new(10u32, "ucoin"),
/// Timestamp::from_seconds(12345),
/// )
/// .with_src_callback(IbcSrcCallback {
/// address: env.contract.address,
/// gas_limit: None,
/// })
/// .build();
/// ```
///
/// Manual serialization:
/// ```rust
/// use cosmwasm_std::{
/// to_json_string, Coin, IbcCallbackRequest, IbcMsg, IbcSrcCallback, IbcTimeout, Response,
/// Timestamp,
/// };
/// # use cosmwasm_std::testing::mock_env;
/// # let env = mock_env();
///
Expand Down
Loading

0 comments on commit 7346ae1

Please sign in to comment.