Skip to content

Commit

Permalink
feat: Add block id to cast (foundry-rs#8074)
Browse files Browse the repository at this point in the history
* add blockId to cast

* Update opts.rs

* Update main.rs

* Update main.rs

* update

* tests

---------

Co-authored-by: DaniPopes <[email protected]>
  • Loading branch information
DoTheBestToGetTheBest and DaniPopes authored Jun 8, 2024
1 parent 0b03a58 commit 91d68ac
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
14 changes: 12 additions & 2 deletions crates/cast/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,20 @@ async fn main() -> Result<()> {
.await?
);
}
CastSubcommand::BlockNumber { rpc } => {
CastSubcommand::BlockNumber { rpc, block } => {
let config = Config::from(&rpc);
let provider = utils::get_provider(&config)?;
println!("{}", Cast::new(provider).block_number().await?);
let number = match block {
Some(id) => provider
.get_block(id, false)
.await?
.ok_or_else(|| eyre::eyre!("block {id:?} not found"))?
.header
.number
.ok_or_else(|| eyre::eyre!("block {id:?} has no block number"))?,
None => Cast::new(provider).block_number().await?,
};
println!("{number}");
}
CastSubcommand::Chain { rpc } => {
let config = Config::from(&rpc);
Expand Down
2 changes: 2 additions & 0 deletions crates/cast/bin/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ pub enum CastSubcommand {
/// Get the latest block number.
#[command(visible_alias = "bn")]
BlockNumber {
/// The hash or tag to query. If not specified, the latest number is returned.
block: Option<BlockId>,
#[command(flatten)]
rpc: RpcOpts,
},
Expand Down
25 changes: 25 additions & 0 deletions crates/cast/tests/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -901,3 +901,28 @@ casttest!(index7201, |_prj, cmd| {
casttest!(index7201_unknown_formula_id, |_prj, cmd| {
cmd.args(["index-7201", "test", "--formula-id", "unknown"]).assert_err();
});

casttest!(block_number, |_prj, cmd| {
let eth_rpc_url = next_http_rpc_endpoint();
let s = cmd.args(["block-number", "--rpc-url", eth_rpc_url.as_str()]).stdout_lossy();
assert!(s.trim().parse::<u64>().unwrap() > 0, "{s}")
});

casttest!(block_number_latest, |_prj, cmd| {
let eth_rpc_url = next_http_rpc_endpoint();
let s = cmd.args(["block-number", "--rpc-url", eth_rpc_url.as_str(), "latest"]).stdout_lossy();
assert!(s.trim().parse::<u64>().unwrap() > 0, "{s}")
});

casttest!(block_number_hash, |_prj, cmd| {
let eth_rpc_url = next_http_rpc_endpoint();
let s = cmd
.args([
"block-number",
"--rpc-url",
eth_rpc_url.as_str(),
"0x88e96d4537bea4d9c05d12549907b32561d3bf31f45aae734cdc119f13406cb6",
])
.stdout_lossy();
assert_eq!(s.trim().parse::<u64>().unwrap(), 1, "{s}")
});

0 comments on commit 91d68ac

Please sign in to comment.