Decode ABI Parameters
Decode raw ABI-encoded bytes using parameter definitions without a parsed contract ABI in viem-go
Loading...
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 ( "encoding/hex" "github.com/ChefBingbong/viem-go/abi")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] boolDecodeAbiParameters(params []AbiParam, data []byte) ([]any, error)common.Address, uint256 → *big.Int or int64, etc.).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)
DecodeAbiParametersInto(params []AbiParam, data []byte, output any) errorDecode 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:]
DecodeWithSelector(params []AbiParam, data []byte) ([4]byte, []any, error)