-
Notifications
You must be signed in to change notification settings - Fork 0
/
EigenEvents.js
721 lines (662 loc) · 24.8 KB
/
EigenEvents.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
import Web3 from "web3"
import AVSDirectoryABI from "./abi/AVSDirectoryABI.json" assert { type: "json" }
import DelegationManagerABI from "./abi/DelegationManagerABI.json" assert { type: "json" }
import StrategyManagerABI from "./abi/StrategyManagerABI.json" assert { type: "json" }
import EigenPodManagerABI from "./abi/EigenPodManagerABI.json" assert { type: "json" }
class EigenEvents {
/**
* Initializes a new instance of the EigenEvents library.
* @param {string} providerUrl - The URL of the Ethereum provider.
* @param {string} [connectionType="http"] - The type of connection to use (http or ws).
*/
constructor(providerUrl, connectionType = "http", onReconnectCallback = null) {
this.providerUrl = providerUrl
this.connectionType = connectionType
this.onReconnectCallback = onReconnectCallback
this.initializeConnection()
this.contracts = this._loadContracts()
}
initializeConnection() {
if (this.connectionType === "ws") {
const options = {
reconnect: {
auto: true,
delay: 5000,
maxAttempts: 5,
onTimeout: false,
},
}
const provider = new Web3.providers.WebsocketProvider(this.providerUrl, options)
this.web3 = new Web3(provider)
provider.on("end", () => {
console.error("WebSocket disconnected...")
this.initializeConnection()
})
provider.on("connect", () => {
console.log("WebSocket connected...")
if (this.onReconnectCallback) {
this.onReconnectCallback()
} else {
console.log(
"Warning: No callback detected, make sure you've turned on your listeners!",
)
}
})
} else {
this.web3 = new Web3(this.providerUrl)
}
}
/*
===========================================
Use these functions to query for event data
===========================================
*/
/**
* All functions are have the same params & return structure
* @param {number|string} fromBlock - Starting block number to fetch events.
* @param {number|string} toBlock - Ending block number to fetch events.
* @param {boolean} realTime - Whether to listen to events in real-time.
* @param {Function|null} callback - Optional callback to handle the events.
* @returns {Promise|undefined} Returns a promise if fetching past events, otherwise undefined.
*/
/**
* ---------------------------------
* Events from DelegationManager.sol
* ---------------------------------
*/
async getOperatorRegisteredEvents(fromBlock, toBlock, realTime = false, callback = null) {
return this.getEvents(
"DelegationManager",
"OperatorRegistered",
fromBlock,
toBlock,
realTime,
callback,
)
}
async getOperatorMetadataURIUpdatedEvents(
fromBlock = null,
toBlock = null,
realTime = false,
callback = null,
) {
return this.getEvents(
"DelegationManager",
"OperatorMetadataURIUpdated",
fromBlock,
toBlock,
realTime,
callback,
)
}
async getMinWithdrawalDelayBlocksSetEvents(
fromBlock,
toBlock,
realTime = false,
callback = null,
) {
return this.getEvents(
"DelegationManager",
"MinWithdrawalDelayBlocksSet",
fromBlock,
toBlock,
realTime,
callback,
)
}
async getOperatorDetailsModifiedEvents(fromBlock, toBlock, realTime = false, callback = null) {
return this.getEvents(
"DelegationManager",
"OperatorDetailsModified",
fromBlock,
toBlock,
realTime,
callback,
)
}
async getOperatorSharesDecreasedEvents(fromBlock, toBlock, realTime = false, callback = null) {
return this.getEvents(
"DelegationManager",
"OperatorSharesDecreased",
fromBlock,
toBlock,
realTime,
callback,
)
}
async getOperatorSharesIncreasedEvents(fromBlock, toBlock, realTime = false, callback = null) {
return this.getEvents(
"DelegationManager",
"OperatorSharesIncreased",
fromBlock,
toBlock,
realTime,
callback,
)
}
async getStakerDelegatedEvents(fromBlock, toBlock, realTime = false, callback = null) {
return this.getEvents(
"DelegationManager",
"StakerDelegated",
fromBlock,
toBlock,
realTime,
callback,
)
}
async getStakerForceUndelegatedEvents(fromBlock, toBlock, realTime = false, callback = null) {
return this.getEvents(
"DelegationManager",
"StakerForceUndelegated",
fromBlock,
toBlock,
realTime,
callback,
)
}
async getStakerUndelegatedEvents(fromBlock, toBlock, realTime = false, callback = null) {
return this.getEvents(
"DelegationManager",
"StakerUndelegated",
fromBlock,
toBlock,
realTime,
callback,
)
}
async getStrategyWithdrawalDelayBlocksSetEvents(
fromBlock,
toBlock,
realTime = false,
callback = null,
) {
return this.getEvents(
"DelegationManager",
"StrategyWithdrawalDelayBlocksSet",
fromBlock,
toBlock,
realTime,
callback,
)
}
async getWithdrawalCompletedEvents(fromBlock, toBlock, realTime = false, callback = null) {
return this.getEvents(
"DelegationManager",
"WithdrawalCompleted",
fromBlock,
toBlock,
realTime,
callback,
)
}
async getWithdrawalMigratedEvents(fromBlock, toBlock, realTime = false, callback = null) {
return this.getEvents(
"DelegationManager",
"WithdrawalMigrated",
fromBlock,
toBlock,
realTime,
callback,
)
}
async getWithdrawalQueuedEvents(fromBlock, toBlock, realTime = false, callback = null) {
return this.getEvents(
"DelegationManager",
"WithdrawalQueued",
fromBlock,
toBlock,
realTime,
callback,
)
}
/**
* -------------------------------
* Events from StrategyManager.sol
* -------------------------------
*/
async getDepositEvents(fromBlock, toBlock, realTime = false, callback = null) {
return this.getEvents("StrategyManager", "Deposit", fromBlock, toBlock, realTime, callback)
}
async getOwnershipTransferredEvents(fromBlock, toBlock, realTime = false, callback = null) {
return this.getEvents(
"StrategyManager",
"OwnershipTransferred",
fromBlock,
toBlock,
realTime,
callback,
)
}
async getStrategyAddedToDepositWhitelistEvents(
fromBlock,
toBlock,
realTime = false,
callback = null,
) {
return this.getEvents(
"StrategyManager",
"StrategyAddedToDepositWhitelist",
fromBlock,
toBlock,
realTime,
callback,
)
}
async getStrategyRemovedFromDepositWhitelistEvents(
fromBlock,
toBlock,
realTime = false,
callback = null,
) {
return this.getEvents(
"StrategyManager",
"StrategyRemovedFromDepositWhitelist",
fromBlock,
toBlock,
realTime,
callback,
)
}
async getStrategyWhitelisterChangedEvents(
fromBlock,
toBlock,
realTime = false,
callback = null,
) {
return this.getEvents(
"StrategyManager",
"StrategyWhitelisterChanged",
fromBlock,
toBlock,
realTime,
callback,
)
}
async getUpdatedThirdPartyTransfersForbiddenEvents(
fromBlock,
toBlock,
realTime = false,
callback = null,
) {
return this.getEvents(
"StrategyManager",
"UpdatedThirdPartyTransfersForbidden",
fromBlock,
toBlock,
realTime,
callback,
)
}
/**
* -------------------------------
* Events from EigenPodManager.sol
* -------------------------------
*/
async getBeaconChainETHDepositedEvents(fromBlock, toBlock, realTime = false, callback = null) {
return this.getEvents(
"EigenPodManager",
"BeaconChainETHDeposited",
fromBlock,
toBlock,
realTime,
callback,
)
}
async getBeaconChainETHWithdrawalCompletedEvents(
fromBlock,
toBlock,
realTime = false,
callback = null,
) {
return this.getEvents(
"EigenPodManager",
"BeaconChainETHWithdrawalCompleted",
fromBlock,
toBlock,
realTime,
callback,
)
}
async getBeaconOracleUpdatedEvents(fromBlock, toBlock, realTime = false, callback = null) {
return this.getEvents(
"EigenPodManager",
"BeaconOracleUpdated",
fromBlock,
toBlock,
realTime,
callback,
)
}
async getPodDeployedEvents(fromBlock, toBlock, realTime = false, callback = null) {
return this.getEvents(
"EigenPodManager",
"PodDeployed",
fromBlock,
toBlock,
realTime,
callback,
)
}
async getPodSharesUpdatedEvents(fromBlock, toBlock, realTime = false, callback = null) {
return this.getEvents(
"EigenPodManager",
"PodSharesUpdated",
fromBlock,
toBlock,
realTime,
callback,
)
}
/**
* ----------------------------
* Events from AVSDirectory.sol
* ----------------------------
*/
async getOperatorAVSRegistrationStatusUpdatedEvents(
fromBlock,
toBlock,
realTime = false,
callback = null,
) {
return this.getEvents(
"AVSDirectory",
"OperatorAVSRegistrationStatusUpdated",
fromBlock,
toBlock,
realTime,
callback,
)
}
async getAVSMetadataURIUpdatedEvents(fromBlock, toBlock, realTime = false, callback = null) {
return this.getEvents(
"AVSDirectory",
"AVSMetadataURIUpdated",
fromBlock,
toBlock,
realTime,
callback,
)
}
/*
==================
Internal functions
==================
*/
/**
* Internal method to load smart contracts using their ABIs and addresses.
* @returns {Object} A dictionary of contract instances.
*/
_loadContracts() {
return {
DelegationManager: new this.web3.eth.Contract(
DelegationManagerABI,
"0x39053D51B77DC0d36036Fc1fCc8Cb819df8Ef37A",
),
AVSDirectory: new this.web3.eth.Contract(
AVSDirectoryABI,
"0x135DDa560e946695d6f155dACaFC6f1F25C1F5AF",
),
StrategyManager: new this.web3.eth.Contract(
StrategyManagerABI,
"0x858646372CC42E1A627fcE94aa7A7033e7CF075A",
),
EigenPodManager: new this.web3.eth.Contract(
EigenPodManagerABI,
"0x91E677b07F7AF907ec9a428aafA9fc14a0d3A338",
),
}
}
/**
* Fetches or listens for events depending on the realTime parameter.
* @param {string} contractName - Name of the contract.
* @param {string} eventName - Name of the event.
* @param {number|string} fromBlock - The block number to start fetching events from.
* @param {number|string} toBlock - The block number to stop fetching events at.
* @param {boolean} realTime - Whether to listen to events in real-time.
* @param {Function|null} callback - Optional callback to handle the events.
* @returns {Promise|undefined} Returns a promise if fetching past events, otherwise undefined.
*/
async getEvents(
contractName,
eventName,
fromBlock = 19492759,
toBlock = "latest",
realTime = false,
callback = null,
) {
if (realTime) {
this._listenToEvents(contractName, eventName, callback)
} else {
return this._fetchPastEvents(contractName, eventName, fromBlock, toBlock, callback)
}
}
/**
* Internal method to fetch past events from a specified contract.
* @param {string} contractName - The name of the contract.
* @param {string} eventName - The event name.
* @param {number|string} fromBlock - Starting block number to fetch events.
* @param {number|string} toBlock - Ending block number to fetch events.
* @param {Function|null} callback - Optional callback to process each event.
* @returns {Promise<Array>} Returns a promise that resolves with an array of formatted event data.
*/
async _fetchPastEvents(contractName, eventName, fromBlock, toBlock, callback) {
const contract = this.contracts[contractName]
const events = await contract.getPastEvents(eventName, { fromBlock, toBlock })
if (callback) {
return Promise.all(
events.map((event) =>
callback(this._formatEventOutput(event, contract, eventName)),
),
)
} else {
return Promise.all(
events.map((event) => this._formatEventOutput(event, contract, eventName)),
)
}
}
/**
* Internal method to listen to real-time events from a specified contract.
* @param {string} contractName - The name of the contract.
* @param {string} eventName - The name of the event to subscribe to.
* @param {Function|null} callback - Callback to handle the event data.
* @returns {Promise} Returns a subscription object for managing the real-time event listening.
*/
async _listenToEvents(contractName, eventName, callback) {
const contract = this.contracts[contractName]
const eventSignature = this._getEventSignature(contract, eventName)
if (!eventSignature) {
console.error(`Event signature for ${eventName} not found in contract ${contractName}`)
return
}
try {
const subscription = await this.web3.eth.subscribe("logs", {
address: contract.options.address,
topics: [eventSignature],
})
subscription.on("data", async (event) => {
const formattedData = await this._formatEventOutput(event, contract, eventName)
if (callback) {
callback(formattedData)
} else {
console.log(formattedData)
}
})
return subscription
} catch (error) {
console.error("Error subscribing to events:", error)
}
}
/**
* Internal method to format the output of an event log.
* @param {Object} event - The event log object.
* @param {Object} contract - The contract instance from which the event originated.
* @param {string} eventName - The name of the event.
* @returns {Object} Returns a formatted event object including decoded parameters and a custom message.
*/
async _formatEventOutput(event, contract, eventName) {
if (!contract || !contract.options || !contract.options.jsonInterface) {
console.error("Invalid contract object:", contract)
throw new Error("The contract object is not properly initialized.")
}
const eventABI = contract.options.jsonInterface.find(
(e) => e.type === "event" && e.name === eventName,
)
if (!eventABI) {
console.error(`No ABI entry found for event name: ${eventName}`)
return
}
const decodedParams = this.web3.eth.abi.decodeLog(
eventABI.inputs,
event.data,
event.topics.slice(1),
)
let returnValues = {}
eventABI.inputs.forEach((input) => {
if (!input.name) return
if (input.type.includes("tuple")) {
returnValues[input.name] = this._decodeTuple(
decodedParams[input.name],
input.components,
)
} else {
returnValues[input.name] = decodedParams[input.name]
}
})
const messageTemplate = this._getMessageTemplate(eventName)
const message = messageTemplate ? messageTemplate(returnValues) : ""
return {
transactionHash: event.transactionHash,
blockNumber: event.blockNumber,
event: eventABI.name,
returnValues: returnValues,
message: message,
}
}
/**
* Internal method to decode tuple parameters from event logs.
* @param {Object} tupleData - The raw tuple data from the log.
* @param {Array} components - The components of the tuple as described in the ABI.
* @returns {Object} Returns a decoded tuple object.
*/
_decodeTuple(tupleData, components) {
let result = {}
components.forEach((component) => {
let value = tupleData[component.name]
if (component.type.includes("tuple")) {
value = this._decodeTuple(value, component.components)
}
result[component.name] = value
})
return result
}
/**
* Internal method to retrieve a message template for a specific event.
* @param {string} eventName - The name of the event.
* @returns {Function} Returns a function that formats the event's parameters into a string message.
*/
_getMessageTemplate(eventName) {
const strategyMapping = {
"0x54945180dB7943c0ed0FEE7EdaB2Bd24620256bc": "cbETH",
"0x93c4b944D05dfe6df7645A86cd2206016c51564D": "stETH",
"0x1BeE69b7dFFfA4E2d53C2a2Df135C388AD25dCD2": "rETH",
"0x9d7eD45EE2E8FC5482fa2428f15C971e6369011d": "ETHx",
"0x13760F50a9d7377e4F20CB8CF9e4c26586c658ff": "ankrETH",
"0xa4C637e0F704745D182e4D38cAb7E7485321d059": "OETH",
"0x57ba429517c3473B6d34CA9aCd56c0e735b94c02": "osETH",
"0x0Fe4F44beE93503346A3Ac9EE5A26b130a5796d6": "swETH",
"0x7CA911E83dabf90C90dD3De5411a10F1A6112184": "wBETH",
"0x8CA7A5d6f3acd3A7A8bC468a8CD0FB14B6BD28b6": "sfrxETH",
"0xAe60d8180437b5C34bB956822ac2710972584473": "lsETH",
"0x298aFB19A105D59E74658C4C334Ff360BadE6dd2": "mETH",
"0xbeaC0eeEeeeeEEeEeEEEEeeEEeEeeeEeeEEBEaC0": "Beacon Chain ETH",
}
const tokenMapping = {
"0xBe9895146f7AF43049ca1c1AE358B0541Ea49704": "cbETH",
"0xae7ab96520de3a18e5e111b5eaab095312d7fe84": "stETH",
"0xae78736cd615f374d3085123a210448e74fc6393": "rETH",
"0xA35b1B31Ce002FBF2058D22F30f95D405200A15b": "ETHx",
"0xE95A203B1a91a908F9B9CE46459d101078c2c3cb": "ankrETH",
"0x856c4Efb76C1D1AE02e20CEB03A2A6a08b0b8dC3": "OETH",
"0xf1c9acdc66974dfb6decb12aa385b9cd01190e38": "osETH",
"0xf951E335afb289353dc249e82926178EaC7DEd78": "swETH",
"0xa2e3356610840701bdf5611a53974510ae27e2e1": "wBETH",
"0xac3e018457b222d93114458476f3e3416abbe38f": "sfrxETH",
"0x8c1BEd5b9a0928467c9B1341Da1D7BD5e10b6549": "lsETH",
"0xd5f7838f5c461feff7fe49ea5ebaf7728bb0adfa": "mETH",
"0xbeaC0eeEeeeeEEeEeEEEEeeEEeEeeeEeeEEBEaC0": "Beacon Chain ETH",
}
const templates = {
// Events from DelegationManager.sol
OperatorRegistered: (params) => `${params.operator} registered as an Operator.`,
OperatorMetadataURIUpdated: (params) =>
`${params.operator} (Operator) updated their metadata to ${params.metadataURI}.`,
OperatorMetadataURIUpdated: (params) =>
`${params.operator} updated their metadata URI to ${params.metadataURI}.`,
MinWithdrawalDelayBlocksSet: (params) =>
`MinWithdrawalDelayBlocks set from ${params.previousMinWithdrawalDelayBlocks} to ${params.newMinWithdrawalDelayBlocks}.`,
OperatorDetailsModified: (params) => `${params.operator} modified their details.`,
OperatorSharesDecreased: (params) => {
const tokenName = strategyMapping[params.strategy] || params.strategy
return `${params.operator} shares in ${tokenName} decreased to ${params.shares} due to undelegation from ${params.staker}`
},
OperatorSharesIncreased: (params) => {
const tokenName = strategyMapping[params.strategy] || params.strategy
return `${params.operator} shares in ${tokenName} increased to ${params.shares} due to delegation from ${params.staker}`
},
StakerDelegated: (params) => `${params.staker} delegated stake to ${params.operator}`,
StakerForceUndelegated: (params) =>
`${params.staker} has been forcibly undelegated by ${params.operator}.`,
StakerUndelegated: (params) =>
`${params.staker} undelegated stake from ${params.operator}`,
StrategyWithdrawalDelayBlocksSet: (params) => {
const tokenName = strategyMapping[params.strategy] || params.strategy
return `WithdrawalDelayBlocks for ${tokenName} changed from ${params.previousWithdrawalDelayBlocks} to ${params.newWithdrawalDelayBlocks}.`
},
WithdrawalCompleted: (params) => `Withdrawal completed: ${params.withdrawalRoot}`,
WithdrawalMigrated: (params) =>
`Withdrawal migrated from ${params.oldWithdrawalRoot} to ${params.newWithdrawalRoot}.`,
WithdrawalQueued: (params) =>
`Withdrawal from ${params.withdrawal.staker} to ${params.withdrawal.withdrawer}`,
// Events from StrategyManager.sol
// TODO: 4 more events from this contract
Deposit: (params) => {
const tokenName = tokenMapping[params.token] || params.token
return `${params.staker} deposited ${tokenName} for ${params.shares} shares`
},
OwnershipTransferred: (params) =>
`Ownership of StrategyManager.sol transferred from ${params.previousOwner} to ${params.newOwner}`,
// Events from EigenPodManager.sol
BeaconChainETHDeposited: (params) =>
`${params.podOwner} deposited Beacon Chain ETH of amount ${params.amount}`,
BeaconChainETHWithdrawalCompleted: (params) =>
`${params.podOwner} Beacon Chain ETH of amount ${params.amount} completed`,
BeaconOracleUpdated: (params) =>
`Beacon Chain Oracle updated to ${params.newOracleAddress}`,
PodDeployed: (params) =>
`New EigenPod deployed by ${params.podOwner} at ${params.eigenPod}`,
PodSharesUpdated: (params) =>
`EigenPod shares of ${params.podOwner} changed by ${params.sharesDelta}`,
// Events from AVSDirectory.sol
OperatorAVSRegistrationStatusUpdated: (params) =>
`Operator ${params.operator} updated registration status with AVS ${params.avs}`, // add what it changed to
AVSMetadataURIUpdated: (params) =>
`${params.avs} (AVS) updated their metadata to ${params.metadataURI}.`,
}
return templates[eventName]
}
/**
* Internal method to retreive signature of an event from its contract ABI.
* @param {Object} contract
* @param {String} eventName
* @returns
*/
_getEventSignature(contract, eventName) {
const eventABI = contract.options.jsonInterface.find(
(e) => e.name === eventName && e.type === "event",
)
if (!eventABI) return null
const signature = `${eventName}(${eventABI.inputs.map((input) => input.type).join(",")})`
return this.web3.utils.sha3(signature)
}
}
export default EigenEvents