Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions cast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,29 @@ where
pub async fn gas_price(&self) -> Result<U256> {
Ok(self.provider.get_gas_price().await?)
}

/// ```no_run
/// use cast::Cast;
/// use ethers_providers::{Provider, Http};
/// use ethers_core::types::Address;
/// use std::{str::FromStr, convert::TryFrom};
///
/// # async fn foo() -> eyre::Result<()> {
/// let provider = Provider::<Http>::try_from("http://localhost:8545")?;
/// let cast = Cast::new(provider);
/// let addr = Address::from_str("0x00000000219ab540356cbb839cbe05303d7705fa")?;
/// let code = cast.code(addr, None).await?;
/// println!("{}", code);
/// # Ok(())
/// # }
/// ```
pub async fn code<T: Into<NameOrAddress> + Send + Sync>(
&self,
who: T,
block: Option<BlockId>,
) -> Result<String> {
Ok(format!("{}", self.provider.get_code(who, block).await?))
}
}

pub struct SimpleCast;
Expand Down
4 changes: 4 additions & 0 deletions cli/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ async fn main() -> eyre::Result<()> {
let provider = Provider::try_from(rpc_url)?;
println!("{}", Cast::new(provider).chain_id().await?);
}
Subcommands::Code { block, who, rpc_url } => {
let provider = Provider::try_from(rpc_url)?;
println!("{}", Cast::new(provider).code(who, block).await?);
}
Subcommands::Namehash { name } => {
println!("{}", SimpleCast::namehash(&name)?);
}
Expand Down
10 changes: 10 additions & 0 deletions cli/src/cast_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ pub enum Subcommands {
#[structopt(short, long, env = "ETH_RPC_URL")]
rpc_url: String,
},
#[structopt(name = "code")]
#[structopt(about = "Prints the bytecode at <address>")]
Code {
#[structopt(long, short, help = "the block you want to query, can also be earliest/latest/pending", parse(try_from_str = parse_block_id))]
block: Option<BlockId>,
#[structopt(help = "the address you want to query", parse(try_from_str = parse_name_or_address))]
who: NameOrAddress,
#[structopt(short, long, env = "ETH_RPC_URL")]
rpc_url: String,
},
#[structopt(name = "gas-price")]
#[structopt(about = "Prints current gas price of target chain")]
GasPrice {
Expand Down