Skip to content

Commit 5a3998d

Browse files
authored
Merge pull request #2428 from CosmWasm/jawoznia/test/ibc2-send-packet-contract
Send `Ibc2Msg::SendPacket` in ibc2 contract (#2418)
2 parents 69cb551 + 97329bf commit 5a3998d

File tree

7 files changed

+100
-23
lines changed

7 files changed

+100
-23
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"title": "ExecuteMsg",
4+
"oneOf": [
5+
{
6+
"type": "object",
7+
"required": [
8+
"increment"
9+
],
10+
"properties": {
11+
"increment": {
12+
"type": "object",
13+
"required": [
14+
"channel_id",
15+
"destination_port"
16+
],
17+
"properties": {
18+
"channel_id": {
19+
"type": "string"
20+
},
21+
"destination_port": {
22+
"type": "string"
23+
}
24+
},
25+
"additionalProperties": false
26+
}
27+
},
28+
"additionalProperties": false
29+
}
30+
]
31+
}

contracts/ibc2/src/bin/schema.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use cosmwasm_schema::write_api;
22
use cosmwasm_std::Empty;
3-
use ibc2::contract::QueryMsg;
3+
use ibc2::msg::QueryMsg;
44

55
fn main() {
66
write_api! {

contracts/ibc2/src/contract.rs

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
1-
use cosmwasm_schema::{cw_serde, QueryResponses};
21
use cosmwasm_std::{
3-
entry_point, from_json, to_json_vec, Binary, Deps, DepsMut, Empty, Env, Ibc2PacketReceiveMsg,
4-
IbcReceiveResponse, MessageInfo, QueryResponse, Response, StdError, StdResult,
2+
entry_point, from_json, to_json_vec, Binary, Deps, DepsMut, Empty, Env, Ibc2Msg,
3+
Ibc2PacketReceiveMsg, Ibc2Payload, IbcReceiveResponse, MessageInfo, QueryResponse, Response,
4+
StdAck, StdError, StdResult, Timestamp,
55
};
6-
use schemars::JsonSchema;
7-
use serde::{Deserialize, Serialize};
86

9-
#[derive(Default, Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
10-
pub struct State {
11-
ibc2_packet_receive_counter: u32,
12-
}
13-
14-
#[cw_serde]
15-
#[derive(QueryResponses)]
16-
pub enum QueryMsg {
17-
#[returns(State)]
18-
QueryState {},
19-
}
20-
21-
const STATE_KEY: &[u8] = b"state";
7+
use crate::msg::QueryMsg;
8+
use crate::state::{State, STATE_KEY};
229

2310
#[entry_point]
2411
pub fn instantiate(
@@ -27,8 +14,12 @@ pub fn instantiate(
2714
_info: MessageInfo,
2815
_msg: Empty,
2916
) -> StdResult<Response> {
30-
deps.storage
31-
.set(STATE_KEY, &to_json_vec(&State::default())?);
17+
deps.storage.set(
18+
STATE_KEY,
19+
&to_json_vec(&State {
20+
ibc2_packet_receive_counter: 0,
21+
})?,
22+
);
3223

3324
Ok(Response::new())
3425
}
@@ -50,7 +41,7 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<QueryResponse> {
5041
pub fn ibc2_packet_receive(
5142
deps: DepsMut,
5243
_env: Env,
53-
_msg: Ibc2PacketReceiveMsg,
44+
msg: Ibc2PacketReceiveMsg,
5445
) -> StdResult<IbcReceiveResponse> {
5546
let data = deps
5647
.storage
@@ -63,6 +54,24 @@ pub fn ibc2_packet_receive(
6354
ibc2_packet_receive_counter: state.ibc2_packet_receive_counter + 1,
6455
})?,
6556
);
57+
// Workaround for now.
58+
let ts = Timestamp::from_nanos(1_577_933_900);
59+
let new_payload = Ibc2Payload::new(
60+
msg.payload.destination_port,
61+
msg.payload.source_port,
62+
msg.payload.version,
63+
msg.payload.encoding,
64+
msg.payload.value,
65+
);
66+
let new_msg = Ibc2Msg::SendPacket {
67+
channel_id: msg.source_client,
68+
payloads: vec![new_payload],
69+
timeout: ts,
70+
// This causes "timeout exceeds the maximum expected value" error returned from the ibc-go.
71+
// timeout: _env.block.time.plus_seconds(5_u64),
72+
};
6673

67-
Ok(IbcReceiveResponse::new([1, 2, 3]))
74+
Ok(IbcReceiveResponse::new(StdAck::success(b"\x01"))
75+
.add_message(new_msg)
76+
.add_attribute("action", "handle_increment"))
6877
}

contracts/ibc2/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
pub mod contract;
2+
pub mod msg;
3+
pub mod state;

contracts/ibc2/src/msg.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use cosmwasm_schema::{cw_serde, QueryResponses};
2+
3+
#[cw_serde]
4+
#[derive(QueryResponses)]
5+
pub enum QueryMsg {
6+
#[returns(crate::state::State)]
7+
QueryState {},
8+
}

contracts/ibc2/src/state.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use schemars::JsonSchema;
2+
use serde::{Deserialize, Serialize};
3+
4+
#[derive(Default, Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
5+
pub struct State {
6+
pub ibc2_packet_receive_counter: u32,
7+
}
8+
9+
pub const STATE_KEY: &[u8] = b"state";

packages/std/src/ibc2.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@ pub struct Ibc2Payload {
2121
pub value: Binary,
2222
}
2323

24+
impl Ibc2Payload {
25+
pub fn new(
26+
source_port: String,
27+
destination_port: String,
28+
version: String,
29+
encoding: String,
30+
value: Binary,
31+
) -> Self {
32+
Self {
33+
source_port,
34+
destination_port,
35+
version,
36+
encoding,
37+
value,
38+
}
39+
}
40+
}
41+
2442
/// These are messages in the IBC lifecycle using the new Ibc2 approach.
2543
/// Only usable by Ibc2-enabled contracts
2644
#[non_exhaustive]

0 commit comments

Comments
 (0)