use serde::de::DeserializeOwned;
use serde_json::Value;
use crate::decode::ValueExt;
use crate::error::Result;
use crate::rpc::Handler;
pub struct Api<H: Handler> {
pub handler: H,
}
impl<H: Handler> Api<H> {
pub fn new(handler: H) -> Api<H> {
Api { handler }
}
pub async fn call(&self, method: &str, params: Vec<Value>) -> Result<Value> {
self.handler.call(method, params).await
}
pub async fn call_as<T: DeserializeOwned>(
&self,
method: &str,
params: Vec<Value>,
) -> Result<T> {
Ok(serde_json::from_value(
self.handler.call(method, params).await?,
)?)
}
pub async fn block_number(&self) -> Result<u64> {
self.handler.call("eth_blockNumber", vec![]).await?.to_u64()
}
pub async fn chain_id(&self) -> Result<u64> {
self.handler.call("eth_chainId", vec![]).await?.to_u64()
}
}