pink_web3/api/
mod.rs

1//! `Web3` implementation
2
3mod accounts;
4mod eth;
5mod eth_filter;
6mod net;
7mod parity;
8mod parity_accounts;
9mod parity_set;
10mod personal;
11// mod traces;
12mod txpool;
13mod web3;
14
15pub use self::{
16    accounts::Accounts,
17    eth::Eth,
18    eth_filter::{BaseFilter, EthFilter},
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    types::{Bytes, TransactionReceipt, TransactionRequest, U64},
32    Transport,
33};
34use futures::Future;
35use core::time::Duration;
36
37/// Common API for all namespaces
38pub trait Namespace<T: Transport>: Clone {
39    /// Creates new API namespace
40    fn new(transport: T) -> Self;
41
42    /// Borrows a transport.
43    fn transport(&self) -> &T;
44}
45
46/// `Web3` wrapper for all namespaces
47#[derive(Debug, Clone)]
48pub struct Web3<T: Transport> {
49    transport: T,
50}
51
52impl<T: Transport> Web3<T> {
53    /// Create new `Web3` with given transport
54    pub fn new(transport: T) -> Self {
55        Web3 { transport }
56    }
57
58    /// Borrows a transport.
59    pub fn transport(&self) -> &T {
60        &self.transport
61    }
62
63    /// Access methods from custom namespace
64    pub fn api<A: Namespace<T>>(&self) -> A {
65        A::new(self.transport.clone())
66    }
67
68    /// Access methods from `accounts` namespace
69    pub fn accounts(&self) -> accounts::Accounts<T> {
70        self.api()
71    }
72
73    /// Access methods from `eth` namespace
74    pub fn eth(&self) -> eth::Eth<T> {
75        self.api()
76    }
77
78    /// Access methods from `net` namespace
79    pub fn net(&self) -> net::Net<T> {
80        self.api()
81    }
82
83    /// Access methods from `web3` namespace
84    pub fn web3(&self) -> web3::Web3<T> {
85        self.api()
86    }
87
88    /// Access filter methods from `eth` namespace
89    pub fn eth_filter(&self) -> eth_filter::EthFilter<T> {
90        self.api()
91    }
92
93    /// Access methods from `parity` namespace
94    pub fn parity(&self) -> parity::Parity<T> {
95        self.api()
96    }
97
98    /// Access methods from `parity_accounts` namespace
99    pub fn parity_accounts(&self) -> parity_accounts::ParityAccounts<T> {
100        self.api()
101    }
102
103    /// Access methods from `parity_set` namespace
104    pub fn parity_set(&self) -> parity_set::ParitySet<T> {
105        self.api()
106    }
107
108    /// Access methods from `personal` namespace
109    pub fn personal(&self) -> personal::Personal<T> {
110        self.api()
111    }
112
113    // /// Access methods from `trace` namespace
114    // pub fn trace(&self) -> traces::Traces<T> {
115    //     self.api()
116    // }
117
118    /// Access methods from `txpool` namespace
119    pub fn txpool(&self) -> txpool::Txpool<T> {
120        self.api()
121    }
122
123    /// Should be used to wait for confirmations
124    pub async fn wait_for_confirmations<F, V>(
125        &self,
126        poll_interval: Duration,
127        confirmations: usize,
128        check: V,
129    ) -> error::Result<()>
130    where
131        F: Future<Output = error::Result<Option<U64>>>,
132        V: confirm::ConfirmationCheck<Check = F>,
133    {
134        confirm::wait_for_confirmations(self.eth(), self.eth_filter(), poll_interval, confirmations, check).await
135    }
136
137    /// Sends transaction and returns future resolved after transaction is confirmed
138    pub async fn send_transaction_with_confirmation(
139        &self,
140        tx: TransactionRequest,
141        poll_interval: Duration,
142        confirmations: usize,
143    ) -> error::Result<TransactionReceipt> {
144        confirm::send_transaction_with_confirmation(self.transport.clone(), tx, poll_interval, confirmations).await
145    }
146
147    /// Sends raw transaction and returns future resolved after transaction is confirmed
148    pub async fn send_raw_transaction_with_confirmation(
149        &self,
150        tx: Bytes,
151        poll_interval: Duration,
152        confirmations: usize,
153    ) -> error::Result<TransactionReceipt> {
154        confirm::send_raw_transaction_with_confirmation(self.transport.clone(), tx, poll_interval, confirmations).await
155    }
156}