2022-09-22 19:40:06 +00:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
2022-09-08 21:46:48 +00:00
|
|
|
use async_trait::async_trait;
|
2022-09-06 17:57:47 +00:00
|
|
|
use ethers::prelude::{Address, Http};
|
2022-09-22 19:40:06 +00:00
|
|
|
use ethers::providers::{HttpRateLimitRetryPolicy, Middleware, Provider, RetryClient};
|
|
|
|
use ethers::types::transaction::eip2718::TypedTransaction;
|
|
|
|
use ethers::types::transaction::eip2930::AccessList;
|
|
|
|
use ethers::types::{
|
|
|
|
BlockId, Bytes, EIP1186ProofResponse, Eip1559TransactionRequest, Transaction,
|
|
|
|
TransactionReceipt, H256, U256,
|
|
|
|
};
|
2022-08-21 15:21:50 +00:00
|
|
|
use eyre::Result;
|
2022-09-22 19:40:06 +00:00
|
|
|
use log::trace;
|
|
|
|
|
|
|
|
use crate::types::CallOpts;
|
2022-08-21 15:21:50 +00:00
|
|
|
|
2022-09-08 21:46:48 +00:00
|
|
|
use super::Rpc;
|
|
|
|
|
|
|
|
pub struct HttpRpc {
|
2022-09-22 19:40:06 +00:00
|
|
|
url: String,
|
|
|
|
provider: Provider<RetryClient<Http>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Clone for HttpRpc {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self::new(&self.url).unwrap()
|
|
|
|
}
|
2022-08-21 15:21:50 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 21:46:48 +00:00
|
|
|
#[async_trait]
|
|
|
|
impl Rpc for HttpRpc {
|
|
|
|
fn new(rpc: &str) -> Result<Self> {
|
2022-09-22 19:40:06 +00:00
|
|
|
let http = Http::from_str(rpc)?;
|
2022-09-22 19:42:47 +00:00
|
|
|
let mut client = RetryClient::new(http, Box::new(HttpRateLimitRetryPolicy), 100, 10);
|
|
|
|
client.set_compute_units(250);
|
2022-09-22 19:40:06 +00:00
|
|
|
let provider = Provider::new(client);
|
|
|
|
Ok(HttpRpc {
|
|
|
|
url: rpc.to_string(),
|
|
|
|
provider,
|
|
|
|
})
|
2022-08-21 15:21:50 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 21:46:48 +00:00
|
|
|
async fn get_proof(
|
2022-09-06 17:57:47 +00:00
|
|
|
&self,
|
|
|
|
address: &Address,
|
|
|
|
slots: &[H256],
|
|
|
|
block: u64,
|
|
|
|
) -> Result<EIP1186ProofResponse> {
|
2022-09-22 19:40:06 +00:00
|
|
|
trace!("fetching proof");
|
2022-09-06 17:57:47 +00:00
|
|
|
let block = Some(BlockId::from(block));
|
|
|
|
let proof_response = self
|
|
|
|
.provider
|
|
|
|
.get_proof(*address, slots.to_vec(), block)
|
|
|
|
.await?;
|
|
|
|
Ok(proof_response)
|
2022-08-21 15:21:50 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 19:40:06 +00:00
|
|
|
async fn create_access_list(&self, opts: &CallOpts, block: u64) -> Result<AccessList> {
|
|
|
|
let block = Some(BlockId::from(block));
|
|
|
|
|
2022-09-24 00:37:28 +00:00
|
|
|
let mut raw_tx = Eip1559TransactionRequest::new();
|
|
|
|
raw_tx.to = Some(opts.to.into());
|
|
|
|
raw_tx.from = opts.from;
|
|
|
|
raw_tx.value = opts.value;
|
|
|
|
raw_tx.gas = Some(opts.gas.unwrap_or(U256::from(100_000_000)));
|
|
|
|
raw_tx.max_fee_per_gas = Some(U256::zero());
|
|
|
|
raw_tx.max_priority_fee_per_gas = Some(U256::zero());
|
|
|
|
raw_tx.data = opts
|
2022-09-22 19:40:06 +00:00
|
|
|
.data
|
|
|
|
.as_ref()
|
|
|
|
.map(|data| Bytes::from(data.as_slice().to_owned()));
|
|
|
|
|
2022-09-24 00:37:28 +00:00
|
|
|
let tx = TypedTransaction::Eip1559(raw_tx);
|
2022-09-22 19:40:06 +00:00
|
|
|
let list = self.provider.create_access_list(&tx, block).await?;
|
|
|
|
|
|
|
|
Ok(list.access_list)
|
|
|
|
}
|
|
|
|
|
2022-09-08 21:46:48 +00:00
|
|
|
async fn get_code(&self, address: &Address, block: u64) -> Result<Vec<u8>> {
|
2022-09-06 17:57:47 +00:00
|
|
|
let block = Some(BlockId::from(block));
|
|
|
|
let code = self.provider.get_code(*address, block).await?;
|
|
|
|
Ok(code.to_vec())
|
2022-08-21 16:59:47 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 21:46:48 +00:00
|
|
|
async fn send_raw_transaction(&self, bytes: &Vec<u8>) -> Result<H256> {
|
2022-09-06 17:57:47 +00:00
|
|
|
let bytes = Bytes::from(bytes.as_slice().to_owned());
|
|
|
|
Ok(self.provider.send_raw_transaction(bytes).await?.tx_hash())
|
2022-09-01 21:07:30 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 21:46:48 +00:00
|
|
|
async fn get_transaction_receipt(&self, tx_hash: &H256) -> Result<Option<TransactionReceipt>> {
|
2022-09-06 17:57:47 +00:00
|
|
|
let receipt = self.provider.get_transaction_receipt(*tx_hash).await?;
|
|
|
|
Ok(receipt)
|
2022-09-02 03:28:37 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 21:46:48 +00:00
|
|
|
async fn get_transaction(&self, tx_hash: &H256) -> Result<Option<Transaction>> {
|
2022-09-06 17:57:47 +00:00
|
|
|
Ok(self.provider.get_transaction(*tx_hash).await?)
|
2022-08-21 16:59:47 +00:00
|
|
|
}
|
|
|
|
}
|