← 返回命令列表

Linux command

cast 命令

网络

复制后可按需替换文件名、目录或参数。

常用示例

Get the balance

cast balance [0x...address] --rpc-url [https://eth-mainnet.g.alchemy.com/v2/key]

Call a contract function

cast call [0x...contract] "balanceOf(address)" [0x...holder] --rpc-url [rpc_url]

Send a transaction

cast send [0x...contract] "transfer(address,uint256)" [0x...to] [amount] --rpc-url [rpc_url] --private-key [0x...]

Decode transaction input data

cast 4byte-decode [0x23b872dd...]

Convert wei to ether

cast from-wei [1000000000000000000]

Convert ether to wei

cast to-wei [1.5]

Get current gas price

cast gas-price --rpc-url [rpc_url]

Decode ABI-encoded data

cast abi-decode "transfer(address,uint256)" [0x...]

Lookup function signature

cast sig "transfer(address,uint256)"

说明

cast is a command-line tool for interacting with Ethereum and EVM-compatible blockchains. It's part of the Foundry toolkit and provides utilities for querying blockchain state, sending transactions, encoding/decoding data, and performing conversions. Cast can interact with deployed contracts by calling view functions or sending state-changing transactions. It handles ABI encoding/decoding, making it easy to work with smart contracts from the command line. The tool also provides utilities for working with Ethereum data formats, including conversions between wei/ether, hex/decimal, and various encoding schemes. It integrates with Etherscan-like services for signature lookups.

参数

call _contract_ _sig_ _args_
Call a contract function (read-only).
send _contract_ _sig_ _args_
Send a transaction to a contract.
balance _address_
Get ETH balance of address.
block _number_
Get block information.
tx _hash_
Get transaction details.
receipt _hash_
Get transaction receipt.
gas-price
Get current gas price.
abi-encode _sig_ _args_
ABI encode function call data.
abi-decode _sig_ _data_
Decode ABI-encoded data.
sig _signature_
Get function selector (4 bytes).
4byte-decode _selector_
Look up function signature.
to-wei _amount_
Convert ether to wei.
from-wei _amount_
Convert wei to ether.
--rpc-url _url_
Ethereum RPC endpoint.
--private-key _key_
Private key for signing.
--chain _chain_
Chain name or ID.

FAQ

What is the cast command used for?

cast is a command-line tool for interacting with Ethereum and EVM-compatible blockchains. It's part of the Foundry toolkit and provides utilities for querying blockchain state, sending transactions, encoding/decoding data, and performing conversions. Cast can interact with deployed contracts by calling view functions or sending state-changing transactions. It handles ABI encoding/decoding, making it easy to work with smart contracts from the command line. The tool also provides utilities for working with Ethereum data formats, including conversions between wei/ether, hex/decimal, and various encoding schemes. It integrates with Etherscan-like services for signature lookups.

How do I run a basic cast example?

Run `cast balance [0x...address] --rpc-url [https://eth-mainnet.g.alchemy.com/v2/key]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does call _contract_ _sig_ _args_ do in cast?

Call a contract function (read-only).