helios/client/src/client.rs

133 lines
3.7 KiB
Rust
Raw Normal View History

2022-08-27 00:05:12 +00:00
use std::sync::Arc;
use std::time::Duration;
2022-08-27 00:05:12 +00:00
2022-08-21 13:13:56 +00:00
use ethers::prelude::{Address, U256};
use ethers::types::{Transaction, TransactionReceipt, H256};
2022-08-21 13:13:56 +00:00
use eyre::Result;
2022-08-29 20:54:58 +00:00
use config::Config;
use consensus::types::Header;
use execution::types::{CallOpts, ExecutionBlock};
use log::warn;
use tokio::spawn;
use tokio::sync::Mutex;
use tokio::time::sleep;
use crate::node::Node;
use crate::rpc::Rpc;
2022-08-21 13:13:56 +00:00
pub struct Client {
node: Arc<Mutex<Node>>,
rpc: Option<Rpc>,
2022-08-21 13:13:56 +00:00
}
impl Client {
pub async fn new(config: Config) -> Result<Self> {
let config = Arc::new(config);
let node = Node::new(config.clone()).await?;
let node = Arc::new(Mutex::new(node));
let rpc = if let Some(port) = config.general.rpc_port {
Some(Rpc::new(node.clone(), port))
} else {
None
};
Ok(Client { node, rpc })
}
pub async fn start(&mut self) -> Result<()> {
self.rpc.as_mut().unwrap().start().await?;
self.node.lock().await.sync().await?;
let node = self.node.clone();
spawn(async move {
loop {
let res = node.lock().await.advance().await;
if let Err(err) = res {
warn!("{}", err);
}
2022-09-02 04:13:22 +00:00
sleep(Duration::from_secs(10)).await;
}
});
Ok(())
2022-08-31 00:31:58 +00:00
}
pub async fn call(&self, opts: &CallOpts, block: &Option<u64>) -> Result<Vec<u8>> {
self.node.lock().await.call(opts, block)
2022-08-24 01:33:48 +00:00
}
pub async fn estimate_gas(&self, opts: &CallOpts) -> Result<u64> {
self.node.lock().await.estimate_gas(opts)
2022-08-27 20:43:27 +00:00
}
pub async fn get_balance(&self, address: &Address, block: &Option<u64>) -> Result<U256> {
self.node.lock().await.get_balance(address, block).await
2022-08-21 13:13:56 +00:00
}
pub async fn get_nonce(&self, address: &Address, block: &Option<u64>) -> Result<u64> {
self.node.lock().await.get_nonce(address, block).await
2022-08-21 13:13:56 +00:00
}
pub async fn get_code(&self, address: &Address, block: &Option<u64>) -> Result<Vec<u8>> {
self.node.lock().await.get_code(address, block).await
2022-08-21 16:59:47 +00:00
}
pub async fn get_storage_at(&self, address: &Address, slot: H256) -> Result<U256> {
self.node.lock().await.get_storage_at(address, slot).await
2022-08-21 21:51:11 +00:00
}
pub async fn send_raw_transaction(&self, bytes: &Vec<u8>) -> Result<H256> {
self.node.lock().await.send_raw_transaction(bytes).await
2022-09-01 21:07:30 +00:00
}
pub async fn get_transaction_receipt(
&self,
tx_hash: &H256,
) -> Result<Option<TransactionReceipt>> {
self.node
.lock()
.await
.get_transaction_receipt(tx_hash)
.await
}
pub async fn get_transaction_by_hash(&self, tx_hash: &H256) -> Result<Option<Transaction>> {
self.node
.lock()
.await
.get_transaction_by_hash(tx_hash)
.await
}
pub async fn get_gas_price(&self) -> Result<U256> {
self.node.lock().await.get_gas_price()
2022-08-29 15:59:02 +00:00
}
pub async fn get_priority_fee(&self) -> Result<U256> {
self.node.lock().await.get_priority_fee()
2022-08-29 16:06:50 +00:00
}
pub async fn get_block_number(&self) -> Result<u64> {
self.node.lock().await.get_block_number()
}
pub async fn get_block_by_number(&self, block: &Option<u64>) -> Result<ExecutionBlock> {
self.node.lock().await.get_block_by_number(block)
2022-09-02 04:13:22 +00:00
}
pub async fn get_block_by_hash(&self, hash: &Vec<u8>) -> Result<ExecutionBlock> {
self.node.lock().await.get_block_by_hash(hash)
2022-08-27 00:05:12 +00:00
}
pub async fn chain_id(&self) -> u64 {
self.node.lock().await.chain_id()
2022-08-21 13:13:56 +00:00
}
pub async fn get_header(&self) -> Header {
self.node.lock().await.get_header().clone()
}
2022-08-21 13:13:56 +00:00
}