Skip to content

Commit

Permalink
Seth: basefee
Browse files Browse the repository at this point in the history
  • Loading branch information
Anish-Agnihotri committed Sep 20, 2021
1 parent 072670a commit 08a856a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
16 changes: 9 additions & 7 deletions dapptools/src/seth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ mod seth_opts;
use seth_opts::{Opts, Subcommands};

use seth::{Seth, SimpleSeth};

use ethers::{
middleware::SignerMiddleware,
providers::{Middleware, Provider},
signers::Signer,
types::NameOrAddress,
};
use ethers::{core::types::{BlockId, BlockNumber::{Latest}}, middleware::SignerMiddleware, providers::{Middleware, Provider}, signers::Signer, types::NameOrAddress};
use std::{convert::TryFrom, str::FromStr};
use structopt::StructOpt;

Expand Down Expand Up @@ -78,6 +72,14 @@ async fn main() -> eyre::Result<()> {
let provider = Provider::try_from(rpc_url)?;
println!("{}", Seth::new(provider).balance(who, block).await?);
}
Subcommands::BaseFee {
block,
rpc_url,
} => {
let provider = Provider::try_from(rpc_url)?;
println!("{}", Seth::new(provider).base_fee(block.unwrap_or(BlockId::Number(Latest))).await?);
}

Subcommands::ResolveName {
who,
rpc_url,
Expand Down
8 changes: 8 additions & 0 deletions dapptools/src/seth_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ pub enum Subcommands {
#[structopt(short, long, env = "ETH_RPC_URL")]
rpc_url: String,
},
#[structopt(name = "basefee")]
#[structopt(about = "Print the block basefee")]
BaseFee {
#[structopt(global = true, help = "the block you want to query, can also be earliest/latest/pending", parse(try_from_str = parse_block_id))]
block: Option<BlockId>,
#[structopt(short, long, env = "ETH_RPC_URL")]
rpc_url: String,
},
#[structopt(name = "resolve-name")]
#[structopt(about = "Returns the address the provided ENS name resolves to")]
ResolveName {
Expand Down
31 changes: 31 additions & 0 deletions seth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,37 @@ where
Ok::<_, eyre::Error>(res)
}

/// ```no_run
/// use seth::Seth;
/// use ethers_providers::{Provider, Http};
/// use std::convert::TryFrom;
///
/// # async fn foo() -> eyre::Result<()> {
/// let provider = Provider::<Http>::try_from("http://localhost:8545")?;
/// let seth = Seth::new(provider);
/// let base_fee = seth.base_fee(13_000_000).await?;
/// println!("{}", base_fee);
/// # Ok(())
/// # }
/// ```
pub async fn base_fee<T: Into<BlockId>>(&self, block: T) -> Result<U256> {
let block = block.into();
let base_fee_hex = Seth::block(
&self,
block,
false,
// Select only baseFee field
Some(String::from("baseFeePerGas")),
false
).await?;
let base_fee_num = U256::from_str_radix(
strip_0x(&base_fee_hex),
16
).expect("Unable to convert baseFee hexadecimal to U256");

Ok(base_fee_num)
}

/// ```no_run
/// use seth::Seth;
/// use ethers_providers::{Provider, Http};
Expand Down

0 comments on commit 08a856a

Please sign in to comment.