helios/src/execution/rpc.rs

45 lines
1.3 KiB
Rust
Raw Normal View History

2022-08-21 15:21:50 +00:00
use ethers::prelude::Address;
use eyre::Result;
2022-08-21 16:59:47 +00:00
use jsonrpsee::{
core::client::ClientT,
http_client::{HttpClient, HttpClientBuilder},
rpc_params,
};
use crate::common::utils::{address_to_hex_string, hex_str_to_bytes, u64_to_hex_string};
2022-08-21 15:21:50 +00:00
use super::types::Proof;
2022-08-21 16:27:19 +00:00
pub struct Rpc {
2022-08-21 15:21:50 +00:00
rpc: String,
}
2022-08-21 16:27:19 +00:00
impl Rpc {
2022-08-21 15:21:50 +00:00
pub fn new(rpc: &str) -> Self {
2022-08-21 16:59:47 +00:00
Rpc {
rpc: rpc.to_string(),
}
2022-08-21 15:21:50 +00:00
}
pub async fn get_proof(&self, address: &Address, block: u64) -> Result<Proof> {
2022-08-21 16:59:47 +00:00
let client = self.client()?;
let block_hex = u64_to_hex_string(block);
let addr_hex = address_to_hex_string(address);
2022-08-21 15:21:50 +00:00
let params = rpc_params!(addr_hex, [""], block_hex);
Ok(client.request("eth_getProof", params).await?)
}
2022-08-21 16:59:47 +00:00
pub async fn get_code(&self, address: &Address, block: u64) -> Result<Vec<u8>> {
let client = self.client()?;
let block_hex = u64_to_hex_string(block);
let addr_hex = address_to_hex_string(address);
let params = rpc_params!(addr_hex, block_hex);
let code: String = client.request("eth_getCode", params).await?;
hex_str_to_bytes(&code)
}
fn client(&self) -> Result<HttpClient> {
Ok(HttpClientBuilder::default().build(&self.rpc)?)
}
}