diff --git a/cast/src/lib.rs b/cast/src/lib.rs index be2614f77cfb0..099e5d46aa874 100644 --- a/cast/src/lib.rs +++ b/cast/src/lib.rs @@ -285,6 +285,29 @@ where pub async fn gas_price(&self) -> Result { 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::::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 + Send + Sync>( + &self, + who: T, + block: Option, + ) -> Result { + Ok(format!("{}", self.provider.get_code(who, block).await?)) + } } pub struct SimpleCast; diff --git a/cli/src/cast.rs b/cli/src/cast.rs index aa1089303362c..cf13ebfc3270d 100644 --- a/cli/src/cast.rs +++ b/cli/src/cast.rs @@ -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)?); } diff --git a/cli/src/cast_opts.rs b/cli/src/cast_opts.rs index 69c0d25b6dd90..40672260c7248 100644 --- a/cli/src/cast_opts.rs +++ b/cli/src/cast_opts.rs @@ -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
")] + 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, + #[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 {