Skip to content

Commit

Permalink
Seth: age
Browse files Browse the repository at this point in the history
  • Loading branch information
Anish-Agnihotri committed Sep 20, 2021
1 parent 5470aad commit 91000f7
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 33 deletions.
47 changes: 45 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions dapptools/src/seth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ async fn main() -> eyre::Result<()> {
seth_send(provider, from, to, sig, args, eth.seth_async).await?;
}
}
Subcommands::Age { block, rpc_url } => {
let provider = Provider::try_from(rpc_url)?;
println!("{}", Seth::new(provider).age(block.unwrap_or(BlockId::Number(Latest))).await?);
}
Subcommands::Balance {
block,
who,
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 @@ -75,6 +75,14 @@ pub enum Subcommands {
#[structopt(flatten)]
eth: EthereumOpts,
},
#[structopt(name = "age")]
#[structopt(about = "Print the timestamp of a block")]
Age {
#[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 = "balance")]
#[structopt(about = "Print the balance of <account> in wei")]
Balance {
Expand Down
1 change: 1 addition & 0 deletions seth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ ethers-providers = { git = "https://github.com/gakonst/ethers-rs", branch = "mas
eyre = "0.6.5"
rustc-hex = "2.1.0"
serde_json = "1.0.67"
chrono = "0.2"
72 changes: 41 additions & 31 deletions seth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use ethers_providers::{Middleware, PendingTransaction};
use eyre::Result;
use rustc_hex::ToHex;
use std::str::FromStr;
use chrono::NaiveDateTime;

use dapp_utils::{encode_args, get_func, to_table};

Expand Down Expand Up @@ -134,37 +135,6 @@ 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 Expand Up @@ -227,6 +197,46 @@ where
Ok(block)
}

async fn block_field_as_num<T: Into<BlockId>>(
&self, block: T,
field: String
) -> Result<U256> {
let block = block.into();
let base_fee_hex = Seth::block(
&self,
block,
false,
// Select only select field
Some(field),
false
).await?;
Ok(U256::from_str_radix(
strip_0x(&base_fee_hex),
16
).expect("Unable to convert hexadecimal to U256"))
}

pub async fn base_fee<T: Into<BlockId>>(&self, block: T) -> Result<U256> {
Ok(Seth::block_field_as_num(
&self,
block,
String::from("baseFeePerGas")
).await?)
}

pub async fn age<T: Into<BlockId>>(&self, block: T) -> Result<String> {
let timestamp_str = Seth::block_field_as_num(
&self,
block,
String::from("timestamp")
).await?.to_string();
let datetime = NaiveDateTime::from_timestamp(
timestamp_str.parse::<i64>().unwrap(),
0
);
Ok(datetime.format("%a %b %e %H:%M:%S %Y").to_string())
}

pub async fn chain_id(&self) -> Result<U256> {
Ok(self.provider.get_chainid().await?)
}
Expand Down

0 comments on commit 91000f7

Please sign in to comment.