viem-goviem-go

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 bytes
values, 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.Int or 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

See also