2022-09-01 19:58:45 +00:00
|
|
|
use ethers::{abi::AbiEncode, types::Address};
|
2022-08-26 01:18:47 +00:00
|
|
|
use eyre::Result;
|
2022-08-27 00:05:12 +00:00
|
|
|
use std::{fmt::Display, net::SocketAddr, str::FromStr, sync::Arc};
|
2022-08-31 00:31:58 +00:00
|
|
|
use tokio::sync::Mutex;
|
2022-08-26 01:18:47 +00:00
|
|
|
|
|
|
|
use jsonrpsee::{
|
2022-08-31 21:40:44 +00:00
|
|
|
core::{async_trait, server::rpc_module::Methods, Error},
|
2022-08-26 01:18:47 +00:00
|
|
|
http_server::{HttpServerBuilder, HttpServerHandle},
|
|
|
|
proc_macros::rpc,
|
|
|
|
};
|
|
|
|
|
|
|
|
use super::Client;
|
2022-09-01 21:07:30 +00:00
|
|
|
use common::utils::{hex_str_to_bytes, u64_to_hex_string};
|
2022-09-01 19:58:45 +00:00
|
|
|
use execution::types::{CallOpts, ExecutionBlock};
|
2022-08-26 01:18:47 +00:00
|
|
|
|
|
|
|
pub struct Rpc {
|
2022-08-31 00:31:58 +00:00
|
|
|
client: Arc<Mutex<Client>>,
|
2022-08-26 01:18:47 +00:00
|
|
|
handle: Option<HttpServerHandle>,
|
2022-08-29 20:54:58 +00:00
|
|
|
port: u16,
|
2022-08-26 01:18:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Rpc {
|
2022-08-31 00:31:58 +00:00
|
|
|
pub fn new(client: Arc<Mutex<Client>>, port: u16) -> Self {
|
2022-08-26 01:18:47 +00:00
|
|
|
Rpc {
|
|
|
|
client,
|
|
|
|
handle: None,
|
2022-08-29 20:54:58 +00:00
|
|
|
port,
|
2022-08-26 01:18:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn start(&mut self) -> Result<SocketAddr> {
|
|
|
|
let rpc_inner = RpcInner {
|
|
|
|
client: self.client.clone(),
|
2022-08-29 20:54:58 +00:00
|
|
|
port: self.port,
|
2022-08-26 01:18:47 +00:00
|
|
|
};
|
|
|
|
let (handle, addr) = start(rpc_inner).await?;
|
|
|
|
self.handle = Some(handle);
|
|
|
|
Ok(addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[rpc(client, server, namespace = "eth")]
|
|
|
|
trait EthRpc {
|
|
|
|
#[method(name = "getBalance")]
|
|
|
|
async fn get_balance(&self, address: &str, block: &str) -> Result<String, Error>;
|
|
|
|
#[method(name = "getTransactionCount")]
|
|
|
|
async fn get_transaction_count(&self, address: &str, block: &str) -> Result<String, Error>;
|
|
|
|
#[method(name = "getCode")]
|
|
|
|
async fn get_code(&self, address: &str, block: &str) -> Result<String, Error>;
|
|
|
|
#[method(name = "call")]
|
|
|
|
async fn call(&self, opts: CallOpts, block: &str) -> Result<String, Error>;
|
2022-08-27 20:43:27 +00:00
|
|
|
#[method(name = "estimateGas")]
|
|
|
|
async fn estimate_gas(&self, opts: CallOpts) -> Result<String, Error>;
|
2022-08-27 00:05:12 +00:00
|
|
|
#[method(name = "chainId")]
|
2022-08-31 00:31:58 +00:00
|
|
|
async fn chain_id(&self) -> Result<String, Error>;
|
2022-08-29 15:59:02 +00:00
|
|
|
#[method(name = "gasPrice")]
|
|
|
|
async fn gas_price(&self) -> Result<String, Error>;
|
|
|
|
#[method(name = "maxPriorityFeePerGas")]
|
|
|
|
async fn max_priority_fee_per_gas(&self) -> Result<String, Error>;
|
2022-08-29 16:06:50 +00:00
|
|
|
#[method(name = "blockNumber")]
|
|
|
|
async fn block_number(&self) -> Result<String, Error>;
|
2022-08-31 21:40:44 +00:00
|
|
|
#[method(name = "getBlockByNumber")]
|
|
|
|
async fn get_block_by_number(&self, num: &str, full_tx: bool) -> Result<ExecutionBlock, Error>;
|
2022-09-01 21:07:30 +00:00
|
|
|
#[method(name = "sendRawTransaction")]
|
|
|
|
async fn send_raw_transaction(&self, bytes: &str) -> Result<String, Error>;
|
2022-08-26 01:18:47 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 21:40:44 +00:00
|
|
|
#[rpc(client, server, namespace = "net")]
|
|
|
|
trait NetRpc {
|
|
|
|
#[method(name = "version")]
|
|
|
|
async fn version(&self) -> Result<String, Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
2022-08-26 01:18:47 +00:00
|
|
|
struct RpcInner {
|
2022-08-31 00:31:58 +00:00
|
|
|
client: Arc<Mutex<Client>>,
|
2022-08-29 20:54:58 +00:00
|
|
|
port: u16,
|
2022-08-26 01:18:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl EthRpcServer for RpcInner {
|
|
|
|
async fn get_balance(&self, address: &str, block: &str) -> Result<String, Error> {
|
2022-08-31 21:40:44 +00:00
|
|
|
let block = convert_err(decode_block(block))?;
|
|
|
|
let address = convert_err(Address::from_str(address))?;
|
|
|
|
let client = self.client.lock().await;
|
|
|
|
let balance = convert_err(client.get_balance(&address, &block).await)?;
|
2022-08-26 01:18:47 +00:00
|
|
|
|
2022-08-31 21:40:44 +00:00
|
|
|
Ok(balance.encode_hex())
|
2022-08-26 01:18:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_transaction_count(&self, address: &str, block: &str) -> Result<String, Error> {
|
2022-08-31 21:40:44 +00:00
|
|
|
let block = convert_err(decode_block(block))?;
|
|
|
|
let address = convert_err(Address::from_str(address))?;
|
|
|
|
let client = self.client.lock().await;
|
|
|
|
let nonce = convert_err(client.get_nonce(&address, &block).await)?;
|
2022-08-26 01:18:47 +00:00
|
|
|
|
2022-08-31 21:40:44 +00:00
|
|
|
Ok(nonce.encode_hex())
|
2022-08-26 01:18:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_code(&self, address: &str, block: &str) -> Result<String, Error> {
|
2022-08-31 21:40:44 +00:00
|
|
|
let block = convert_err(decode_block(block))?;
|
|
|
|
let address = convert_err(Address::from_str(address))?;
|
|
|
|
let client = self.client.lock().await;
|
|
|
|
let code = convert_err(client.get_code(&address, &block).await)?;
|
2022-08-26 01:18:47 +00:00
|
|
|
|
2022-08-31 21:40:44 +00:00
|
|
|
Ok(hex::encode(code))
|
2022-08-26 01:18:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn call(&self, opts: CallOpts, block: &str) -> Result<String, Error> {
|
2022-08-31 21:40:44 +00:00
|
|
|
let block = convert_err(decode_block(block))?;
|
|
|
|
let client = self.client.lock().await;
|
2022-09-01 19:58:45 +00:00
|
|
|
let res = convert_err(client.call(&opts, &block))?;
|
2022-08-31 21:40:44 +00:00
|
|
|
|
2022-09-01 19:58:45 +00:00
|
|
|
Ok(format!("0x{}", hex::encode(res)))
|
2022-08-26 01:18:47 +00:00
|
|
|
}
|
2022-08-27 00:05:12 +00:00
|
|
|
|
2022-08-27 20:43:27 +00:00
|
|
|
async fn estimate_gas(&self, opts: CallOpts) -> Result<String, Error> {
|
2022-08-31 00:31:58 +00:00
|
|
|
let client = self.client.lock().await;
|
2022-09-01 19:58:45 +00:00
|
|
|
let gas = convert_err(client.estimate_gas(&opts))?;
|
|
|
|
|
2022-08-27 20:43:27 +00:00
|
|
|
Ok(u64_to_hex_string(gas))
|
|
|
|
}
|
|
|
|
|
2022-08-31 00:31:58 +00:00
|
|
|
async fn chain_id(&self) -> Result<String, Error> {
|
|
|
|
let client = self.client.lock().await;
|
|
|
|
let id = client.chain_id();
|
2022-08-27 00:05:12 +00:00
|
|
|
Ok(u64_to_hex_string(id))
|
|
|
|
}
|
2022-08-29 15:59:02 +00:00
|
|
|
|
|
|
|
async fn gas_price(&self) -> Result<String, Error> {
|
2022-08-31 00:31:58 +00:00
|
|
|
let client = self.client.lock().await;
|
2022-08-31 21:40:44 +00:00
|
|
|
let gas_price = convert_err(client.get_gas_price())?;
|
2022-08-29 15:59:02 +00:00
|
|
|
Ok(gas_price.encode_hex())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn max_priority_fee_per_gas(&self) -> Result<String, Error> {
|
2022-08-31 00:31:58 +00:00
|
|
|
let client = self.client.lock().await;
|
2022-08-31 21:40:44 +00:00
|
|
|
let tip = convert_err(client.get_priority_fee())?;
|
2022-08-29 15:59:02 +00:00
|
|
|
Ok(tip.encode_hex())
|
|
|
|
}
|
2022-08-29 16:06:50 +00:00
|
|
|
|
|
|
|
async fn block_number(&self) -> Result<String, Error> {
|
2022-08-31 00:31:58 +00:00
|
|
|
let client = self.client.lock().await;
|
2022-08-31 21:40:44 +00:00
|
|
|
let num = convert_err(client.get_block_number())?;
|
2022-08-29 16:06:50 +00:00
|
|
|
Ok(u64_to_hex_string(num))
|
|
|
|
}
|
2022-08-31 21:40:44 +00:00
|
|
|
|
|
|
|
async fn get_block_by_number(
|
|
|
|
&self,
|
|
|
|
block: &str,
|
|
|
|
_full_tx: bool,
|
|
|
|
) -> Result<ExecutionBlock, Error> {
|
|
|
|
let block = convert_err(decode_block(block))?;
|
|
|
|
let client = self.client.lock().await;
|
|
|
|
let block = convert_err(client.get_block_by_number(&block))?;
|
|
|
|
|
|
|
|
Ok(block)
|
|
|
|
}
|
2022-09-01 21:07:30 +00:00
|
|
|
|
|
|
|
async fn send_raw_transaction(&self, bytes: &str) -> Result<String, Error> {
|
|
|
|
let client = self.client.lock().await;
|
|
|
|
let bytes = convert_err(hex_str_to_bytes(bytes))?;
|
|
|
|
let tx_hash = convert_err(client.send_raw_transaction(&bytes).await)?;
|
|
|
|
Ok(hex::encode(tx_hash))
|
|
|
|
}
|
2022-08-31 21:40:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl NetRpcServer for RpcInner {
|
|
|
|
async fn version(&self) -> Result<String, Error> {
|
|
|
|
let client = self.client.lock().await;
|
|
|
|
Ok(client.chain_id().to_string())
|
|
|
|
}
|
2022-08-26 01:18:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn start(rpc: RpcInner) -> Result<(HttpServerHandle, SocketAddr)> {
|
2022-08-29 20:54:58 +00:00
|
|
|
let addr = format!("127.0.0.1:{}", rpc.port);
|
|
|
|
let server = HttpServerBuilder::default().build(addr).await?;
|
2022-08-26 01:18:47 +00:00
|
|
|
|
|
|
|
let addr = server.local_addr()?;
|
2022-08-31 21:40:44 +00:00
|
|
|
|
|
|
|
let mut methods = Methods::new();
|
|
|
|
let eth_methods: Methods = EthRpcServer::into_rpc(rpc.clone()).into();
|
|
|
|
let net_methods: Methods = NetRpcServer::into_rpc(rpc).into();
|
|
|
|
|
|
|
|
methods.merge(eth_methods)?;
|
|
|
|
methods.merge(net_methods)?;
|
|
|
|
|
|
|
|
let handle = server.start(methods)?;
|
2022-08-26 01:18:47 +00:00
|
|
|
|
|
|
|
Ok((handle, addr))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn convert_err<T, E: Display>(res: Result<T, E>) -> Result<T, Error> {
|
2022-08-31 21:40:44 +00:00
|
|
|
res.map_err(|err| {
|
|
|
|
println!("{}", err.to_string());
|
|
|
|
Error::Custom(err.to_string())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn decode_block(block: &str) -> Result<Option<u64>> {
|
|
|
|
match block {
|
|
|
|
"latest" => Ok(None),
|
|
|
|
_ => {
|
|
|
|
if block.starts_with("0x") {
|
|
|
|
Ok(Some(u64::from_str_radix(
|
|
|
|
block.strip_prefix("0x").unwrap(),
|
|
|
|
16,
|
|
|
|
)?))
|
|
|
|
} else {
|
|
|
|
Ok(Some(block.parse()?))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-26 01:18:47 +00:00
|
|
|
}
|