-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathibc_exports.rs
256 lines (229 loc) · 8.3 KB
/
ibc_exports.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#![cfg(all(feature = "stargate", target_arch = "wasm32"))]
use std::fmt;
use std::vec::Vec;
use schemars::JsonSchema;
use serde::Serialize;
use crate::deps::DepsMut;
use crate::exports::make_dependencies;
use crate::ibc::{IbcBasicResponse, IbcChannel};
use crate::memory::{consume_region, release_buffer, Region};
use crate::results::ContractResult;
use crate::serde::{from_slice, to_vec};
use crate::types::Env;
use crate::{IbcAcknowledgementWithPacket, IbcPacket, IbcReceiveResponse};
// TODO: replace with https://doc.rust-lang.org/std/ops/trait.Try.html once stabilized
macro_rules! r#try_into_contract_result {
($expr:expr) => {
match $expr {
Ok(val) => val,
Err(err) => {
return ContractResult::Err(err.to_string());
}
}
};
($expr:expr,) => {
$crate::try_into_contract_result!($expr)
};
}
/// do_ibc_channel_open is designed for use with #[entry_point] to make a "C" extern
///
/// contract_fn does the protocol version negotiation during channel handshake phase
pub fn do_ibc_channel_open<E>(
contract_fn: &dyn Fn(DepsMut, Env, IbcChannel) -> Result<(), E>,
env_ptr: u32,
msg_ptr: u32,
) -> u32
where
E: ToString,
{
let res = _do_ibc_channel_open(contract_fn, env_ptr as *mut Region, msg_ptr as *mut Region);
let v = to_vec(&res).unwrap();
release_buffer(v) as u32
}
fn _do_ibc_channel_open<E>(
contract_fn: &dyn Fn(DepsMut, Env, IbcChannel) -> Result<(), E>,
env_ptr: *mut Region,
msg_ptr: *mut Region,
) -> ContractResult<()>
where
E: ToString,
{
let env: Vec<u8> = unsafe { consume_region(env_ptr) };
let msg: Vec<u8> = unsafe { consume_region(msg_ptr) };
let env: Env = try_into_contract_result!(from_slice(&env));
let msg: IbcChannel = try_into_contract_result!(from_slice(&msg));
let mut deps = make_dependencies();
contract_fn(deps.as_mut(), env, msg).into()
}
/// do_ibc_channel_connect is designed for use with #[entry_point] to make a "C" extern
///
/// contract_fn is a callback when a IBC channel is established (after both sides agree in open)
pub fn do_ibc_channel_connect<C, E>(
contract_fn: &dyn Fn(DepsMut, Env, IbcChannel) -> Result<IbcBasicResponse<C>, E>,
env_ptr: u32,
msg_ptr: u32,
) -> u32
where
C: Serialize + Clone + fmt::Debug + PartialEq + JsonSchema,
E: ToString,
{
let res = _do_ibc_channel_connect(contract_fn, env_ptr as *mut Region, msg_ptr as *mut Region);
let v = to_vec(&res).unwrap();
release_buffer(v) as u32
}
fn _do_ibc_channel_connect<C, E>(
contract_fn: &dyn Fn(DepsMut, Env, IbcChannel) -> Result<IbcBasicResponse<C>, E>,
env_ptr: *mut Region,
msg_ptr: *mut Region,
) -> ContractResult<IbcBasicResponse<C>>
where
C: Serialize + Clone + fmt::Debug + PartialEq + JsonSchema,
E: ToString,
{
let env: Vec<u8> = unsafe { consume_region(env_ptr) };
let msg: Vec<u8> = unsafe { consume_region(msg_ptr) };
let env: Env = try_into_contract_result!(from_slice(&env));
let msg: IbcChannel = try_into_contract_result!(from_slice(&msg));
let mut deps = make_dependencies();
contract_fn(deps.as_mut(), env, msg).into()
}
/// do_ibc_channel_close is designed for use with #[entry_point] to make a "C" extern
///
/// contract_fn is a callback when a IBC channel belonging to this contract is closed
pub fn do_ibc_channel_close<C, E>(
contract_fn: &dyn Fn(DepsMut, Env, IbcChannel) -> Result<IbcBasicResponse<C>, E>,
env_ptr: u32,
msg_ptr: u32,
) -> u32
where
C: Serialize + Clone + fmt::Debug + PartialEq + JsonSchema,
E: ToString,
{
let res = _do_ibc_channel_close(contract_fn, env_ptr as *mut Region, msg_ptr as *mut Region);
let v = to_vec(&res).unwrap();
release_buffer(v) as u32
}
fn _do_ibc_channel_close<C, E>(
contract_fn: &dyn Fn(DepsMut, Env, IbcChannel) -> Result<IbcBasicResponse<C>, E>,
env_ptr: *mut Region,
msg_ptr: *mut Region,
) -> ContractResult<IbcBasicResponse<C>>
where
C: Serialize + Clone + fmt::Debug + PartialEq + JsonSchema,
E: ToString,
{
let env: Vec<u8> = unsafe { consume_region(env_ptr) };
let msg: Vec<u8> = unsafe { consume_region(msg_ptr) };
let env: Env = try_into_contract_result!(from_slice(&env));
let msg: IbcChannel = try_into_contract_result!(from_slice(&msg));
let mut deps = make_dependencies();
contract_fn(deps.as_mut(), env, msg).into()
}
/// do_ibc_packet_receive is designed for use with #[entry_point] to make a "C" extern
///
/// contract_fn is called when this chain receives an IBC Packet on a channel belonging
/// to this contract
pub fn do_ibc_packet_receive<C, E>(
contract_fn: &dyn Fn(DepsMut, Env, IbcPacket) -> Result<IbcReceiveResponse<C>, E>,
env_ptr: u32,
msg_ptr: u32,
) -> u32
where
C: Serialize + Clone + fmt::Debug + PartialEq + JsonSchema,
E: ToString,
{
let res = _do_ibc_packet_receive(contract_fn, env_ptr as *mut Region, msg_ptr as *mut Region);
let v = to_vec(&res).unwrap();
release_buffer(v) as u32
}
fn _do_ibc_packet_receive<C, E>(
contract_fn: &dyn Fn(DepsMut, Env, IbcPacket) -> Result<IbcReceiveResponse<C>, E>,
env_ptr: *mut Region,
msg_ptr: *mut Region,
) -> ContractResult<IbcReceiveResponse<C>>
where
C: Serialize + Clone + fmt::Debug + PartialEq + JsonSchema,
E: ToString,
{
let env: Vec<u8> = unsafe { consume_region(env_ptr) };
let msg: Vec<u8> = unsafe { consume_region(msg_ptr) };
let env: Env = try_into_contract_result!(from_slice(&env));
let msg: IbcPacket = try_into_contract_result!(from_slice(&msg));
let mut deps = make_dependencies();
contract_fn(deps.as_mut(), env, msg).into()
}
/// do_ibc_packet_ack is designed for use with #[entry_point] to make a "C" extern
///
/// contract_fn is called when this chain receives an IBC Acknowledgement for a packet
/// that this contract previously sent
pub fn do_ibc_packet_ack<C, E>(
contract_fn: &dyn Fn(
DepsMut,
Env,
IbcAcknowledgementWithPacket,
) -> Result<IbcBasicResponse<C>, E>,
env_ptr: u32,
msg_ptr: u32,
) -> u32
where
C: Serialize + Clone + fmt::Debug + PartialEq + JsonSchema,
E: ToString,
{
let res = _do_ibc_packet_ack(contract_fn, env_ptr as *mut Region, msg_ptr as *mut Region);
let v = to_vec(&res).unwrap();
release_buffer(v) as u32
}
fn _do_ibc_packet_ack<C, E>(
contract_fn: &dyn Fn(
DepsMut,
Env,
IbcAcknowledgementWithPacket,
) -> Result<IbcBasicResponse<C>, E>,
env_ptr: *mut Region,
msg_ptr: *mut Region,
) -> ContractResult<IbcBasicResponse<C>>
where
C: Serialize + Clone + fmt::Debug + PartialEq + JsonSchema,
E: ToString,
{
let env: Vec<u8> = unsafe { consume_region(env_ptr) };
let msg: Vec<u8> = unsafe { consume_region(msg_ptr) };
let env: Env = try_into_contract_result!(from_slice(&env));
let msg: IbcAcknowledgementWithPacket = try_into_contract_result!(from_slice(&msg));
let mut deps = make_dependencies();
contract_fn(deps.as_mut(), env, msg).into()
}
/// do_ibc_packet_timeout is designed for use with #[entry_point] to make a "C" extern
///
/// contract_fn is called when a packet that this contract previously sent has provably
/// timedout and will never be relayed to the calling chain. This generally behaves
/// like ick_ack_fn upon an acknowledgement containing an error.
pub fn do_ibc_packet_timeout<C, E>(
contract_fn: &dyn Fn(DepsMut, Env, IbcPacket) -> Result<IbcBasicResponse<C>, E>,
env_ptr: u32,
msg_ptr: u32,
) -> u32
where
C: Serialize + Clone + fmt::Debug + PartialEq + JsonSchema,
E: ToString,
{
let res = _do_ibc_packet_timeout(contract_fn, env_ptr as *mut Region, msg_ptr as *mut Region);
let v = to_vec(&res).unwrap();
release_buffer(v) as u32
}
fn _do_ibc_packet_timeout<C, E>(
contract_fn: &dyn Fn(DepsMut, Env, IbcPacket) -> Result<IbcBasicResponse<C>, E>,
env_ptr: *mut Region,
msg_ptr: *mut Region,
) -> ContractResult<IbcBasicResponse<C>>
where
C: Serialize + Clone + fmt::Debug + PartialEq + JsonSchema,
E: ToString,
{
let env: Vec<u8> = unsafe { consume_region(env_ptr) };
let msg: Vec<u8> = unsafe { consume_region(msg_ptr) };
let env: Env = try_into_contract_result!(from_slice(&env));
let msg: IbcPacket = try_into_contract_result!(from_slice(&msg));
let mut deps = make_dependencies();
contract_fn(deps.as_mut(), env, msg).into()
}