gsdk 2.0.0-pre.1

Rust SDK of the Gear network
Documentation
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
// Copyright (C) Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

//! Gear storage apis

use crate::{
    Api, BlockNumber, GearGasNode, GearGasNodeId, GearPages, IntoAccountId32, IntoSubstrate,
    gear::{
        self,
        runtime_types::{
            frame_system::AccountInfo,
            gear_common::storage::primitives::Interval,
            gear_core::{
                pages::Page,
                program::{ActiveProgram, Program},
            },
            pallet_balances::types::AccountData,
            pallet_gear_bank::pallet::BankAccount,
            vara_runtime::RuntimeEvent,
        },
    },
    result::{Error, FailedPage, Result},
};
use futures::prelude::*;
use gear_core::{
    code::{CodeMetadata, InstrumentedCode},
    ids::{ActorId, CodeId, MessageId},
    message::UserStoredMessage,
    pages::GearPage,
    program::MemoryInfix,
};
use gsdk_codegen::at_block;
use sp_core::crypto::AccountId32;
use subxt::{
    error::MetadataError,
    ext::subxt_core::storage::address::StorageHashers,
    metadata::types::StorageEntryType,
    storage::{Address, StaticStorageKey, StorageKey},
    utils::{H256, Yes},
};

impl Api {
    /// Shortcut for fetching a value from storage at specified block.
    ///
    /// # You may not need this.
    ///
    /// Read the docs of [`Api`] to checkout the wrapped storage queries,
    /// we need this function only when we want to execute a query which
    /// has not been wrapped in `gsdk`.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use gsdk::{Api, metadata::storage::SystemStorage};
    ///
    /// let api = Api::new(None);
    ///
    /// {
    ///     let address = Api::storage(SystemStorage::Number);
    ///     let bn = api.storage_fetch(address).await?;
    /// }
    ///
    /// // The code above equals to the following code due to
    /// // the implemented storage query `number` in `Api`.
    ///
    /// {
    ///     let bn = api.number().await?;
    /// }
    /// ```
    #[at_block]
    pub async fn storage_fetch_at<'a, Addr>(
        &self,
        address: &'a Addr,
        block_hash: Option<H256>,
    ) -> Result<Addr::Target>
    where
        Addr: Address<IsFetchable = Yes> + 'a,
    {
        self.storage_at(block_hash)
            .await?
            .fetch(address)
            .await?
            .ok_or(Error::StorageEntryNotFound)
    }
}

// frame-system
impl Api {
    /// Get account info by its address at specified block.
    #[at_block]
    pub async fn account_info_at(
        &self,
        address: impl IntoAccountId32,
        block_hash: Option<H256>,
    ) -> Result<AccountInfo<u32, AccountData<u128>>> {
        self.storage_fetch_at(
            &gear::storage().system().account(address.into_account_id()),
            block_hash,
        )
        .await
    }

    /// Get account data by its address at specified block.
    #[at_block]
    pub async fn account_data_at(
        &self,
        address: impl IntoAccountId32,
        block_hash: Option<H256>,
    ) -> Result<AccountData<u128>> {
        self.account_info_at(address, block_hash)
            .map_ok(|info| info.data)
            .await
    }

    /// Get block number.
    pub async fn number(&self) -> Result<u32> {
        self.storage_fetch(&gear::storage().system().number()).await
    }

    /// Get the free funds balance of the account identified by `account_id` at specified block.
    #[at_block]
    pub async fn free_balance_at(
        &self,
        account_id: impl IntoAccountId32,
        block_hash: Option<H256>,
    ) -> Result<u128> {
        Ok(self.account_data_at(account_id, block_hash).await?.free)
    }

    /// Get the reserved funds balance of the account identified by `account_id` at specified block.
    #[at_block]
    pub async fn reserved_balance_at(
        &self,
        account_id: impl IntoAccountId32,
        block_hash: Option<H256>,
    ) -> Result<u128> {
        Ok(self.account_data_at(account_id, block_hash).await?.reserved)
    }

    /// Get the total balance of the account identified by `account_id` at specified block.
    ///
    /// Total balance includes free and reserved funds.
    #[at_block]
    pub async fn total_balance_at(
        &self,
        account_id: impl IntoAccountId32,
        block_hash: Option<H256>,
    ) -> Result<u128> {
        let data = self.account_data_at(account_id, block_hash).await?;

        data.free
            .checked_add(data.reserved)
            .ok_or(Error::BalanceOverflow)
    }

    /// Get events at specified block.
    #[at_block]
    pub async fn block_events_at(&self, block_hash: Option<H256>) -> Result<Vec<RuntimeEvent>> {
        let addr = gear::storage().system().events();

        let evs = self.storage_fetch_at(&addr, block_hash).await?;

        Ok(evs.into_iter().map(|ev| ev.event).collect())
    }
}

