Skip to content

Commit 30d2b3b

Browse files
committed
Update ibc-reflect contract
1 parent 241b08a commit 30d2b3b

File tree

5 files changed

+33
-61
lines changed

5 files changed

+33
-61
lines changed

contracts/ibc-reflect/schema/acknowledgement_msg_balances.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "http://json-schema.org/draft-07/schema#",
33
"title": "AcknowledgementMsgBalances",
4-
"description": "This is the final result type that is created and serialized in a contract for every init/execute/migrate call. The VM then deserializes this type to distinguish between successful and failed executions.\n\nWe use a custom type here instead of Rust's Result because we want to be able to define the serialization, which is a public interface. Every language that compiles to Wasm and runs in the ComsWasm VM needs to create the same JSON representation.\n\n# Examples\n\nSuccess:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let response: Response = Response::default(); let result: ContractResult<Response> = ContractResult::Ok(response); assert_eq!(to_vec(&result).unwrap(), br#\"{\"ok\":{\"submessages\":[],\"messages\":[],\"attributes\":[],\"data\":null}}\"#.to_vec()); ```\n\nFailure:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let error_msg = String::from(\"Something went wrong\"); let result: ContractResult<Response> = ContractResult::Err(error_msg); assert_eq!(to_vec(&result).unwrap(), br#\"{\"error\":\"Something went wrong\"}\"#.to_vec()); ```",
4+
"description": "This is the final result type that is created and serialized in a contract for every init/execute/migrate call. The VM then deserializes this type to distinguish between successful and failed executions.\n\nWe use a custom type here instead of Rust's Result because we want to be able to define the serialization, which is a public interface. Every language that compiles to Wasm and runs in the ComsWasm VM needs to create the same JSON representation.\n\n# Examples\n\nSuccess:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let response: Response = Response::default(); let result: ContractResult<Response> = ContractResult::Ok(response); assert_eq!(to_vec(&result).unwrap(), br#\"{\"ok\":{\"messages\":[],\"attributes\":[],\"data\":null}}\"#.to_vec()); ```\n\nFailure:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let error_msg = String::from(\"Something went wrong\"); let result: ContractResult<Response> = ContractResult::Err(error_msg); assert_eq!(to_vec(&result).unwrap(), br#\"{\"error\":\"Something went wrong\"}\"#.to_vec()); ```",
55
"anyOf": [
66
{
77
"type": "object",

contracts/ibc-reflect/schema/acknowledgement_msg_dispatch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "http://json-schema.org/draft-07/schema#",
33
"title": "AcknowledgementMsgDispatch",
4-
"description": "This is the final result type that is created and serialized in a contract for every init/execute/migrate call. The VM then deserializes this type to distinguish between successful and failed executions.\n\nWe use a custom type here instead of Rust's Result because we want to be able to define the serialization, which is a public interface. Every language that compiles to Wasm and runs in the ComsWasm VM needs to create the same JSON representation.\n\n# Examples\n\nSuccess:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let response: Response = Response::default(); let result: ContractResult<Response> = ContractResult::Ok(response); assert_eq!(to_vec(&result).unwrap(), br#\"{\"ok\":{\"submessages\":[],\"messages\":[],\"attributes\":[],\"data\":null}}\"#.to_vec()); ```\n\nFailure:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let error_msg = String::from(\"Something went wrong\"); let result: ContractResult<Response> = ContractResult::Err(error_msg); assert_eq!(to_vec(&result).unwrap(), br#\"{\"error\":\"Something went wrong\"}\"#.to_vec()); ```",
4+
"description": "This is the final result type that is created and serialized in a contract for every init/execute/migrate call. The VM then deserializes this type to distinguish between successful and failed executions.\n\nWe use a custom type here instead of Rust's Result because we want to be able to define the serialization, which is a public interface. Every language that compiles to Wasm and runs in the ComsWasm VM needs to create the same JSON representation.\n\n# Examples\n\nSuccess:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let response: Response = Response::default(); let result: ContractResult<Response> = ContractResult::Ok(response); assert_eq!(to_vec(&result).unwrap(), br#\"{\"ok\":{\"messages\":[],\"attributes\":[],\"data\":null}}\"#.to_vec()); ```\n\nFailure:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let error_msg = String::from(\"Something went wrong\"); let result: ContractResult<Response> = ContractResult::Err(error_msg); assert_eq!(to_vec(&result).unwrap(), br#\"{\"error\":\"Something went wrong\"}\"#.to_vec()); ```",
55
"anyOf": [
66
{
77
"type": "object",

contracts/ibc-reflect/schema/acknowledgement_msg_who_am_i.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "http://json-schema.org/draft-07/schema#",
33
"title": "AcknowledgementMsgWhoAmI",
4-
"description": "This is the final result type that is created and serialized in a contract for every init/execute/migrate call. The VM then deserializes this type to distinguish between successful and failed executions.\n\nWe use a custom type here instead of Rust's Result because we want to be able to define the serialization, which is a public interface. Every language that compiles to Wasm and runs in the ComsWasm VM needs to create the same JSON representation.\n\n# Examples\n\nSuccess:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let response: Response = Response::default(); let result: ContractResult<Response> = ContractResult::Ok(response); assert_eq!(to_vec(&result).unwrap(), br#\"{\"ok\":{\"submessages\":[],\"messages\":[],\"attributes\":[],\"data\":null}}\"#.to_vec()); ```\n\nFailure:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let error_msg = String::from(\"Something went wrong\"); let result: ContractResult<Response> = ContractResult::Err(error_msg); assert_eq!(to_vec(&result).unwrap(), br#\"{\"error\":\"Something went wrong\"}\"#.to_vec()); ```",
4+
"description": "This is the final result type that is created and serialized in a contract for every init/execute/migrate call. The VM then deserializes this type to distinguish between successful and failed executions.\n\nWe use a custom type here instead of Rust's Result because we want to be able to define the serialization, which is a public interface. Every language that compiles to Wasm and runs in the ComsWasm VM needs to create the same JSON representation.\n\n# Examples\n\nSuccess:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let response: Response = Response::default(); let result: ContractResult<Response> = ContractResult::Ok(response); assert_eq!(to_vec(&result).unwrap(), br#\"{\"ok\":{\"messages\":[],\"attributes\":[],\"data\":null}}\"#.to_vec()); ```\n\nFailure:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let error_msg = String::from(\"Something went wrong\"); let result: ContractResult<Response> = ContractResult::Err(error_msg); assert_eq!(to_vec(&result).unwrap(), br#\"{\"error\":\"Something went wrong\"}\"#.to_vec()); ```",
55
"anyOf": [
66
{
77
"type": "object",

contracts/ibc-reflect/src/contract.rs

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use cosmwasm_std::{
2-
attr, entry_point, from_slice, to_binary, wasm_execute, BankMsg, Binary, ContractResult,
3-
CosmosMsg, Deps, DepsMut, Empty, Env, Event, IbcAcknowledgement, IbcBasicResponse, IbcChannel,
4-
IbcOrder, IbcPacket, IbcReceiveResponse, MessageInfo, Order, QueryResponse, Reply, ReplyOn,
5-
Response, StdError, StdResult, SubMsg, SubcallResponse, WasmMsg,
2+
attr, entry_point, from_slice, subcall, to_binary, wasm_execute, BankMsg, Binary,
3+
ContractResult, CosmosMsg, Deps, DepsMut, Empty, Env, Event, IbcAcknowledgement,
4+
IbcBasicResponse, IbcChannel, IbcOrder, IbcPacket, IbcReceiveResponse, MessageInfo, Order,
5+
QueryResponse, Reply, ReplyOn, Response, StdError, StdResult, SubMsg, SubcallResponse, WasmMsg,
66
};
77

88
use crate::msg::{
@@ -29,10 +29,8 @@ pub fn instantiate(
2929
config(deps.storage).save(&cfg)?;
3030

3131
Ok(Response {
32-
submessages: vec![],
33-
messages: vec![],
3432
attributes: vec![attr("action", "instantiate")],
35-
data: None,
33+
..Response::default()
3634
})
3735
}
3836

@@ -86,10 +84,8 @@ pub fn handle_init_callback(deps: DepsMut, response: SubcallResponse) -> StdResu
8684
})?;
8785

8886
Ok(Response {
89-
submessages: vec![],
90-
messages: vec![],
9187
attributes: vec![attr("action", "execute_init_callback")],
92-
data: None,
88+
..Response::default()
9389
})
9490
}
9591

@@ -167,19 +163,13 @@ pub fn ibc_channel_connect(
167163
funds: vec![],
168164
label: format!("ibc-reflect-{}", &chan_id),
169165
};
170-
let sub_msg = SubMsg {
171-
id: INIT_CALLBACK_ID,
172-
msg: msg.into(),
173-
gas_limit: None,
174-
reply_on: ReplyOn::Success,
175-
};
166+
let msg = subcall(msg, INIT_CALLBACK_ID, ReplyOn::Success);
176167

177168
// store the channel id for the reply handler
178169
pending_channel(deps.storage).save(&chan_id)?;
179170

180171
Ok(IbcBasicResponse {
181-
messages: vec![],
182-
submessages: vec![sub_msg],
172+
messages: vec![msg],
183173
attributes: vec![attr("action", "ibc_connect"), attr("channel_id", chan_id)],
184174
})
185175
}
@@ -199,7 +189,7 @@ pub fn ibc_channel_close(
199189

200190
// transfer current balance if any (steal the money)
201191
let amount = deps.querier.query_all_balances(&reflect_addr)?;
202-
let messages: Vec<CosmosMsg<Empty>> = if !amount.is_empty() {
192+
let messages: Vec<SubMsg<Empty>> = if !amount.is_empty() {
203193
let bank_msg = BankMsg::Send {
204194
to_address: env.contract.address.into(),
205195
amount,
@@ -215,7 +205,6 @@ pub fn ibc_channel_close(
215205
let steal_funds = !messages.is_empty();
216206

217207
Ok(IbcBasicResponse {
218-
submessages: vec![],
219208
messages,
220209
attributes: vec![
221210
attr("action", "ibc_close"),
@@ -263,7 +252,6 @@ pub fn ibc_packet_receive(
263252
let acknowledgement = encode_ibc_error(format!("invalid packet: {}", e));
264253
Ok(IbcReceiveResponse {
265254
acknowledgement,
266-
submessages: vec![],
267255
messages: vec![],
268256
attributes: vec![],
269257
})
@@ -280,7 +268,6 @@ fn receive_who_am_i(deps: DepsMut, caller: String) -> StdResult<IbcReceiveRespon
280268
// and we are golden
281269
Ok(IbcReceiveResponse {
282270
acknowledgement,
283-
submessages: vec![],
284271
messages: vec![],
285272
attributes: vec![attr("action", "receive_who_am_i")],
286273
})
@@ -298,7 +285,6 @@ fn receive_balances(deps: DepsMut, caller: String) -> StdResult<IbcReceiveRespon
298285
// and we are golden
299286
Ok(IbcReceiveResponse {
300287
acknowledgement,
301-
submessages: vec![],
302288
messages: vec![],
303289
attributes: vec![attr("action", "receive_balances")],
304290
})
@@ -320,17 +306,11 @@ fn receive_dispatch(
320306
let wasm_msg = wasm_execute(reflect_addr, &reflect_msg, vec![])?;
321307

322308
// we wrap it in a submessage to properly report errors
323-
let sub_msg = SubMsg {
324-
id: RECEIVE_DISPATCH_ID,
325-
msg: wasm_msg.into(),
326-
gas_limit: None,
327-
reply_on: ReplyOn::Error,
328-
};
309+
let msg = subcall(wasm_msg, RECEIVE_DISPATCH_ID, ReplyOn::Error);
329310

330311
Ok(IbcReceiveResponse {
331312
acknowledgement,
332-
submessages: vec![sub_msg],
333-
messages: vec![],
313+
messages: vec![msg],
334314
attributes: vec![attr("action", "receive_dispatch")],
335315
})
336316
}
@@ -343,7 +323,6 @@ pub fn ibc_packet_ack(
343323
_ack: IbcAcknowledgement,
344324
) -> StdResult<IbcBasicResponse> {
345325
Ok(IbcBasicResponse {
346-
submessages: vec![],
347326
messages: vec![],
348327
attributes: vec![attr("action", "ibc_packet_ack")],
349328
})
@@ -357,7 +336,6 @@ pub fn ibc_packet_timeout(
357336
_packet: IbcPacket,
358337
) -> StdResult<IbcBasicResponse> {
359338
Ok(IbcBasicResponse {
360-
submessages: vec![],
361339
messages: vec![],
362340
attributes: vec![attr("action", "ibc_packet_timeout")],
363341
})
@@ -417,8 +395,8 @@ mod tests {
417395
// then we connect (with counter-party version set)
418396
let handshake_connect = mock_ibc_channel(channel_id, IbcOrder::Ordered, IBC_VERSION);
419397
let res = ibc_channel_connect(deps.branch(), mock_env(), handshake_connect).unwrap();
420-
assert_eq!(1, res.submessages.len());
421-
let id = res.submessages[0].id;
398+
assert_eq!(1, res.messages.len());
399+
let id = res.messages[0].id;
422400

423401
// fake a reply and ensure this works
424402
let response = Reply {
@@ -471,15 +449,15 @@ mod tests {
471449
let handshake_connect = mock_ibc_channel(channel_id, IbcOrder::Ordered, IBC_VERSION);
472450
let res = ibc_channel_connect(deps.as_mut(), mock_env(), handshake_connect).unwrap();
473451
// and set up a reflect account
474-
assert_eq!(1, res.submessages.len());
475-
let id = res.submessages[0].id;
452+
assert_eq!(1, res.messages.len());
453+
let id = res.messages[0].id;
476454
if let CosmosMsg::Wasm(WasmMsg::Instantiate {
477455
admin,
478456
code_id,
479457
msg: _,
480458
funds,
481459
label,
482-
}) = &res.submessages[0].msg
460+
}) = &res.messages[0].msg
483461
{
484462
assert_eq!(*admin, None);
485463
assert_eq!(*code_id, REFLECT_ID);
@@ -568,16 +546,15 @@ mod tests {
568546
ack.unwrap();
569547

570548
// and we dispatch the BankMsg via submessage
571-
assert_eq!(0, res.messages.len());
572-
assert_eq!(1, res.submessages.len());
573-
assert_eq!(RECEIVE_DISPATCH_ID, res.submessages[0].id);
549+
assert_eq!(1, res.messages.len());
550+
assert_eq!(RECEIVE_DISPATCH_ID, res.messages[0].id);
574551

575552
// parse the output, ensuring it matches
576553
if let CosmosMsg::Wasm(WasmMsg::Execute {
577554
contract_addr,
578555
msg,
579556
funds,
580-
}) = &res.submessages[0].msg
557+
}) = &res.messages[0].msg
581558
{
582559
assert_eq!(account, contract_addr.as_str());
583560
assert_eq!(0, funds.len());
@@ -600,7 +577,7 @@ mod tests {
600577
let packet = mock_ibc_packet_recv(channel_id, &bad_data).unwrap();
601578
let res = ibc_packet_receive(deps.as_mut(), mock_env(), packet).unwrap();
602579
// we didn't dispatch anything
603-
assert_eq!(0, res.submessages.len());
580+
assert_eq!(0, res.messages.len());
604581
// acknowledgement is an error
605582
let ack: AcknowledgementMsg<DispatchResponse> = from_slice(&res.acknowledgement).unwrap();
606583
assert_eq!(ack.unwrap_err(), "invalid packet: Error parsing into type ibc_reflect::msg::PacketMsg: unknown variant `reflect_code_id`, expected one of `dispatch`, `who_am_i`, `balances`");
@@ -634,7 +611,7 @@ mod tests {
634611
assert_eq!(1, res.messages.len());
635612
if let CosmosMsg::Wasm(WasmMsg::Execute {
636613
contract_addr, msg, ..
637-
}) = &res.messages[0]
614+
}) = &res.messages[0].msg
638615
{
639616
assert_eq!(contract_addr.as_str(), account);
640617
let reflect: ReflectExecuteMsg = from_slice(msg).unwrap();

contracts/ibc-reflect/tests/integration.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ fn connect<T: Into<String>>(
8585
// then we connect (with counter-party version set)
8686
let handshake_connect = mock_ibc_channel(channel_id, IbcOrder::Ordered, IBC_VERSION);
8787
let res: IbcBasicResponse = ibc_channel_connect(deps, mock_env(), handshake_connect).unwrap();
88-
assert_eq!(1, res.submessages.len());
89-
let id = res.submessages[0].id;
88+
assert_eq!(1, res.messages.len());
89+
let id = res.messages[0].id;
9090

9191
// fake a reply and ensure this works
9292
let response = Reply {
@@ -141,15 +141,15 @@ fn proper_handshake_flow() {
141141
let res: IbcBasicResponse =
142142
ibc_channel_connect(&mut deps, mock_env(), handshake_connect).unwrap();
143143
// and set up a reflect account
144-
assert_eq!(1, res.submessages.len());
145-
let id = res.submessages[0].id;
144+
assert_eq!(1, res.messages.len());
145+
let id = res.messages[0].id;
146146
if let CosmosMsg::Wasm(WasmMsg::Instantiate {
147147
admin,
148148
code_id,
149149
msg: _,
150150
funds,
151151
label,
152-
}) = &res.submessages[0].msg
152+
}) = &res.messages[0].msg
153153
{
154154
assert_eq!(*admin, None);
155155
assert_eq!(*code_id, REFLECT_ID);
@@ -234,27 +234,22 @@ fn handle_dispatch_packet() {
234234
// receive a packet for an unregistered channel returns app-level error (not Result::Err)
235235
let packet = mock_ibc_packet_recv(channel_id, &ibc_msg).unwrap();
236236
let res: IbcReceiveResponse = ibc_packet_receive(&mut deps, mock_env(), packet).unwrap();
237-
println!(
238-
"{}",
239-
String::from_utf8(res.acknowledgement.0.clone()).unwrap()
240-
);
241237

242238
// assert app-level success
243239
let ack: AcknowledgementMsg<DispatchResponse> =
244240
from_slice(&res.acknowledgement, DESERIALIZATION_LIMIT).unwrap();
245241
ack.unwrap();
246242

247243
// and we dispatch the BankMsg
248-
assert_eq!(0, res.messages.len());
249-
assert_eq!(1, res.submessages.len());
250-
assert_eq!(RECEIVE_DISPATCH_ID, res.submessages[0].id);
244+
assert_eq!(1, res.messages.len());
245+
assert_eq!(RECEIVE_DISPATCH_ID, res.messages[0].id);
251246

252247
// parse the output, ensuring it matches
253248
if let CosmosMsg::Wasm(WasmMsg::Execute {
254249
contract_addr,
255250
msg,
256251
funds,
257-
}) = &res.submessages[0].msg
252+
}) = &res.messages[0].msg
258253
{
259254
assert_eq!(account, contract_addr.as_str());
260255
assert_eq!(0, funds.len());
@@ -277,7 +272,7 @@ fn handle_dispatch_packet() {
277272
let packet = mock_ibc_packet_recv(channel_id, &bad_data).unwrap();
278273
let res: IbcReceiveResponse = ibc_packet_receive(&mut deps, mock_env(), packet).unwrap();
279274
// we didn't dispatch anything
280-
assert_eq!(0, res.submessages.len());
275+
assert_eq!(0, res.messages.len());
281276
// acknowledgement is an error
282277
let ack: AcknowledgementMsg<DispatchResponse> =
283278
from_slice(&res.acknowledgement, DESERIALIZATION_LIMIT).unwrap();

0 commit comments

Comments
 (0)