Decode ABI Parameters
Decode raw ABI-encoded bytes using parameter definitions without a parsed contract ABI in viem-go
DecodeAbiParameters (and DecodeAbiParametersInto, DecodeWithSelector) decode ABI-encoded bytes using a list of AbiParam definitions. They are standalone functions: no parsed ABI required.
Import
import ("encoding/hex""github.com/ChefBingbong/viem-go/abi")
DecodeAbiParameters
Decode raw ABI-encoded bytes using AbiParam definitions. Returns a slice of decoded values in order.
import ("encoding/hex""github.com/ChefBingbong/viem-go/abi")params := []abi.AbiParam{{Name: "x", Type: "string"},{Name: "y", Type: "uint256"},{Name: "z", Type: "bool"},}data, _ := hex.DecodeString("...") // ABI-encoded bytesvalues, err := abi.DecodeAbiParameters(params, data)if err != nil {log.Fatal(err)}// values[0] string, values[1] *big.Int or int64, values[2] bool
- Signature:
DecodeAbiParameters(params []AbiParam, data []byte) ([]any, error) - Decoded types match ABI Types (address →
common.Address, uint256 →*big.Intor int64, etc.).
DecodeAbiParametersInto
Decode into a provided struct or slice (pointer). Useful for typed results.
var out struct {
X string
Y *big.Int
Z bool
}
err := abi.DecodeAbiParameters(params, data, &out)
- Signature:
DecodeAbiParametersInto(params []AbiParam, data []byte, output any) error - output must be a pointer to a struct (fields by position) or a slice.
DecodeWithSelector
Decode data that starts with a 4-byte selector: returns the selector and decoded parameters (data after the selector).
selector, values, err := abi.DecodeWithSelector(params, data)
// selector is [4]byte, values is []any from data[4:]
- Signature:
DecodeWithSelector(params []AbiParam, data []byte) ([4]byte, []any, error)
Go notes
- For decoding function calldata or return data with a parsed ABI, use contract-level DecodeFunctionData and DecodeFunctionResult.
See also
- Encode Parameters — EncodeAbiParameters
- Parse ABI — get a parsed ABI for contract decoding
- Types — AbiParam and type mapping