2022-08-27 00:05:12 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-08-21 13:13:56 +00:00
|
|
|
use ethers::prelude::{Address, U256};
|
|
|
|
use eyre::Result;
|
|
|
|
|
2022-08-27 00:05:12 +00:00
|
|
|
use crate::common::config::Config;
|
2022-08-21 15:21:50 +00:00
|
|
|
use crate::consensus::types::Header;
|
|
|
|
use crate::consensus::ConsensusClient;
|
2022-08-24 01:33:48 +00:00
|
|
|
use crate::execution::evm::Evm;
|
2022-08-26 01:18:47 +00:00
|
|
|
use crate::execution::ExecutionClient;
|
2022-08-21 13:13:56 +00:00
|
|
|
|
|
|
|
pub struct Client {
|
|
|
|
consensus: ConsensusClient,
|
2022-08-26 22:13:35 +00:00
|
|
|
execution: ExecutionClient,
|
2022-08-27 00:05:12 +00:00
|
|
|
config: Arc<Config>,
|
2022-08-21 13:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Client {
|
2022-08-27 00:05:12 +00:00
|
|
|
pub async fn new(config: Arc<Config>) -> Result<Self> {
|
|
|
|
let consensus_rpc = &config.general.consensus_rpc;
|
|
|
|
let checkpoint_hash = &config.general.checkpoint;
|
|
|
|
let execution_rpc = &config.general.execution_rpc;
|
|
|
|
|
|
|
|
let consensus =
|
|
|
|
ConsensusClient::new(consensus_rpc, checkpoint_hash, config.clone()).await?;
|
2022-08-21 13:13:56 +00:00
|
|
|
let execution = ExecutionClient::new(execution_rpc);
|
|
|
|
|
|
|
|
Ok(Client {
|
|
|
|
consensus,
|
2022-08-26 22:13:35 +00:00
|
|
|
execution,
|
2022-08-27 00:05:12 +00:00
|
|
|
config,
|
2022-08-21 13:13:56 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn sync(&mut self) -> Result<()> {
|
|
|
|
self.consensus.sync().await
|
|
|
|
}
|
|
|
|
|
2022-08-26 01:18:47 +00:00
|
|
|
pub async fn call(&self, to: &Address, calldata: &Vec<u8>, value: U256) -> Result<Vec<u8>> {
|
2022-08-24 01:33:48 +00:00
|
|
|
let payload = self.consensus.get_execution_payload().await?;
|
|
|
|
let mut evm = Evm::new(self.execution.clone(), payload);
|
|
|
|
evm.call(to, calldata, value)
|
|
|
|
}
|
|
|
|
|
2022-08-26 01:18:47 +00:00
|
|
|
pub async fn get_balance(&self, address: &Address) -> Result<U256> {
|
2022-08-21 13:13:56 +00:00
|
|
|
let payload = self.consensus.get_execution_payload().await?;
|
2022-08-21 21:51:11 +00:00
|
|
|
let account = self.execution.get_account(&address, None, &payload).await?;
|
2022-08-21 13:13:56 +00:00
|
|
|
Ok(account.balance)
|
|
|
|
}
|
|
|
|
|
2022-08-26 01:18:47 +00:00
|
|
|
pub async fn get_nonce(&self, address: &Address) -> Result<U256> {
|
2022-08-21 13:13:56 +00:00
|
|
|
let payload = self.consensus.get_execution_payload().await?;
|
2022-08-21 21:51:11 +00:00
|
|
|
let account = self.execution.get_account(&address, None, &payload).await?;
|
2022-08-21 13:13:56 +00:00
|
|
|
Ok(account.nonce)
|
|
|
|
}
|
|
|
|
|
2022-08-26 01:18:47 +00:00
|
|
|
pub async fn get_code(&self, address: &Address) -> Result<Vec<u8>> {
|
2022-08-21 16:59:47 +00:00
|
|
|
let payload = self.consensus.get_execution_payload().await?;
|
|
|
|
self.execution.get_code(&address, &payload).await
|
|
|
|
}
|
|
|
|
|
2022-08-26 01:18:47 +00:00
|
|
|
pub async fn get_storage_at(&self, address: &Address, slot: U256) -> Result<U256> {
|
2022-08-21 21:51:11 +00:00
|
|
|
let payload = self.consensus.get_execution_payload().await?;
|
|
|
|
let account = self
|
|
|
|
.execution
|
|
|
|
.get_account(address, Some(&[slot]), &payload)
|
|
|
|
.await?;
|
|
|
|
let value = account.slots.get(&slot);
|
|
|
|
match value {
|
|
|
|
Some(value) => Ok(*value),
|
|
|
|
None => Err(eyre::eyre!("Slot Not Found")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-27 00:05:12 +00:00
|
|
|
pub fn chain_id(&self) -> u64 {
|
|
|
|
self.config.general.chain_id
|
|
|
|
}
|
|
|
|
|
2022-08-21 13:13:56 +00:00
|
|
|
pub fn get_header(&self) -> &Header {
|
|
|
|
self.consensus.get_head()
|
|
|
|
}
|
|
|
|
}
|