1use serde::de::DeserializeOwned;
4use serde_json::Value;
5
6use crate::decode::ValueExt;
7use crate::error::Result;
8use crate::rpc::Handler;
9
10pub struct Api<H: Handler> {
13 pub handler: H,
15}
16
17impl<H: Handler> Api<H> {
18 pub fn new(handler: H) -> Api<H> {
20 Api { handler }
21 }
22
23 pub async fn call(&self, method: &str, params: Vec<Value>) -> Result<Value> {
25 self.handler.call(method, params).await
26 }
27
28 pub async fn call_as<T: DeserializeOwned>(
30 &self,
31 method: &str,
32 params: Vec<Value>,
33 ) -> Result<T> {
34 Ok(serde_json::from_value(
35 self.handler.call(method, params).await?,
36 )?)
37 }
38
39 pub async fn block_number(&self) -> Result<u64> {
41 self.handler.call("eth_blockNumber", vec![]).await?.to_u64()
42 }
43
44 pub async fn chain_id(&self) -> Result<u64> {
46 self.handler.call("eth_chainId", vec![]).await?.to_u64()
47 }
48}