use std::sync::Arc; use ethers::prelude::{Address, U256}; use eyre::Result; use crate::consensus::types::Header; use crate::consensus::ConsensusClient; use crate::execution::evm::Evm; use crate::execution::ExecutionClient; pub struct Client { consensus: ConsensusClient, execution: Arc, } impl Client { pub async fn new( consensus_rpc: &str, execution_rpc: &str, checkpoint_hash: &str, ) -> Result { let consensus = ConsensusClient::new(consensus_rpc, checkpoint_hash).await?; let execution = ExecutionClient::new(execution_rpc); Ok(Client { consensus, execution: Arc::new(execution), }) } pub async fn sync(&mut self) -> Result<()> { self.consensus.sync().await } pub async fn call(&self, to: &Address, calldata: &Vec, value: U256) -> Result> { let payload = self.consensus.get_execution_payload().await?; let mut evm = Evm::new(self.execution.clone(), payload); evm.call(to, calldata, value) } pub async fn get_balance(&self, address: &Address) -> Result { let payload = self.consensus.get_execution_payload().await?; let account = self.execution.get_account(&address, None, &payload).await?; Ok(account.balance) } pub async fn get_nonce(&self, address: &Address) -> Result { let payload = self.consensus.get_execution_payload().await?; let account = self.execution.get_account(&address, None, &payload).await?; Ok(account.nonce) } pub async fn get_code(&self, address: &Address) -> Result> { let payload = self.consensus.get_execution_payload().await?; self.execution.get_code(&address, &payload).await } pub async fn get_storage_at(&self, address: &Address, slot: U256) -> Result { 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")), } } pub fn get_header(&self) -> &Header { self.consensus.get_head() } }