// pallet-timestamp
impl Api {
    /// Return a timestamp of the block.
    pub async fn block_timestamp(&self, block_hash: Option<H256>) -> Result<u64> {
        self.storage_fetch_at(&gear::storage().timestamp().now(), block_hash)
            .await
    }
}

// pallet-session
impl Api {
    /// Get all validators from pallet_session.
    pub async fn validators(&self) -> Result<Vec<AccountId32>> {
        Ok(self
            .storage_fetch(&gear::storage().session().validators())
            .await?
            .into_iter()
            .map(|id| id.into_substrate())
            .collect())
    }
}

// pallet-gas
impl Api {
    /// Get value of gas total issuance at specified block.
    #[at_block]
    pub async fn total_issuance_at(&self, block_hash: Option<H256>) -> Result<u64> {
        self.storage_fetch_at(&gear::storage().gear_gas().total_issuance(), block_hash)
            .await
    }

    /// Get Gear gas nodes by their ids at specified block.
    #[at_block]
    pub async fn gas_nodes_at(
        &self,
        gas_node_ids: impl IntoIterator<Item = GearGasNodeId>,
        block_hash: Option<H256>,
    ) -> Result<Vec<(GearGasNodeId, GearGasNode)>> {
        stream::iter(gas_node_ids)
            .then(|gas_node_id| async move {
                let addr = gear::storage().gear_gas().gas_nodes(gas_node_id.clone());
                let gas_node = self.storage_fetch_at(&addr, block_hash).await?;

                Ok((gas_node_id.clone(), gas_node))
            })
            .try_collect()
            .await
    }
}

// pallet-gear-bank
impl Api {
    /// Get Gear bank account data at specified block.
    #[at_block]
    pub async fn bank_info_at(
        &self,
        account_id: impl IntoAccountId32,
        block_hash: Option<H256>,
    ) -> Result<BankAccount<u128>> {
        self.storage_fetch_at(
            &gear::storage()
                .gear_bank()
                .bank(account_id.into_account_id()),
            block_hash,
        )
        .await
    }

    /// Get Gear bank's sovereign account id.
    pub async fn bank_address(&self) -> Result<AccountId32> {
        Ok(self
            .storage_fetch(&gear::storage().gear_bank().bank_address())
            .await?
            .into_substrate())
    }
}

// pallet-gear
impl Api {
    /// Check whether the message queue processing is stopped or not.
    pub async fn execute_inherent(&self) -> Result<bool> {
        Ok(self
            .storage()
            .at_latest()
            .await?
            .fetch_or_default(&gear::storage().gear().execute_inherent())
            .await?)
    }

    /// Get gear block number.
    pub async fn gear_block_number(&self, block_hash: Option<H256>) -> Result<BlockNumber> {
        Ok(self
            .storage_at(block_hash)
            .await?
            .fetch_or_default(&gear::storage().gear().block_number())
            .await?)
    }
}

// pallet-gear-program
impl Api {
    /// Returns original WASM code for the given `CodeId` at specified block.
    #[at_block]
    pub async fn original_code_at(
        &self,
        code_id: CodeId,
        block_hash: Option<H256>,
    ) -> Result<Vec<u8>> {
        self.storage_fetch_at(
            &gear::storage()
                .gear_program()
                .original_code_storage(code_id),
            block_hash,
        )
        .await
    }

    /// Get `InstrumentedCode` by its `CodeId` at specified block.
    #[at_block]
    pub async fn instrumented_code_storage_at(
        &self,
        code_id: CodeId,
        block_hash: Option<H256>,
    ) -> Result<InstrumentedCode> {
        self.storage_fetch_at(
            &gear::storage()
                .gear_program()
                .instrumented_code_storage(code_id),
            block_hash,
        )
        .await
    }

    /// Get `CodeMetadata` by its `CodeId` at specified block.
    #[at_block]
    pub async fn code_metadata_storage_at(
        &self,
        code_id: CodeId,
        block_hash: Option<H256>,
    ) -> Result<CodeMetadata> {
        self.storage_fetch_at(
            &gear::storage()
                .gear_program()
                .code_metadata_storage(code_id),
            block_hash,
        )
        .await
    }

    /// Returns `ActiveProgram` for the given `ActorId` at specified block.
    #[at_block]
    pub async fn active_program_at(
        &self,
        program_id: ActorId,
        block_hash: Option<H256>,
    ) -> Result<ActiveProgram<BlockNumber>> {
        match self.program_at(program_id, block_hash).await? {
            Program::Active(p) => Ok(p),
            _ => Err(Error::ProgramTerminated),
        }
    }

