ic_web3/api/
mod.rs

1//! `Web3` implementation
2
3mod accounts;
4mod eth;
5mod eth_filter;
6mod eth_subscribe;
7mod net;
8mod parity;
9mod parity_accounts;
10mod parity_set;
11mod personal;
12mod traces;
13mod txpool;
14mod web3;
15
16pub use self::{
17    accounts::Accounts,
18    eth::Eth,
19    eth_filter::{BaseFilter, EthFilter},
20    eth_subscribe::{EthSubscribe, SubscriptionId, SubscriptionStream},
21    net::Net,
22    parity::Parity,
23    parity_accounts::ParityAccounts,
24    parity_set::ParitySet,
25    personal::Personal,
26    traces::Traces,
27    txpool::Txpool,
28    web3::Web3 as Web3Api,
29};
30
31use crate::{
32    confirm, error,
33    types::{Bytes, TransactionReceipt, TransactionRequest, U64},
34    DuplexTransport, Transport,
35};
36use futures::Future;
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    /// Access methods from custom namespace
66    pub fn api<A: Namespace<T>>(&self) -> A {
67        A::new(self.transport.clone())
68    }
69
70    /// Access methods from `accounts` namespace
71    pub fn accounts(&self) -> accounts::Accounts<T> {
72        self.api()
73    }
74
75    /// Access methods from `eth` namespace
76    pub fn eth(&self) -> eth::Eth<T> {
77        self.api()
78    }
79
80    /// Access methods from `net` namespace
81    pub fn net(&self) -> net::Net<T> {
82        self.api()
83    }
84
85    /// Access methods from `web3` namespace
86    pub fn web3(&self) -> web3::Web3<T> {
87        self.api()
88    }
89
90    /// Access filter methods from `eth` namespace
91    pub fn eth_filter(&self) -> eth_filter::EthFilter<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    ) -> error::Result<TransactionReceipt> {
146        confirm::send_transaction_with_confirmation(self.transport.clone(), tx, poll_interval, confirmations).await
147    }
148
149    /// Sends raw transaction and returns future resolved after transaction is confirmed
150    pub async fn send_raw_transaction_with_confirmation(
151        &self,
152        tx: Bytes,
153        poll_interval: Duration,
154        confirmations: usize,
155    ) -> error::Result<TransactionReceipt> {
156        confirm::send_raw_transaction_with_confirmation(self.transport.clone(), tx, poll_interval, confirmations).await
157    }
158}
159
160impl<T: DuplexTransport> Web3<T> {
161    /// Access subscribe methods from `eth` namespace
162    pub fn eth_subscribe(&self) -> eth_subscribe::EthSubscribe<T> {
163        self.api()
164    }
165}