ic_web3_rs/api/
mod.rs

1//! `Web3` implementation
2
3mod accounts;
4mod eth;
5mod eth_subscribe;
6mod net;
7mod parity;
8mod parity_accounts;
9mod parity_set;
10mod personal;
11mod traces;
12mod txpool;
13mod web3;
14
15pub use self::{
16    accounts::Accounts,
17    eth::Eth,
18    eth_subscribe::{EthSubscribe, SubscriptionId, SubscriptionStream},
19    net::Net,
20    parity::Parity,
21    parity_accounts::ParityAccounts,
22    parity_set::ParitySet,
23    personal::Personal,
24    traces::Traces,
25    txpool::Txpool,
26    web3::Web3 as Web3Api,
27};
28
29use crate::{
30    confirm, error,
31    transports::ic_http_client::CallOptions,
32    types::{Bytes, TransactionReceipt, TransactionRequest, U64},
33    DuplexTransport, Error, RequestId, Transport,
34};
35use futures::Future;
36use jsonrpc_core::types::Call;
37use std::time::Duration;
38
39/// Common API for all namespaces
40pub trait Namespace<T: Transport>: Clone {
41    /// Creates new API namespace
42    fn new(transport: T) -> Self;
43
44    /// Borrows a transport.
45    fn transport(&self) -> &T;
46}
47
48/// `Web3` wrapper for all namespaces
49#[derive(Debug, Clone)]
50pub struct Web3<T: Transport> {
51    transport: T,
52}
53
54impl<T: Transport> Web3<T> {
55    /// Create new `Web3` with given transport
56    pub fn new(transport: T) -> Self {
57        Web3 { transport }
58    }
59
60    /// Borrows a transport.
61    pub fn transport(&self) -> &T {
62        &self.transport
63    }
64
65    /// set the max response bytes
66    pub fn set_max_response_bytes(&mut self, bytes: u64) {
67        self.transport.set_max_response_bytes(bytes)
68    }
69
70    /// Access methods from custom namespace
71    pub fn api<A: Namespace<T>>(&self) -> A {
72        A::new(self.transport.clone())
73    }
74
75    /// Access methods from `accounts` namespace
76    pub fn accounts(&self) -> accounts::Accounts<T> {
77        self.api()
78    }
79
80    /// Access methods from `eth` namespace
81    pub fn eth(&self) -> eth::Eth<T> {
82        self.api()
83    }
84
85    /// Access methods from `net` namespace
86    pub fn net(&self) -> net::Net<T> {
87        self.api()
88    }
89
90    /// Access methods from `web3` namespace
91    pub fn web3(&self) -> web3::Web3<T> {
92        self.api()
93    }
94
95    /// Access methods from `parity` namespace
96    pub fn parity(&self) -> parity::Parity<T> {
97        self.api()
98    }
99
100    /// Access methods from `parity_accounts` namespace
101    pub fn parity_accounts(&self) -> parity_accounts::ParityAccounts<T> {
102        self.api()
103    }
104
105    /// Access methods from `parity_set` namespace
106    pub fn parity_set(&self) -> parity_set::ParitySet<T> {
107        self.api()
108    }
109
110    /// Access methods from `personal` namespace
111    pub fn personal(&self) -> personal::Personal<T> {
112        self.api()
113    }
114
115    /// Access methods from `trace` namespace
116    pub fn trace(&self) -> traces::Traces<T> {
117        self.api()
118    }
119
120    /// Access methods from `txpool` namespace
121    pub fn txpool(&self) -> txpool::Txpool<T> {
122        self.api()
123    }
124
125    ///// Should be used to wait for confirmations
126    //pub async fn wait_for_confirmations<F, V>(
127    //    &self,
128    //    poll_interval: Duration,
129    //    confirmations: usize,
130    //    check: V,
131    //) -> error::Result<()>
132    //where
133    //    F: Future<Output = error::Result<Option<U64>>>,
134    //    V: confirm::ConfirmationCheck<Check = F>,
135    //{
136    //    confirm::wait_for_confirmations(self.eth(), self.eth_filter(), poll_interval, confirmations, check).await
137    //}
138
139    /// Sends transaction and returns future resolved after transaction is confirmed
140    pub async fn send_transaction_with_confirmation(
141        &self,
142        tx: TransactionRequest,
143        poll_interval: Duration,
144        confirmations: usize,
145        options: CallOptions,
146    ) -> error::Result<TransactionReceipt> {
147        confirm::send_transaction_with_confirmation(self.transport.clone(), tx, poll_interval, confirmations, options)
148            .await
149    }
150
151    /// Sends raw transaction and returns future resolved after transaction is confirmed
152    pub async fn send_raw_transaction_with_confirmation(
153        &self,
154        tx: Bytes,
155        poll_interval: Duration,
156        confirmations: usize,
157        options: CallOptions,
158    ) -> error::Result<TransactionReceipt> {
159        confirm::send_raw_transaction_with_confirmation(
160            self.transport.clone(),
161            tx,
162            poll_interval,
163            confirmations,
164            options,
165        )
166        .await
167    }
168
169    /// Call json rpc directly
170    pub async fn json_rpc_call(&self, body: &str, options: CallOptions) -> error::Result<String> {
171        let request: Call = serde_json::from_str(body).map_err(|_| Error::Decoder(body.to_string()))?;
172        // currently, the request id is not used
173        self.transport
174            .send(RequestId::default(), request, options)
175            .await
176            .map(|v| format!("{}", v))
177    }
178}
179
180impl<T: DuplexTransport> Web3<T> {
181    /// Access subscribe methods from `eth` namespace
182    pub fn eth_subscribe(&self) -> eth_subscribe::EthSubscribe<T> {
183        self.api()
184    }
185}