1mod 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
39pub trait Namespace<T: Transport>: Clone {
41 fn new(transport: T) -> Self;
43
44 fn transport(&self) -> &T;
46}
47
48#[derive(Debug, Clone)]
50pub struct Web3<T: Transport> {
51 transport: T,
52}
53
54impl<T: Transport> Web3<T> {
55 pub fn new(transport: T) -> Self {
57 Web3 { transport }
58 }
59
60 pub fn transport(&self) -> &T {
62 &self.transport
63 }
64
65 pub fn api<A: Namespace<T>>(&self) -> A {
67 A::new(self.transport.clone())
68 }
69
70 pub fn accounts(&self) -> accounts::Accounts<T> {
72 self.api()
73 }
74
75 pub fn eth(&self) -> eth::Eth<T> {
77 self.api()
78 }
79
80 pub fn net(&self) -> net::Net<T> {
82 self.api()
83 }
84
85 pub fn web3(&self) -> web3::Web3<T> {
87 self.api()
88 }
89
90 pub fn eth_filter(&self) -> eth_filter::EthFilter<T> {
92 self.api()
93 }
94
95 pub fn parity(&self) -> parity::Parity<T> {
97 self.api()
98 }
99
100 pub fn parity_accounts(&self) -> parity_accounts::ParityAccounts<T> {
102 self.api()
103 }
104
105 pub fn parity_set(&self) -> parity_set::ParitySet<T> {
107 self.api()
108 }
109
110 pub fn personal(&self) -> personal::Personal<T> {
112 self.api()
113 }
114
115 pub fn trace(&self) -> traces::Traces<T> {
117 self.api()
118 }
119
120 pub fn txpool(&self) -> txpool::Txpool<T> {
122 self.api()
123 }
124
125 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 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 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 pub fn eth_subscribe(&self) -> eth_subscribe::EthSubscribe<T> {
163 self.api()
164 }
165}