Skip to content

Commit 84ba4d2

Browse files
committed
Send Ibc2Msg::SendPacket in ibc2 contract (#2418)
1 parent f3236ad commit 84ba4d2

File tree

7 files changed

+101
-22
lines changed

7 files changed

+101
-22
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: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
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-
}
7+
use crate::msg::QueryMsg;
8+
use crate::state::{State, STATE_KEY};
209

21-
const STATE_KEY: &[u8] = b"state";
10+
pub const PACKET_LIFETIME: u64 = 60 * 60;
2211

2312
#[entry_point]
2413
pub fn instantiate(
@@ -27,8 +16,12 @@ pub fn instantiate(
2716
_info: MessageInfo,
2817
_msg: Empty,
2918
) -> StdResult<Response> {
30-
deps.storage
31-
.set(STATE_KEY, &to_json_vec(&State::default())?);
19+
deps.storage.set(
20+
STATE_KEY,
21+
&to_json_vec(&State {
22+
ibc2_packet_receive_counter: 1000,
23+
})?,
24+
);
3225

3326
Ok(Response::new())
3427
}
@@ -50,7 +43,7 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<QueryResponse> {
5043
pub fn ibc2_packet_receive(
5144
deps: DepsMut,
5245
_env: Env,
53-
_msg: Ibc2PacketReceiveMsg,
46+
msg: Ibc2PacketReceiveMsg,
5447
) -> StdResult<IbcReceiveResponse> {
5548
let data = deps
5649
.storage
@@ -63,6 +56,24 @@ pub fn ibc2_packet_receive(
6356
ibc2_packet_receive_counter: state.ibc2_packet_receive_counter + 1,
6457
})?,
6558
);
59+
// Workaround for now.
60+
let ts = Timestamp::from_nanos(1_577_933_900);
61+
let new_payload = Ibc2Payload::new(
62+
msg.payload.destination_port,
63+
msg.payload.source_port,
64+
msg.payload.version,
65+
msg.payload.encoding,
66+
msg.payload.value,
67+
);
68+
let new_msg = Ibc2Msg::SendPacket {
69+
channel_id: msg.source_client,
70+
payloads: vec![new_payload],
71+
timeout: ts,
72+
// This causes "timeout exceeds the maximum expected value" error returned from the ibc-go.
73+
// timeout: _env.block.time.plus_seconds(5_u64),
74+
};
6675

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

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)