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
//! gear api rpc methods
use crate::{
    api::{signer::Signer, types},
    result::Result,
};

use std::sync::Arc;
use subxt::{
    rpc::{rpc_params, ClientT},
    sp_core::{Bytes, H256},
    RpcClient,
};

impl Signer {
    /// get rpc client
    pub fn rpc(&self) -> Arc<RpcClient> {
        self.client.rpc().client.clone()
    }

    /// public key of the signer in H256
    pub fn source(&self) -> H256 {
        AsRef::<[u8; 32]>::as_ref(self.signer.account_id()).into()
    }

    /// gear_getInitGasSpent
    pub async fn get_init_gas_spent(
        &self,
        code: Bytes,
        payload: Bytes,
        value: u64,
        at: Option<H256>,
    ) -> Result<types::GasInfo> {
        self.rpc()
            .request(
                "gear_calculateInitUploadGas",
                rpc_params![self.source(), code, payload, value, true, at],
            )
            .await
            .map_err(Into::into)
    }

    /// gear_getHandleGasSpent
    #[allow(dead_code)]
    pub async fn get_handle_gas_spent(
        &self,
        dest: H256,
        payload: Bytes,
        value: u128,
        at: Option<H256>,
    ) -> Result<types::GasInfo> {
        self.rpc()
            .request(
                "gear_calculateHandleGas",
                rpc_params![self.source(), dest, payload, value, true, at],
            )
            .await
            .map_err(Into::into)
    }
}