EmitEvent
func EmitEvent(emitter common.Address, stateDB vm.StateDB, contractABI abi.ABI, eventName string, args ...interface{}) error Constructs and emits an EVM log event. It automatically separates indexed arguments (topics) from non-indexed arguments (data) based on the provided ABI definition and inserts the log into the StateDB.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
emitter | common.Address | ✓ | The address of the contract (precompile) emitting the event. |
stateDB | vm.StateDB | ✓ | The current EVM state database interface, used to store the log. |
contractABI | abi.ABI | ✓ | The parsed ABI of the contract containing the event definition. |
eventName | string | ✓ | The name of the event to emit (must exist in contractABI). |
args | ...interface{} | ✓ | Variadic arguments matching the event's inputs in order. |
Returns
Type:
error Returns nil on success. Returns error if event is not found, argument count mismatches, or packing fails.
Errors
| Code | Name | Description |
|---|---|---|
EventNotFound | Event Not Found | The specified `eventName` does not exist in the provided ABI. |
ArgCountMismatch | Argument Count Mismatch | The number of provided `args` does not match the event definition. |
TopicLimitExceeded | Topic Limit Exceeded | The event requires more than 4 topics (including the signature), which is the EVM limit. |
Examples
Emitting a Transfer Event
Emits a standard ERC20 Transfer event. The utility automatically handles topic hashing for indexed args.
// Assuming contractABI is loaded and contains "Transfer(address,address,uint256)"
// Transfer has 2 indexed args (from, to) and 1 non-indexed (value)
err := utils.EmitEvent(
contractAddress,
stateDB,
contractABI,
"Transfer",
fromAddress, // indexed
toAddress, // indexed
amount, // non-indexed
)
if err != nil {
return nil, fmt.Errorf("failed to emit Transfer event: %w", err)
}