ic_web3_rs/api/
web3.rs

1//! `Web3` namespace
2
3use crate::{
4    api::Namespace,
5    helpers::{self, CallFuture},
6    transports::ic_http_client::CallOptions,
7    types::{Bytes, H256},
8    Transport,
9};
10
11/// `Web3` namespace
12#[derive(Debug, Clone)]
13pub struct Web3<T> {
14    transport: T,
15}
16
17impl<T: Transport> Namespace<T> for Web3<T> {
18    fn new(transport: T) -> Self
19    where
20        Self: Sized,
21    {
22        Web3 { transport }
23    }
24
25    fn transport(&self) -> &T {
26        &self.transport
27    }
28}
29
30impl<T: Transport> Web3<T> {
31    /// Returns client version
32    pub fn client_version(&self, options: CallOptions) -> CallFuture<String, T::Out> {
33        CallFuture::new(self.transport.execute("web3_clientVersion", vec![], options))
34    }
35
36    /// Returns sha3 of the given data
37    pub fn sha3(&self, bytes: Bytes, options: CallOptions) -> CallFuture<H256, T::Out> {
38        let bytes = helpers::serialize(&bytes);
39        CallFuture::new(self.transport.execute("web3_sha3", vec![bytes], options))
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use super::Web3;
46    use crate::{api::Namespace, rpc::Value, transports::ic_http_client::CallOptions, types::H256};
47    use hex_literal::hex;
48
49    rpc_test! (
50      Web3:client_version, CallOptions::default() => "web3_clientVersion", Vec::<String>::new();
51      Value::String("Test123".into()) => "Test123"
52    );
53
54    rpc_test! (
55      Web3:sha3, hex!("01020304"),CallOptions::default()
56      =>
57      "web3_sha3", vec![r#""0x01020304""#];
58      Value::String("0x0000000000000000000000000000000000000000000000000000000000000123".into()) => H256::from_low_u64_be(0x123)
59    );
60}