Skip to content

Commit

Permalink
add ethereum utils
Browse files Browse the repository at this point in the history
  • Loading branch information
keyleu committed Jan 13, 2025
1 parent e2801c6 commit 912639c
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 11 deletions.
24 changes: 13 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
[package]
name = "localic-utils"
name = "localic-utils"
version = "0.1.0"
edition = "2021"

[dependencies]
localic-std = { git = "https://github.com/strangelove-ventures/interchaintest", branch = "main" }
cosmwasm-std = "1.5.5"
serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.120"
thiserror = "1.0.61"
localic-std = { git = "https://github.com/strangelove-ventures/interchaintest", branch = "main" }
cosmwasm-std = "1.5.5"
serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.120"
thiserror = "1.0.61"
derive_builder = "0.20.0"
log = "0.4.22"
astroport = "5.1.0"
reqwest = { version = "0.11.20", features = ["rustls-tls"] }
sha2 = "0.10.8"
log = "0.4.22"
astroport = "5.1.0"
reqwest = { version = "0.11.20", features = ["rustls-tls"] }
sha2 = "0.10.8"
alloy = { version = "0.9.2", features = ["full"] }
tokio = "1.40.0"

[dev-dependencies]
env_logger = "0.11.3"
hex = "0.4.3"
hex = "0.4.3"
90 changes: 90 additions & 0 deletions src/utils/ethereum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use alloy::{
consensus::Account,
network::Ethereum,
primitives::{Address, TxHash, U256},
providers::{
fillers::{BlobGasFiller, ChainIdFiller, FillProvider, GasFiller, JoinFill, NonceFiller},
Identity, Provider, ProviderBuilder, RootProvider,
},
rpc::types::{Transaction, TransactionReceipt, TransactionRequest},
transports::http::{reqwest::Url, Client, Http},
};
use std::error::Error;
use tokio::runtime::Runtime;

// Define the individual fillers in a nested structure
type BaseFillChain = JoinFill<NonceFiller, ChainIdFiller>;
type WithBlobGas = JoinFill<BlobGasFiller, BaseFillChain>;
type WithGas = JoinFill<GasFiller, WithBlobGas>;
type AllFillers = JoinFill<Identity, WithGas>;

/// Helper client to interact with Ethereum in a synchronous way.
pub struct EthClient {
pub provider: FillProvider<AllFillers, RootProvider<Http<Client>>, Http<Client>, Ethereum>,
pub rt: Runtime,
}

impl EthClient {
pub fn new(url: &str) -> Result<Self, Box<dyn Error>> {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()?;

let url = Url::parse(url)?;
let provider = ProviderBuilder::new()
// Adds the `ChainIdFiller`, `GasFiller` and the `NonceFiller` layers.
// This is the recommended way to set up the provider.
.with_recommended_fillers()
.on_http(url);

Ok(Self { provider, rt })
}

pub fn get_block_number(&self) -> Result<u64, Box<dyn Error>> {
let number = self.rt.block_on(self.provider.get_block_number())?;
Ok(number)
}

pub fn get_balance(&self, address: Address) -> Result<U256, Box<dyn Error>> {
let balance = self
.rt
.block_on(async { self.provider.get_balance(address).await })?;
Ok(balance)
}

pub fn get_accounts_addresses(&self) -> Result<Vec<Address>, Box<dyn Error>> {
let accounts = self
.rt
.block_on(async { self.provider.get_accounts().await })?;
Ok(accounts)
}

pub fn get_account(&self, address: Address) -> Result<Account, Box<dyn Error>> {
let account = self
.rt
.block_on(async { self.provider.get_account(address).await })?;
Ok(account)
}

pub fn send_transaction(
&self,
tx: TransactionRequest,
) -> Result<TransactionReceipt, Box<dyn Error>> {
self.rt.block_on(async {
let tx_hash = self.provider.send_transaction(tx).await?;
let receipt = tx_hash.get_receipt().await?;

Ok(receipt)
})
}

pub fn get_transaction_by_hash(
&self,
tx_hash: TxHash,
) -> Result<Option<Transaction>, Box<dyn Error>> {
let tx = self
.rt
.block_on(async { self.provider.get_transaction_by_hash(tx_hash).await })?;
Ok(tx)
}
}
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod ethereum;
pub mod fs;
pub mod queries;
pub mod setup;
Expand Down

0 comments on commit 912639c

Please sign in to comment.