Skip to content

Commit

Permalink
WIP feat: Seth command parity pt. 1 (#14)
Browse files Browse the repository at this point in the history
* Seth: --to-fix

* Seth: block-number

* Seth: basefee

* Seth: chain-id

* Seth: age

* Seth: namehash

* Seth: keccak

* Seth: gas-price

* Seth: chain

* Seth: --to-wei

* Seth: --to-uint256 rough

* Seth: --to-dec

* Seth: --to-ascii

* Quickfix: fixing test types

* Cleanup PR

* Fix return types as Result

* chore: cargo fmt / clippy

Co-authored-by: Georgios Konstantopoulos <[email protected]>
  • Loading branch information
Anish-Agnihotri and gakonst authored Sep 21, 2021
1 parent 2c1f8b8 commit c2a55ba
Show file tree
Hide file tree
Showing 5 changed files with 433 additions and 6 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.

68 changes: 66 additions & 2 deletions dapptools/src/seth.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
mod seth_opts;
use seth_opts::{Opts, Subcommands};

use seth::{Seth, SimpleSeth};

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

Expand All @@ -25,9 +25,33 @@ async fn main() -> eyre::Result<()> {
Subcommands::ToCheckSumAddress { address } => {
println!("{}", SimpleSeth::checksum_address(&address)?);
}
Subcommands::ToAscii { hexdata } => {
println!("{}", SimpleSeth::to_ascii(&hexdata)?);
}
Subcommands::ToBytes32 { bytes } => {
println!("{}", SimpleSeth::bytes32(&bytes)?);
}
Subcommands::ToDec { hexvalue } => {
println!("{}", SimpleSeth::to_dec(&hexvalue)?);
}
Subcommands::ToFix { decimals, value } => {
println!(
"{}",
SimpleSeth::to_fix(unwrap_or_stdin(decimals)?, unwrap_or_stdin(value)?)?
);
}
Subcommands::ToUint256 { value } => {
println!("{}", SimpleSeth::to_uint256(value)?);
}
Subcommands::ToWei { value, unit } => {
println!(
"{}",
SimpleSeth::to_wei(
unwrap_or_stdin(value)?,
unit.unwrap_or_else(|| String::from("wei"))
)?
);
}
Subcommands::Block {
rpc_url,
block,
Expand All @@ -43,6 +67,10 @@ async fn main() -> eyre::Result<()> {
.await?
);
}
Subcommands::BlockNumber { rpc_url } => {
let provider = Provider::try_from(rpc_url)?;
println!("{}", Seth::new(provider).block_number().await?);
}
Subcommands::Call {
rpc_url,
address,
Expand All @@ -52,6 +80,17 @@ async fn main() -> eyre::Result<()> {
let provider = Provider::try_from(rpc_url)?;
println!("{}", Seth::new(provider).call(address, &sig, args).await?);
}
Subcommands::Chain { rpc_url } => {
let provider = Provider::try_from(rpc_url)?;
println!("{}", Seth::new(provider).chain().await?);
}
Subcommands::ChainId { rpc_url } => {
let provider = Provider::try_from(rpc_url)?;
println!("{}", Seth::new(provider).chain_id().await?);
}
Subcommands::Namehash { name } => {
println!("{}", SimpleSeth::namehash(&name)?);
}
Subcommands::SendTx { eth, to, sig, args } => {
let provider = Provider::try_from(eth.rpc_url.as_str())?;
if let Some(signer) = eth.signer()? {
Expand All @@ -63,6 +102,15 @@ 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 All @@ -71,6 +119,22 @@ 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::GasPrice { rpc_url } => {
let provider = Provider::try_from(rpc_url)?;
println!("{}", Seth::new(provider).gas_price().await?);
}
Subcommands::Keccak { data } => {
println!("{}", SimpleSeth::keccak(&data)?);
}
Subcommands::ResolveName {
who,
rpc_url,
Expand Down
67 changes: 67 additions & 0 deletions dapptools/src/seth_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,30 @@ pub enum Subcommands {
#[structopt(name = "--to-checksum-address")]
#[structopt(about = "convert an address to a checksummed format (EIP-55)")]
ToCheckSumAddress { address: Address },
#[structopt(name = "--to-ascii")]
#[structopt(about = "convert hex data to text data")]
ToAscii { hexdata: String },
#[structopt(name = "--to-bytes32")]
#[structopt(about = "left-pads a hex bytes string to 32 bytes)")]
ToBytes32 { bytes: String },
#[structopt(name = "--to-dec")]
#[structopt(about = "convert hex value into decimal number")]
ToDec { hexvalue: String },
#[structopt(name = "--to-fix")]
#[structopt(about = "convert integers into fixed point with specified decimals")]
ToFix {
decimals: Option<u128>,
value: Option<u128>,
},
#[structopt(name = "--to-uint256")]
#[structopt(about = "convert a number into uint256 hex string with 0x prefix")]
ToUint256 { value: String },
#[structopt(name = "--to-wei")]
#[structopt(about = "convert an ETH amount into wei")]
ToWei {
value: Option<u128>,
unit: Option<String>,
},
#[structopt(name = "block")]
#[structopt(
about = "Prints information about <block>. If <field> is given, print only the value of that field"
Expand All @@ -39,6 +60,12 @@ pub enum Subcommands {
#[structopt(long, env = "ETH_RPC_URL")]
rpc_url: String,
},
#[structopt(name = "block-number")]
#[structopt(about = "Prints latest block number")]
BlockNumber {
#[structopt(long, env = "ETH_RPC_URL")]
rpc_url: String,
},
#[structopt(name = "call")]
#[structopt(about = "Perform a local call to <to> without publishing a transaction.")]
Call {
Expand All @@ -49,6 +76,21 @@ pub enum Subcommands {
#[structopt(long, env = "ETH_RPC_URL")]
rpc_url: String,
},
#[structopt(name = "chain")]
#[structopt(about = "Prints symbolic name of current blockchain by checking genesis hash")]
Chain {
#[structopt(long, env = "ETH_RPC_URL")]
rpc_url: String,
},
#[structopt(name = "chain-id")]
#[structopt(about = "returns ethereum chain id")]
ChainId {
#[structopt(long, env = "ETH_RPC_URL")]
rpc_url: String,
},
#[structopt(name = "namehash")]
#[structopt(about = "returns ENS namehash of provided name")]
Namehash { name: String },
#[structopt(name = "send")]
#[structopt(about = "Publish a transaction signed by <from> to call <to> with <data>")]
SendTx {
Expand All @@ -61,6 +103,14 @@ pub enum Subcommands {
#[structopt(flatten)]
eth: EthereumOpts,
},
#[structopt(name = "age")]
#[structopt(about = "Prints 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 All @@ -71,6 +121,23 @@ pub enum Subcommands {
#[structopt(short, long, env = "ETH_RPC_URL")]
rpc_url: String,
},
#[structopt(name = "basefee")]
#[structopt(about = "Print the basefee of a block")]
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 = "gas-price")]
#[structopt(about = "Prints current gas price of target chain")]
GasPrice {
#[structopt(short, long, env = "ETH_RPC_URL")]
rpc_url: String,
},
#[structopt(name = "keccak")]
#[structopt(about = "Keccak-256 hashes arbitrary data")]
Keccak { data: String },
#[structopt(name = "resolve-name")]
#[structopt(about = "Returns the address the provided ENS name resolves to")]
ResolveName {
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"
Loading

0 comments on commit c2a55ba

Please sign in to comment.