Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cast): support short cut for querying erc20 balance in cast balance #6828

Merged
merged 6 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
48 changes: 40 additions & 8 deletions crates/cast/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
extern crate tracing;

use alloy_primitives::{keccak256, Address, B256};
use cast::{Cast, SimpleCast};
use cast::{Cast, SimpleCast, TxBuilder};
use clap::{CommandFactory, Parser};
use clap_complete::generate;
use ethers_core::types::{BlockId, BlockNumber::Latest};
use ethers_core::types::{BlockId, BlockNumber::Latest, NameOrAddress};
use ethers_providers::Middleware;
use eyre::Result;
use foundry_cli::{handler, prompt, stdin, utils};
use foundry_common::{
abi::get_event,
fmt::format_tokens,
fs,
runtime_client::RuntimeClient,
selectors::{
decode_calldata, decode_event_topic, decode_function_selector, import_selectors,
parse_signatures, pretty_calldata, ParsedSignatures, SelectorImportData,
Expand All @@ -26,6 +27,7 @@ pub mod cmd;
pub mod opts;

use opts::{Opts, Subcommands, ToBaseArgs};
type Provider = ethers_providers::Provider<RuntimeClient>;
fenghaojiang marked this conversation as resolved.
Show resolved Hide resolved

#[tokio::main]
async fn main() -> Result<()> {
Expand Down Expand Up @@ -200,14 +202,44 @@ async fn main() -> Result<()> {
Cast::new(provider).age(block.unwrap_or(BlockId::Number(Latest))).await?
);
}
Subcommands::Balance { block, who, ether, rpc } => {
Subcommands::Balance { block, who, ether, rpc, token } => {
let config = Config::from(&rpc);
let provider = utils::get_provider(&config)?;
let value = Cast::new(provider).balance(who, block).await?;
if ether {
println!("{}", SimpleCast::from_wei(&value.to_string(), "eth")?);
} else {
println!("{value}");

match token {
Some(token) => {
let chain = utils::get_chain(config.chain, &provider).await?;
let mut builder: TxBuilder<'_, Provider> = TxBuilder::new(
&provider,
NameOrAddress::Address(Address::ZERO.to_ethers()),
Some(NameOrAddress::Address(token.to_ethers())),
chain,
true,
)
.await?;

let account_addr = match who {
NameOrAddress::Name(ens_name) => provider.resolve_name(&ens_name).await?,
NameOrAddress::Address(addr) => addr,
};

builder
.set_args(
"balanceOf(address) returns (uint256)",
vec![format!("{account_addr:#x}")],
)
.await?;
let builder_output = builder.build();
println!("{}", Cast::new(provider).call(builder_output, block).await?);
}
None => {
let value = Cast::new(provider).balance(who, block).await?;
if ether {
println!("{}", SimpleCast::from_wei(&value.to_string(), "eth")?);
} else {
println!("{value}");
}
}
}
}
Subcommands::BaseFee { block, rpc } => {
Expand Down
4 changes: 4 additions & 0 deletions crates/cast/bin/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,10 @@ pub enum Subcommands {

#[clap(flatten)]
rpc: RpcOpts,

/// Token address to query, with the method `balanceOf(address) return (uint256)`
#[clap(short, long)]
token: Option<Address>,
fenghaojiang marked this conversation as resolved.
Show resolved Hide resolved
fenghaojiang marked this conversation as resolved.
Show resolved Hide resolved
},

/// Get the basefee of a block.
Expand Down