ABI encoding
Cast encodes and decodes data according to the Ethereum ABI specification.
Encoding calldata
Encode a function call
$ cast calldata "transfer(address,uint256)" 0xRecipient 1000000000000000000Encode with named function
$ cast calldata "approve(address spender, uint256 amount)" $SPENDER $AMOUNTEncode constructor arguments
$ cast abi-encode "constructor(string,uint256)" "MyToken" 1000000Decoding calldata
Decode with known signature
$ cast calldata-decode "transfer(address,uint256)" 0xa9059cbb000000000000000000000000...Decode using 4byte database
$ cast 4byte-decode 0x1F1F897F676d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003e71) "fulfillRandomness(bytes32,uint256)"
0x676d000000000000000000000000000000000000000000000000000000000000
999Look up function selector
$ cast 4byte 0xa9059cbbABI encoding primitives
Encode raw ABI data
$ cast abi-encode "foo(uint256,address)" 42 0xRecipientDecode ABI data
$ cast abi-decode "foo(uint256,address)" 0x000000000000000000000000...Decode output data
$ cast abi-decode --output "balanceOf(address)(uint256)" 0x0000000000000000000000000000000000000000000000000de0b6b3a7640000Function selectors
Compute function selector
$ cast sig "transfer(address,uint256)"Get full signature from selector
$ cast 4byte 0xa9059cbbCompute event topic
$ cast sig-event "Transfer(address indexed,address indexed,uint256)"Decoding events
Decode event data
$ cast abi-decode --event "Transfer(address indexed from, address indexed to, uint256 value)" $DATALook up event by topic
$ cast 4byte-event 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efWorking with types
Encode bytes
$ cast abi-encode "foo(bytes)" 0xdeadbeefEncode dynamic array
$ cast abi-encode "foo(uint256[])" "[1,2,3]"Encode tuple
$ cast abi-encode "foo((uint256,address))" "(42,0xRecipient)"Encode string
$ cast abi-encode "foo(string)" "Hello, World!"Type conversions
Parse units (e.g., ether to wei)
$ cast to-wei 1.5 etherFormat units (e.g., wei to ether)
$ cast from-wei 1500000000000000000Convert to hex
$ cast to-hex 255Convert from hex
$ cast to-dec 0xffConvert to bytes32
$ cast to-bytes32 "Hello"Format bytes32 as string
$ cast parse-bytes32-string 0x48656c6c6f000000000000000000000000000000000000000000000000000000Hashing
Keccak256 hash
$ cast keccak "Hello, World!"Hash raw bytes
$ cast keccak 0xdeadbeefCompute storage slot for mapping
$ cast index address 0xOwner 0Interface utilities
Generate interface from ABI
$ cast interface ./out/Counter.sol/Counter.jsonGenerate interface from Etherscan
$ cast interface 0xContractAddress --etherscan-api-key $KEYPretty print ABI
$ cast abi-print ./out/Counter.sol/Counter.jsonWas this helpful?
