Skip to content
Logo

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 1000000000000000000
Encode with named function
$ cast calldata "approve(address spender, uint256 amount)" $SPENDER $AMOUNT
Encode constructor arguments
$ cast abi-encode "constructor(string,uint256)" "MyToken" 1000000

Decoding calldata

Decode with known signature
$ cast calldata-decode "transfer(address,uint256)" 0xa9059cbb000000000000000000000000...
Decode using 4byte database
$ cast 4byte-decode 0x1F1F897F676d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003e7
1) "fulfillRandomness(bytes32,uint256)"
0x676d000000000000000000000000000000000000000000000000000000000000
999
Look up function selector
$ cast 4byte 0xa9059cbb

ABI encoding primitives

Encode raw ABI data
$ cast abi-encode "foo(uint256,address)" 42 0xRecipient
Decode ABI data
$ cast abi-decode "foo(uint256,address)" 0x000000000000000000000000...
Decode output data
$ cast abi-decode --output "balanceOf(address)(uint256)" 0x0000000000000000000000000000000000000000000000000de0b6b3a7640000

Function selectors

Compute function selector
$ cast sig "transfer(address,uint256)"
Get full signature from selector
$ cast 4byte 0xa9059cbb
Compute 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)" $DATA
Look up event by topic
$ cast 4byte-event 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef

Working with types

Encode bytes
$ cast abi-encode "foo(bytes)" 0xdeadbeef
Encode 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 ether
Format units (e.g., wei to ether)
$ cast from-wei 1500000000000000000
Convert to hex
$ cast to-hex 255
Convert from hex
$ cast to-dec 0xff
Convert to bytes32
$ cast to-bytes32 "Hello"
Format bytes32 as string
$ cast parse-bytes32-string 0x48656c6c6f000000000000000000000000000000000000000000000000000000

Hashing

Keccak256 hash
$ cast keccak "Hello, World!"
Hash raw bytes
$ cast keccak 0xdeadbeef
Compute storage slot for mapping
$ cast index address 0xOwner 0

Interface utilities

Generate interface from ABI
$ cast interface ./out/Counter.sol/Counter.json
Generate interface from Etherscan
$ cast interface 0xContractAddress --etherscan-api-key $KEY
Pretty print ABI
$ cast abi-print ./out/Counter.sol/Counter.json

Was this helpful?