@metamask/eth-block-tracker 9.0.0 → 9.0.2

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.
@@ -16,6 +16,7 @@ export declare class PollingBlockTracker extends SafeEventEmitter implements Blo
16
16
  private readonly _usePastBlocks;
17
17
  private _currentBlock;
18
18
  private _blockResetTimeout?;
19
+ private _pollingTimeout?;
19
20
  private readonly _provider;
20
21
  private readonly _pollingInterval;
21
22
  private readonly _retryTimeout;
@@ -42,7 +43,12 @@ export declare class PollingBlockTracker extends SafeEventEmitter implements Blo
42
43
  checkForLatestBlock(): Promise<string>;
43
44
  private _start;
44
45
  private _end;
45
- private _synchronize;
46
46
  private _updateLatestBlock;
47
47
  private _fetchLatestBlock;
48
+ /**
49
+ * The core polling function that runs after each interval.
50
+ * Updates the latest block and then queues the next update.
51
+ */
52
+ private _updateAndQueue;
53
+ _clearPollingTimeout(): void;
48
54
  }
@@ -42,7 +42,7 @@ class PollingBlockTracker extends safe_event_emitter_1.default {
42
42
  }
43
43
  async destroy() {
44
44
  this._cancelBlockResetTimeout();
45
- await this._maybeEnd();
45
+ this._maybeEnd();
46
46
  super.removeAllListeners();
47
47
  }
48
48
  isRunning() {
@@ -98,23 +98,23 @@ class PollingBlockTracker extends safe_event_emitter_1.default {
98
98
  }
99
99
  this._maybeEnd();
100
100
  }
