Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement TransferMsgBuilder #2167

Merged
merged 8 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,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 @@ -64,6 +67,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::StdResult;
use crate::{to_json_binary, Binary};

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