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
use crate::{
    api::{config::GearConfig, generated::api::RuntimeApi, signer::Signer},
    result::Result,
};
use core::ops::{Deref, DerefMut};
use subxt::{ClientBuilder, PolkadotExtrinsicParams};

pub mod config;
mod constants;
pub mod events;
pub mod generated;
pub mod signer;
mod storage;
pub mod types;
mod utils;

const DEFAULT_GEAR_ENDPOINT: &str = "wss://rpc-node.gear-tech.io:443";

/// gear api
#[derive(Clone)]
pub struct Api(RuntimeApi<GearConfig, PolkadotExtrinsicParams<GearConfig>>);

impl Api {
    /// Build runtime api
    pub async fn new(url: Option<&str>) -> Result<Self> {
        Ok(Self(
            ClientBuilder::new()
                .set_url(url.unwrap_or(DEFAULT_GEAR_ENDPOINT))
                .build()
                .await?
                .to_runtime_api::<RuntimeApi<GearConfig, PolkadotExtrinsicParams<GearConfig>>>(),
        ))
    }

    /// New signer from api
    pub fn signer(self, suri: &str, passwd: Option<&str>) -> Result<Signer> {
        Signer::new(self, suri, passwd)
    }

    /// Try new signer from api
    pub fn try_signer(self, passwd: Option<&str>) -> Result<Signer> {
        Signer::try_new(self, passwd)
    }
}

impl Deref for Api {
    type Target = RuntimeApi<GearConfig, PolkadotExtrinsicParams<GearConfig>>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for Api {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}