0
# Event Processing
1
2
Event signature encoding and log data decoding with support for indexed parameters and anonymous events. Essential for processing Ethereum event logs and building event filters.
3
4
## Capabilities
5
6
### Encode Event Signature
7
8
Encodes the event name to its ABI signature, which is the sha3 hash of the event name including input types.
9
10
```typescript { .api }
11
/**
12
* Encodes the event name to its ABI signature (sha3 hash)
13
* @param functionName - The event name to encode or the AbiEventFragment object
14
* @returns The ABI signature of the event (32 bytes)
15
*/
16
function encodeEventSignature(functionName: string | AbiEventFragment): string;
17
```
18
19
**Usage Examples:**
20
21
```typescript
22
import { encodeEventSignature } from "web3-eth-abi";
23
24
// Using event name string
25
const signature1 = encodeEventSignature('myEvent(uint256,bytes32)');
26
console.log(signature1);
27
// 0xf2eeb729e636a8cb783be044acf6b7b1e2c5863735b60d6daae84c366ee87d97
28
29
// Using JSON interface object
30
const signature2 = encodeEventSignature({
31
name: "myEvent",
32
type: "event",
33
inputs: [
34
{ type: "uint256", name: "myNumber" },
35
{ type: "bytes32", name: "myBytes" }
36
]
37
});
38
console.log(signature2);
39
// 0xf2eeb729e636a8cb783be044acf6b7b1e2c5863735b60d6daae84c366ee87d97
40
41
// ERC-20 Transfer event
42
const transferSignature = encodeEventSignature({
43
inputs: [
44
{ indexed: true, name: "from", type: "address" },
45
{ indexed: true, name: "to", type: "address" },
46
{ indexed: false, name: "value", type: "uint256" }
47
],
48
name: "Transfer",
49
type: "event"
50
});
51
console.log(transferSignature);
52
// 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef
53
```
54
55
### Decode Event Log
56
57
Decodes ABI-encoded log data and indexed topic data from Ethereum event logs. Handles both indexed and non-indexed parameters correctly.
58
59
```typescript { .api }
60
/**
61
* Decodes ABI-encoded log data and indexed topic data
62
* @param inputs - Array of ABI parameter inputs defining the event structure
63
* @param data - The ABI byte code in the data field of a log
64
* @param topics - Array with indexed parameter topics (without topic[0] for non-anonymous events)
65
* @returns Decoded parameters object with numeric and named keys
66
*/
67
function decodeLog<ReturnType extends DecodedParams>(
68
inputs: Array<AbiParameter> | ReadonlyArray<AbiParameter>,
69
data: HexString,
70
topics: string | string[]
71
): ReturnType;
72
```
73
74
**Usage Examples:**
75
76
```typescript
77
import { decodeLog } from "web3-eth-abi";
78
79
// Decode event log with mixed indexed/non-indexed parameters
80
const decodedLog = decodeLog(
81
[
82
{ type: "string", name: "myString" },
83
{ type: "uint256", name: "myNumber", indexed: true },
84
{ type: "uint8", name: "mySmallNumber", indexed: true }
85
],
86
"0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000748656c6c6f252100000000000000000000000000000000000000000000000000",
87
[
88
"0x000000000000000000000000000000000000000000000000000000000000f310",
89
"0x0000000000000000000000000000000000000000000000000000000000000010"
90
]
91
);
92
93
console.log(decodedLog);
94
// {
95
// '0': 'Hello%!',
96
// '1': 62224n,
97
// '2': 16n,
98
// __length__: 3,
99
// myString: 'Hello%!',
100
// myNumber: 62224n,
101
// mySmallNumber: 16n
102
// }
103
104
// Decode ERC-20 Transfer event
105
const transferLog = decodeLog(
106
[
107
{ type: "address", name: "from", indexed: true },
108
{ type: "address", name: "to", indexed: true },
109
{ type: "uint256", name: "value", indexed: false }
110
],
111
"0x000000000000000000000000000000000000000000000000016345785d8a0000",
112
[
113
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", // event signature
114
"0x000000000000000000000000a0b86a33e6288db8e74b93f60e5c5e7b56b7b8f5", // from
115
"0x000000000000000000000000742d35cc6638c2532c1ccf6344d4039be6498c2f" // to
116
]
117
);
118
119
console.log(transferLog);
120
// {
121
// '0': '0xa0b86a33e6288db8e74b93f60e5c5e7b56b7b8f5',
122
// '1': '0x742d35cc6638c2532c1ccf6344d4039be6498c2f',
123
// '2': 100000000000000000n,
124
// __length__: 3,
125
// from: '0xa0b86a33e6288db8e74b93f60e5c5e7b56b7b8f5',
126
// to: '0x742d35cc6638c2532c1ccf6344d4039be6498c2f',
127
// value: 100000000000000000n
128
// }
129
130
// Decode anonymous event (no event signature in topics)
131
const anonymousLog = decodeLog(
132
[
133
{ type: "uint256", name: "value1", indexed: true },
134
{ type: "uint256", name: "value2", indexed: true },
135
{ type: "string", name: "message" }
136
],
137
"0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000d48656c6c6f20576f726c64210000000000000000000000000000000000000000",
138
[
139
"0x0000000000000000000000000000000000000000000000000000000000000001",
140
"0x0000000000000000000000000000000000000000000000000000000000000002"
141
]
142
);
143
144
console.log(anonymousLog);
145
// {
146
// '0': 1n,
147
// '1': 2n,
148
// '2': 'Hello World!',
149
// __length__: 3,
150
// value1: 1n,
151
// value2: 2n,
152
// message: 'Hello World!'
153
// }
154
```
155
156
## Event Processing Concepts
157
158
### Indexed Parameters
159
160
Indexed parameters are stored in the `topics` array of the event log and are limited to 32 bytes. Complex types (like strings and bytes) are hashed when indexed. Up to 3 parameters can be indexed per event.
161
162
### Non-Indexed Parameters
163
164
Non-indexed parameters are stored in the `data` field of the event log and can be of any size. They are ABI-encoded together.
165
166
### Anonymous Events
167
168
Anonymous events don't include the event signature hash as the first topic, so all topics correspond directly to indexed parameters.
169
170
### Topic Handling
171
172
- For non-anonymous events: `topics[0]` is the event signature, `topics[1...]` are indexed parameters
173
- For anonymous events: `topics[0...]` are indexed parameters directly
174
- The `decodeLog` function automatically handles the offset based on the number of topics vs indexed parameters
175
176
## Types
177
178
```typescript { .api }
179
interface AbiEventFragment {
180
type: "event";
181
name?: string;
182
inputs?: AbiParameter[];
183
anonymous?: boolean;
184
}
185
186
interface AbiParameter {
187
name: string;
188
type: string;
189
indexed?: boolean;
190
components?: AbiParameter[];
191
}
192
193
interface DecodedParams {
194
[key: string]: unknown;
195
__length__: number;
196
}
197
198
type HexString = string;
199
```