@atomiqlabs/chain-evm 1.1.6 → 1.1.7

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.
@@ -38,6 +38,10 @@ export declare class EVMChainEventsBrowser implements ChainEvents<EVMSwapData> {
38
38
  protected readonly spvVaultContractLogFilter: EventFilter;
39
39
  protected readonly swapContractLogFilter: EventFilter;
40
40
  protected unconfirmedEventQueue: AtomiqTypedEvent[];
41
+ protected confirmedEventQueue: {
42
+ event: AtomiqTypedEvent;
43
+ block: Block;
44
+ }[];
41
45
  constructor(chainInterface: EVMChainInterface, evmSwapContract: EVMSwapContract, evmSpvVaultContract: EVMSpvVaultContract<any>, pollIntervalSeconds?: number);
42
46
  private addProcessedEvent;
43
47
  private isEventProcessed;
@@ -21,6 +21,7 @@ class EVMChainEventsBrowser {
21
21
  this.listeners = [];
22
22
  this.logger = (0, Utils_1.getLogger)("EVMChainEventsBrowser: ");
23
23
  this.unconfirmedEventQueue = [];
24
+ this.confirmedEventQueue = [];
24
25
  this.wsStarted = false;
25
26
  this.chainInterface = chainInterface;
26
27
  this.provider = chainInterface.provider;
@@ -330,34 +331,42 @@ class EVMChainEventsBrowser {
330
331
  return this.addOrRemoveBlockListener();
331
332
  }
332
333
  async checkUnconfirmedEventsFinality() {
333
- const latestSafeBlock = await this.provider.getBlock(this.chainInterface.config.safeBlockTag);
334
- const events = this.unconfirmedEventQueue.filter(event => {
335
- return event.blockNumber <= latestSafeBlock.number;
336
- });
337
- const blocks = {};
338
- for (let event of events) {
339
- const block = blocks[event.blockNumber] ?? (blocks[event.blockNumber] = await this.provider.getBlock(event.blockNumber));
340
- if (block.hash === event.blockHash) {
341
- //Valid event
342
- await this.processEvents([event], block);
343
- const index = this.unconfirmedEventQueue.indexOf(event);
344
- if (index !== -1)
345
- this.unconfirmedEventQueue.splice(index, 1);
346
- }
347
- else {
348
- //Block hash doesn't match
334
+ if (this.unconfirmedEventQueue.length > 0) {
335
+ const latestSafeBlock = await this.provider.getBlock(this.chainInterface.config.safeBlockTag);
336
+ const events = this.unconfirmedEventQueue.filter(event => {
337
+ return event.blockNumber <= latestSafeBlock.number;
338
+ });
339
+ const blocks = {};
340
+ for (let event of events) {
341
+ const block = blocks[event.blockNumber] ?? (blocks[event.blockNumber] = await this.provider.getBlock(event.blockNumber));
342
+ if (block.hash === event.blockHash) {
343
+ //Valid event
344
+ const index = this.unconfirmedEventQueue.indexOf(event);
345
+ if (index !== -1)
346
+ this.unconfirmedEventQueue.splice(index, 1);
347
+ this.confirmedEventQueue.push({ event, block });
348
+ }
349
+ else {
350
+ //Block hash doesn't match
351
+ }
349
352
  }
350
353
  }
354
+ for (let confirmedEvent of this.confirmedEventQueue) {
355
+ await this.processEvents([confirmedEvent.event], confirmedEvent.block);
356
+ const index = this.confirmedEventQueue.indexOf(confirmedEvent);
357
+ if (index !== -1)
358
+ this.confirmedEventQueue.splice(index, 1);
359
+ }
351
360
  }
352
361
  async addOrRemoveBlockListener() {
353
362
  if (this.chainInterface.config.finalityCheckStrategy.type !== "blocks")
354
363
  return;
355
- if (this.unconfirmedEventQueue.length > 0) {
356
- this.logger.debug(`addOrRemoveBlockListener(): Adding block listener, unconfirmed event count: ${this.unconfirmedEventQueue.length}`);
364
+ if (this.unconfirmedEventQueue.length > 0 || this.confirmedEventQueue.length > 0) {
365
+ this.logger.debug(`addOrRemoveBlockListener(): Adding block listener, unconfirmed/confirmed event count: ${this.unconfirmedEventQueue.length + this.confirmedEventQueue.length}`);
357
366
  await this.provider.on("block", this.blockListener);
358
367
  }
359
368
  else {
360
- this.logger.debug(`addOrRemoveBlockListener(): Removing block listener, unconfirmed event count: ${this.unconfirmedEventQueue.length}`);
369
+ this.logger.debug(`addOrRemoveBlockListener(): Removing block listener, unconfirmed/confirmed event count: ${this.unconfirmedEventQueue.length + this.confirmedEventQueue.length}`);
361
370
  await this.provider.off("block", this.blockListener);
362
371
  }
363
372
  }
@@ -366,7 +375,7 @@ class EVMChainEventsBrowser {
366
375
  check = async () => {
367
376
  if (!this.wsStarted)
368
377
  return;
369
- if (this.unconfirmedEventQueue.length > 0) {
378
+ if (this.unconfirmedEventQueue.length > 0 || this.confirmedEventQueue.length > 0) {
370
379
  try {
371
380
  await this.checkUnconfirmedEventsFinality();
372
381
  }
@@ -386,7 +395,7 @@ class EVMChainEventsBrowser {
386
395
  this.blockListener = async (blockNumber) => {
387
396
  if (processing)
388
397
  return;
389
- if (this.unconfirmedEventQueue.length === 0)
398
+ if (this.unconfirmedEventQueue.length === 0 && this.confirmedEventQueue.length === 0)
390
399
  return;
391
400
  processing = true;
392
401
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atomiqlabs/chain-evm",
3
- "version": "1.1.6",
3
+ "version": "1.1.7",
4
4
  "description": "EVM specific base implementation",
5
5
  "main": "./dist/index.js",
6
6
  "types:": "./dist/index.d.ts",
@@ -60,6 +60,7 @@ export class EVMChainEventsBrowser implements ChainEvents<EVMSwapData> {
60
60
  protected readonly swapContractLogFilter: EventFilter;
61
61
 
62
62
  protected unconfirmedEventQueue: AtomiqTypedEvent[] = [];
63
+ protected confirmedEventQueue: {event: AtomiqTypedEvent, block: Block}[] = [];
63
64
 
64
65
  constructor(
65
66
  chainInterface: EVMChainInterface,
@@ -450,33 +451,41 @@ export class EVMChainEventsBrowser implements ChainEvents<EVMSwapData> {
450
451
  protected wsStarted: boolean = false;
451
452
 
452
453
  protected async checkUnconfirmedEventsFinality() {
453
- const latestSafeBlock = await this.provider.getBlock(this.chainInterface.config.safeBlockTag);
454
+ if(this.unconfirmedEventQueue.length>0) {
455
+ const latestSafeBlock = await this.provider.getBlock(this.chainInterface.config.safeBlockTag);
454
456
 
455
- const events = this.unconfirmedEventQueue.filter(event => {
456
- return event.blockNumber <= latestSafeBlock.number;
457
- });
457
+ const events = this.unconfirmedEventQueue.filter(event => {
458
+ return event.blockNumber <= latestSafeBlock.number;
459
+ });
458
460
 
459
- const blocks: {[blockNumber: number]: Block} = {};
460
- for(let event of events) {
461
- const block = blocks[event.blockNumber] ?? (blocks[event.blockNumber] = await this.provider.getBlock(event.blockNumber));
462
- if(block.hash===event.blockHash) {
463
- //Valid event
464
- await this.processEvents([event], block);
465
- const index = this.unconfirmedEventQueue.indexOf(event);
466
- if(index!==-1) this.unconfirmedEventQueue.splice(index, 1);
467
- } else {
468
- //Block hash doesn't match
461
+ const blocks: {[blockNumber: number]: Block} = {};
462
+ for(let event of events) {
463
+ const block = blocks[event.blockNumber] ?? (blocks[event.blockNumber] = await this.provider.getBlock(event.blockNumber));
464
+ if(block.hash===event.blockHash) {
465
+ //Valid event
466
+ const index = this.unconfirmedEventQueue.indexOf(event);
467
+ if(index!==-1) this.unconfirmedEventQueue.splice(index, 1);
468
+ this.confirmedEventQueue.push({event, block});
469
+ } else {
470
+ //Block hash doesn't match
471
+ }
469
472
  }
470
473
  }
474
+
475
+ for(let confirmedEvent of this.confirmedEventQueue) {
476
+ await this.processEvents([confirmedEvent.event], confirmedEvent.block);
477
+ const index = this.confirmedEventQueue.indexOf(confirmedEvent);
478
+ if(index!==-1) this.confirmedEventQueue.splice(index, 1);
479
+ }
471
480
  }
472
481
 
473
482
  protected async addOrRemoveBlockListener() {
474
483
  if(this.chainInterface.config.finalityCheckStrategy.type!=="blocks") return;
475
- if(this.unconfirmedEventQueue.length>0) {
476
- this.logger.debug(`addOrRemoveBlockListener(): Adding block listener, unconfirmed event count: ${this.unconfirmedEventQueue.length}`);
484
+ if(this.unconfirmedEventQueue.length>0 || this.confirmedEventQueue.length>0) {
485
+ this.logger.debug(`addOrRemoveBlockListener(): Adding block listener, unconfirmed/confirmed event count: ${this.unconfirmedEventQueue.length + this.confirmedEventQueue.length}`);
477
486
  await this.provider.on("block", this.blockListener);
478
487
  } else {
479
- this.logger.debug(`addOrRemoveBlockListener(): Removing block listener, unconfirmed event count: ${this.unconfirmedEventQueue.length}`);
488
+ this.logger.debug(`addOrRemoveBlockListener(): Removing block listener, unconfirmed/confirmed event count: ${this.unconfirmedEventQueue.length + this.confirmedEventQueue.length}`);
480
489
  await this.provider.off("block", this.blockListener);
481
490
  }
482
491
  }
@@ -485,7 +494,7 @@ export class EVMChainEventsBrowser implements ChainEvents<EVMSwapData> {
485
494
  let check: () => Promise<void>;
486
495
  check = async () => {
487
496
  if(!this.wsStarted) return;
488
- if(this.unconfirmedEventQueue.length>0) {
497
+ if(this.unconfirmedEventQueue.length>0 || this.confirmedEventQueue.length>0) {
489
498
  try {
490
499
  await this.checkUnconfirmedEventsFinality();
491
500
  } catch (e) {
@@ -504,7 +513,7 @@ export class EVMChainEventsBrowser implements ChainEvents<EVMSwapData> {
504
513
  let processing = false;
505
514
  this.blockListener = async (blockNumber: number) => {
506
515
  if(processing) return;
507
- if(this.unconfirmedEventQueue.length===0) return;
516
+ if(this.unconfirmedEventQueue.length===0 && this.confirmedEventQueue.length===0) return;
508
517
  processing = true;
509
518
  try {
510
519
  await this.checkUnconfirmedEventsFinality();