@metamask/eth-block-tracker 12.2.0 → 13.0.0
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.
- package/CHANGELOG.md +16 -1
- package/README.md +2 -7
- package/dist/{BlockTracker.js → BlockTracker.cjs} +1 -1
- package/dist/BlockTracker.cjs.map +1 -0
- package/dist/{BlockTracker.d.ts → BlockTracker.d.cts} +3 -2
- package/dist/BlockTracker.d.cts.map +1 -0
- package/dist/BlockTracker.d.mts +9 -0
- package/dist/BlockTracker.d.mts.map +1 -0
- package/dist/BlockTracker.mjs +2 -0
- package/dist/BlockTracker.mjs.map +1 -0
- package/dist/{PollingBlockTracker.js → PollingBlockTracker.cjs} +6 -2
- package/dist/PollingBlockTracker.cjs.map +1 -0
- package/dist/{PollingBlockTracker.d.ts → PollingBlockTracker.d.cts} +6 -5
- package/dist/PollingBlockTracker.d.cts.map +1 -0
- package/dist/PollingBlockTracker.d.mts +65 -0
- package/dist/PollingBlockTracker.d.mts.map +1 -0
- package/dist/PollingBlockTracker.mjs +353 -0
- package/dist/PollingBlockTracker.mjs.map +1 -0
- package/dist/{index.js → index.cjs} +2 -3
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +3 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +3 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/dist/{logging-utils.js → logging-utils.cjs} +1 -1
- package/dist/logging-utils.cjs.map +1 -0
- package/dist/{logging-utils.d.ts → logging-utils.d.cts} +2 -1
- package/dist/logging-utils.d.cts.map +1 -0
- package/dist/logging-utils.d.mts +5 -0
- package/dist/logging-utils.d.mts.map +1 -0
- package/dist/logging-utils.mjs +4 -0
- package/dist/logging-utils.mjs.map +1 -0
- package/package.json +66 -40
- package/dist/BlockTracker.js.map +0 -1
- package/dist/PollingBlockTracker.js.map +0 -1
- package/dist/index.d.ts +0 -2
- package/dist/index.js.map +0 -1
- package/dist/logging-utils.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [13.0.0]
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
|
|
14
|
+
- **BREAKING:** Distribute separate CommonJS and ESM files ([#351](https://github.com/MetaMask/eth-block-tracker/pull/351))
|
|
15
|
+
- Use the `exports` field of `package.json`, breaking some previously valid imports.
|
|
16
|
+
|
|
17
|
+
## [12.2.1]
|
|
18
|
+
|
|
19
|
+
### Fixed
|
|
20
|
+
|
|
21
|
+
- `PollingBlockTracker.checkForLatestBlock()` and `getLatestBlock()` now clear the cached block after `blockReset` duration when called and the `PollingBlockTracker` is not polling. ([#348](https://github.com/MetaMask/eth-block-tracker/pull/348))
|
|
22
|
+
|
|
10
23
|
## [12.2.0]
|
|
11
24
|
|
|
12
25
|
### Changed
|
|
@@ -300,7 +313,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
300
313
|
|
|
301
314
|
- Add RpcBlockTracker
|
|
302
315
|
|
|
303
|
-
[Unreleased]: https://github.com/MetaMask/eth-block-tracker/compare/
|
|
316
|
+
[Unreleased]: https://github.com/MetaMask/eth-block-tracker/compare/v13.0.0...HEAD
|
|
317
|
+
[13.0.0]: https://github.com/MetaMask/eth-block-tracker/compare/v12.2.1...v13.0.0
|
|
318
|
+
[12.2.1]: https://github.com/MetaMask/eth-block-tracker/compare/v12.2.0...v12.2.1
|
|
304
319
|
[12.2.0]: https://github.com/MetaMask/eth-block-tracker/compare/v12.1.0...v12.2.0
|
|
305
320
|
[12.1.0]: https://github.com/MetaMask/eth-block-tracker/compare/v12.0.1...v12.1.0
|
|
306
321
|
[12.0.1]: https://github.com/MetaMask/eth-block-tracker/compare/v12.0.0...v12.0.1
|
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# @metamask/eth-block-tracker
|
|
2
2
|
|
|
3
|
+
<table><tr><td><p align="center"><b>⚠️ PLEASE READ ⚠️</b></p><p align="center">This package is currently being migrated to our <a href="https://github.com/MetaMask/core"><code>core</code></a> monorepo. Please do not make any commits to this repository while this migration is taking place, as they will not be transferred over.</p></td></tr></table>
|
|
4
|
+
|
|
3
5
|
This module walks the Ethereum blockchain, keeping track of the latest block. It uses a web3 provider as a data source and will continuously poll for the next block.
|
|
4
6
|
|
|
5
7
|
## Installation
|
|
@@ -106,35 +108,28 @@ Run `yarn lint` to run the linter, or run `yarn lint:fix` to run the linter and
|
|
|
106
108
|
The project follows the same release process as the other libraries in the MetaMask organization. The GitHub Actions [`action-create-release-pr`](https://github.com/MetaMask/action-create-release-pr) and [`action-publish-release`](https://github.com/MetaMask/action-publish-release) are used to automate the release process; see those repositories for more information about how they work.
|
|
107
109
|
|
|
108
110
|
1. Choose a release version.
|
|
109
|
-
|
|
110
111
|
- The release version should be chosen according to SemVer. Analyze the changes to see whether they include any breaking changes, new features, or deprecations, then choose the appropriate SemVer version. See [the SemVer specification](https://semver.org/) for more information.
|
|
111
112
|
|
|
112
113
|
2. If this release is backporting changes onto a previous release, then ensure there is a major version branch for that version (e.g. `1.x` for a `v1` backport release).
|
|
113
|
-
|
|
114
114
|
- The major version branch should be set to the most recent release with that major version. For example, when backporting a `v1.0.2` release, you'd want to ensure there was a `1.x` branch that was set to the `v1.0.1` tag.
|
|
115
115
|
|
|
116
116
|
3. Trigger the [`workflow_dispatch`](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#workflow_dispatch) event [manually](https://docs.github.com/en/actions/managing-workflow-runs/manually-running-a-workflow) for the `Create Release Pull Request` action to create the release PR.
|
|
117
|
-
|
|
118
117
|
- For a backport release, the base branch should be the major version branch that you ensured existed in step 2. For a normal release, the base branch should be the main branch for that repository (which should be the default value).
|
|
119
118
|
- This should trigger the [`action-create-release-pr`](https://github.com/MetaMask/action-create-release-pr) workflow to create the release PR.
|
|
120
119
|
|
|
121
120
|
4. Update the changelog to move each change entry into the appropriate change category ([See here](https://keepachangelog.com/en/1.0.0/#types) for the full list of change categories, and the correct ordering), and edit them to be more easily understood by users of the package.
|
|
122
|
-
|
|
123
121
|
- Generally any changes that don't affect consumers of the package (e.g. lockfile changes or development environment changes) are omitted. Exceptions may be made for changes that might be of interest despite not having an effect upon the published package (e.g. major test improvements, security improvements, improved documentation, etc.).
|
|
124
122
|
- Try to explain each change in terms that users of the package would understand (e.g. avoid referencing internal variables/concepts).
|
|
125
123
|
- Consolidate related changes into one change entry if it makes it easier to explain.
|
|
126
124
|
- Run `yarn auto-changelog validate --rc --prettier` to check that the changelog is correctly formatted.
|
|
127
125
|
|
|
128
126
|
5. Review and QA the release.
|
|
129
|
-
|
|
130
127
|
- If changes are made to the base branch, the release branch will need to be updated with these changes and review/QA will need to restart again. As such, it's probably best to avoid merging other PRs into the base branch while review is underway.
|
|
131
128
|
|
|
132
129
|
6. Squash & Merge the release.
|
|
133
|
-
|
|
134
130
|
- This should trigger the [`action-publish-release`](https://github.com/MetaMask/action-publish-release) workflow to tag the final release commit and publish the release on GitHub.
|
|
135
131
|
|
|
136
132
|
7. Publish the release on npm.
|
|
137
|
-
|
|
138
133
|
- Be very careful to use a clean local environment to publish the release, and follow exactly the same steps used during CI.
|
|
139
134
|
- Use `npm publish --dry-run` to examine the release contents to ensure the correct files are included. Compare to previous releases if necessary (e.g. using `https://unpkg.com/browse/[package name]@[package version]/`).
|
|
140
135
|
- Once you are confident the release contents are correct, publish the release using `npm publish`.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BlockTracker.cjs","sourceRoot":"","sources":["../src/BlockTracker.ts"],"names":[],"mappings":"","sourcesContent":["import type SafeEventEmitter from '@metamask/safe-event-emitter';\n\nexport type BlockTracker = SafeEventEmitter & {\n destroy(): Promise<void>;\n\n isRunning(): boolean;\n\n getCurrentBlock(): string | null;\n\n getLatestBlock(): Promise<string>;\n\n checkForLatestBlock(): Promise<string>;\n};\n"]}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import type SafeEventEmitter from
|
|
2
|
-
export
|
|
1
|
+
import type SafeEventEmitter from "@metamask/safe-event-emitter";
|
|
2
|
+
export type BlockTracker = SafeEventEmitter & {
|
|
3
3
|
destroy(): Promise<void>;
|
|
4
4
|
isRunning(): boolean;
|
|
5
5
|
getCurrentBlock(): string | null;
|
|
6
6
|
getLatestBlock(): Promise<string>;
|
|
7
7
|
checkForLatestBlock(): Promise<string>;
|
|
8
8
|
};
|
|
9
|
+
//# sourceMappingURL=BlockTracker.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BlockTracker.d.cts","sourceRoot":"","sources":["../src/BlockTracker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,gBAAgB,qCAAqC;AAEjE,MAAM,MAAM,YAAY,GAAG,gBAAgB,GAAG;IAC5C,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAEzB,SAAS,IAAI,OAAO,CAAC;IAErB,eAAe,IAAI,MAAM,GAAG,IAAI,CAAC;IAEjC,cAAc,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAElC,mBAAmB,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;CACxC,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type SafeEventEmitter from "@metamask/safe-event-emitter";
|
|
2
|
+
export type BlockTracker = SafeEventEmitter & {
|
|
3
|
+
destroy(): Promise<void>;
|
|
4
|
+
isRunning(): boolean;
|
|
5
|
+
getCurrentBlock(): string | null;
|
|
6
|
+
getLatestBlock(): Promise<string>;
|
|
7
|
+
checkForLatestBlock(): Promise<string>;
|
|
8
|
+
};
|
|
9
|
+
//# sourceMappingURL=BlockTracker.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BlockTracker.d.mts","sourceRoot":"","sources":["../src/BlockTracker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,gBAAgB,qCAAqC;AAEjE,MAAM,MAAM,YAAY,GAAG,gBAAgB,GAAG;IAC5C,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAEzB,SAAS,IAAI,OAAO,CAAC;IAErB,eAAe,IAAI,MAAM,GAAG,IAAI,CAAC;IAEjC,cAAc,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAElC,mBAAmB,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;CACxC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BlockTracker.mjs","sourceRoot":"","sources":["../src/BlockTracker.ts"],"names":[],"mappings":"","sourcesContent":["import type SafeEventEmitter from '@metamask/safe-event-emitter';\n\nexport type BlockTracker = SafeEventEmitter & {\n destroy(): Promise<void>;\n\n isRunning(): boolean;\n\n getCurrentBlock(): string | null;\n\n getLatestBlock(): Promise<string>;\n\n checkForLatestBlock(): Promise<string>;\n};\n"]}
|
|
@@ -19,7 +19,7 @@ exports.PollingBlockTracker = void 0;
|
|
|
19
19
|
const safe_event_emitter_1 = __importDefault(require("@metamask/safe-event-emitter"));
|
|
20
20
|
const utils_1 = require("@metamask/utils");
|
|
21
21
|
const json_rpc_random_id_1 = __importDefault(require("json-rpc-random-id"));
|
|
22
|
-
const logging_utils_1 = require("./logging-utils");
|
|
22
|
+
const logging_utils_1 = require("./logging-utils.cjs");
|
|
23
23
|
const log = (0, logging_utils_1.createModuleLogger)(logging_utils_1.projectLogger, 'polling-block-tracker');
|
|
24
24
|
const createRandomId = (0, json_rpc_random_id_1.default)();
|
|
25
25
|
const sec = 1000;
|
|
@@ -246,6 +246,10 @@ class PollingBlockTracker extends safe_event_emitter_1.default {
|
|
|
246
246
|
// fetch + set latest block
|
|
247
247
|
const latestBlock = await this._fetchLatestBlock();
|
|
248
248
|
this._newPotentialLatest(latestBlock);
|
|
249
|
+
if (!this._isRunning) {
|
|
250
|
+
// Ensure the one-time update is eventually reset once it's stale
|
|
251
|
+
this._setupBlockResetTimeout();
|
|
252
|
+
}
|
|
249
253
|
// _newPotentialLatest() ensures that this._currentBlock is not null
|
|
250
254
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
251
255
|
return this._currentBlock;
|
|
@@ -346,4 +350,4 @@ _PollingBlockTracker_internalEventListeners = new WeakMap(), _PollingBlockTracke
|
|
|
346
350
|
function hexToInt(hexInt) {
|
|
347
351
|
return Number.parseInt(hexInt, 16);
|
|
348
352
|
}
|
|
349
|
-
//# sourceMappingURL=PollingBlockTracker.
|
|
353
|
+
//# sourceMappingURL=PollingBlockTracker.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PollingBlockTracker.cjs","sourceRoot":"","sources":["../src/PollingBlockTracker.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AACA,sFAA4D;AAC5D,2CAKyB;AACzB,4EAAmD;AAGnD,uDAAoE;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,kBAAkB,GAAwB,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;AAkBnE,MAAa,mBACX,SAAQ,4BAAgB;IA+BxB,YAAY,OAAmC,EAAE;QAC/C,wBAAwB;QACxB,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE;YAClB,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC,CAAC;SACjE;QAED,KAAK,EAAE,CAAC;;QAZD,sDAA8C,EAAE,EAAC;QAE1D,0DAA+D;QAE/D,oDAAyD;QAUvD,SAAS;QACT,IAAI,CAAC,mBAAmB,GAAG,IAAI,CAAC,kBAAkB,IAAI,EAAE,GAAG,GAAG,CAAC;QAC/D,IAAI,CAAC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pB;QAED,IAAI,CAAC,eAAe,GAAG,UAAU,CAAC;QAElC,IAAI,CAAC,IAAI,CAAC,0BAA0B,CAAC,CAAC;IACxC,CAAC;IAED,oBAAoB;QAClB,IAAI,IAAI,CAAC,eAAe,EAAE;YACxB,YAAY,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;YACnC,IAAI,CAAC,eAAe,GAAG,SAAS,CAAC;SAClC;IACH,CAAC;CAiBF;AApZD,kDAoZC;sTAfsB,QAA0B;IAC7C,uBAAA,IAAI,mDAAwB,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;AAC9C,CAAC,qGAEuB,QAA0B;IAChD,uBAAA,IAAI,mDAAwB,CAAC,MAAM,CACjC,uBAAA,IAAI,mDAAwB,CAAC,OAAO,CAAC,QAAQ,CAAC,EAC9C,CAAC,CACF,CAAC;AACJ,CAAC,yGAEyB,KAAc;;IACtC,MAAA,uBAAA,IAAI,+CAAoB,0CAAE,MAAM,CAAC,KAAK,CAAC,CAAC;IACxC,uBAAA,IAAI,2CAAuB,SAAS,MAAA,CAAC;AACvC,CAAC;AAGH;;;;;;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 {\n createDeferredPromise,\n type DeferredPromise,\n getErrorMessage,\n type JsonRpcRequest,\n} from '@metamask/utils';\nimport getCreateRandomId from 'json-rpc-random-id';\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 blockTrackerEvents: (string | symbol)[] = ['sync', 'latest'];\n\nexport type PollingBlockTrackerOptions = {\n provider?: SafeEventEmitterProvider;\n pollingInterval?: number;\n retryTimeout?: number;\n keepEventLoopActive?: boolean;\n setSkipCacheFlag?: boolean;\n blockResetDuration?: number;\n usePastBlocks?: boolean;\n};\n\ntype ExtendedJsonRpcRequest = {\n skipCache?: boolean;\n} & JsonRpcRequest<[]>;\n\ntype InternalListener = (value: string) => void;\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 readonly #internalEventListeners: InternalListener[] = [];\n\n #pendingLatestBlock?: Omit<DeferredPromise<string>, 'resolve'>;\n\n #pendingFetch?: Omit<DeferredPromise<string>, 'resolve'>;\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 super.removeAllListeners();\n this._maybeEnd();\n }\n\n isRunning(): boolean {\n return this._isRunning;\n }\n\n getCurrentBlock(): string | null {\n return this._currentBlock;\n }\n\n async getLatestBlock({\n useCache = true,\n }: { useCache?: boolean } = {}): Promise<string> {\n // return if available\n if (this._currentBlock && useCache) {\n return this._currentBlock;\n }\n\n if (this.#pendingLatestBlock) {\n return await this.#pendingLatestBlock.promise;\n }\n\n const { promise, resolve, reject } = createDeferredPromise<string>({\n suppressUnhandledRejection: true,\n });\n this.#pendingLatestBlock = { reject, promise };\n\n if (this._isRunning) {\n try {\n // If tracker is running, wait for next block with timeout\n const onLatestBlock = (value: string) => {\n this.#removeInternalListener(onLatestBlock);\n this.removeListener('latest', onLatestBlock);\n resolve(value);\n };\n\n this.#addInternalListener(onLatestBlock);\n this.once('latest', onLatestBlock);\n\n return await promise;\n } catch (error) {\n reject(error);\n throw error;\n } finally {\n this.#pendingLatestBlock = undefined;\n }\n } else {\n // If tracker isn't running, just fetch directly\n try {\n const latestBlock = await this._updateLatestBlock();\n resolve(latestBlock);\n return latestBlock;\n } catch (error) {\n reject(error);\n throw error;\n } finally {\n // We want to rate limit calls to this method if we made a direct fetch\n // for the block number because the BlockTracker was not running. We\n // achieve this by delaying the unsetting of the #pendingLatestBlock promise.\n setTimeout(() => {\n this.#pendingLatestBlock = undefined;\n }, this._pollingInterval);\n }\n }\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.#rejectPendingLatestBlock(new Error('Block tracker destroyed'));\n this.emit('_ended');\n }\n\n private _getBlockTrackerEventCount(): number {\n return (\n blockTrackerEvents\n .map((eventName) => this.listeners(eventName))\n .flat()\n // internal listeners are not included in the count\n .filter((listener) =>\n this.#internalEventListeners.every(\n (internalListener) => !Object.is(internalListener, listener),\n ),\n ).length\n );\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 /**\n * Checks for the latest block, updates the internal state, and returns the\n * value immediately rather than waiting for the next polling interval.\n *\n * @deprecated Use {@link getLatestBlock} instead.\n * @returns A promise that resolves to the latest block number.\n */\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<string> {\n // fetch + set latest block\n const latestBlock = await this._fetchLatestBlock();\n this._newPotentialLatest(latestBlock);\n\n if (!this._isRunning) {\n // Ensure the one-time update is eventually reset once it's stale\n this._setupBlockResetTimeout();\n }\n\n // _newPotentialLatest() ensures that this._currentBlock is not null\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n return this._currentBlock!;\n }\n\n private async _fetchLatestBlock(): Promise<string> {\n // If there's already a pending fetch, reuse it\n if (this.#pendingFetch) {\n return await this.#pendingFetch.promise;\n }\n\n // Create a new deferred promise for this request\n const { promise, resolve, reject } = createDeferredPromise<string>({\n suppressUnhandledRejection: true,\n });\n this.#pendingFetch = { reject, promise };\n\n try {\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 result = await this._provider.request<[], string>(req);\n log('Got result', result);\n resolve(result);\n return result;\n } catch (error) {\n log('Encountered error fetching block', getErrorMessage(error));\n reject(error);\n this.#rejectPendingLatestBlock(error);\n throw error;\n } finally {\n this.#pendingFetch = undefined;\n }\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 (error: unknown) {\n try {\n this.emit('error', error);\n } catch {\n console.error(`Error updating latest block: ${getErrorMessage(error)}`);\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 #addInternalListener(listener: InternalListener) {\n this.#internalEventListeners.push(listener);\n }\n\n #removeInternalListener(listener: InternalListener) {\n this.#internalEventListeners.splice(\n this.#internalEventListeners.indexOf(listener),\n 1,\n );\n }\n\n #rejectPendingLatestBlock(error: unknown) {\n this.#pendingLatestBlock?.reject(error);\n this.#pendingLatestBlock = undefined;\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,7 +1,7 @@
|
|
|
1
|
-
import type { SafeEventEmitterProvider } from
|
|
2
|
-
import SafeEventEmitter from
|
|
3
|
-
import type { BlockTracker } from
|
|
4
|
-
export
|
|
1
|
+
import type { SafeEventEmitterProvider } from "@metamask/eth-json-rpc-provider";
|
|
2
|
+
import SafeEventEmitter from "@metamask/safe-event-emitter";
|
|
3
|
+
import type { BlockTracker } from "./BlockTracker.cjs";
|
|
4
|
+
export type PollingBlockTrackerOptions = {
|
|
5
5
|
provider?: SafeEventEmitterProvider;
|
|
6
6
|
pollingInterval?: number;
|
|
7
7
|
retryTimeout?: number;
|
|
@@ -9,7 +9,7 @@ export interface PollingBlockTrackerOptions {
|
|
|
9
9
|
setSkipCacheFlag?: boolean;
|
|
10
10
|
blockResetDuration?: number;
|
|
11
11
|
usePastBlocks?: boolean;
|
|
12
|
-
}
|
|
12
|
+
};
|
|
13
13
|
export declare class PollingBlockTracker extends SafeEventEmitter implements BlockTracker {
|
|
14
14
|
#private;
|
|
15
15
|
private _isRunning;
|
|
@@ -62,3 +62,4 @@ export declare class PollingBlockTracker extends SafeEventEmitter implements Blo
|
|
|
62
62
|
private _updateAndQueue;
|
|
63
63
|
_clearPollingTimeout(): void;
|
|
64
64
|
}
|
|
65
|
+
//# sourceMappingURL=PollingBlockTracker.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PollingBlockTracker.d.cts","sourceRoot":"","sources":["../src/PollingBlockTracker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,wCAAwC;AAChF,OAAO,gBAAgB,qCAAqC;AAS5D,OAAO,KAAK,EAAE,YAAY,EAAE,2BAAuB;AASnD,MAAM,MAAM,0BAA0B,GAAG;IACvC,QAAQ,CAAC,EAAE,wBAAwB,CAAC;IACpC,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB,CAAC;AAQF,qBAAa,mBACX,SAAQ,gBACR,YAAW,YAAY;;IAEvB,OAAO,CAAC,UAAU,CAAU;IAE5B,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAAS;IAE7C,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAU;IAEzC,OAAO,CAAC,aAAa,CAAgB;IAErC,OAAO,CAAC,kBAAkB,CAAC,CAAgC;IAE3D,OAAO,CAAC,eAAe,CAAC,CAAgC;IAExD,OAAO,CAAC,QAAQ,CAAC,SAAS,CAA2B;IAErD,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAS;IAE1C,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAS;IAEvC,OAAO,CAAC,QAAQ,CAAC,oBAAoB,CAAU;IAE/C,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAU;gBAQhC,IAAI,GAAE,0BAA+B;IAgC3C,OAAO;IAMb,SAAS,IAAI,OAAO;IAIpB,eAAe,IAAI,MAAM,GAAG,IAAI;IAI1B,cAAc,CAAC,EACnB,QAAe,GAChB,GAAE;QAAE,QAAQ,CAAC,EAAE,OAAO,CAAA;KAAO,GAAG,OAAO,CAAC,MAAM,CAAC;IAuDhD,kBAAkB,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM;IAgB9C,OAAO,CAAC,oBAAoB;IAS5B,OAAO,CAAC,cAAc;IAQtB,OAAO,CAAC,iBAAiB;IAQzB,OAAO,CAAC,WAAW;IAYnB,OAAO,CAAC,SAAS;IAYjB,OAAO,CAAC,0BAA0B;IAclC,OAAO,CAAC,kBAAkB;IAc1B,OAAO,CAAC,mBAAmB;IAO3B,OAAO,CAAC,gBAAgB;IAOxB,OAAO,CAAC,uBAAuB;IAe/B,OAAO,CAAC,wBAAwB;IAMhC,OAAO,CAAC,kBAAkB;IAI1B;;;;;;OAMG;IACG,mBAAmB;IAKzB,OAAO,CAAC,MAAM;IAMd,OAAO,CAAC,IAAI;YAIE,kBAAkB;YAelB,iBAAiB;IAsC/B;;;OAGG;YACW,eAAe;IAoC7B,oBAAoB;CAsBrB"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { SafeEventEmitterProvider } from "@metamask/eth-json-rpc-provider";
|
|
2
|
+
import SafeEventEmitter from "@metamask/safe-event-emitter";
|
|
3
|
+
import type { BlockTracker } from "./BlockTracker.mjs";
|
|
4
|
+
export type PollingBlockTrackerOptions = {
|
|
5
|
+
provider?: SafeEventEmitterProvider;
|
|
6
|
+
pollingInterval?: number;
|
|
7
|
+
retryTimeout?: number;
|
|
8
|
+
keepEventLoopActive?: boolean;
|
|
9
|
+
setSkipCacheFlag?: boolean;
|
|
10
|
+
blockResetDuration?: number;
|
|
11
|
+
usePastBlocks?: boolean;
|
|
12
|
+
};
|
|
13
|
+
export declare class PollingBlockTracker extends SafeEventEmitter implements BlockTracker {
|
|
14
|
+
#private;
|
|
15
|
+
private _isRunning;
|
|
16
|
+
private readonly _blockResetDuration;
|
|
17
|
+
private readonly _usePastBlocks;
|
|
18
|
+
private _currentBlock;
|
|
19
|
+
private _blockResetTimeout?;
|
|
20
|
+
private _pollingTimeout?;
|
|
21
|
+
private readonly _provider;
|
|
22
|
+
private readonly _pollingInterval;
|
|
23
|
+
private readonly _retryTimeout;
|
|
24
|
+
private readonly _keepEventLoopActive;
|
|
25
|
+
private readonly _setSkipCacheFlag;
|
|
26
|
+
constructor(opts?: PollingBlockTrackerOptions);
|
|
27
|
+
destroy(): Promise<void>;
|
|
28
|
+
isRunning(): boolean;
|
|
29
|
+
getCurrentBlock(): string | null;
|
|
30
|
+
getLatestBlock({ useCache, }?: {
|
|
31
|
+
useCache?: boolean;
|
|
32
|
+
}): Promise<string>;
|
|
33
|
+
removeAllListeners(eventName?: string | symbol): this;
|
|
34
|
+
private _setupInternalEvents;
|
|
35
|
+
private _onNewListener;
|
|
36
|
+
private _onRemoveListener;
|
|
37
|
+
private _maybeStart;
|
|
38
|
+
private _maybeEnd;
|
|
39
|
+
private _getBlockTrackerEventCount;
|
|
40
|
+
private _shouldUseNewBlock;
|
|
41
|
+
private _newPotentialLatest;
|
|
42
|
+
private _setCurrentBlock;
|
|
43
|
+
private _setupBlockResetTimeout;
|
|
44
|
+
private _cancelBlockResetTimeout;
|
|
45
|
+
private _resetCurrentBlock;
|
|
46
|
+
/**
|
|
47
|
+
* Checks for the latest block, updates the internal state, and returns the
|
|
48
|
+
* value immediately rather than waiting for the next polling interval.
|
|
49
|
+
*
|
|
50
|
+
* @deprecated Use {@link getLatestBlock} instead.
|
|
51
|
+
* @returns A promise that resolves to the latest block number.
|
|
52
|
+
*/
|
|
53
|
+
checkForLatestBlock(): Promise<string>;
|
|
54
|
+
private _start;
|
|
55
|
+
private _end;
|
|
56
|
+
private _updateLatestBlock;
|
|
57
|
+
private _fetchLatestBlock;
|
|
58
|
+
/**
|
|
59
|
+
* The core polling function that runs after each interval.
|
|
60
|
+
* Updates the latest block and then queues the next update.
|
|
61
|
+
*/
|
|
62
|
+
private _updateAndQueue;
|
|
63
|
+
_clearPollingTimeout(): void;
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=PollingBlockTracker.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PollingBlockTracker.d.mts","sourceRoot":"","sources":["../src/PollingBlockTracker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,wCAAwC;AAChF,OAAO,gBAAgB,qCAAqC;AAS5D,OAAO,KAAK,EAAE,YAAY,EAAE,2BAAuB;AASnD,MAAM,MAAM,0BAA0B,GAAG;IACvC,QAAQ,CAAC,EAAE,wBAAwB,CAAC;IACpC,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB,CAAC;AAQF,qBAAa,mBACX,SAAQ,gBACR,YAAW,YAAY;;IAEvB,OAAO,CAAC,UAAU,CAAU;IAE5B,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAAS;IAE7C,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAU;IAEzC,OAAO,CAAC,aAAa,CAAgB;IAErC,OAAO,CAAC,kBAAkB,CAAC,CAAgC;IAE3D,OAAO,CAAC,eAAe,CAAC,CAAgC;IAExD,OAAO,CAAC,QAAQ,CAAC,SAAS,CAA2B;IAErD,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAS;IAE1C,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAS;IAEvC,OAAO,CAAC,QAAQ,CAAC,oBAAoB,CAAU;IAE/C,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAU;gBAQhC,IAAI,GAAE,0BAA+B;IAgC3C,OAAO;IAMb,SAAS,IAAI,OAAO;IAIpB,eAAe,IAAI,MAAM,GAAG,IAAI;IAI1B,cAAc,CAAC,EACnB,QAAe,GAChB,GAAE;QAAE,QAAQ,CAAC,EAAE,OAAO,CAAA;KAAO,GAAG,OAAO,CAAC,MAAM,CAAC;IAuDhD,kBAAkB,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM;IAgB9C,OAAO,CAAC,oBAAoB;IAS5B,OAAO,CAAC,cAAc;IAQtB,OAAO,CAAC,iBAAiB;IAQzB,OAAO,CAAC,WAAW;IAYnB,OAAO,CAAC,SAAS;IAYjB,OAAO,CAAC,0BAA0B;IAclC,OAAO,CAAC,kBAAkB;IAc1B,OAAO,CAAC,mBAAmB;IAO3B,OAAO,CAAC,gBAAgB;IAOxB,OAAO,CAAC,uBAAuB;IAe/B,OAAO,CAAC,wBAAwB;IAMhC,OAAO,CAAC,kBAAkB;IAI1B;;;;;;OAMG;IACG,mBAAmB;IAKzB,OAAO,CAAC,MAAM;IAMd,OAAO,CAAC,IAAI;YAIE,kBAAkB;YAelB,iBAAiB;IAsC/B;;;OAGG;YACW,eAAe;IAoC7B,oBAAoB;CAsBrB"}
|
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
+
};
|
|
6
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
7
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
10
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
11
|
+
};
|
|
12
|
+
var _PollingBlockTracker_instances, _PollingBlockTracker_internalEventListeners, _PollingBlockTracker_pendingLatestBlock, _PollingBlockTracker_pendingFetch, _PollingBlockTracker_addInternalListener, _PollingBlockTracker_removeInternalListener, _PollingBlockTracker_rejectPendingLatestBlock;
|
|
13
|
+
function $importDefault(module) {
|
|
14
|
+
if (module === null || module === void 0 ? void 0 : module.__esModule) {
|
|
15
|
+
return module.default;
|
|
16
|
+
}
|
|
17
|
+
return module;
|
|
18
|
+
}
|
|
19
|
+
import SafeEventEmitter from "@metamask/safe-event-emitter";
|
|
20
|
+
import { createDeferredPromise, getErrorMessage } from "@metamask/utils";
|
|
21
|
+
import $getCreateRandomId from "json-rpc-random-id";
|
|
22
|
+
const getCreateRandomId = $importDefault($getCreateRandomId);
|
|
23
|
+
import { projectLogger, createModuleLogger } from "./logging-utils.mjs";
|
|
24
|
+
const log = createModuleLogger(projectLogger, 'polling-block-tracker');
|
|
25
|
+
const createRandomId = getCreateRandomId();
|
|
26
|
+
const sec = 1000;
|
|
27
|
+
const blockTrackerEvents = ['sync', 'latest'];
|
|
28
|
+
export class PollingBlockTracker extends SafeEventEmitter {
|
|
29
|
+
constructor(opts = {}) {
|
|
30
|
+
// parse + validate args
|
|
31
|
+
if (!opts.provider) {
|
|
32
|
+
throw new Error('PollingBlockTracker - no provider specified.');
|
|
33
|
+
}
|
|
34
|
+
super();
|
|
35
|
+
_PollingBlockTracker_instances.add(this);
|
|
36
|
+
_PollingBlockTracker_internalEventListeners.set(this, []);
|
|
37
|
+
_PollingBlockTracker_pendingLatestBlock.set(this, void 0);
|
|
38
|
+
_PollingBlockTracker_pendingFetch.set(this, void 0);
|
|
39
|
+
// config
|
|
40
|
+
this._blockResetDuration = opts.blockResetDuration || 20 * sec;
|
|
41
|
+
this._usePastBlocks = opts.usePastBlocks || false;
|
|
42
|
+
// state
|
|
43
|
+
this._currentBlock = null;
|
|
44
|
+
this._isRunning = false;
|
|
45
|
+
// bind functions for internal use
|
|
46
|
+
this._onNewListener = this._onNewListener.bind(this);
|
|
47
|
+
this._onRemoveListener = this._onRemoveListener.bind(this);
|
|
48
|
+
this._resetCurrentBlock = this._resetCurrentBlock.bind(this);
|
|
49
|
+
// listen for handler changes
|
|
50
|
+
this._setupInternalEvents();
|
|
51
|
+
// config
|
|
52
|
+
this._provider = opts.provider;
|
|
53
|
+
this._pollingInterval = opts.pollingInterval || 20 * sec;
|
|
54
|
+
this._retryTimeout = opts.retryTimeout || this._pollingInterval / 10;
|
|
55
|
+
this._keepEventLoopActive =
|
|
56
|
+
opts.keepEventLoopActive === undefined ? true : opts.keepEventLoopActive;
|
|
57
|
+
this._setSkipCacheFlag = opts.setSkipCacheFlag || false;
|
|
58
|
+
}
|
|
59
|
+
async destroy() {
|
|
60
|
+
this._cancelBlockResetTimeout();
|
|
61
|
+
super.removeAllListeners();
|
|
62
|
+
this._maybeEnd();
|
|
63
|
+
}
|
|
64
|
+
isRunning() {
|
|
65
|
+
return this._isRunning;
|
|
66
|
+
}
|
|
67
|
+
getCurrentBlock() {
|
|
68
|
+
return this._currentBlock;
|
|
69
|
+
}
|
|
70
|
+
async getLatestBlock({ useCache = true, } = {}) {
|
|
71
|
+
// return if available
|
|
72
|
+
if (this._currentBlock && useCache) {
|
|
73
|
+
return this._currentBlock;
|
|
74
|
+
}
|
|
75
|
+
if (__classPrivateFieldGet(this, _PollingBlockTracker_pendingLatestBlock, "f")) {
|
|
76
|
+
return await __classPrivateFieldGet(this, _PollingBlockTracker_pendingLatestBlock, "f").promise;
|
|
77
|
+
}
|
|
78
|
+
const { promise, resolve, reject } = createDeferredPromise({
|
|
79
|
+
suppressUnhandledRejection: true,
|
|
80
|
+
});
|
|
81
|
+
__classPrivateFieldSet(this, _PollingBlockTracker_pendingLatestBlock, { reject, promise }, "f");
|
|
82
|
+
if (this._isRunning) {
|
|
83
|
+
try {
|
|
84
|
+
// If tracker is running, wait for next block with timeout
|
|
85
|
+
const onLatestBlock = (value) => {
|
|
86
|
+
__classPrivateFieldGet(this, _PollingBlockTracker_instances, "m", _PollingBlockTracker_removeInternalListener).call(this, onLatestBlock);
|
|
87
|
+
this.removeListener('latest', onLatestBlock);
|
|
88
|
+
resolve(value);
|
|
89
|
+
};
|
|
90
|
+
__classPrivateFieldGet(this, _PollingBlockTracker_instances, "m", _PollingBlockTracker_addInternalListener).call(this, onLatestBlock);
|
|
91
|
+
this.once('latest', onLatestBlock);
|
|
92
|
+
return await promise;
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
reject(error);
|
|
96
|
+
throw error;
|
|
97
|
+
}
|
|
98
|
+
finally {
|
|
99
|
+
__classPrivateFieldSet(this, _PollingBlockTracker_pendingLatestBlock, undefined, "f");
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
// If tracker isn't running, just fetch directly
|
|
104
|
+
try {
|
|
105
|
+
const latestBlock = await this._updateLatestBlock();
|
|
106
|
+
resolve(latestBlock);
|
|
107
|
+
return latestBlock;
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
reject(error);
|
|
111
|
+
throw error;
|
|
112
|
+
}
|
|
113
|
+
finally {
|
|
114
|
+
// We want to rate limit calls to this method if we made a direct fetch
|
|
115
|
+
// for the block number because the BlockTracker was not running. We
|
|
116
|
+
// achieve this by delaying the unsetting of the #pendingLatestBlock promise.
|
|
117
|
+
setTimeout(() => {
|
|
118
|
+
__classPrivateFieldSet(this, _PollingBlockTracker_pendingLatestBlock, undefined, "f");
|
|
119
|
+
}, this._pollingInterval);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
// dont allow module consumer to remove our internal event listeners
|
|
124
|
+
removeAllListeners(eventName) {
|
|
125
|
+
// perform default behavior, preserve fn arity
|
|
126
|
+
if (eventName) {
|
|
127
|
+
super.removeAllListeners(eventName);
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
super.removeAllListeners();
|
|
131
|
+
}
|
|
132
|
+
// re-add internal events
|
|
133
|
+
this._setupInternalEvents();
|
|
134
|
+
// trigger stop check just in case
|
|
135
|
+
this._onRemoveListener();
|
|
136
|
+
return this;
|
|
137
|
+
}
|
|
138
|
+
_setupInternalEvents() {
|
|
139
|
+
// first remove listeners for idempotence
|
|
140
|
+
this.removeListener('newListener', this._onNewListener);
|
|
141
|
+
this.removeListener('removeListener', this._onRemoveListener);
|
|
142
|
+
// then add them
|
|
143
|
+
this.on('newListener', this._onNewListener);
|
|
144
|
+
this.on('removeListener', this._onRemoveListener);
|
|
145
|
+
}
|
|
146
|
+
_onNewListener(eventName) {
|
|
147
|
+
// `newListener` is called *before* the listener is added
|
|
148
|
+
if (blockTrackerEvents.includes(eventName)) {
|
|
149
|
+
// TODO: Handle dangling promise
|
|
150
|
+
this._maybeStart();
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
_onRemoveListener() {
|
|
154
|
+
// `removeListener` is called *after* the listener is removed
|
|
155
|
+
if (this._getBlockTrackerEventCount() > 0) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
this._maybeEnd();
|
|
159
|
+
}
|
|
160
|
+
_maybeStart() {
|
|
161
|
+
if (this._isRunning) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
this._isRunning = true;
|
|
165
|
+
// cancel setting latest block to stale
|
|
166
|
+
this._cancelBlockResetTimeout();
|
|
167
|
+
this._start();
|
|
168
|
+
this.emit('_started');
|
|
169
|
+
}
|
|
170
|
+
_maybeEnd() {
|
|
171
|
+
if (!this._isRunning) {
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
this._isRunning = false;
|
|
175
|
+
this._setupBlockResetTimeout();
|
|
176
|
+
this._end();
|
|
177
|
+
__classPrivateFieldGet(this, _PollingBlockTracker_instances, "m", _PollingBlockTracker_rejectPendingLatestBlock).call(this, new Error('Block tracker destroyed'));
|
|
178
|
+
this.emit('_ended');
|
|
179
|
+
}
|
|
180
|
+
_getBlockTrackerEventCount() {
|
|
181
|
+
return (blockTrackerEvents
|
|
182
|
+
.map((eventName) => this.listeners(eventName))
|
|
183
|
+
.flat()
|
|
184
|
+
// internal listeners are not included in the count
|
|
185
|
+
.filter((listener) => __classPrivateFieldGet(this, _PollingBlockTracker_internalEventListeners, "f").every((internalListener) => !Object.is(internalListener, listener))).length);
|
|
186
|
+
}
|
|
187
|
+
_shouldUseNewBlock(newBlock) {
|
|
188
|
+
const currentBlock = this._currentBlock;
|
|
189
|
+
if (!currentBlock) {
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
const newBlockInt = hexToInt(newBlock);
|
|
193
|
+
const currentBlockInt = hexToInt(currentBlock);
|
|
194
|
+
return ((this._usePastBlocks && newBlockInt < currentBlockInt) ||
|
|
195
|
+
newBlockInt > currentBlockInt);
|
|
196
|
+
}
|
|
197
|
+
_newPotentialLatest(newBlock) {
|
|
198
|
+
if (!this._shouldUseNewBlock(newBlock)) {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
this._setCurrentBlock(newBlock);
|
|
202
|
+
}
|
|
203
|
+
_setCurrentBlock(newBlock) {
|
|
204
|
+
const oldBlock = this._currentBlock;
|
|
205
|
+
this._currentBlock = newBlock;
|
|
206
|
+
this.emit('latest', newBlock);
|
|
207
|
+
this.emit('sync', { oldBlock, newBlock });
|
|
208
|
+
}
|
|
209
|
+
_setupBlockResetTimeout() {
|
|
210
|
+
// clear any existing timeout
|
|
211
|
+
this._cancelBlockResetTimeout();
|
|
212
|
+
// clear latest block when stale
|
|
213
|
+
this._blockResetTimeout = setTimeout(this._resetCurrentBlock, this._blockResetDuration);
|
|
214
|
+
// nodejs - dont hold process open
|
|
215
|
+
if (this._blockResetTimeout.unref) {
|
|
216
|
+
this._blockResetTimeout.unref();
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
_cancelBlockResetTimeout() {
|
|
220
|
+
if (this._blockResetTimeout) {
|
|
221
|
+
clearTimeout(this._blockResetTimeout);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
_resetCurrentBlock() {
|
|
225
|
+
this._currentBlock = null;
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Checks for the latest block, updates the internal state, and returns the
|
|
229
|
+
* value immediately rather than waiting for the next polling interval.
|
|
230
|
+
*
|
|
231
|
+
* @deprecated Use {@link getLatestBlock} instead.
|
|
232
|
+
* @returns A promise that resolves to the latest block number.
|
|
233
|
+
*/
|
|
234
|
+
async checkForLatestBlock() {
|
|
235
|
+
await this._updateLatestBlock();
|
|
236
|
+
return await this.getLatestBlock();
|
|
237
|
+
}
|
|
238
|
+
_start() {
|
|
239
|
+
// Intentionally not awaited as this starts the polling via a timeout chain.
|
|
240
|
+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
241
|
+
this._updateAndQueue();
|
|
242
|
+
}
|
|
243
|
+
_end() {
|
|
244
|
+
this._clearPollingTimeout();
|
|
245
|
+
}
|
|
246
|
+
async _updateLatestBlock() {
|
|
247
|
+
// fetch + set latest block
|
|
248
|
+
const latestBlock = await this._fetchLatestBlock();
|
|
249
|
+
this._newPotentialLatest(latestBlock);
|
|
250
|
+
if (!this._isRunning) {
|
|
251
|
+
// Ensure the one-time update is eventually reset once it's stale
|
|
252
|
+
this._setupBlockResetTimeout();
|
|
253
|
+
}
|
|
254
|
+
// _newPotentialLatest() ensures that this._currentBlock is not null
|
|
255
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
256
|
+
return this._currentBlock;
|
|
257
|
+
}
|
|
258
|
+
async _fetchLatestBlock() {
|
|
259
|
+
// If there's already a pending fetch, reuse it
|
|
260
|
+
if (__classPrivateFieldGet(this, _PollingBlockTracker_pendingFetch, "f")) {
|
|
261
|
+
return await __classPrivateFieldGet(this, _PollingBlockTracker_pendingFetch, "f").promise;
|
|
262
|
+
}
|
|
263
|
+
// Create a new deferred promise for this request
|
|
264
|
+
const { promise, resolve, reject } = createDeferredPromise({
|
|
265
|
+
suppressUnhandledRejection: true,
|
|
266
|
+
});
|
|
267
|
+
__classPrivateFieldSet(this, _PollingBlockTracker_pendingFetch, { reject, promise }, "f");
|
|
268
|
+
try {
|
|
269
|
+
const req = {
|
|
270
|
+
jsonrpc: '2.0',
|
|
271
|
+
id: createRandomId(),
|
|
272
|
+
method: 'eth_blockNumber',
|
|
273
|
+
params: [],
|
|
274
|
+
};
|
|
275
|
+
if (this._setSkipCacheFlag) {
|
|
276
|
+
req.skipCache = true;
|
|
277
|
+
}
|
|
278
|
+
log('Making request', req);
|
|
279
|
+
const result = await this._provider.request(req);
|
|
280
|
+
log('Got result', result);
|
|
281
|
+
resolve(result);
|
|
282
|
+
return result;
|
|
283
|
+
}
|
|
284
|
+
catch (error) {
|
|
285
|
+
log('Encountered error fetching block', getErrorMessage(error));
|
|
286
|
+
reject(error);
|
|
287
|
+
__classPrivateFieldGet(this, _PollingBlockTracker_instances, "m", _PollingBlockTracker_rejectPendingLatestBlock).call(this, error);
|
|
288
|
+
throw error;
|
|
289
|
+
}
|
|
290
|
+
finally {
|
|
291
|
+
__classPrivateFieldSet(this, _PollingBlockTracker_pendingFetch, undefined, "f");
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* The core polling function that runs after each interval.
|
|
296
|
+
* Updates the latest block and then queues the next update.
|
|
297
|
+
*/
|
|
298
|
+
async _updateAndQueue() {
|
|
299
|
+
let interval = this._pollingInterval;
|
|
300
|
+
try {
|
|
301
|
+
await this._updateLatestBlock();
|
|
302
|
+
}
|
|
303
|
+
catch (error) {
|
|
304
|
+
try {
|
|
305
|
+
this.emit('error', error);
|
|
306
|
+
}
|
|
307
|
+
catch (_a) {
|
|
308
|
+
console.error(`Error updating latest block: ${getErrorMessage(error)}`);
|
|
309
|
+
}
|
|
310
|
+
interval = this._retryTimeout;
|
|
311
|
+
}
|
|
312
|
+
if (!this._isRunning) {
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
this._clearPollingTimeout();
|
|
316
|
+
const timeoutRef = setTimeout(() => {
|
|
317
|
+
// Intentionally not awaited as this just continues the polling loop.
|
|
318
|
+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
319
|
+
this._updateAndQueue();
|
|
320
|
+
}, interval);
|
|
321
|
+
if (timeoutRef.unref && !this._keepEventLoopActive) {
|
|
322
|
+
timeoutRef.unref();
|
|
323
|
+
}
|
|
324
|
+
this._pollingTimeout = timeoutRef;
|
|
325
|
+
this.emit('_waitingForNextIteration');
|
|
326
|
+
}
|
|
327
|
+
_clearPollingTimeout() {
|
|
328
|
+
if (this._pollingTimeout) {
|
|
329
|
+
clearTimeout(this._pollingTimeout);
|
|
330
|
+
this._pollingTimeout = undefined;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
_PollingBlockTracker_internalEventListeners = new WeakMap(), _PollingBlockTracker_pendingLatestBlock = new WeakMap(), _PollingBlockTracker_pendingFetch = new WeakMap(), _PollingBlockTracker_instances = new WeakSet(), _PollingBlockTracker_addInternalListener = function _PollingBlockTracker_addInternalListener(listener) {
|
|
335
|
+
__classPrivateFieldGet(this, _PollingBlockTracker_internalEventListeners, "f").push(listener);
|
|
336
|
+
}, _PollingBlockTracker_removeInternalListener = function _PollingBlockTracker_removeInternalListener(listener) {
|
|
337
|
+
__classPrivateFieldGet(this, _PollingBlockTracker_internalEventListeners, "f").splice(__classPrivateFieldGet(this, _PollingBlockTracker_internalEventListeners, "f").indexOf(listener), 1);
|
|
338
|
+
}, _PollingBlockTracker_rejectPendingLatestBlock = function _PollingBlockTracker_rejectPendingLatestBlock(error) {
|
|
339
|
+
var _a;
|
|
340
|
+
(_a = __classPrivateFieldGet(this, _PollingBlockTracker_pendingLatestBlock, "f")) === null || _a === void 0 ? void 0 : _a.reject(error);
|
|
341
|
+
__classPrivateFieldSet(this, _PollingBlockTracker_pendingLatestBlock, undefined, "f");
|
|
342
|
+
};
|
|
343
|
+
/**
|
|
344
|
+
* Converts a number represented as a string in hexadecimal format into a native
|
|
345
|
+
* number.
|
|
346
|
+
*
|
|
347
|
+
* @param hexInt - The hex string.
|
|
348
|
+
* @returns The number.
|
|
349
|
+
*/
|
|
350
|
+
function hexToInt(hexInt) {
|
|
351
|
+
return Number.parseInt(hexInt, 16);
|
|
352
|
+
}
|
|
353
|
+
//# sourceMappingURL=PollingBlockTracker.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PollingBlockTracker.mjs","sourceRoot":"","sources":["../src/PollingBlockTracker.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AACA,OAAO,gBAAgB,qCAAqC;AAC5D,OAAO,EACL,qBAAqB,EAErB,eAAe,EAEhB,wBAAwB;AACzB,OAAO,kBAAiB,2BAA2B;;AAGnD,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,4BAAwB;AAEpE,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,uBAAuB,CAAC,CAAC;AACvE,MAAM,cAAc,GAAG,iBAAiB,EAAE,CAAC;AAC3C,MAAM,GAAG,GAAG,IAAI,CAAC;AAEjB,MAAM,kBAAkB,GAAwB,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;AAkBnE,MAAM,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C,IAAI,CAAC,UAAU,EAAE;YACpB,OAAO;SACR;QAED,IAAI,CAAC,oBAAoB,EAAE,CAAC;QAE5B,MAAM,UAAU,GAAG,UAAU,CAAC,GAAG,EAAE;YACjC,qEAAqE;YACrE,mEAAmE;YACnE,IAAI,CAAC,eAAe,EAAE,CAAC;QACzB,CAAC,EAAE,QAAQ,CAAC,CAAC;QAEb,IAAI,UAAU,CAAC,KAAK,IAAI,CAAC,IAAI,CAAC,oBAAoB,EAAE;YAClD,UAAU,CAAC,KAAK,EAAE,CAAC;SACpB;QAED,IAAI,CAAC,eAAe,GAAG,UAAU,CAAC;QAElC,IAAI,CAAC,IAAI,CAAC,0BAA0B,CAAC,CAAC;IACxC,CAAC;IAED,oBAAoB;QAClB,IAAI,IAAI,CAAC,eAAe,EAAE;YACxB,YAAY,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;YACnC,IAAI,CAAC,eAAe,GAAG,SAAS,CAAC;SAClC;IACH,CAAC;CAiBF;sTAfsB,QAA0B;IAC7C,uBAAA,IAAI,mDAAwB,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;AAC9C,CAAC,qGAEuB,QAA0B;IAChD,uBAAA,IAAI,mDAAwB,CAAC,MAAM,CACjC,uBAAA,IAAI,mDAAwB,CAAC,OAAO,CAAC,QAAQ,CAAC,EAC9C,CAAC,CACF,CAAC;AACJ,CAAC,yGAEyB,KAAc;;IACtC,MAAA,uBAAA,IAAI,+CAAoB,0CAAE,MAAM,CAAC,KAAK,CAAC,CAAC;IACxC,uBAAA,IAAI,2CAAuB,SAAS,MAAA,CAAC;AACvC,CAAC;AAGH;;;;;;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 {\n createDeferredPromise,\n type DeferredPromise,\n getErrorMessage,\n type JsonRpcRequest,\n} from '@metamask/utils';\nimport getCreateRandomId from 'json-rpc-random-id';\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 blockTrackerEvents: (string | symbol)[] = ['sync', 'latest'];\n\nexport type PollingBlockTrackerOptions = {\n provider?: SafeEventEmitterProvider;\n pollingInterval?: number;\n retryTimeout?: number;\n keepEventLoopActive?: boolean;\n setSkipCacheFlag?: boolean;\n blockResetDuration?: number;\n usePastBlocks?: boolean;\n};\n\ntype ExtendedJsonRpcRequest = {\n skipCache?: boolean;\n} & JsonRpcRequest<[]>;\n\ntype InternalListener = (value: string) => void;\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 readonly #internalEventListeners: InternalListener[] = [];\n\n #pendingLatestBlock?: Omit<DeferredPromise<string>, 'resolve'>;\n\n #pendingFetch?: Omit<DeferredPromise<string>, 'resolve'>;\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 super.removeAllListeners();\n this._maybeEnd();\n }\n\n isRunning(): boolean {\n return this._isRunning;\n }\n\n getCurrentBlock(): string | null {\n return this._currentBlock;\n }\n\n async getLatestBlock({\n useCache = true,\n }: { useCache?: boolean } = {}): Promise<string> {\n // return if available\n if (this._currentBlock && useCache) {\n return this._currentBlock;\n }\n\n if (this.#pendingLatestBlock) {\n return await this.#pendingLatestBlock.promise;\n }\n\n const { promise, resolve, reject } = createDeferredPromise<string>({\n suppressUnhandledRejection: true,\n });\n this.#pendingLatestBlock = { reject, promise };\n\n if (this._isRunning) {\n try {\n // If tracker is running, wait for next block with timeout\n const onLatestBlock = (value: string) => {\n this.#removeInternalListener(onLatestBlock);\n this.removeListener('latest', onLatestBlock);\n resolve(value);\n };\n\n this.#addInternalListener(onLatestBlock);\n this.once('latest', onLatestBlock);\n\n return await promise;\n } catch (error) {\n reject(error);\n throw error;\n } finally {\n this.#pendingLatestBlock = undefined;\n }\n } else {\n // If tracker isn't running, just fetch directly\n try {\n const latestBlock = await this._updateLatestBlock();\n resolve(latestBlock);\n return latestBlock;\n } catch (error) {\n reject(error);\n throw error;\n } finally {\n // We want to rate limit calls to this method if we made a direct fetch\n // for the block number because the BlockTracker was not running. We\n // achieve this by delaying the unsetting of the #pendingLatestBlock promise.\n setTimeout(() => {\n this.#pendingLatestBlock = undefined;\n }, this._pollingInterval);\n }\n }\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.#rejectPendingLatestBlock(new Error('Block tracker destroyed'));\n this.emit('_ended');\n }\n\n private _getBlockTrackerEventCount(): number {\n return (\n blockTrackerEvents\n .map((eventName) => this.listeners(eventName))\n .flat()\n // internal listeners are not included in the count\n .filter((listener) =>\n this.#internalEventListeners.every(\n (internalListener) => !Object.is(internalListener, listener),\n ),\n ).length\n );\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 /**\n * Checks for the latest block, updates the internal state, and returns the\n * value immediately rather than waiting for the next polling interval.\n *\n * @deprecated Use {@link getLatestBlock} instead.\n * @returns A promise that resolves to the latest block number.\n */\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<string> {\n // fetch + set latest block\n const latestBlock = await this._fetchLatestBlock();\n this._newPotentialLatest(latestBlock);\n\n if (!this._isRunning) {\n // Ensure the one-time update is eventually reset once it's stale\n this._setupBlockResetTimeout();\n }\n\n // _newPotentialLatest() ensures that this._currentBlock is not null\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n return this._currentBlock!;\n }\n\n private async _fetchLatestBlock(): Promise<string> {\n // If there's already a pending fetch, reuse it\n if (this.#pendingFetch) {\n return await this.#pendingFetch.promise;\n }\n\n // Create a new deferred promise for this request\n const { promise, resolve, reject } = createDeferredPromise<string>({\n suppressUnhandledRejection: true,\n });\n this.#pendingFetch = { reject, promise };\n\n try {\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 result = await this._provider.request<[], string>(req);\n log('Got result', result);\n resolve(result);\n return result;\n } catch (error) {\n log('Encountered error fetching block', getErrorMessage(error));\n reject(error);\n this.#rejectPendingLatestBlock(error);\n throw error;\n } finally {\n this.#pendingFetch = undefined;\n }\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 (error: unknown) {\n try {\n this.emit('error', error);\n } catch {\n console.error(`Error updating latest block: ${getErrorMessage(error)}`);\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 #addInternalListener(listener: InternalListener) {\n this.#internalEventListeners.push(listener);\n }\n\n #removeInternalListener(listener: InternalListener) {\n this.#internalEventListeners.splice(\n this.#internalEventListeners.indexOf(listener),\n 1,\n );\n }\n\n #rejectPendingLatestBlock(error: unknown) {\n this.#pendingLatestBlock?.reject(error);\n this.#pendingLatestBlock = undefined;\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"]}
|
|
@@ -14,6 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./PollingBlockTracker"), exports);
|
|
18
|
-
|
|
19
|
-
//# sourceMappingURL=index.js.map
|
|
17
|
+
__exportStar(require("./PollingBlockTracker.cjs"), exports);
|
|
18
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4DAAsC","sourcesContent":["export * from './PollingBlockTracker';\nexport type * from './BlockTracker';\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,0CAAsC;AACtC,wCAAoC"}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,0CAAsC;AACtC,wCAAoC"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,0CAAsC","sourcesContent":["export * from './PollingBlockTracker';\nexport type * from './BlockTracker';\n"]}
|
|
@@ -4,4 +4,4 @@ exports.createModuleLogger = exports.projectLogger = void 0;
|
|
|
4
4
|
const utils_1 = require("@metamask/utils");
|
|
5
5
|
Object.defineProperty(exports, "createModuleLogger", { enumerable: true, get: function () { return utils_1.createModuleLogger; } });
|
|
6
6
|
exports.projectLogger = (0, utils_1.createProjectLogger)('eth-block-tracker');
|
|
7
|
-
//# sourceMappingURL=logging-utils.
|
|
7
|
+
//# sourceMappingURL=logging-utils.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logging-utils.cjs","sourceRoot":"","sources":["../src/logging-utils.ts"],"names":[],"mappings":";;;AAAA,2CAA0E;AAIjE,mGAJqB,0BAAkB,OAIrB;AAFd,QAAA,aAAa,GAAG,IAAA,2BAAmB,EAAC,mBAAmB,CAAC,CAAC","sourcesContent":["import { createProjectLogger, createModuleLogger } from '@metamask/utils';\n\nexport const projectLogger = createProjectLogger('eth-block-tracker');\n\nexport { createModuleLogger };\n"]}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/// <reference types="debug" />
|
|
2
|
-
import { createModuleLogger } from
|
|
2
|
+
import { createModuleLogger } from "@metamask/utils";
|
|
3
3
|
export declare const projectLogger: import("debug").Debugger;
|
|
4
4
|
export { createModuleLogger };
|
|
5
|
+
//# sourceMappingURL=logging-utils.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logging-utils.d.cts","sourceRoot":"","sources":["../src/logging-utils.ts"],"names":[],"mappings":";AAAA,OAAO,EAAuB,kBAAkB,EAAE,wBAAwB;AAE1E,eAAO,MAAM,aAAa,0BAA2C,CAAC;AAEtE,OAAO,EAAE,kBAAkB,EAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logging-utils.d.mts","sourceRoot":"","sources":["../src/logging-utils.ts"],"names":[],"mappings":";AAAA,OAAO,EAAuB,kBAAkB,EAAE,wBAAwB;AAE1E,eAAO,MAAM,aAAa,0BAA2C,CAAC;AAEtE,OAAO,EAAE,kBAAkB,EAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logging-utils.mjs","sourceRoot":"","sources":["../src/logging-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,wBAAwB;AAE1E,MAAM,CAAC,MAAM,aAAa,GAAG,mBAAmB,CAAC,mBAAmB,CAAC,CAAC;AAEtE,OAAO,EAAE,kBAAkB,EAAE,CAAC","sourcesContent":["import { createProjectLogger, createModuleLogger } from '@metamask/utils';\n\nexport const projectLogger = createProjectLogger('eth-block-tracker');\n\nexport { createModuleLogger };\n"]}
|
package/package.json
CHANGED
|
@@ -1,67 +1,92 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask/eth-block-tracker",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "13.0.0",
|
|
4
4
|
"description": "A block tracker for the Ethereum blockchain. Keeps track of the latest block",
|
|
5
|
+
"homepage": "https://github.com/MetaMask/eth-block-tracker#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/MetaMask/eth-block-tracker/issues"
|
|
8
|
+
},
|
|
5
9
|
"repository": {
|
|
6
10
|
"type": "git",
|
|
7
11
|
"url": "https://github.com/MetaMask/eth-block-tracker.git"
|
|
8
12
|
},
|
|
9
13
|
"license": "MIT",
|
|
10
|
-
"
|
|
11
|
-
"
|
|
14
|
+
"sideEffects": false,
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"import": {
|
|
18
|
+
"types": "./dist/index.d.mts",
|
|
19
|
+
"default": "./dist/index.mjs"
|
|
20
|
+
},
|
|
21
|
+
"require": {
|
|
22
|
+
"types": "./dist/index.d.cts",
|
|
23
|
+
"default": "./dist/index.cjs"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"./package.json": "./package.json"
|
|
27
|
+
},
|
|
28
|
+
"main": "./dist/index.cjs",
|
|
29
|
+
"module": "./dist/index.mjs",
|
|
30
|
+
"types": "./dist/index.d.cts",
|
|
12
31
|
"files": [
|
|
13
|
-
"dist
|
|
32
|
+
"dist"
|
|
14
33
|
],
|
|
15
34
|
"scripts": {
|
|
16
|
-
"build": "
|
|
35
|
+
"build": "ts-bridge --project tsconfig.build.json --clean",
|
|
17
36
|
"build:clean": "rimraf dist && yarn build",
|
|
18
|
-
"
|
|
37
|
+
"build:docs": "typedoc",
|
|
38
|
+
"lint": "yarn lint:eslint && yarn lint:constraints && yarn lint:misc --check && yarn lint:dependencies --check && yarn lint:changelog",
|
|
39
|
+
"lint:changelog": "auto-changelog validate --prettier",
|
|
19
40
|
"lint:constraints": "yarn constraints",
|
|
20
|
-
"lint:
|
|
21
|
-
"lint:fix": "
|
|
22
|
-
"lint:
|
|
41
|
+
"lint:dependencies": "depcheck && yarn dedupe --check",
|
|
42
|
+
"lint:dependencies:fix": "depcheck && yarn dedupe",
|
|
43
|
+
"lint:eslint": "eslint . --cache",
|
|
44
|
+
"lint:fix": "yarn lint:eslint --fix && yarn lint:constraints --fix && yarn lint:misc --write && yarn lint:dependencies:fix && yarn lint:changelog",
|
|
45
|
+
"lint:misc": "prettier '**/*.json' '**/*.md' '**/*.yml' '!.yarnrc.yml' --ignore-path .gitignore --no-error-on-unmatched-pattern",
|
|
23
46
|
"prepack": "./scripts/prepack.sh",
|
|
24
|
-
"test": "jest",
|
|
47
|
+
"test": "jest && attw --pack",
|
|
25
48
|
"test:watch": "jest --watch"
|
|
26
49
|
},
|
|
27
50
|
"dependencies": {
|
|
28
51
|
"@metamask/eth-json-rpc-provider": "^5.0.0",
|
|
29
52
|
"@metamask/safe-event-emitter": "^3.1.1",
|
|
30
53
|
"@metamask/utils": "^11.0.1",
|
|
31
|
-
"json-rpc-random-id": "^1.0.1"
|
|
32
|
-
"pify": "^5.0.0"
|
|
54
|
+
"json-rpc-random-id": "^1.0.1"
|
|
33
55
|
},
|
|
34
56
|
"devDependencies": {
|
|
35
|
-
"@
|
|
36
|
-
"@
|
|
37
|
-
"@
|
|
38
|
-
"@metamask/
|
|
39
|
-
"@metamask/eslint-config
|
|
40
|
-
"@metamask/eslint-config-
|
|
57
|
+
"@arethetypeswrong/cli": "^0.15.3",
|
|
58
|
+
"@jest/types": "^27.5.1",
|
|
59
|
+
"@lavamoat/allow-scripts": "^3.0.4",
|
|
60
|
+
"@metamask/auto-changelog": "^3.4.4",
|
|
61
|
+
"@metamask/eslint-config": "^14.0.0",
|
|
62
|
+
"@metamask/eslint-config-jest": "^14.0.0",
|
|
63
|
+
"@metamask/eslint-config-nodejs": "^14.0.0",
|
|
64
|
+
"@metamask/eslint-config-typescript": "^14.0.0",
|
|
41
65
|
"@metamask/json-rpc-engine": "^10.0.0",
|
|
42
|
-
"@
|
|
66
|
+
"@ts-bridge/cli": "^0.6.3",
|
|
67
|
+
"@types/jest": "^27.4.1",
|
|
43
68
|
"@types/json-rpc-random-id": "^1.0.1",
|
|
44
|
-
"@types/node": "^18.
|
|
45
|
-
"@types
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"eslint": "^
|
|
50
|
-
"eslint-
|
|
51
|
-
"eslint-
|
|
52
|
-
"eslint-plugin-
|
|
53
|
-
"eslint-plugin-
|
|
54
|
-
"eslint-plugin-
|
|
55
|
-
"eslint-plugin-
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"prettier": "^2.7.1",
|
|
60
|
-
"prettier-plugin-packagejson": "^2.2.11",
|
|
69
|
+
"@types/node": "^18.18",
|
|
70
|
+
"@yarnpkg/types": "^4.0.1",
|
|
71
|
+
"depcheck": "^1.4.3",
|
|
72
|
+
"eslint": "^9.11.0",
|
|
73
|
+
"eslint-config-prettier": "^9.1.0",
|
|
74
|
+
"eslint-import-resolver-typescript": "^3.6.3",
|
|
75
|
+
"eslint-plugin-import-x": "^4.3.0",
|
|
76
|
+
"eslint-plugin-jest": "^28.8.3",
|
|
77
|
+
"eslint-plugin-jsdoc": "^50.2.4",
|
|
78
|
+
"eslint-plugin-n": "^17.10.3",
|
|
79
|
+
"eslint-plugin-prettier": "^5.2.1",
|
|
80
|
+
"eslint-plugin-promise": "^7.1.0",
|
|
81
|
+
"jest": "^27.5.1",
|
|
82
|
+
"prettier": "^3.3.3",
|
|
83
|
+
"prettier-plugin-packagejson": "^2.5.8",
|
|
61
84
|
"rimraf": "^3.0.2",
|
|
62
|
-
"ts-jest": "^
|
|
63
|
-
"ts-node": "^10.
|
|
64
|
-
"
|
|
85
|
+
"ts-jest": "^27.1.4",
|
|
86
|
+
"ts-node": "^10.9.1",
|
|
87
|
+
"typedoc": "^0.24.8",
|
|
88
|
+
"typescript": "~5.2.2",
|
|
89
|
+
"typescript-eslint": "^8.7.0"
|
|
65
90
|
},
|
|
66
91
|
"packageManager": "yarn@4.5.3",
|
|
67
92
|
"engines": {
|
|
@@ -73,7 +98,8 @@
|
|
|
73
98
|
},
|
|
74
99
|
"lavamoat": {
|
|
75
100
|
"allowScripts": {
|
|
76
|
-
"@lavamoat/preinstall-always-fail": false
|
|
101
|
+
"@lavamoat/preinstall-always-fail": false,
|
|
102
|
+
"eslint-plugin-import-x>unrs-resolver": false
|
|
77
103
|
}
|
|
78
104
|
}
|
|
79
105
|
}
|
package/dist/BlockTracker.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"BlockTracker.js","sourceRoot":"","sources":["../src/BlockTracker.ts"],"names":[],"mappings":"","sourcesContent":["import type SafeEventEmitter from '@metamask/safe-event-emitter';\n\nexport type BlockTracker = SafeEventEmitter & {\n destroy(): Promise<void>;\n\n isRunning(): boolean;\n\n getCurrentBlock(): string | null;\n\n getLatestBlock(): Promise<string>;\n\n checkForLatestBlock(): Promise<string>;\n};\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"PollingBlockTracker.js","sourceRoot":"","sources":["../src/PollingBlockTracker.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AACA,sFAA4D;AAC5D,2CAKyB;AACzB,4EAAmD;AAGnD,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,kBAAkB,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AAE;YACpB,OAAO;SACR;QAED,IAAI,CAAC,oBAAoB,EAAE,CAAC;QAE5B,MAAM,UAAU,GAAG,UAAU,CAAC,GAAG,EAAE;YACjC,qEAAqE;YACrE,mEAAmE;YACnE,IAAI,CAAC,eAAe,EAAE,CAAC;QACzB,CAAC,EAAE,QAAQ,CAAC,CAAC;QAEb,IAAI,UAAU,CAAC,KAAK,IAAI,CAAC,IAAI,CAAC,oBAAoB,EAAE;YAClD,UAAU,CAAC,KAAK,EAAE,CAAC;SACpB;QAED,IAAI,CAAC,eAAe,GAAG,UAAU,CAAC;QAElC,IAAI,CAAC,IAAI,CAAC,0BAA0B,CAAC,CAAC;IACxC,CAAC;IAED,oBAAoB;QAClB,IAAI,IAAI,CAAC,eAAe,EAAE;YACxB,YAAY,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;YACnC,IAAI,CAAC,eAAe,GAAG,SAAS,CAAC;SAClC;IACH,CAAC;CAiBF;AA9YD,kDA8YC;sTAfsB,QAA0B;IAC7C,uBAAA,IAAI,mDAAwB,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;AAC9C,CAAC,qGAEuB,QAA0B;IAChD,uBAAA,IAAI,mDAAwB,CAAC,MAAM,CACjC,uBAAA,IAAI,mDAAwB,CAAC,OAAO,CAAC,QAAQ,CAAC,EAC9C,CAAC,CACF,CAAC;AACJ,CAAC,yGAEyB,KAAc;;IACtC,MAAA,uBAAA,IAAI,+CAAoB,0CAAE,MAAM,CAAC,KAAK,CAAC,CAAC;IACxC,uBAAA,IAAI,2CAAuB,SAAS,MAAA,CAAC;AACvC,CAAC;AAGH;;;;;;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 {\n createDeferredPromise,\n type DeferredPromise,\n getErrorMessage,\n type JsonRpcRequest,\n} from '@metamask/utils';\nimport getCreateRandomId from 'json-rpc-random-id';\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 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\ntype InternalListener = (value: string) => void;\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 readonly #internalEventListeners: InternalListener[] = [];\n\n #pendingLatestBlock?: Omit<DeferredPromise<string>, 'resolve'>;\n\n #pendingFetch?: Omit<DeferredPromise<string>, 'resolve'>;\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 super.removeAllListeners();\n this._maybeEnd();\n }\n\n isRunning(): boolean {\n return this._isRunning;\n }\n\n getCurrentBlock(): string | null {\n return this._currentBlock;\n }\n\n async getLatestBlock({\n useCache = true,\n }: { useCache?: boolean } = {}): Promise<string> {\n // return if available\n if (this._currentBlock && useCache) {\n return this._currentBlock;\n }\n\n if (this.#pendingLatestBlock) {\n return await this.#pendingLatestBlock.promise;\n }\n\n const { promise, resolve, reject } = createDeferredPromise<string>({\n suppressUnhandledRejection: true,\n });\n this.#pendingLatestBlock = { reject, promise };\n\n if (this._isRunning) {\n try {\n // If tracker is running, wait for next block with timeout\n const onLatestBlock = (value: string) => {\n this.#removeInternalListener(onLatestBlock);\n this.removeListener('latest', onLatestBlock);\n resolve(value);\n };\n\n this.#addInternalListener(onLatestBlock);\n this.once('latest', onLatestBlock);\n\n return await promise;\n } catch (error) {\n reject(error);\n throw error;\n } finally {\n this.#pendingLatestBlock = undefined;\n }\n } else {\n // If tracker isn't running, just fetch directly\n try {\n const latestBlock = await this._updateLatestBlock();\n resolve(latestBlock);\n return latestBlock;\n } catch (error) {\n reject(error);\n throw error;\n } finally {\n // We want to rate limit calls to this method if we made a direct fetch\n // for the block number because the BlockTracker was not running. We\n // achieve this by delaying the unsetting of the #pendingLatestBlock promise.\n setTimeout(() => {\n this.#pendingLatestBlock = undefined;\n }, this._pollingInterval);\n }\n }\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.#rejectPendingLatestBlock(new Error('Block tracker destroyed'));\n this.emit('_ended');\n }\n\n private _getBlockTrackerEventCount(): number {\n return (\n blockTrackerEvents\n .map((eventName) => this.listeners(eventName))\n .flat()\n // internal listeners are not included in the count\n .filter((listener) =>\n this.#internalEventListeners.every(\n (internalListener) => !Object.is(internalListener, listener),\n ),\n ).length\n );\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 /**\n * Checks for the latest block, updates the internal state, and returns the\n * value immediately rather than waiting for the next polling interval.\n *\n * @deprecated Use {@link getLatestBlock} instead.\n * @returns A promise that resolves to the latest block number.\n */\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<string> {\n // fetch + set latest block\n const latestBlock = await this._fetchLatestBlock();\n this._newPotentialLatest(latestBlock);\n // _newPotentialLatest() ensures that this._currentBlock is not null\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n return this._currentBlock!;\n }\n\n private async _fetchLatestBlock(): Promise<string> {\n // If there's already a pending fetch, reuse it\n if (this.#pendingFetch) {\n return await this.#pendingFetch.promise;\n }\n\n // Create a new deferred promise for this request\n const { promise, resolve, reject } = createDeferredPromise<string>({\n suppressUnhandledRejection: true,\n });\n this.#pendingFetch = { reject, promise };\n\n try {\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 result = await this._provider.request<[], string>(req);\n log('Got result', result);\n resolve(result);\n return result;\n } catch (error) {\n log('Encountered error fetching block', getErrorMessage(error));\n reject(error);\n this.#rejectPendingLatestBlock(error);\n throw error;\n } finally {\n this.#pendingFetch = undefined;\n }\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 (error: unknown) {\n try {\n this.emit('error', error);\n } catch {\n console.error(`Error updating latest block: ${getErrorMessage(error)}`);\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 #addInternalListener(listener: InternalListener) {\n this.#internalEventListeners.push(listener);\n }\n\n #removeInternalListener(listener: InternalListener) {\n this.#internalEventListeners.splice(\n this.#internalEventListeners.indexOf(listener),\n 1,\n );\n }\n\n #rejectPendingLatestBlock(error: unknown) {\n this.#pendingLatestBlock?.reject(error);\n this.#pendingLatestBlock = undefined;\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/dist/index.d.ts
DELETED
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,wDAAsC;AACtC,iDAA+B","sourcesContent":["export * from './PollingBlockTracker';\nexport * from './BlockTracker';\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"logging-utils.js","sourceRoot":"","sources":["../src/logging-utils.ts"],"names":[],"mappings":";;;AAAA,2CAA0E;AAIjE,mGAJqB,0BAAkB,OAIrB;AAFd,QAAA,aAAa,GAAG,IAAA,2BAAmB,EAAC,mBAAmB,CAAC,CAAC","sourcesContent":["import { createProjectLogger, createModuleLogger } from '@metamask/utils';\n\nexport const projectLogger = createProjectLogger('eth-block-tracker');\n\nexport { createModuleLogger };\n"]}
|