ABI Selectors & Items
Compute selectors and event topics, format signatures, and parse event logs for contract ABIs in viem-go
Compute selectors and event topics, format signatures, and parse event logs for contract ABIs in viem-go
Compute function/error selectors and event topics, format human-readable signatures, and batch-parse event logs using a parsed contract ABI. For generic lookup by name or selector (GetAbiItem), see Get Abi Item in the ABI section.
import ( "encoding/hex" "github.com/ethereum/go-ethereum/common" "github.com/ChefBingbong/viem-go/abi")Compute the 4-byte function (or error) selector from a signature string: first 4 bytes of keccak256(signature).
import ( "encoding/hex" "github.com/ChefBingbong/viem-go/abi")
sel := abi.ComputeSelector("transfer(address,uint256)")// sel is [4]byte; hex: 0xa9059cbb
hexSel := abi.ComputeSelectorHex("transfer(address,uint256)")// "0xa9059cbb"0x prefix.0x hex string.0x) to 4-byte selector.Compute the 32-byte event topic (keccak256 of the event signature). Used as the first topic in eth_getLogs for non-anonymous events.
topic := abi.ComputeEventTopic("Transfer(address,address,uint256)")
// topic is common.Hash
hexTopic := abi.ComputeEventTopicHex("Transfer(address,address,uint256)")
// "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
"transfer(address,uint256)".map[string][4]byte of common ERC20/ERC721 function selectors (e.g. "transfer", "balanceOf").map[string]common.Hash for common events (e.g. "Transfer", "Approval").For generic GetAbiItem (by name or hex selector with overload resolution), see Get Abi Item.
Produce a human-readable signature for a function, event, or error (no argument values).
fn, _ := parsed.GetFunction("transfer")
sig, err := abi.FormatAbiItem(fn)
// "transfer(address,uint256)"
// Parameters only (types, or types + names)
typesOnly := abi.FormatAbiParams(fn.Inputs, false) // "address,uint256"
withNames := abi.FormatAbiParams(fn.Inputs, true) // "address to, uint256 amount"
Format an ABI item with actual argument values (e.g. for logging).
formatted := abi.FormatAbiItemWithArgs(fn, []any{to, amount}, nil)
// "transfer(0x1234..., 1000000000000000000)"
formatted := abi.FormatAbiItemWithArgs(fn, []any{to, amount}, &abi.FormatAbiItemWithArgsOptions{
IncludeFunctionName: true,
IncludeName: true,
})
// "transfer(to: 0x1234..., amount: 1000000000000000000)"
Decode multiple raw logs in one go. Keeps only logs whose first topic matches an event on the ABI; optionally filter by event name or indexed args.
import "github.com/ChefBingbong/viem-go/abi"
rawLogs := []abi.RawLog{ {Address: addr, Topics: topics1, Data: data1}, {Address: addr, Topics: topics2, Data: data2},}
// Parse all events that match the ABIparsed := parsedABI.ParseEventLogs(rawLogs, nil)
// Only Transfer eventsparsed := parsedABI.ParseEventLogs(rawLogs, &abi.ParseEventLogsOptions{ EventName: []string{"Transfer"},})
// Filter by indexed argsparsed := parsedABI.ParseEventLogs(rawLogs, &abi.ParseEventLogsOptions{ EventName: []string{"Transfer"}, Args: map[string]any{"from": sender}, Strict: true,})
for _, log := range parsed { fmt.Println(log.EventName, log.Args)}[]common.Hash), Data; optional BlockNumber, TransactionHash, LogIndex.map[string]any), Address, Topics, Data, plus optional receipt fields.Signature: (a *ABI) ParseEventLogs(logs []RawLog, opts *ParseEventLogsOptions) []ParsedEventLog
Resolve an overloaded function by name (and args) or by selector, then encode with EncodeWithPrepared. Useful when encoding the same function repeatedly.
prepared, err := parsed.PrepareEncodeFunctionData("transfer", to, amount)
if err != nil {
log.Fatal(err)
}
// prepared.FunctionName, prepared.FunctionSelector, prepared.Abi
data, err := parsed.EncodeWithPrepared(prepared, to, amount)
PreparedFunctionData has Abi (slice with the matched function), FunctionSelector ([4]byte), FunctionName (string).