@atomiqlabs/chain-evm 1.1.3 → 1.1.5
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,53 @@ 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 = this.unconfirmedEventQueue.filter(event => {
|
|
353
|
+
return event.blockNumber <= latestSafeBlock.number;
|
|
354
|
+
});
|
|
355
|
+
const blocks = {};
|
|
356
|
+
for (let event of events) {
|
|
357
|
+
const block = blocks[event.blockNumber] ?? (blocks[event.blockNumber] = await this.provider.getBlock(event.blockNumber));
|
|
358
|
+
if (block.hash === event.blockHash) {
|
|
359
|
+
//Valid event
|
|
360
|
+
await this.processEvents([event], block);
|
|
361
|
+
const index = this.unconfirmedEventQueue.indexOf(event);
|
|
362
|
+
if (index !== -1)
|
|
363
|
+
this.unconfirmedEventQueue.splice(index, 1);
|
|
364
|
+
}
|
|
365
|
+
else {
|
|
366
|
+
//Block hash doesn't match
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
catch (e) {
|
|
371
|
+
this.logger.error(`on('block'): Error when processing new block ${blockNumber}:`, e);
|
|
372
|
+
}
|
|
373
|
+
processing = false;
|
|
374
|
+
await this.addOrRemoveBlockListener();
|
|
375
|
+
};
|
|
332
376
|
await this.provider.on(this.spvVaultContractLogFilter, this.spvVaultContractListener = (log) => {
|
|
333
377
|
let events = this.evmSpvVaultContract.Events.toTypedEvents([log]);
|
|
334
378
|
events = events.filter(val => !val.removed);
|
|
@@ -339,42 +383,6 @@ class EVMChainEventsBrowser {
|
|
|
339
383
|
events = events.filter(val => !val.removed && (val.eventName === "Initialize" || val.eventName === "Refund" || val.eventName === "Claim"));
|
|
340
384
|
this.handleWsEvents(events);
|
|
341
385
|
});
|
|
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
386
|
}
|
|
379
387
|
async init() {
|
|
380
388
|
if (this.provider.websocket != null) {
|
package/package.json
CHANGED
|
@@ -439,45 +439,37 @@ 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;
|
|
471
468
|
try {
|
|
472
469
|
const latestSafeBlock = await this.provider.getBlock(this.chainInterface.config.safeBlockTag);
|
|
473
470
|
|
|
474
|
-
const events =
|
|
475
|
-
|
|
476
|
-
if(event.blockNumber <= latestSafeBlock.number) {
|
|
477
|
-
events.push(event);
|
|
478
|
-
return false;
|
|
479
|
-
}
|
|
480
|
-
return true;
|
|
471
|
+
const events = this.unconfirmedEventQueue.filter(event => {
|
|
472
|
+
return event.blockNumber <= latestSafeBlock.number;
|
|
481
473
|
});
|
|
482
474
|
|
|
483
475
|
const blocks: {[blockNumber: number]: Block} = {};
|
|
@@ -486,6 +478,8 @@ export class EVMChainEventsBrowser implements ChainEvents<EVMSwapData> {
|
|
|
486
478
|
if(block.hash===event.blockHash) {
|
|
487
479
|
//Valid event
|
|
488
480
|
await this.processEvents([event], block);
|
|
481
|
+
const index = this.unconfirmedEventQueue.indexOf(event);
|
|
482
|
+
if(index!==-1) this.unconfirmedEventQueue.splice(index, 1);
|
|
489
483
|
} else {
|
|
490
484
|
//Block hash doesn't match
|
|
491
485
|
}
|
|
@@ -494,6 +488,19 @@ export class EVMChainEventsBrowser implements ChainEvents<EVMSwapData> {
|
|
|
494
488
|
this.logger.error(`on('block'): Error when processing new block ${blockNumber}:`, e);
|
|
495
489
|
}
|
|
496
490
|
processing = false;
|
|
491
|
+
await this.addOrRemoveBlockListener();
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
await this.provider.on(this.spvVaultContractLogFilter, this.spvVaultContractListener = (log) => {
|
|
495
|
+
let events = this.evmSpvVaultContract.Events.toTypedEvents([log]);
|
|
496
|
+
events = events.filter(val => !val.removed);
|
|
497
|
+
this.handleWsEvents(events);
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
await this.provider.on(this.swapContractLogFilter, this.swapContractListener = (log) => {
|
|
501
|
+
let events = this.evmSwapContract.Events.toTypedEvents([log]);
|
|
502
|
+
events = events.filter(val => !val.removed && (val.eventName==="Initialize" || val.eventName==="Refund" || val.eventName==="Claim"));
|
|
503
|
+
this.handleWsEvents(events);
|
|
497
504
|
});
|
|
498
505
|
}
|
|
499
506
|
|