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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Copyright (C) Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
//! Gear API RPC methods
use crate::{
Api, GasInfo, IntoAccountId32,
result::{Error, Result},
utils,
};
use gear_core::{
ids::{CodeId, MessageId},
rpc::{CalculateReplyForHandleResult, ReplyInfo},
};
use gear_core_errors::ReplyCode;
use gsdk_codegen::at_block;
use parity_scale_codec::Decode;
use serde::Deserialize;
use subxt::{ext::subxt_rpcs::rpc_params, utils::H256};
#[derive(Deserialize)]
struct LegacyReplyInfo {
#[serde(deserialize_with = "deserialize_hex_bytes")]
payload: Vec<u8>,
value: u128,
code: ReplyCode,
}
impl From<LegacyReplyInfo> for ReplyInfo {
fn from(reply: LegacyReplyInfo) -> Self {
Self {
payload: reply.payload,
value: reply.value,
code: reply.code,
}
}
}
fn deserialize_hex_bytes<'de, D>(deserializer: D) -> std::result::Result<Vec<u8>, D::Error>
where
D: serde::Deserializer<'de>,
{
use serde::de::Error;
let value = String::deserialize(deserializer)?;
let value = value.strip_prefix("0x").unwrap_or(&value);
hex::decode(value).map_err(D::Error::custom)
}
impl Api {
/// Calculates the gas required to create a program from a
/// code and process an initialization message at specified block.
///
/// Actually call `gear_calculateInitCreateGas` RPC method. The
/// function's parameters are:
///
/// - `origin` (optional) is the caller's public address;
/// - `code_id` is the uploaded code identifier that can be obtained by
/// calling the [`SignedApi::upload_code`] function;
/// - `payload` vector contains data to be processed by the program;
/// - `value` to be transferred to the program's account;
/// - `allow_other_panics` flag indicates ignoring a trap during the
/// program's execution;
///
/// [`SignedApi::upload_code`]: crate::SignedApi::upload_code
#[at_block]
pub async fn calculate_create_gas_at(
&self,
origin: impl IntoAccountId32,
code_id: CodeId,
payload: impl AsRef<[u8]>,
value: u128,
allow_other_panics: bool,
block_hash: Option<H256>,
) -> Result<GasInfo> {
self.rpc()
.request(
"gear_calculateInitCreateGas",
rpc_params![
H256(origin.into_account_id().0),
H256(code_id.into()),
hex::encode(payload),
value,
allow_other_panics,
block_hash
],
)
.await
.map_err(Into::into)
}
/// Calculates the gas required to upload a program and
/// process an initialization message at specified block.
///
/// Actually calls `gear_calculateInitUploadGas` RPC method. The
/// function's parameters are:
///
/// - `origin` (optional) is the caller's public address;
/// - `code` is the buffer containing the Wasm binary code of the Gear
/// program;
/// - `payload` vector contains data to be processed by the program;
/// - `value` to be transferred to the program's account;
/// - `allow_other_panics` flag indicates ignoring a trap during the
/// program's execution;
#[at_block]
pub async fn calculate_upload_gas_at(
&self,
origin: impl IntoAccountId32,
code: impl AsRef<[u8]>,
payload: impl AsRef<[u8]>,
value: u128,
allow_other_panics: bool,
block_hash: Option<H256>,
) -> Result<GasInfo> {
self.rpc()
.request(
"gear_calculateInitUploadGas",
rpc_params![
H256(origin.into_account_id().0),
hex::encode(code),
hex::encode(payload),
value,
allow_other_panics,
block_hash
],
)
.await
.map_err(Into::into)
}
/// Calculates the gas required to handle a message at specified block.
///
/// Actually sends the `gear_calculateHandleGas` RPC to the node. The
/// function's parameters are:
///
/// - `origin` (optional) is the caller's public address;
/// - `destination` is the program address;
/// - `payload` vector contains data to be processed by the program;
/// - `value` to be transferred to the program's account;
/// - `allow_other_panics` flag indicates ignoring a trap during the
/// program's execution;
#[at_block]
pub async fn calculate_handle_gas_at(
&self,
origin: impl IntoAccountId32,
destination: impl IntoAccountId32,
payload: impl AsRef<[u8]>,
value: u128,
allow_other_panics: bool,
block_hash: Option<H256>,
) -> Result<GasInfo> {
self.rpc()
.request(
"gear_calculateHandleGas",
rpc_params![
H256(origin.into_account_id().0),
H256(destination.into_account_id().0),
hex::encode(payload),
value,
allow_other_panics,
block_hash
],
)
.await
.map_err(Into::into)
}
/// Calculates the gas required to reply to the received
/// message from the mailbox at specified block.
///
/// Actually calls `gear_calculateReplyGas` RPC method. The
/// function's parameters are:
///
/// - `origin` (optional) is the caller's public address;
/// - `message_id` is a message identifier required to find it in the
/// mailbox;
/// - `exit_code` is the status code of the reply;
/// - `payload` vector contains data to be processed by the program;
/// - `value` to be transferred to the program's account;
/// - `allow_other_panics` flag indicates ignoring a trap during the
/// program's execution;
#[at_block]
pub async fn calculate_reply_gas_at(
&self,
origin: impl IntoAccountId32,
message_id: MessageId,
payload: impl AsRef<[u8]>,
value: u128,
allow_other_panics: bool,
block_hash: Option<H256>,
) -> Result<GasInfo> {
self.rpc()
.request(
"gear_calculateReplyGas",
rpc_params![
H256(origin.into_account_id().0),
H256(message_id.into()),
hex::encode(payload),
value,
allow_other_panics,
block_hash
],
)
.await
.map_err(Into::into)
}
/// Reads the program's metahash at specified block.
///
/// Actually calls `gear_readMetahash` RPC method.
#[at_block]
pub async fn read_metahash_at(
&self,
program_id: impl IntoAccountId32,
block_hash: Option<H256>,
) -> Result<H256> {
self.rpc()
.request(
"gear_readMetahash",
rpc_params![H256(program_id.into_account_id().0), block_hash],
)
.await
.map_err(Into::into)
}
/// Reads the program's state as a byte vector at specified block.
///
/// Actually sends the `gear_readState` RPC to the node.
#[at_block]
pub async fn read_state_bytes_at(
&self,
program_id: impl IntoAccountId32,
payload: impl AsRef<[u8]>,
block_hash: Option<H256>,
) -> Result<Vec<u8>> {
let response: String = self
.rpc()
.request(
"gear_readState",
rpc_params![
H256(program_id.into_account_id().0),
hex::encode(payload),
block_hash
],
)
.await?;
utils::hex_to_vec(response)
}
/// Reads the programs's state as a decoded value at specified block.
///
/// See [`Self::read_state_bytes_at`] for details.
#[at_block]
pub async fn read_state_at<T: Decode>(
&self,
program_id: impl IntoAccountId32,
payload: impl AsRef<[u8]>,
block_hash: Option<H256>,
) -> Result<T> {
let bytes = self
.read_state_bytes_at(program_id, payload, block_hash)
.await?;
Ok(T::decode(&mut bytes.as_slice())?)
}
/// Reads a named custom section from the original WASM code stored
/// on-chain. When `block_hash` is `None`, the best block is used.
///
/// Actually calls `gear_readWasmCustomSection` RPC method. A successful
/// `None` response means either that the node has no code for `code_id` or
/// that the stored WASM does not contain `section_name`.
/// Primary use case: retrieving a Sails IDL embedded in the `sails:idl`
/// custom section at specified block.
#[at_block]
pub async fn read_wasm_custom_section_at(
&self,
code_id: CodeId,
section_name: impl AsRef<str>,
block_hash: Option<H256>,
) -> Result<Option<sp_core::Bytes>> {
self.rpc()
.request(
"gear_readWasmCustomSection",
rpc_params![H256(code_id.into()), section_name.as_ref(), block_hash],
)
.await
.map_err(Into::into)
}
/// Calls `runtime_wasmBlobVersion` RPC method at specified block.
#[at_block]
pub async fn runtime_wasm_blob_version_at(&self, block_hash: Option<H256>) -> Result<String> {
self.rpc()
.request("runtime_wasmBlobVersion", rpc_params![block_hash])
.await
.map_err(Into::into)
}
/// Calculates a reply to a given message at specified block.
///
/// Actually calls `gear_calculateReplyForHandle` RPC method. The
/// function's parameters are:
///
/// - `origin` (optional) is the caller's public address;
/// - `destination` is the program address;
/// - `payload` vector contains data to be processed by the program;
/// - `gas_limit`: maximum amount of gas the program can spend before it is
/// halted.
/// - `value` to be transferred to the program's account;
#[at_block]
pub async fn calculate_reply_for_handle_at(
&self,
origin: impl IntoAccountId32,
destination: impl IntoAccountId32,
payload: impl AsRef<[u8]>,
gas_limit: u64,
value: u128,
block_hash: Option<H256>,
) -> Result<ReplyInfo> {
let reply: LegacyReplyInfo = self
.rpc()
.request(
"gear_calculateReplyForHandle",
rpc_params![
H256(origin.into_account_id().0),
H256(destination.into_account_id().0),
hex::encode(payload),
gas_limit,
value,
block_hash
],
)
.await
.map_err(Error::from)?;
Ok(reply.into())
}
/// Calculates a reply and user messages for a given message at specified block.
///
/// Actually calls `gear_calculateReplyForHandleResult` RPC method.
#[at_block]
pub async fn calculate_reply_for_handle_result_at(
&self,
origin: impl IntoAccountId32,
destination: impl IntoAccountId32,
payload: impl AsRef<[u8]>,
gas_limit: u64,
value: u128,
block_hash: Option<H256>,
) -> Result<CalculateReplyForHandleResult> {
self.rpc()
.request(
"gear_calculateReplyForHandleResult",
rpc_params![
H256(origin.into_account_id().0),
H256(destination.into_account_id().0),
hex::encode(payload),
gas_limit,
value,
block_hash
],
)
.await
.map_err(Into::into)
}
}