101
- async _maybeStart() {
101
+ _maybeStart() {
102
102
  if (this._isRunning) {
103
103
  return;
104
104
  }
105
105
  this._isRunning = true;
106
106
  // cancel setting latest block to stale
107
107
  this._cancelBlockResetTimeout();
108
- await this._start();
108
+ this._start();
109
109
  this.emit('_started');
110
110
  }
111
- async _maybeEnd() {
111
+ _maybeEnd() {
112
112
  if (!this._isRunning) {
113
113
  return;
114
114
  }
115
115
  this._isRunning = false;
116
116
  this._setupBlockResetTimeout();
117
- await this._end();
117
+ this._end();
118
118
  this.emit('_ended');
119
119
  }
120
120
  _getBlockTrackerEventCount() {
@@ -167,34 +167,13 @@ class PollingBlockTracker extends safe_event_emitter_1.default {
167
167
  await this._updateLatestBlock();
168
168
  return await this.getLatestBlock();
169
169
  }
170
- async _start() {
171
- this._synchronize();
172
- }
173
- async _end() {
174
- // No-op
170
+ _start() {
171
+ // Intentionally not awaited as this starts the polling via a timeout chain.
172
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
173
+ this._updateAndQueue();
175
174
  }
176
- async _synchronize() {
177
- var _a;
178
- while (this._isRunning) {
179
- try {
180
- await this._updateLatestBlock();
181
- const promise = timeout(this._pollingInterval, !this._keepEventLoopActive);
182
- this.emit('_waitingForNextIteration');
183
- await promise;
184
- }
185
- catch (err) {
186
- const newErr = new Error(`PollingBlockTracker - encountered an error while attempting to update latest block:\n${(_a = err.stack) !== null && _a !== void 0 ? _a : err}`);
187
- try {
188
- this.emit('error', newErr);
189
- }
190
- catch (emitErr) {
191
- console.error(newErr);
192
- }
193
- const promise = timeout(this._retryTimeout, !this._keepEventLoopActive);
194
- this.emit('_waitingForNextIteration');
195
- await promise;
196
- }
197
- }
175
+ _end() {
176
+ this._clearPollingTimeout();
198
177
  }
199
178
  async _updateLatestBlock() {
200
179
  // fetch + set latest block
@@ -219,26 +198,49 @@ class PollingBlockTracker extends safe_event_emitter_1.default {
219
198
  }
220
199
  return res.result;
221
200
  }
222
- }
223
- exports.PollingBlockTracker = PollingBlockTracker;
224
- /**
225
- * Waits for the specified amount of time.
226
- *
227
- * @param duration - The amount of time in milliseconds.
228
- * @param unref - Assuming this function is run in a Node context, governs
229
- * whether Node should wait before the `setTimeout` has completed before ending
230
- * the process (true for no, false for yes). Defaults to false.
231
- * @returns A promise that can be used to wait.
232
- */
233
- async function timeout(duration, unref) {
234
- return new Promise((resolve) => {
235
- const timeoutRef = setTimeout(resolve, duration);
236
- // don't keep process open
237
- if (timeoutRef.unref && unref) {
201
+ /**
202
+ * The core polling function that runs after each interval.
203
+ * Updates the latest block and then queues the next update.
204
+ */
205
+ async _updateAndQueue() {
206
+ var _a;
207
+ let interval = this._pollingInterval;
208
+ try {
209
+ await this._updateLatestBlock();
210
+ }
211
+ catch (err) {
212
+ const newErr = new Error(`PollingBlockTracker - encountered an error while attempting to update latest block:\n${(_a = err.stack) !== null && _a !== void 0 ? _a : err}`);
213
+ try {
214
+ this.emit('error', newErr);
215
+ }
216
+ catch (emitErr) {
217
+ console.error(newErr);
218
+ }
219
+ interval = this._retryTimeout;
220
+ }
221
+ if (!this._isRunning) {
222
+ return;
223
+ }
224
+ this._clearPollingTimeout();
225
+ const timeoutRef = setTimeout(() => {
226
+ // Intentionally not awaited as this just continues the polling loop.
227
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
228
+ this._updateAndQueue();
229
+ }, interval);
230
+ if (timeoutRef.unref && !this._keepEventLoopActive) {
238
231
  timeoutRef.unref();
239
232
  }
240
- });
233
+ this._pollingTimeout = timeoutRef;
234
+ this.emit('_waitingForNextIteration');
235
+ }
236
+ _clearPollingTimeout() {
237
+ if (this._pollingTimeout) {
238
+ clearTimeout(this._pollingTimeout);
239
+ this._pollingTimeout = undefined;
240
+ }
241
+ }
241
242
  }
243
+ exports.PollingBlockTracker = PollingBlockTracker;
242
244
  /**
243
245
  * Converts a number represented as a string in hexadecimal format into a native
244
246
  * number.
@@ -1 +1 @@
1
- {"version":3,"file":"PollingBlockTracker.js","sourceRoot":"","sources":["../src/PollingBlockTracker.ts"],"names":[],"mappings":";;;;;;AACA,sFAA4D;AAE5D,4EAAmD;AACnD,gDAAwB;AAGxB,mDAAoE;AAEpE,MAAM,GAAG,GAAG,IAAA,kCAAkB,EAAC,6BAAa,EAAE,uBAAuB,CAAC,CAAC;AACvE,MAAM,cAAc,GAAG,IAAA,4BAAiB,GAAE,CAAC;AAC3C,MAAM,GAAG,GAAG,IAAI,CAAC;AAEjB,MAAM,YAAY,GAAG,CAAC,WAAmB,EAAE,YAAoB,EAAE,EAAE,CACjE,WAAW,GAAG,YAAY,CAAC;AAC7B,MAAM,kBAAkB,GAAwB,CAAC,MA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AK,CAAC,iBAAiB;QAC7B,MAAM,GAAG,GAA2B;YAClC,OAAO,EAAE,KAAK;YACd,EAAE,EAAE,cAAc,EAAE;YACpB,MAAM,EAAE,iBAAiB;YACzB,MAAM,EAAE,EAAE;SACX,CAAC;QACF,IAAI,IAAI,CAAC,iBAAiB,EAAE;YAC1B,GAAG,CAAC,SAAS,GAAG,IAAI,CAAC;SACtB;QAED,GAAG,CAAC,gBAAgB,EAAE,GAAG,CAAC,CAAC;QAC3B,MAAM,GAAG,GAAG,MAAM,IAAA,cAAI,EAAC,CAAC,EAAE,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC;QACpE,GAAG,CAAC,cAAc,EAAE,GAAG,CAAC,CAAC;QACzB,IAAI,GAAG,CAAC,KAAK,EAAE;YACb,MAAM,IAAI,KAAK,CACb,4DAA4D,GAAG,CAAC,KAAK,CAAC,OAAO,EAAE,CAChF,CAAC;SACH;QACD,OAAO,GAAG,CAAC,MAAM,CAAC;IACpB,CAAC;CACF;AAlRD,kDAkRC;AAED;;;;;;;;GAQG;AACH,KAAK,UAAU,OAAO,CAAC,QAAgB,EAAE,KAAc;IACrD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC7B,MAAM,UAAU,GAAG,UAAU,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;QACjD,0BAA0B;QAC1B,IAAI,UAAU,CAAC,KAAK,IAAI,KAAK,EAAE;YAC7B,UAAU,CAAC,KAAK,EAAE,CAAC;SACpB;IACH,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;;;;GAMG;AACH,SAAS,QAAQ,CAAC,MAAc;IAC9B,OAAO,MAAM,CAAC,QAAQ,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;AACrC,CAAC","sourcesContent":["import type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider';\nimport SafeEventEmitter from '@metamask/safe-event-emitter';\nimport type { JsonRpcRequest } from '@metamask/utils';\nimport getCreateRandomId from 'json-rpc-random-id';\nimport pify from 'pify';\n\nimport type { BlockTracker } from './BlockTracker';\nimport { projectLogger, createModuleLogger } from './logging-utils';\n\nconst log = createModuleLogger(projectLogger, 'polling-block-tracker');\nconst createRandomId = getCreateRandomId();\nconst sec = 1000;\n\nconst calculateSum = (accumulator: number, currentValue: number) =>\n accumulator + currentValue;\nconst blockTrackerEvents: (string | symbol)[] = ['sync', 'latest'];\n\nexport interface PollingBlockTrackerOptions {\n provider?: SafeEventEmitterProvider;\n pollingInterval?: number;\n retryTimeout?: number;\n keepEventLoopActive?: boolean;\n setSkipCacheFlag?: boolean;\n blockResetDuration?: number;\n usePastBlocks?: boolean;\n}\n\ninterface ExtendedJsonRpcRequest extends JsonRpcRequest<[]> {\n skipCache?: boolean;\n}\n\nexport class PollingBlockTracker\n extends SafeEventEmitter\n implements BlockTracker\n{\n private _isRunning: boolean;\n\n private readonly _blockResetDuration: number;\n\n private readonly _usePastBlocks: boolean;\n\n private _currentBlock: string | null;\n\n private _blockResetTimeout?: ReturnType<typeof setTimeout>;\n\n private readonly _provider: SafeEventEmitterProvider;\n\n private readonly _pollingInterval: number;\n\n private readonly _retryTimeout: number;\n\n private readonly _keepEventLoopActive: boolean;\n\n private readonly _setSkipCacheFlag: boolean;\n\n constructor(opts: PollingBlockTrackerOptions = {}) {\n // parse + validate args\n if (!opts.provider) {\n throw new Error('PollingBlockTracker - no provider specified.');\n }\n\n super();\n\n // config\n this._blockResetDuration = opts.blockResetDuration || 20 * sec;\n this._usePastBlocks = opts.usePastBlocks || false;\n // state\n this._currentBlock = null;\n this._isRunning = false;\n\n // bind functions for internal use\n this._onNewListener = this._onNewListener.bind(this);\n this._onRemoveListener = this._onRemoveListener.bind(this);\n this._resetCurrentBlock = this._resetCurrentBlock.bind(this);\n\n // listen for handler changes\n this._setupInternalEvents();\n\n // config\n this._provider = opts.provider;\n this._pollingInterval = opts.pollingInterval || 20 * sec;\n this._retryTimeout = opts.retryTimeout || this._pollingInterval / 10;\n this._keepEventLoopActive =\n opts.keepEventLoopActive === undefined ? true : opts.keepEventLoopActive;\n this._setSkipCacheFlag = opts.setSkipCacheFlag || false;\n }\n\n async destroy() {\n this._cancelBlockResetTimeout();\n await this._maybeEnd();\n super.removeAllListeners();\n }\n\n isRunning(): boolean {\n return this._isRunning;\n }\n\n getCurrentBlock(): string | null {\n return this._currentBlock;\n }\n\n async getLatestBlock(): Promise<string> {\n // return if available\n if (this._currentBlock) {\n return this._currentBlock;\n }\n // wait for a new latest block\n const latestBlock: string = await new Promise((resolve) =>\n this.once('latest', resolve),\n );\n // return newly set current block\n return latestBlock;\n }\n\n // dont allow module consumer to remove our internal event listeners\n removeAllListeners(eventName?: string | symbol) {\n // perform default behavior, preserve fn arity\n if (eventName) {\n super.removeAllListeners(eventName);\n } else {\n super.removeAllListeners();\n }\n\n // re-add internal events\n this._setupInternalEvents();\n // trigger stop check just in case\n this._onRemoveListener();\n\n return this;\n }\n\n private _setupInternalEvents(): void {\n // first remove listeners for idempotence\n this.removeListener('newListener', this._onNewListener);\n this.removeListener('removeListener', this._onRemoveListener);\n // then add them\n this.on('newListener', this._onNewListener);\n this.on('removeListener', this._onRemoveListener);\n }\n\n private _onNewListener(eventName: string | symbol): void {\n // `newListener` is called *before* the listener is added\n if (blockTrackerEvents.includes(eventName)) {\n // TODO: Handle dangling promise\n this._maybeStart();\n }\n }\n\n private _onRemoveListener(): void {\n // `removeListener` is called *after* the listener is removed\n if (this._getBlockTrackerEventCount() > 0) {\n return;\n }\n this._maybeEnd();\n }\n\n private async _maybeStart(): Promise<void> {\n if (this._isRunning) {\n return;\n }\n this._isRunning = true;\n // cancel setting latest block to stale\n this._cancelBlockResetTimeout();\n await this._start();\n this.emit('_started');\n }\n\n private async _maybeEnd(): Promise<void> {\n if (!this._isRunning) {\n return;\n }\n this._isRunning = false;\n this._setupBlockResetTimeout();\n await this._end();\n this.emit('_ended');\n }\n\n private _getBlockTrackerEventCount(): number {\n return blockTrackerEvents\n .map((eventName) => this.listenerCount(eventName))\n .reduce(calculateSum);\n }\n\n private _shouldUseNewBlock(newBlock: string) {\n const currentBlock = this._currentBlock;\n if (!currentBlock) {\n return true;\n }\n const newBlockInt = hexToInt(newBlock);\n const currentBlockInt = hexToInt(currentBlock);\n\n return (\n (this._usePastBlocks && newBlockInt < currentBlockInt) ||\n newBlockInt > currentBlockInt\n );\n }\n\n private _newPotentialLatest(newBlock: string): void {\n if (!this._shouldUseNewBlock(newBlock)) {\n return;\n }\n this._setCurrentBlock(newBlock);\n }\n\n private _setCurrentBlock(newBlock: string): void {\n const oldBlock = this._currentBlock;\n this._currentBlock = newBlock;\n this.emit('latest', newBlock);\n this.emit('sync', { oldBlock, newBlock });\n }\n\n private _setupBlockResetTimeout(): void {\n // clear any existing timeout\n this._cancelBlockResetTimeout();\n // clear latest block when stale\n this._blockResetTimeout = setTimeout(\n this._resetCurrentBlock,\n this._blockResetDuration,\n );\n\n // nodejs - dont hold process open\n if (this._blockResetTimeout.unref) {\n this._blockResetTimeout.unref();\n }\n }\n\n private _cancelBlockResetTimeout(): void {\n if (this._blockResetTimeout) {\n clearTimeout(this._blockResetTimeout);\n }\n }\n\n private _resetCurrentBlock(): void {\n this._currentBlock = null;\n }\n\n // trigger block polling\n async checkForLatestBlock() {\n await this._updateLatestBlock();\n return await this.getLatestBlock();\n }\n\n private async _start(): Promise<void> {\n this._synchronize();\n }\n\n private async _end(): Promise<void> {\n // No-op\n }\n\n private async _synchronize(): Promise<void> {\n while (this._isRunning) {\n try {\n await this._updateLatestBlock();\n const promise = timeout(\n this._pollingInterval,\n !this._keepEventLoopActive,\n );\n this.emit('_waitingForNextIteration');\n await promise;\n } catch (err: any) {\n const newErr = new Error(\n `PollingBlockTracker - encountered an error while attempting to update latest block:\\n${\n err.stack ?? err\n }`,\n );\n try {\n this.emit('error', newErr);\n } catch (emitErr) {\n console.error(newErr);\n }\n const promise = timeout(this._retryTimeout, !this._keepEventLoopActive);\n this.emit('_waitingForNextIteration');\n await promise;\n }\n }\n }\n\n private async _updateLatestBlock(): Promise<void> {\n // fetch + set latest block\n const latestBlock = await this._fetchLatestBlock();\n this._newPotentialLatest(latestBlock);\n }\n\n private async _fetchLatestBlock(): Promise<string> {\n const req: ExtendedJsonRpcRequest = {\n jsonrpc: '2.0',\n id: createRandomId(),\n method: 'eth_blockNumber',\n params: [],\n };\n if (this._setSkipCacheFlag) {\n req.skipCache = true;\n }\n\n log('Making request', req);\n const res = await pify((cb) => this._provider.sendAsync(req, cb))();\n log('Got response', res);\n if (res.error) {\n throw new Error(\n `PollingBlockTracker - encountered error fetching block:\\n${res.error.message}`,\n );\n }\n return res.result;\n }\n}\n\n/**\n * Waits for the specified amount of time.\n *\n * @param duration - The amount of time in milliseconds.\n * @param unref - Assuming this function is run in a Node context, governs\n * whether Node should wait before the `setTimeout` has completed before ending\n * the process (true for no, false for yes). Defaults to false.\n * @returns A promise that can be used to wait.\n */\nasync function timeout(duration: number, unref: boolean) {\n return new Promise((resolve) => {\n const timeoutRef = setTimeout(resolve, duration);\n // don't keep process open\n if (timeoutRef.unref && unref) {\n timeoutRef.unref();\n }\n });\n}\n\n/**\n * Converts a number represented as a string in hexadecimal format into a native\n * number.\n *\n * @param hexInt - The hex string.\n * @returns The number.\n */\nfunction hexToInt(hexInt: string): number {\n return Number.parseInt(hexInt, 16);\n}\n"]}
1
+ {"version":3,"file":"PollingBlockTracker.js","sourceRoot":"","sources":["../src/PollingBlockTracker.ts"],"names":[],"mappings":";;;;;;AACA,sFAA4D;AAE5D,4EAAmD;AACnD,gDAAwB;AAGxB,mDAAoE;AAEpE,MAAM,GAAG,GAAG,IAAA,kCAAkB,EAAC,6BAAa,EAAE,uBAAuB,CAAC,CAAC;AACvE,MAAM,cAAc,GAAG,IAAA,4BAAiB,GAAE,CAAC;AAC3C,MAAM,GAAG,GAAG,IAAI,CAAC;AAEjB,MAAM,YAAY,GAAG,CAAC,WAAmB,EA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eEventEmitterProvider } from '@metamask/eth-json-rpc-provider';\nimport SafeEventEmitter from '@metamask/safe-event-emitter';\nimport type { JsonRpcRequest } from '@metamask/utils';\nimport getCreateRandomId from 'json-rpc-random-id';\nimport pify from 'pify';\n\nimport type { BlockTracker } from './BlockTracker';\nimport { projectLogger, createModuleLogger } from './logging-utils';\n\nconst log = createModuleLogger(projectLogger, 'polling-block-tracker');\nconst createRandomId = getCreateRandomId();\nconst sec = 1000;\n\nconst calculateSum = (accumulator: number, currentValue: number) =>\n accumulator + currentValue;\nconst blockTrackerEvents: (string | symbol)[] = ['sync', 'latest'];\n\nexport interface PollingBlockTrackerOptions {\n provider?: SafeEventEmitterProvider;\n pollingInterval?: number;\n retryTimeout?: number;\n keepEventLoopActive?: boolean;\n setSkipCacheFlag?: boolean;\n blockResetDuration?: number;\n usePastBlocks?: boolean;\n}\n\ninterface ExtendedJsonRpcRequest extends JsonRpcRequest<[]> {\n skipCache?: boolean;\n}\n\nexport class PollingBlockTracker\n extends SafeEventEmitter\n implements BlockTracker\n{\n private _isRunning: boolean;\n\n private readonly _blockResetDuration: number;\n\n private readonly _usePastBlocks: boolean;\n\n private _currentBlock: string | null;\n\n private _blockResetTimeout?: ReturnType<typeof setTimeout>;\n\n private _pollingTimeout?: ReturnType<typeof setTimeout>;\n\n private readonly _provider: SafeEventEmitterProvider;\n\n private readonly _pollingInterval: number;\n\n private readonly _retryTimeout: number;\n\n private readonly _keepEventLoopActive: boolean;\n\n private readonly _setSkipCacheFlag: boolean;\n\n constructor(opts: PollingBlockTrackerOptions = {}) {\n // parse + validate args\n if (!opts.provider) {\n throw new Error('PollingBlockTracker - no provider specified.');\n }\n\n super();\n\n // config\n this._blockResetDuration = opts.blockResetDuration || 20 * sec;\n this._usePastBlocks = opts.usePastBlocks || false;\n // state\n this._currentBlock = null;\n this._isRunning = false;\n\n // bind functions for internal use\n this._onNewListener = this._onNewListener.bind(this);\n this._onRemoveListener = this._onRemoveListener.bind(this);\n this._resetCurrentBlock = this._resetCurrentBlock.bind(this);\n\n // listen for handler changes\n this._setupInternalEvents();\n\n // config\n this._provider = opts.provider;\n this._pollingInterval = opts.pollingInterval || 20 * sec;\n this._retryTimeout = opts.retryTimeout || this._pollingInterval / 10;\n this._keepEventLoopActive =\n opts.keepEventLoopActive === undefined ? true : opts.keepEventLoopActive;\n this._setSkipCacheFlag = opts.setSkipCacheFlag || false;\n }\n\n async destroy() {\n this._cancelBlockResetTimeout();\n this._maybeEnd();\n super.removeAllListeners();\n }\n\n isRunning(): boolean {\n return this._isRunning;\n }\n\n getCurrentBlock(): string | null {\n return this._currentBlock;\n }\n\n async getLatestBlock(): Promise<string> {\n // return if available\n if (this._currentBlock) {\n return this._currentBlock;\n }\n // wait for a new latest block\n const latestBlock: string = await new Promise((resolve) =>\n this.once('latest', resolve),\n );\n // return newly set current block\n return latestBlock;\n }\n\n // dont allow module consumer to remove our internal event listeners\n removeAllListeners(eventName?: string | symbol) {\n // perform default behavior, preserve fn arity\n if (eventName) {\n super.removeAllListeners(eventName);\n } else {\n super.removeAllListeners();\n }\n\n // re-add internal events\n this._setupInternalEvents();\n // trigger stop check just in case\n this._onRemoveListener();\n\n return this;\n }\n\n private _setupInternalEvents(): void {\n // first remove listeners for idempotence\n this.removeListener('newListener', this._onNewListener);\n this.removeListener('removeListener', this._onRemoveListener);\n // then add them\n this.on('newListener', this._onNewListener);\n this.on('removeListener', this._onRemoveListener);\n }\n\n private _onNewListener(eventName: string | symbol): void {\n // `newListener` is called *before* the listener is added\n if (blockTrackerEvents.includes(eventName)) {\n // TODO: Handle dangling promise\n this._maybeStart();\n }\n }\n\n private _onRemoveListener(): void {\n // `removeListener` is called *after* the listener is removed\n if (this._getBlockTrackerEventCount() > 0) {\n return;\n }\n this._maybeEnd();\n }\n\n private _maybeStart() {\n if (this._isRunning) {\n return;\n }\n\n this._isRunning = true;\n // cancel setting latest block to stale\n this._cancelBlockResetTimeout();\n this._start();\n this.emit('_started');\n }\n\n private _maybeEnd() {\n if (!this._isRunning) {\n return;\n }\n\n this._isRunning = false;\n this._setupBlockResetTimeout();\n this._end();\n this.emit('_ended');\n }\n\n private _getBlockTrackerEventCount(): number {\n return blockTrackerEvents\n .map((eventName) => this.listenerCount(eventName))\n .reduce(calculateSum);\n }\n\n private _shouldUseNewBlock(newBlock: string) {\n const currentBlock = this._currentBlock;\n if (!currentBlock) {\n return true;\n }\n const newBlockInt = hexToInt(newBlock);\n const currentBlockInt = hexToInt(currentBlock);\n\n return (\n (this._usePastBlocks && newBlockInt < currentBlockInt) ||\n newBlockInt > currentBlockInt\n );\n }\n\n private _newPotentialLatest(newBlock: string): void {\n if (!this._shouldUseNewBlock(newBlock)) {\n return;\n }\n this._setCurrentBlock(newBlock);\n }\n\n private _setCurrentBlock(newBlock: string): void {\n const oldBlock = this._currentBlock;\n this._currentBlock = newBlock;\n this.emit('latest', newBlock);\n this.emit('sync', { oldBlock, newBlock });\n }\n\n private _setupBlockResetTimeout(): void {\n // clear any existing timeout\n this._cancelBlockResetTimeout();\n // clear latest block when stale\n this._blockResetTimeout = setTimeout(\n this._resetCurrentBlock,\n this._blockResetDuration,\n );\n\n // nodejs - dont hold process open\n if (this._blockResetTimeout.unref) {\n this._blockResetTimeout.unref();\n }\n }\n\n private _cancelBlockResetTimeout(): void {\n if (this._blockResetTimeout) {\n clearTimeout(this._blockResetTimeout);\n }\n }\n\n private _resetCurrentBlock(): void {\n this._currentBlock = null;\n }\n\n // trigger block polling\n async checkForLatestBlock() {\n await this._updateLatestBlock();\n return await this.getLatestBlock();\n }\n\n private _start() {\n // Intentionally not awaited as this starts the polling via a timeout chain.\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this._updateAndQueue();\n }\n\n private _end() {\n this._clearPollingTimeout();\n }\n\n private async _updateLatestBlock(): Promise<void> {\n // fetch + set latest block\n const latestBlock = await this._fetchLatestBlock();\n this._newPotentialLatest(latestBlock);\n }\n\n private async _fetchLatestBlock(): Promise<string> {\n const req: ExtendedJsonRpcRequest = {\n jsonrpc: '2.0',\n id: createRandomId(),\n method: 'eth_blockNumber',\n params: [] as [],\n };\n if (this._setSkipCacheFlag) {\n req.skipCache = true;\n }\n\n log('Making request', req);\n const res = await pify((cb) => this._provider.sendAsync(req, cb))();\n log('Got response', res);\n if (res.error) {\n throw new Error(\n `PollingBlockTracker - encountered error fetching block:\\n${res.error.message}`,\n );\n }\n return res.result;\n }\n\n /**\n * The core polling function that runs after each interval.\n * Updates the latest block and then queues the next update.\n */\n private async _updateAndQueue() {\n let interval = this._pollingInterval;\n\n try {\n await this._updateLatestBlock();\n } catch (err: any) {\n const newErr = new Error(\n `PollingBlockTracker - encountered an error while attempting to update latest block:\\n${\n err.stack ?? err\n }`,\n );\n\n try {\n this.emit('error', newErr);\n } catch (emitErr) {\n console.error(newErr);\n }\n\n interval = this._retryTimeout;\n }\n\n if (!this._isRunning) {\n return;\n }\n\n this._clearPollingTimeout();\n\n const timeoutRef = setTimeout(() => {\n // Intentionally not awaited as this just continues the polling loop.\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this._updateAndQueue();\n }, interval);\n\n if (timeoutRef.unref && !this._keepEventLoopActive) {\n timeoutRef.unref();\n }\n\n this._pollingTimeout = timeoutRef;\n\n this.emit('_waitingForNextIteration');\n }\n\n _clearPollingTimeout() {\n if (this._pollingTimeout) {\n clearTimeout(this._pollingTimeout);\n this._pollingTimeout = undefined;\n }\n }\n}\n\n/**\n * Converts a number represented as a string in hexadecimal format into a native\n * number.\n *\n * @param hexInt - The hex string.\n * @returns The number.\n */\nfunction hexToInt(hexInt: string): number {\n return Number.parseInt(hexInt, 16);\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/eth-block-tracker",
3
- "version": "9.0.0",
3
+ "version": "9.0.2",
4
4
  "description": "A block tracker for the Ethereum blockchain. Keeps track of the latest block.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -25,7 +25,7 @@
25
25
  "test:watch": "jest --watch"
26
26
  },
27
27
  "dependencies": {
28
- "@metamask/eth-json-rpc-provider": "^2.1.0",
28
+ "@metamask/eth-json-rpc-provider": "^2.3.1",
29
29
  "@metamask/safe-event-emitter": "^3.0.0",
30
30
  "@metamask/utils": "^8.1.0",
31
31
  "json-rpc-random-id": "^1.0.1",