ic_web3_rs/api/
mod.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
//! `Web3` implementation

mod accounts;
mod eth;
mod eth_subscribe;
mod net;
mod parity;
mod parity_accounts;
mod parity_set;
mod personal;
mod traces;
mod txpool;
mod web3;

pub use self::{
    accounts::Accounts,
    eth::Eth,
    eth_subscribe::{EthSubscribe, SubscriptionId, SubscriptionStream},
    net::Net,
    parity::Parity,
    parity_accounts::ParityAccounts,
    parity_set::ParitySet,
    personal::Personal,
    traces::Traces,
    txpool::Txpool,
    web3::Web3 as Web3Api,
};

use crate::{
    confirm, error,
    transports::ic_http_client::CallOptions,
    types::{Bytes, TransactionReceipt, TransactionRequest, U64},
    DuplexTransport, Error, RequestId, Transport,
};
use futures::Future;
use jsonrpc_core::types::Call;
use std::time::Duration;

/// Common API for all namespaces
pub trait Namespace<T: Transport>: Clone {
    /// Creates new API namespace
    fn new(transport: T) -> Self;

    /// Borrows a transport.
    fn transport(&self) -> &T;
}

/// `Web3` wrapper for all namespaces
#[derive(Debug, Clone)]
pub struct Web3<T: Transport> {
    transport: T,
}

impl<T: Transport> Web3<T> {
    /// Create new `Web3` with given transport
    pub fn new(transport: T) -> Self {
        Web3 { transport }
    }

    /// Borrows a transport.
    pub fn transport(&self) -> &T {
        &self.transport
    }

    /// set the max response bytes
    pub fn set_max_response_bytes(&mut self, bytes: u64) {
        self.transport.set_max_response_bytes(bytes)
    }

    /// Access methods from custom namespace
    pub fn api<A: Namespace<T>>(&self) -> A {
        A::new(self.transport.clone())
    }

    /// Access methods from `accounts` namespace
    pub fn accounts(&self) -> accounts::Accounts<T> {
        self.api()
    }

    /// Access methods from `eth` namespace
    pub fn eth(&self) -> eth::Eth<T> {
        self.api()
    }

    /// Access methods from `net` namespace
    pub fn net(&self) -> net::Net<T> {
        self.api()
    }

    /// Access methods from `web3` namespace
    pub fn web3(&self) -> web3::Web3<T> {
        self.api()
    }

    /// Access methods from `parity` namespace
    pub fn parity(&self) -> parity::Parity<T> {
        self.api()
    }

    /// Access methods from `parity_accounts` namespace
    pub fn parity_accounts(&self) -> parity_accounts::ParityAccounts<T> {
        self.api()
    }

    /// Access methods from `parity_set` namespace
    pub fn parity_set(&self) -> parity_set::ParitySet<T> {
        self.api()
    }

    /// Access methods from `personal` namespace
    pub fn personal(&self) -> personal::Personal<T> {
        self.api()
    }

    /// Access methods from `trace` namespace
    pub fn trace(&self) -> traces::Traces<T> {
        self.api()
    }

    /// Access methods from `txpool` namespace
    pub fn txpool(&self) -> txpool::Txpool<T> {
        self.api()
    }

    ///// Should be used to wait for confirmations
    //pub async fn wait_for_confirmations<F, V>(
    //    &self,
    //    poll_interval: Duration,
    //    confirmations: usize,
    //    check: V,
    //) -> error::Result<()>
    //where
    //    F: Future<Output = error::Result<Option<U64>>>,
    //    V: confirm::ConfirmationCheck<Check = F>,
    //{
    //    confirm::wait_for_confirmations(self.eth(), self.eth_filter(), poll_interval, confirmations, check).await
    //}

    /// Sends transaction and returns future resolved after transaction is confirmed
    pub async fn send_transaction_with_confirmation(
        &self,
        tx: TransactionRequest,
        poll_interval: Duration,
        confirmations: usize,
        options: CallOptions,
    ) -> error::Result<TransactionReceipt> {
        confirm::send_transaction_with_confirmation(self.transport.clone(), tx, poll_interval, confirmations, options)
            .await
    }

    /// Sends raw transaction and returns future resolved after transaction is confirmed
    pub async fn send_raw_transaction_with_confirmation(
        &self,
        tx: Bytes,
        poll_interval: Duration,
        confirmations: usize,
        options: CallOptions,
    ) -> error::Result<TransactionReceipt> {
        confirm::send_raw_transaction_with_confirmation(
            self.transport.clone(),
            tx,
            poll_interval,
            confirmations,
            options,
        )
        .await
    }

    /// Call json rpc directly
    pub async fn json_rpc_call(&self, body: &str, options: CallOptions) -> error::Result<String> {
        let request: Call = serde_json::from_str(body).map_err(|_| Error::Decoder(body.to_string()))?;
        // currently, the request id is not used
        self.transport
            .send(RequestId::default(), request, options)
            .await
            .map(|v| format!("{}", v))
    }
}

impl<T: DuplexTransport> Web3<T> {
    /// Access subscribe methods from `eth` namespace
    pub fn eth_subscribe(&self) -> eth_subscribe::EthSubscribe<T> {
        self.api()
    }
}