@atomiqlabs/chain-evm 1.1.3 → 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -93,6 +93,7 @@ export declare class EVMChainEventsBrowser implements ChainEvents<EVMSwapData> {
|
|
|
93
93
|
protected swapContractListener: (log: Log) => void;
|
|
94
94
|
protected blockListener: (blockNumber: number) => Promise<void>;
|
|
95
95
|
protected wsStarted: boolean;
|
|
96
|
+
protected addOrRemoveBlockListener(): Promise<void>;
|
|
96
97
|
protected setupWebsocket(): Promise<void>;
|
|
97
98
|
init(): Promise<void>;
|
|
98
99
|
stop(): Promise<void>;
|
|
@@ -326,9 +326,55 @@ class EVMChainEventsBrowser {
|
|
|
326
326
|
return this.processEvents(events);
|
|
327
327
|
}
|
|
328
328
|
this.unconfirmedEventQueue.push(...events);
|
|
329
|
+
return this.addOrRemoveBlockListener();
|
|
330
|
+
}
|
|
331
|
+
async addOrRemoveBlockListener() {
|
|
332
|
+
if (this.unconfirmedEventQueue.length > 0) {
|
|
333
|
+
this.logger.debug(`addOrRemoveBlockListener(): Adding block listener, unconfirmed event count: ${this.unconfirmedEventQueue.length}`);
|
|
334
|
+
await this.provider.on("block", this.blockListener);
|
|
335
|
+
}
|
|
336
|
+
else {
|
|
337
|
+
this.logger.debug(`addOrRemoveBlockListener(): Removing block listener, unconfirmed event count: ${this.unconfirmedEventQueue.length}`);
|
|
338
|
+
await this.provider.off("block", this.blockListener);
|
|
339
|
+
}
|
|
329
340
|
}
|
|
330
341
|
async setupWebsocket() {
|
|
331
342
|
this.wsStarted = true;
|
|
343
|
+
let processing = false;
|
|
344
|
+
this.blockListener = async (blockNumber) => {
|
|
345
|
+
if (processing)
|
|
346
|
+
return;
|
|
347
|
+
if (this.unconfirmedEventQueue.length === 0)
|
|
348
|
+
return;
|
|
349
|
+
processing = true;
|
|
350
|
+
try {
|
|
351
|
+
const latestSafeBlock = await this.provider.getBlock(this.chainInterface.config.safeBlockTag);
|
|
352
|
+
const events = [];
|
|
353
|
+
this.unconfirmedEventQueue = this.unconfirmedEventQueue.filter(event => {
|
|
354
|
+
if (event.blockNumber <= latestSafeBlock.number) {
|
|
355
|
+
events.push(event);
|
|
356
|
+
return false;
|
|
357
|
+
}
|
|
358
|
+
return true;
|
|
359
|
+
});
|
|
360
|
+
const blocks = {};
|
|
361
|
+
for (let event of events) {
|
|
362
|
+
const block = blocks[event.blockNumber] ?? (blocks[event.blockNumber] = await this.provider.getBlock(event.blockNumber));
|
|
363
|
+
if (block.hash === event.blockHash) {
|
|
364
|
+
//Valid event
|
|
365
|
+
await this.processEvents([event], block);
|
|
366
|
+
}
|
|
367
|
+
else {
|
|
368
|
+
//Block hash doesn't match
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
catch (e) {
|
|
373
|
+
this.logger.error(`on('block'): Error when processing new block ${blockNumber}:`, e);
|
|
374
|
+
}
|
|
375
|
+
processing = false;
|
|
376
|
+
await this.addOrRemoveBlockListener();
|
|
377
|
+
};
|
|
332
378
|
await this.provider.on(this.spvVaultContractLogFilter, this.spvVaultContractListener = (log) => {
|
|
333
379
|
let events = this.evmSpvVaultContract.Events.toTypedEvents([log]);
|
|
334
380
|
events = events.filter(val => !val.removed);
|
|
@@ -339,42 +385,6 @@ class EVMChainEventsBrowser {
|
|
|
339
385
|
events = events.filter(val => !val.removed && (val.eventName === "Initialize" || val.eventName === "Refund" || val.eventName === "Claim"));
|
|
340
386
|
this.handleWsEvents(events);
|
|
341
387
|
});
|
|
342
|
-
const safeBlockTag = this.chainInterface.config.safeBlockTag;
|
|
343
|
-
let processing = false;
|
|
344
|
-
if (safeBlockTag !== "latest" && safeBlockTag !== "pending")
|
|
345
|
-
await this.provider.on("block", this.blockListener = async (blockNumber) => {
|
|
346
|
-
if (processing)
|
|
347
|
-
return;
|
|
348
|
-
if (this.unconfirmedEventQueue.length === 0)
|
|
349
|
-
return;
|
|
350
|
-
processing = true;
|
|
351
|
-
try {
|
|
352
|
-
const latestSafeBlock = await this.provider.getBlock(this.chainInterface.config.safeBlockTag);
|
|
353
|
-
const events = [];
|
|
354
|
-
this.unconfirmedEventQueue = this.unconfirmedEventQueue.filter(event => {
|
|
355
|
-
if (event.blockNumber <= latestSafeBlock.number) {
|
|
356
|
-
events.push(event);
|
|
357
|
-
return false;
|
|
358
|
-
}
|
|
359
|
-
return true;
|
|
360
|
-
});
|
|
361
|
-
const blocks = {};
|
|
362
|
-
for (let event of events) {
|
|
363
|
-
const block = blocks[event.blockNumber] ?? (blocks[event.blockNumber] = await this.provider.getBlock(event.blockNumber));
|
|
364
|
-
if (block.hash === event.blockHash) {
|
|
365
|
-
//Valid event
|
|
366
|
-
await this.processEvents([event], block);
|
|
367
|
-
}
|
|
368
|
-
else {
|
|
369
|
-
//Block hash doesn't match
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
catch (e) {
|
|
374
|
-
this.logger.error(`on('block'): Error when processing new block ${blockNumber}:`, e);
|
|
375
|
-
}
|
|
376
|
-
processing = false;
|
|
377
|
-
});
|
|
378
388
|
}
|
|
379
389
|
async init() {
|
|
380
390
|
if (this.provider.websocket != null) {
|
package/package.json
CHANGED
|
@@ -439,32 +439,29 @@ export class EVMChainEventsBrowser implements ChainEvents<EVMSwapData> {
|
|
|
439
439
|
return this.processEvents(events);
|
|
440
440
|
}
|
|
441
441
|
this.unconfirmedEventQueue.push(...events);
|
|
442
|
+
return this.addOrRemoveBlockListener();
|
|
442
443
|
}
|
|
443
444
|
|
|
444
445
|
protected spvVaultContractListener: (log: Log) => void;
|
|
445
446
|
protected swapContractListener: (log: Log) => void;
|
|
446
447
|
protected blockListener: (blockNumber: number) => Promise<void>;
|
|
447
|
-
|
|
448
448
|
protected wsStarted: boolean = false;
|
|
449
449
|
|
|
450
|
+
protected async addOrRemoveBlockListener() {
|
|
451
|
+
if(this.unconfirmedEventQueue.length>0) {
|
|
452
|
+
this.logger.debug(`addOrRemoveBlockListener(): Adding block listener, unconfirmed event count: ${this.unconfirmedEventQueue.length}`);
|
|
453
|
+
await this.provider.on("block", this.blockListener);
|
|
454
|
+
} else {
|
|
455
|
+
this.logger.debug(`addOrRemoveBlockListener(): Removing block listener, unconfirmed event count: ${this.unconfirmedEventQueue.length}`);
|
|
456
|
+
await this.provider.off("block", this.blockListener);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
450
460
|
protected async setupWebsocket() {
|
|
451
461
|
this.wsStarted = true;
|
|
452
462
|
|
|
453
|
-
await this.provider.on(this.spvVaultContractLogFilter, this.spvVaultContractListener = (log) => {
|
|
454
|
-
let events = this.evmSpvVaultContract.Events.toTypedEvents([log]);
|
|
455
|
-
events = events.filter(val => !val.removed);
|
|
456
|
-
this.handleWsEvents(events);
|
|
457
|
-
});
|
|
458
|
-
|
|
459
|
-
await this.provider.on(this.swapContractLogFilter, this.swapContractListener = (log) => {
|
|
460
|
-
let events = this.evmSwapContract.Events.toTypedEvents([log]);
|
|
461
|
-
events = events.filter(val => !val.removed && (val.eventName==="Initialize" || val.eventName==="Refund" || val.eventName==="Claim"));
|
|
462
|
-
this.handleWsEvents(events);
|
|
463
|
-
});
|
|
464
|
-
|
|
465
|
-
const safeBlockTag = this.chainInterface.config.safeBlockTag;
|
|
466
463
|
let processing = false;
|
|
467
|
-
|
|
464
|
+
this.blockListener = async (blockNumber: number) => {
|
|
468
465
|
if(processing) return;
|
|
469
466
|
if(this.unconfirmedEventQueue.length===0) return;
|
|
470
467
|
processing = true;
|
|
@@ -494,6 +491,19 @@ export class EVMChainEventsBrowser implements ChainEvents<EVMSwapData> {
|
|
|
494
491
|
this.logger.error(`on('block'): Error when processing new block ${blockNumber}:`, e);
|
|
495
492
|
}
|
|
496
493
|
processing = false;
|
|
494
|
+
await this.addOrRemoveBlockListener();
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
await this.provider.on(this.spvVaultContractLogFilter, this.spvVaultContractListener = (log) => {
|
|
498
|
+
let events = this.evmSpvVaultContract.Events.toTypedEvents([log]);
|
|
499
|
+
events = events.filter(val => !val.removed);
|
|
500
|
+
this.handleWsEvents(events);
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
await this.provider.on(this.swapContractLogFilter, this.swapContractListener = (log) => {
|
|
504
|
+
let events = this.evmSwapContract.Events.toTypedEvents([log]);
|
|
505
|
+
events = events.filter(val => !val.removed && (val.eventName==="Initialize" || val.eventName==="Refund" || val.eventName==="Claim"));
|
|
506
|
+
this.handleWsEvents(events);
|
|
497
507
|
});
|
|
498
508
|
}
|
|
499
509
|
|