Skip to content

Commit

Permalink
feat(cast): cast codesize (foundry-rs#5184)
Browse files Browse the repository at this point in the history
* feat: cast codesize command

* make ci happy
  • Loading branch information
Sabnock01 authored Jun 20, 2023
1 parent 12ea9f6 commit 6eba7e4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions cast/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
- [x] `chain-id`
- [x] `client`
- [x] `code`
- [x] `codesize`
- [ ] `debug`
- [x] `estimate`
- [x] `etherscan-source`
Expand Down
26 changes: 26 additions & 0 deletions cast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,32 @@ where
}
}

/// Example
///
/// ```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 codesize = cast.codesize(addr, None).await?;
/// println!("{}", codesize);
/// # Ok(())
/// # }
/// ```
pub async fn codesize<T: Into<NameOrAddress> + Send + Sync>(
&self,
who: T,
block: Option<BlockId>,
) -> Result<String> {
let code = self.provider.get_code(who, block).await?.to_vec();
Ok(format!("{}", code.len()))
}

/// # Example
///
/// ```no_run
Expand Down
5 changes: 5 additions & 0 deletions cli/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,11 @@ async fn main() -> eyre::Result<()> {
let provider = utils::get_provider(&config)?;
println!("{}", Cast::new(provider).code(who, block, disassemble).await?);
}
Subcommands::Codesize { block, who, rpc } => {
let config = Config::from(&rpc);
let provider = utils::get_provider(&config)?;
println!("{}", Cast::new(provider).codesize(who, block).await?);
}
Subcommands::ComputeAddress { address, nonce, rpc } => {
let config = Config::from(&rpc);
let provider = utils::get_provider(&config)?;
Expand Down
17 changes: 17 additions & 0 deletions cli/src/opts/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,23 @@ pub enum Subcommands {
rpc: RpcOpts,
},

/// Get the runtime bytecode size of a contract.
#[clap(visible_alias = "cs")]
Codesize {
/// The block height to query at.
///
/// Can also be the tags earliest, finalized, safe, latest, or pending.
#[clap(long, short = 'B')]
block: Option<BlockId>,

/// The contract address.
#[clap(value_parser = NameOrAddress::from_str)]
who: NameOrAddress,

#[clap(flatten)]
rpc: RpcOpts,
},

/// Get the current gas price.
#[clap(name = "gas-price", visible_alias = "g")]
GasPrice {
Expand Down

0 comments on commit 6eba7e4

Please sign in to comment.