1mod 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
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 set_max_response_bytes(&mut self, bytes: u64) {
67 self.transport.set_max_response_bytes(bytes)
68 }
69
70 pub fn api<A: Namespace<T>>(&self) -> A {
72 A::new(self.transport.clone())
73 }
74
75 pub fn accounts(&self) -> accounts::Accounts<T> {
77 self.api()
78 }
79
80 pub fn eth(&self) -> eth::Eth<T> {
82 self.api()
83 }
84
85 pub fn net(&self) -> net::Net<T> {
87 self.api()
88 }
89
90 pub fn web3(&self) -> web3::Web3<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 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 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 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 self.transport
174 .send(RequestId::default(), request, options)
175 .await
176 .map(|v| format!("{}", v))
177 }
178}
179
180impl<T: DuplexTransport> Web3<T> {
181 pub fn eth_subscribe(&self) -> eth_subscribe::EthSubscribe<T> {
183 self.api()
184 }
185}