Documentation
¶
Overview ¶
Package eventdecoder maintains a compact registry of event signatures and helpers to decode logs into typed values using minimal ABI fragments.
Two usage modes are supported:
- Register ABI sources at runtime (JSON or *core.SmartContract_ABI)
- Use builtin signatures generated from common ecosystems (e.g., TRC20)
Given topics and data from a TransactionInfo_Log, DecodeLog will look up the first topic's signature, build a minimal ABI for the matched signature, and return a DecodedEvent. Unknown signatures fall back to a placeholder name instead of failing.
Quick Start ¶
ev, _ := eventdecoder.DecodeLog(topics, data) _ = ev.EventName
To expand coverage, call RegisterABIJSON or RegisterABIObject with known contract ABIs. Builtins include TRC20 events.
Built-in Events ¶
The package includes built-in support for common TRC20 events:
- Transfer(address,address,uint256)
- Approval(address,address,uint256)
Registering Custom ABIs ¶
To decode custom events, register their ABIs:
eventdecoder.RegisterABIJSON(abiJSON) // or var abi core.SmartContract_ABI _ = json.Unmarshal([]byte(abiJSON), &abi) eventdecoder.RegisterABIObject(&abi)
Decoded Event Structure ¶
The decoded event has the following structure:
type DecodedEvent struct {
EventName string
Signature string
Inputs []EventInput
}
type EventInput struct {
Name string
Type string
Value interface{}
}
Error Handling ¶
Common error types:
- ErrNoMatchingABI - No ABI registered for the event signature
- ErrInvalidTopicCount - Mismatch between expected and actual topic count
- ErrInvalidDataLength - Data length doesn't match expected size
Always check for errors in production code.
Example ¶
Example is a package-level example showing DecodeLog with built-in TRC20 signatures.
package main
import (
"encoding/hex"
"fmt"
"github.com/kslamph/tronlib/pkg/eventdecoder"
)
func main() {
// Synthetic TRC20 Transfer event
sigTopic, _ := hex.DecodeString("ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef")
fromTopic, _ := hex.DecodeString("000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48")
toTopic, _ := hex.DecodeString("0000000000000000000000004e83362442b8d1bec281594cea3050c8eb01311c")
amountData, _ := hex.DecodeString("00000000000000000000000000000000000000000000000000000000000003e8") // 1000
ev, _ := eventdecoder.DecodeLog([][]byte{sigTopic, fromTopic, toTopic}, amountData)
fmt.Println(ev.EventName)
}
Output: Transfer
Index ¶
- func DecodeEventSignature(sig []byte) (string, bool)
- func RegisterABIEntries(entries []*core.SmartContract_ABI_Entry) error
- func RegisterABIJSON(abiJSON string) error
- func RegisterABIObject(abi *core.SmartContract_ABI) error
- type DecodedEvent
- type DecodedEventParameter
- type EventDef
- type ParamDef
- type SimpleABIParser
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DecodeEventSignature ¶
DecodeEventSignature returns the canonical event signature string for the given 4-byte signature if known The boolean indicates whether the signature was found in the registry
func RegisterABIEntries ¶
func RegisterABIEntries(entries []*core.SmartContract_ABI_Entry) error
RegisterABIEntries registers all event entries from the provided list (non-event entries are ignored)
func RegisterABIJSON ¶
RegisterABIJSON registers all event entries from a JSON ABI string
Example ¶
ExampleRegisterABIJSON demonstrates extending the decoder with a custom ABI.
package main
import (
"github.com/kslamph/tronlib/pkg/eventdecoder"
)
func main() {
abiJSON := `{"entrys":[{"type":"event","name":"Custom","inputs":[{"name":"x","type":"uint256","indexed":true}]}]}`
_ = eventdecoder.RegisterABIJSON(abiJSON)
}
Output:
func RegisterABIObject ¶
func RegisterABIObject(abi *core.SmartContract_ABI) error
RegisterABIObject registers all event entries from a SmartContract_ABI object
Types ¶
type DecodedEvent ¶ added in v1.0.4
type DecodedEvent struct {
EventName string `json:"eventName"`
Parameters []DecodedEventParameter `json:"parameters"`
Contract string `json:"contract"`
}
DecodedEvent represents a decoded event
func DecodeLog ¶
func DecodeLog(topics [][]byte, data []byte) (*DecodedEvent, error)
DecodeLog decodes a single log using the global 4-byte signature registry
func DecodeLogs ¶
func DecodeLogs(logs []*core.TransactionInfo_Log) ([]*DecodedEvent, error)
DecodeLogs decodes a slice of logs using the global 4-byte signature registry
type DecodedEventParameter ¶ added in v1.0.4
type DecodedEventParameter struct {
Name string `json:"name"`
Type string `json:"type"`
Value string `json:"value"`
Indexed bool `json:"indexed"`
}
DecodedEventParameter represents a decoded event parameter
type SimpleABIParser ¶ added in v1.0.4
type SimpleABIParser struct{}
Simple ABI parser for JSON parsing
func NewSimpleABIParser ¶ added in v1.0.4
func NewSimpleABIParser() *SimpleABIParser
func (*SimpleABIParser) ParseABI ¶ added in v1.0.4
func (p *SimpleABIParser) ParseABI(abiJSON string) (*core.SmartContract_ABI, error)
ParseABI parses a standard Solidity ABI JSON string into a SmartContract_ABI.