    /// Get pages of an active program at specified block.
    #[at_block]
    pub async fn program_pages_at(
        &self,
        program_id: ActorId,
        block_hash: Option<H256>,
    ) -> Result<GearPages> {
        let address = gear::storage()
            .gear_program()
            .memory_pages_iter1(program_id);

        let metadata = self.metadata();
        let hashers = subxt::ext::subxt_core::storage::lookup_storage_entry_details(
            address.pallet_name(),
            address.entry_name(),
            &metadata,
        )
        .and_then(|(_, entry)| StorageHashers::new(entry.entry_type(), metadata.types()))
        .map_err(subxt::Error::from)?;
        let pages = self
            .storage_at(block_hash)
            .await?
            .iter(address)
            .try_flatten_stream()
            .map_err(Error::from)
            .and_then(|pair| {
                std::future::ready({
                    // FIXME: Do not decode key manually.
                    //        Requires a fix from `subxt`.
                    <(
                        StaticStorageKey<ActorId>,
                        StaticStorageKey<MemoryInfix>,
                        StaticStorageKey<Page>,
                    ) as StorageKey>::decode_storage_key(
                        &mut &pair.key_bytes[32..],
                        &mut hashers.iter(),
                        metadata.types(),
                    )
                    .map_err(subxt::Error::from)
                    .map_err(Error::from)
                    .and_then(|(_, _, page_index)| {
                        Ok((page_index.into_key().0.try_into()?, pair.value))
                    })
                })
            })
            .try_collect()
            .await?;

        Ok(pages)
    }

    /// Get inheritor address by program id at specified block.
    #[at_block]
    pub async fn inheritor_of_at(
        &self,
        program_id: ActorId,
        block_hash: Option<H256>,
    ) -> Result<Option<ActorId>> {
        Ok(match self.program_at(program_id, block_hash).await? {
            Program::Exited(p) => Some(p),
            _ => None,
        })
    }

    /// Get pages of active program at specified block.
    #[at_block]
    pub async fn specified_program_pages_at(
        &self,
        program_id: ActorId,
        page_numbers: impl IntoIterator<Item = GearPage>,
        block_hash: Option<H256>,
    ) -> Result<GearPages> {
        futures::stream::iter(page_numbers)
            .then(|page| async move {
                let addr = gear::storage().gear_program().memory_pages(
                    program_id,
                    // memory infix is always zero now
                    MemoryInfix::default(),
                    page.into(),
                );

                let page_buf = self
                    .storage_at(block_hash)
                    .await?
                    .fetch(&addr)
                    .await?
                    .ok_or_else(|| FailedPage::new(page, program_id).not_found())?;

                Ok((page, page_buf))
            })
            .try_collect()
            .await
    }

    /// Get program by its id at specified block.
    #[at_block]
    pub async fn program_at(
        &self,
        program_id: ActorId,
        block_hash: Option<H256>,
    ) -> Result<Program<BlockNumber>> {
        self.storage_fetch_at(
            &gear::storage().gear_program().program_storage(program_id),
            block_hash,
        )
        .await
    }
}

// pallet-gear-messenger
impl Api {
    /// Get a message identified by `message_id` from the `account_id`'s
    /// mailbox at specified block.
    #[at_block]
    pub async fn mailbox_account_message_at(
        &self,
        account_id: impl IntoAccountId32,
        message_id: MessageId,
        block_hash: Option<H256>,
    ) -> Result<Option<(UserStoredMessage, Interval<u32>)>> {
        Ok(self
            .storage_fetch_at(
                &gear::storage()
                    .gear_messenger()
                    .mailbox(account_id.into_account_id(), message_id),
                block_hash,
            )
            .await
            .ok())
    }

    /// Retrieves up to `count` mailbox messages or for
    /// the provided `address` at specified block.
    #[at_block]
    pub async fn mailbox_messages_at(
        &self,
        account_id: impl IntoAccountId32,
        count: usize,
        block_hash: Option<H256>,
    ) -> Result<Vec<(UserStoredMessage, Interval<u32>)>> {
        self.storage_at(block_hash)
            .await?
            .iter(
                gear::storage()
                    .gear_messenger()
                    .mailbox_iter1(account_id.into_account_id()),
            )
            .await?
            .map_ok(|pair| pair.value)
            .take(count)
            .try_collect()
            .await
            .map_err(Error::from)
    }
}

/// Get storage entry type id using `metadata` and storage entry `address`
pub(crate) fn storage_type_id(
    metadata: &subxt::Metadata,
    address: &impl Address,
) -> Result<u32, MetadataError> {
    let storage_type = metadata
        .pallet_by_name_err(address.pallet_name())?
        .storage()
        .ok_or_else(|| MetadataError::StorageNotFoundInPallet(address.pallet_name().to_owned()))?
        .entry_by_name(address.entry_name())
        .ok_or_else(|| MetadataError::StorageEntryNotFound(address.entry_name().to_owned()))?
        .entry_type();

    let storage_type_id = *match storage_type {
        StorageEntryType::Plain(id) => id,
        StorageEntryType::Map { value_ty, .. } => value_ty,
    };

    Ok(storage_type_id)
}