@metamask-previews/eth-json-rpc-middleware 21.0.0-preview-70abd50a → 21.0.0-preview-536ed51
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 +3 -0
- package/dist/block-cache.cjs +41 -32
- package/dist/block-cache.cjs.map +1 -1
- package/dist/block-cache.d.cts +12 -3
- package/dist/block-cache.d.cts.map +1 -1
- package/dist/block-cache.d.mts +12 -3
- package/dist/block-cache.d.mts.map +1 -1
- package/dist/block-cache.mjs +41 -32
- package/dist/block-cache.mjs.map +1 -1
- package/dist/block-ref-rewrite.cjs +12 -8
- package/dist/block-ref-rewrite.cjs.map +1 -1
- package/dist/block-ref-rewrite.d.cts +3 -3
- package/dist/block-ref-rewrite.d.cts.map +1 -1
- package/dist/block-ref-rewrite.d.mts +3 -3
- package/dist/block-ref-rewrite.d.mts.map +1 -1
- package/dist/block-ref-rewrite.mjs +12 -8
- package/dist/block-ref-rewrite.mjs.map +1 -1
- package/dist/block-ref.cjs +17 -10
- package/dist/block-ref.cjs.map +1 -1
- package/dist/block-ref.d.cts +12 -3
- package/dist/block-ref.d.cts.map +1 -1
- package/dist/block-ref.d.mts +12 -3
- package/dist/block-ref.d.mts.map +1 -1
- package/dist/block-ref.mjs +17 -10
- package/dist/block-ref.mjs.map +1 -1
- package/dist/block-tracker-inspector.cjs +28 -25
- package/dist/block-tracker-inspector.cjs.map +1 -1
- package/dist/block-tracker-inspector.d.cts +3 -3
- package/dist/block-tracker-inspector.d.cts.map +1 -1
- package/dist/block-tracker-inspector.d.mts +3 -3
- package/dist/block-tracker-inspector.d.mts.map +1 -1
- package/dist/block-tracker-inspector.mjs +28 -25
- package/dist/block-tracker-inspector.mjs.map +1 -1
- package/dist/fetch.cjs +8 -12
- package/dist/fetch.cjs.map +1 -1
- package/dist/fetch.d.cts +5 -3
- package/dist/fetch.d.cts.map +1 -1
- package/dist/fetch.d.mts +5 -3
- package/dist/fetch.d.mts.map +1 -1
- package/dist/fetch.mjs +8 -12
- package/dist/fetch.mjs.map +1 -1
- package/dist/inflight-cache.cjs +61 -45
- package/dist/inflight-cache.cjs.map +1 -1
- package/dist/inflight-cache.d.cts +12 -3
- package/dist/inflight-cache.d.cts.map +1 -1
- package/dist/inflight-cache.d.mts +12 -3
- package/dist/inflight-cache.d.mts.map +1 -1
- package/dist/inflight-cache.mjs +61 -45
- package/dist/inflight-cache.mjs.map +1 -1
- package/dist/methods/wallet-request-execution-permissions.cjs +20 -9
- package/dist/methods/wallet-request-execution-permissions.cjs.map +1 -1
- package/dist/methods/wallet-request-execution-permissions.d.cts +14 -3
- package/dist/methods/wallet-request-execution-permissions.d.cts.map +1 -1
- package/dist/methods/wallet-request-execution-permissions.d.mts +14 -3
- package/dist/methods/wallet-request-execution-permissions.d.mts.map +1 -1
- package/dist/methods/wallet-request-execution-permissions.mjs +18 -7
- package/dist/methods/wallet-request-execution-permissions.mjs.map +1 -1
- package/dist/methods/wallet-revoke-execution-permission.cjs +23 -11
- package/dist/methods/wallet-revoke-execution-permission.cjs.map +1 -1
- package/dist/methods/wallet-revoke-execution-permission.d.cts +19 -5
- package/dist/methods/wallet-revoke-execution-permission.d.cts.map +1 -1
- package/dist/methods/wallet-revoke-execution-permission.d.mts +19 -5
- package/dist/methods/wallet-revoke-execution-permission.d.mts.map +1 -1
- package/dist/methods/wallet-revoke-execution-permission.mjs +20 -8
- package/dist/methods/wallet-revoke-execution-permission.mjs.map +1 -1
- package/dist/providerAsMiddleware.cjs +18 -1
- package/dist/providerAsMiddleware.cjs.map +1 -1
- package/dist/providerAsMiddleware.d.cts +18 -3
- package/dist/providerAsMiddleware.d.cts.map +1 -1
- package/dist/providerAsMiddleware.d.mts +18 -3
- package/dist/providerAsMiddleware.d.mts.map +1 -1
- package/dist/providerAsMiddleware.mjs +16 -0
- package/dist/providerAsMiddleware.mjs.map +1 -1
- package/dist/retryOnEmpty.cjs +8 -11
- package/dist/retryOnEmpty.cjs.map +1 -1
- package/dist/retryOnEmpty.d.cts +3 -3
- package/dist/retryOnEmpty.d.cts.map +1 -1
- package/dist/retryOnEmpty.d.mts +3 -3
- package/dist/retryOnEmpty.d.mts.map +1 -1
- package/dist/retryOnEmpty.mjs +8 -11
- package/dist/retryOnEmpty.mjs.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +0 -5
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +0 -5
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/dist/utils/error.cjs +7 -0
- package/dist/utils/error.cjs.map +1 -1
- package/dist/utils/error.d.cts +7 -0
- package/dist/utils/error.d.cts.map +1 -1
- package/dist/utils/error.d.mts +7 -0
- package/dist/utils/error.d.mts.map +1 -1
- package/dist/utils/error.mjs +7 -0
- package/dist/utils/error.mjs.map +1 -1
- package/dist/utils/validation.cjs +34 -2
- package/dist/utils/validation.cjs.map +1 -1
- package/dist/utils/validation.d.cts +29 -3
- package/dist/utils/validation.d.cts.map +1 -1
- package/dist/utils/validation.d.mts +29 -3
- package/dist/utils/validation.d.mts.map +1 -1
- package/dist/utils/validation.mjs +34 -2
- package/dist/utils/validation.mjs.map +1 -1
- package/dist/wallet.cjs +199 -92
- package/dist/wallet.cjs.map +1 -1
- package/dist/wallet.d.cts +35 -8
- package/dist/wallet.d.cts.map +1 -1
- package/dist/wallet.d.mts +35 -8
- package/dist/wallet.d.mts.map +1 -1
- package/dist/wallet.mjs +200 -93
- package/dist/wallet.mjs.map +1 -1
- package/package.json +2 -1
package/CHANGELOG.md
CHANGED
|
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
9
9
|
|
|
10
10
|
### Changed
|
|
11
11
|
|
|
12
|
+
- **BREAKING:** Migrate to `JsonRpcEngineV2` ([#7065](https://github.com/MetaMask/core/pull/7065))
|
|
13
|
+
- Migrates all middleware from `JsonRpcEngine` to `JsonRpcEngineV2`.
|
|
14
|
+
- To continue using this package with the legacy `JsonRpcEngine`, use the `asLegacyMiddleware` backwards compatibility function.
|
|
12
15
|
- **BREAKING:** Use `InternalProvider` instead of `SafeEventEmitterProvider` ([#6796](https://github.com/MetaMask/core/pull/6796))
|
|
13
16
|
- Wherever a `SafeEventEmitterProvider` was expected, an `InternalProvider` is now expected instead.
|
|
14
17
|
- **BREAKING:** Stop retrying `undefined` results for methods that include a block tag parameter ([#7001](https://github.com/MetaMask/core/pull/7001))
|
package/dist/block-cache.cjs
CHANGED
|
@@ -1,7 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
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");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
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");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
13
|
+
var _BlockCacheStrategy_cache;
|
|
2
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
15
|
exports.createBlockCacheMiddleware = void 0;
|
|
4
|
-
const json_rpc_engine_1 = require("@metamask/json-rpc-engine");
|
|
5
16
|
const logging_utils_1 = require("./logging-utils.cjs");
|
|
6
17
|
const cache_1 = require("./utils/cache.cjs");
|
|
7
18
|
const log = (0, logging_utils_1.createModuleLogger)(logging_utils_1.projectLogger, 'block-cache');
|
|
@@ -12,15 +23,16 @@ const emptyValues = [undefined, null, '\u003cnil\u003e'];
|
|
|
12
23
|
//
|
|
13
24
|
class BlockCacheStrategy {
|
|
14
25
|
constructor() {
|
|
15
|
-
this
|
|
26
|
+
_BlockCacheStrategy_cache.set(this, void 0);
|
|
27
|
+
__classPrivateFieldSet(this, _BlockCacheStrategy_cache, {}, "f");
|
|
16
28
|
}
|
|
17
29
|
getBlockCache(blockNumberHex) {
|
|
18
30
|
const blockNumber = Number.parseInt(blockNumberHex, 16);
|
|
19
|
-
let blockCache = this
|
|
31
|
+
let blockCache = __classPrivateFieldGet(this, _BlockCacheStrategy_cache, "f")[blockNumber];
|
|
20
32
|
// create new cache if necesary
|
|
21
33
|
if (!blockCache) {
|
|
22
34
|
const newCache = {};
|
|
23
|
-
this
|
|
35
|
+
__classPrivateFieldGet(this, _BlockCacheStrategy_cache, "f")[blockNumber] = newCache;
|
|
24
36
|
blockCache = newCache;
|
|
25
37
|
}
|
|
26
38
|
return blockCache;
|
|
@@ -80,18 +92,24 @@ class BlockCacheStrategy {
|
|
|
80
92
|
clearBefore(oldBlockHex) {
|
|
81
93
|
const oldBlockNumber = Number.parseInt(oldBlockHex, 16);
|
|
82
94
|
// clear old caches
|
|
83
|
-
Object.keys(this
|
|
95
|
+
Object.keys(__classPrivateFieldGet(this, _BlockCacheStrategy_cache, "f"))
|
|
84
96
|
.map(Number)
|
|
85
97
|
.filter((num) => num < oldBlockNumber)
|
|
86
|
-
.forEach((num) => delete this
|
|
98
|
+
.forEach((num) => delete __classPrivateFieldGet(this, _BlockCacheStrategy_cache, "f")[num]);
|
|
87
99
|
}
|
|
88
100
|
}
|
|
101
|
+
_BlockCacheStrategy_cache = new WeakMap();
|
|
102
|
+
/**
|
|
103
|
+
* Creates a middleware that caches block-related requests.
|
|
104
|
+
*
|
|
105
|
+
* @param options - The options for the middleware.
|
|
106
|
+
* @param options.blockTracker - The block tracker to use.
|
|
107
|
+
* @returns The block cache middleware.
|
|
108
|
+
*/
|
|
89
109
|
function createBlockCacheMiddleware({ blockTracker, } = {}) {
|
|
90
|
-
// validate options
|
|
91
110
|
if (!blockTracker) {
|
|
92
111
|
throw new Error('createBlockCacheMiddleware - No PollingBlockTracker specified');
|
|
93
112
|
}
|
|
94
|
-
// create caching strategies
|
|
95
113
|
const blockCache = new BlockCacheStrategy();
|
|
96
114
|
const strategies = {
|
|
97
115
|
[cache_1.CacheStrategy.Permanent]: blockCache,
|
|
@@ -99,28 +117,23 @@ function createBlockCacheMiddleware({ blockTracker, } = {}) {
|
|
|
99
117
|
[cache_1.CacheStrategy.Fork]: blockCache,
|
|
100
118
|
[cache_1.CacheStrategy.Never]: undefined,
|
|
101
119
|
};
|
|
102
|
-
return
|
|
103
|
-
|
|
104
|
-
if (req.skipCache) {
|
|
120
|
+
return async ({ request, next, context }) => {
|
|
121
|
+
if (context.get('skipCache')) {
|
|
105
122
|
return next();
|
|
106
123
|
}
|
|
107
|
-
|
|
108
|
-
const type = (0, cache_1.cacheTypeForMethod)(req.method);
|
|
124
|
+
const type = (0, cache_1.cacheTypeForMethod)(request.method);
|
|
109
125
|
const strategy = strategies[type];
|
|
110
|
-
// If there's no strategy in place, pass it down the chain.
|
|
111
126
|
if (!strategy) {
|
|
112
127
|
return next();
|
|
113
128
|
}
|
|
114
|
-
|
|
115
|
-
if (!strategy.canCacheRequest(req)) {
|
|
129
|
+
if (!strategy.canCacheRequest(request)) {
|
|
116
130
|
return next();
|
|
117
131
|
}
|
|
118
|
-
|
|
119
|
-
const requestBlockTag = (0, cache_1.blockTagForRequest)(req);
|
|
132
|
+
const requestBlockTag = (0, cache_1.blockTagForRequest)(request);
|
|
120
133
|
const blockTag = requestBlockTag && typeof requestBlockTag === 'string'
|
|
121
134
|
? requestBlockTag
|
|
122
135
|
: 'latest';
|
|
123
|
-
log('blockTag = %o, req = %o', blockTag,
|
|
136
|
+
log('blockTag = %o, req = %o', blockTag, request);
|
|
124
137
|
// get exact block number
|
|
125
138
|
let requestedBlockNumber;
|
|
126
139
|
if (blockTag === 'earliest') {
|
|
@@ -128,7 +141,6 @@ function createBlockCacheMiddleware({ blockTracker, } = {}) {
|
|
|
128
141
|
requestedBlockNumber = '0x00';
|
|
129
142
|
}
|
|
130
143
|
else if (blockTag === 'latest') {
|
|
131
|
-
// fetch latest block number
|
|
132
144
|
log('Fetching latest block number to determine cache key');
|
|
133
145
|
const latestBlockNumber = await blockTracker.getLatestBlock();
|
|
134
146
|
// clear all cache before latest block
|
|
@@ -137,29 +149,26 @@ function createBlockCacheMiddleware({ blockTracker, } = {}) {
|
|
|
137
149
|
requestedBlockNumber = latestBlockNumber;
|
|
138
150
|
}
|
|
139
151
|
else {
|
|
140
|
-
//
|
|
152
|
+
// we have a hex number
|
|
141
153
|
requestedBlockNumber = blockTag;
|
|
142
154
|
}
|
|
143
155
|
// end on a hit, continue on a miss
|
|
144
|
-
const cacheResult = await strategy.get(
|
|
156
|
+
const cacheResult = await strategy.get(request, requestedBlockNumber);
|
|
145
157
|
if (cacheResult === undefined) {
|
|
146
158
|
// cache miss
|
|
147
159
|
// wait for other middleware to handle request
|
|
148
160
|
log('No cache stored under block number %o, carrying request forward', requestedBlockNumber);
|
|
149
|
-
await next();
|
|
161
|
+
const result = await next();
|
|
150
162
|
// add result to cache
|
|
151
163
|
// it's safe to cast res.result as Block, due to runtime type checks
|
|
152
164
|
// performed when strategy.set is called
|
|
153
|
-
log('Populating cache with',
|
|
154
|
-
await strategy.set(
|
|
165
|
+
log('Populating cache with', result);
|
|
166
|
+
await strategy.set(request, requestedBlockNumber, result);
|
|
167
|
+
return result;
|
|
155
168
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
res.result = cacheResult;
|
|
160
|
-
}
|
|
161
|
-
return undefined;
|
|
162
|
-
});
|
|
169
|
+
log('Cache hit, reusing cache result stored under block number %o', requestedBlockNumber);
|
|
170
|
+
return cacheResult;
|
|
171
|
+
};
|
|
163
172
|
}
|
|
164
173
|
exports.createBlockCacheMiddleware = createBlockCacheMiddleware;
|
|
165
174
|
//# sourceMappingURL=block-cache.cjs.map
|
package/dist/block-cache.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block-cache.cjs","sourceRoot":"","sources":["../src/block-cache.ts"],"names":[],"mappings":";;;AACA,+DAAkE;AAGlE,uDAAoE;AASpE,6CAMuB;AAEvB,MAAM,GAAG,GAAG,IAAA,kCAAkB,EAAC,6BAAa,EAAE,aAAa,CAAC,CAAC;AAC7D,0EAA0E;AAC1E,MAAM,WAAW,GAAG,CAAC,SAAS,EAAE,IAAI,EAAE,iBAAiB,CAAC,CAAC;AAMzD,EAAE;AACF,mBAAmB;AACnB,EAAE;AAEF,MAAM,kBAAkB;IAGtB;QACE,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;IAClB,CAAC;IAED,aAAa,CAAC,cAAsB;QAClC,MAAM,WAAW,GAAW,MAAM,CAAC,QAAQ,CAAC,cAAc,EAAE,EAAE,CAAC,CAAC;QAChE,IAAI,UAAU,GAAe,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;QACrD,+BAA+B;QAC/B,IAAI,CAAC,UAAU,EAAE;YACf,MAAM,QAAQ,GAAe,EAAE,CAAC;YAChC,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,QAAQ,CAAC;YACnC,UAAU,GAAG,QAAQ,CAAC;SACvB;QACD,OAAO,UAAU,CAAC;IACpB,CAAC;IAED,KAAK,CAAC,GAAG,CACP,OAAuB,EACvB,oBAA4B;QAE5B,qBAAqB;QACrB,MAAM,UAAU,GAAe,IAAI,CAAC,aAAa,CAAC,oBAAoB,CAAC,CAAC;QACxE,gCAAgC;QAChC,MAAM,UAAU,GAAkB,IAAA,iCAAyB,EAAC,OAAO,EAAE,IAAI,CAAC,CAAC;QAC3E,OAAO,UAAU,CAAC,CAAC,CAAC,UAAU,CAAC,UA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,wCAAwC;YACxC,GAAG,CAAC,uBAAuB,EAAE,GAAG,CAAC,CAAC;YAClC,MAAM,QAAQ,CAAC,GAAG,CAAC,GAAG,EAAE,oBAAoB,EAAE,GAAG,CAAC,MAAe,CAAC,CAAC;SACpE;aAAM;YACL,4BAA4B;YAC5B,GAAG,CACD,8DAA8D,EAC9D,oBAAoB,CACrB,CAAC;YACF,GAAG,CAAC,MAAM,GAAG,WAAW,CAAC;SAC1B;QACD,OAAO,SAAS,CAAC;IACnB,CAAC,CACF,CAAC;AACJ,CAAC;AApGD,gEAoGC","sourcesContent":["import type { PollingBlockTracker } from '@metamask/eth-block-tracker';\nimport { createAsyncMiddleware } from '@metamask/json-rpc-engine';\nimport type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils';\n\nimport { projectLogger, createModuleLogger } from './logging-utils';\nimport type {\n Block,\n BlockCache,\n // eslint-disable-next-line @typescript-eslint/no-shadow\n Cache,\n JsonRpcCacheMiddleware,\n JsonRpcRequestToCache,\n} from './types';\nimport {\n cacheIdentifierForRequest,\n blockTagForRequest,\n cacheTypeForMethod,\n canCache,\n CacheStrategy,\n} from './utils/cache';\n\nconst log = createModuleLogger(projectLogger, 'block-cache');\n// `<nil>` comes from https://github.com/ethereum/go-ethereum/issues/16925\nconst emptyValues = [undefined, null, '\\u003cnil\\u003e'];\n\ntype BlockCacheMiddlewareOptions = {\n blockTracker?: PollingBlockTracker;\n};\n\n//\n// Cache Strategies\n//\n\nclass BlockCacheStrategy {\n private cache: Cache;\n\n constructor() {\n this.cache = {};\n }\n\n getBlockCache(blockNumberHex: string): BlockCache {\n const blockNumber: number = Number.parseInt(blockNumberHex, 16);\n let blockCache: BlockCache = this.cache[blockNumber];\n // create new cache if necesary\n if (!blockCache) {\n const newCache: BlockCache = {};\n this.cache[blockNumber] = newCache;\n blockCache = newCache;\n }\n return blockCache;\n }\n\n async get(\n request: JsonRpcRequest,\n requestedBlockNumber: string,\n ): Promise<Block | undefined> {\n // lookup block cache\n const blockCache: BlockCache = this.getBlockCache(requestedBlockNumber);\n // lookup payload in block cache\n const identifier: string | null = cacheIdentifierForRequest(request, true);\n return identifier ? blockCache[identifier] : undefined;\n }\n\n async set(\n request: JsonRpcRequest,\n requestedBlockNumber: string,\n result: Block,\n ): Promise<void> {\n // check if we can cached this result\n const canCacheResult: boolean = this.canCacheResult(request, result);\n if (!canCacheResult) {\n return;\n }\n\n // set the value in the cache\n const identifier: string | null = cacheIdentifierForRequest(request, true);\n if (!identifier) {\n return;\n }\n const blockCache: BlockCache = this.getBlockCache(requestedBlockNumber);\n blockCache[identifier] = result;\n }\n\n canCacheRequest(request: JsonRpcRequest): boolean {\n // check request method\n if (!canCache(request.method)) {\n return false;\n }\n // check blockTag\n const blockTag = blockTagForRequest(request);\n\n if (blockTag === 'pending') {\n return false;\n }\n // can be cached\n return true;\n }\n\n canCacheResult(request: JsonRpcRequest, result: Block): boolean {\n // never cache empty values (e.g. undefined)\n if (emptyValues.includes(result as any)) {\n return false;\n }\n\n // check if transactions have block reference before caching\n if (\n request.method &&\n ['eth_getTransactionByHash', 'eth_getTransactionReceipt'].includes(\n request.method,\n )\n ) {\n if (\n !result?.blockHash ||\n result.blockHash ===\n '0x0000000000000000000000000000000000000000000000000000000000000000'\n ) {\n return false;\n }\n }\n // otherwise true\n return true;\n }\n\n // removes all block caches with block number lower than `oldBlockHex`\n clearBefore(oldBlockHex: string): void {\n const oldBlockNumber: number = Number.parseInt(oldBlockHex, 16);\n // clear old caches\n Object.keys(this.cache)\n .map(Number)\n .filter((num) => num < oldBlockNumber)\n .forEach((num) => delete this.cache[num]);\n }\n}\n\nexport function createBlockCacheMiddleware({\n blockTracker,\n}: BlockCacheMiddlewareOptions = {}): JsonRpcCacheMiddleware<\n JsonRpcParams,\n Json\n> {\n // validate options\n if (!blockTracker) {\n throw new Error(\n 'createBlockCacheMiddleware - No PollingBlockTracker specified',\n );\n }\n\n // create caching strategies\n const blockCache: BlockCacheStrategy = new BlockCacheStrategy();\n const strategies: Record<CacheStrategy, BlockCacheStrategy | undefined> = {\n [CacheStrategy.Permanent]: blockCache,\n [CacheStrategy.Block]: blockCache,\n [CacheStrategy.Fork]: blockCache,\n [CacheStrategy.Never]: undefined,\n };\n\n return createAsyncMiddleware(\n async (req: JsonRpcRequestToCache<JsonRpcParams>, res, next) => {\n // allow cach to be skipped if so specified\n if (req.skipCache) {\n return next();\n }\n // check type and matching strategy\n const type = cacheTypeForMethod(req.method);\n const strategy = strategies[type];\n // If there's no strategy in place, pass it down the chain.\n if (!strategy) {\n return next();\n }\n\n // If the strategy can't cache this request, ignore it.\n if (!strategy.canCacheRequest(req)) {\n return next();\n }\n\n // get block reference (number or keyword)\n const requestBlockTag = blockTagForRequest(req);\n const blockTag =\n requestBlockTag && typeof requestBlockTag === 'string'\n ? requestBlockTag\n : 'latest';\n\n log('blockTag = %o, req = %o', blockTag, req);\n\n // get exact block number\n let requestedBlockNumber: string;\n if (blockTag === 'earliest') {\n // this just exists for symmetry with \"latest\"\n requestedBlockNumber = '0x00';\n } else if (blockTag === 'latest') {\n // fetch latest block number\n log('Fetching latest block number to determine cache key');\n const latestBlockNumber = await blockTracker.getLatestBlock();\n // clear all cache before latest block\n log(\n 'Clearing values stored under block numbers before %o',\n latestBlockNumber,\n );\n blockCache.clearBefore(latestBlockNumber);\n requestedBlockNumber = latestBlockNumber;\n } else {\n // We have a hex number\n requestedBlockNumber = blockTag;\n }\n // end on a hit, continue on a miss\n const cacheResult: Block | undefined = await strategy.get(\n req,\n requestedBlockNumber,\n );\n if (cacheResult === undefined) {\n // cache miss\n // wait for other middleware to handle request\n log(\n 'No cache stored under block number %o, carrying request forward',\n requestedBlockNumber,\n );\n await next();\n\n // add result to cache\n // it's safe to cast res.result as Block, due to runtime type checks\n // performed when strategy.set is called\n log('Populating cache with', res);\n await strategy.set(req, requestedBlockNumber, res.result as Block);\n } else {\n // fill in result from cache\n log(\n 'Cache hit, reusing cache result stored under block number %o',\n requestedBlockNumber,\n );\n res.result = cac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D,GAAG,CACD,8DAA8D,EAC9D,oBAAoB,CACrB,CAAC;QACF,OAAO,WAAW,CAAC;IACrB,CAAC,CAAC;AACJ,CAAC;AAzFD,gEAyFC","sourcesContent":["import type { PollingBlockTracker } from '@metamask/eth-block-tracker';\nimport type {\n JsonRpcMiddleware,\n MiddlewareContext,\n} from '@metamask/json-rpc-engine/v2';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\n\nimport { projectLogger, createModuleLogger } from './logging-utils';\nimport type {\n Block,\n BlockCache,\n // eslint-disable-next-line @typescript-eslint/no-shadow\n Cache,\n} from './types';\nimport {\n cacheIdentifierForRequest,\n blockTagForRequest,\n cacheTypeForMethod,\n canCache,\n CacheStrategy,\n} from './utils/cache';\n\nconst log = createModuleLogger(projectLogger, 'block-cache');\n// `<nil>` comes from https://github.com/ethereum/go-ethereum/issues/16925\nconst emptyValues: unknown[] = [undefined, null, '\\u003cnil\\u003e'];\n\ntype BlockCacheMiddlewareOptions = {\n blockTracker?: PollingBlockTracker;\n};\n\n//\n// Cache Strategies\n//\n\nclass BlockCacheStrategy {\n #cache: Cache;\n\n constructor() {\n this.#cache = {};\n }\n\n getBlockCache(blockNumberHex: string): BlockCache {\n const blockNumber: number = Number.parseInt(blockNumberHex, 16);\n let blockCache: BlockCache = this.#cache[blockNumber];\n // create new cache if necesary\n if (!blockCache) {\n const newCache: BlockCache = {};\n this.#cache[blockNumber] = newCache;\n blockCache = newCache;\n }\n return blockCache;\n }\n\n async get(\n request: JsonRpcRequest,\n requestedBlockNumber: string,\n ): Promise<Block | undefined> {\n // lookup block cache\n const blockCache: BlockCache = this.getBlockCache(requestedBlockNumber);\n // lookup payload in block cache\n const identifier: string | null = cacheIdentifierForRequest(request, true);\n return identifier ? blockCache[identifier] : undefined;\n }\n\n async set(\n request: JsonRpcRequest,\n requestedBlockNumber: string,\n result: Block,\n ): Promise<void> {\n // check if we can cached this result\n const canCacheResult: boolean = this.canCacheResult(request, result);\n if (!canCacheResult) {\n return;\n }\n\n // set the value in the cache\n const identifier: string | null = cacheIdentifierForRequest(request, true);\n if (!identifier) {\n return;\n }\n const blockCache: BlockCache = this.getBlockCache(requestedBlockNumber);\n blockCache[identifier] = result;\n }\n\n canCacheRequest(request: JsonRpcRequest): boolean {\n // check request method\n if (!canCache(request.method)) {\n return false;\n }\n // check blockTag\n const blockTag = blockTagForRequest(request);\n\n if (blockTag === 'pending') {\n return false;\n }\n // can be cached\n return true;\n }\n\n canCacheResult(request: JsonRpcRequest, result: Block): boolean {\n // never cache empty values (e.g. undefined)\n if (emptyValues.includes(result)) {\n return false;\n }\n\n // check if transactions have block reference before caching\n if (\n request.method &&\n ['eth_getTransactionByHash', 'eth_getTransactionReceipt'].includes(\n request.method,\n )\n ) {\n if (\n !result?.blockHash ||\n result.blockHash ===\n '0x0000000000000000000000000000000000000000000000000000000000000000'\n ) {\n return false;\n }\n }\n // otherwise true\n return true;\n }\n\n // removes all block caches with block number lower than `oldBlockHex`\n clearBefore(oldBlockHex: string): void {\n const oldBlockNumber: number = Number.parseInt(oldBlockHex, 16);\n // clear old caches\n Object.keys(this.#cache)\n .map(Number)\n .filter((num) => num < oldBlockNumber)\n .forEach((num) => delete this.#cache[num]);\n }\n}\n\n/**\n * Creates a middleware that caches block-related requests.\n *\n * @param options - The options for the middleware.\n * @param options.blockTracker - The block tracker to use.\n * @returns The block cache middleware.\n */\nexport function createBlockCacheMiddleware({\n blockTracker,\n}: BlockCacheMiddlewareOptions = {}): JsonRpcMiddleware<\n JsonRpcRequest,\n Json,\n MiddlewareContext<{ skipCache?: boolean }>\n> {\n if (!blockTracker) {\n throw new Error(\n 'createBlockCacheMiddleware - No PollingBlockTracker specified',\n );\n }\n\n const blockCache: BlockCacheStrategy = new BlockCacheStrategy();\n const strategies: Record<CacheStrategy, BlockCacheStrategy | undefined> = {\n [CacheStrategy.Permanent]: blockCache,\n [CacheStrategy.Block]: blockCache,\n [CacheStrategy.Fork]: blockCache,\n [CacheStrategy.Never]: undefined,\n };\n\n return async ({ request, next, context }) => {\n if (context.get('skipCache')) {\n return next();\n }\n\n const type = cacheTypeForMethod(request.method);\n const strategy = strategies[type];\n if (!strategy) {\n return next();\n }\n\n if (!strategy.canCacheRequest(request)) {\n return next();\n }\n\n const requestBlockTag = blockTagForRequest(request);\n const blockTag =\n requestBlockTag && typeof requestBlockTag === 'string'\n ? requestBlockTag\n : 'latest';\n\n log('blockTag = %o, req = %o', blockTag, request);\n\n // get exact block number\n let requestedBlockNumber: string;\n if (blockTag === 'earliest') {\n // this just exists for symmetry with \"latest\"\n requestedBlockNumber = '0x00';\n } else if (blockTag === 'latest') {\n log('Fetching latest block number to determine cache key');\n const latestBlockNumber = await blockTracker.getLatestBlock();\n\n // clear all cache before latest block\n log(\n 'Clearing values stored under block numbers before %o',\n latestBlockNumber,\n );\n blockCache.clearBefore(latestBlockNumber);\n requestedBlockNumber = latestBlockNumber;\n } else {\n // we have a hex number\n requestedBlockNumber = blockTag;\n }\n\n // end on a hit, continue on a miss\n const cacheResult = await strategy.get(request, requestedBlockNumber);\n if (cacheResult === undefined) {\n // cache miss\n // wait for other middleware to handle request\n log(\n 'No cache stored under block number %o, carrying request forward',\n requestedBlockNumber,\n );\n const result = await next();\n\n // add result to cache\n // it's safe to cast res.result as Block, due to runtime type checks\n // performed when strategy.set is called\n log('Populating cache with', result);\n await strategy.set(request, requestedBlockNumber, result as Block);\n return result;\n }\n log(\n 'Cache hit, reusing cache result stored under block number %o',\n requestedBlockNumber,\n );\n return cacheResult;\n };\n}\n"]}
|
package/dist/block-cache.d.cts
CHANGED
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
import type { PollingBlockTracker } from "@metamask/eth-block-tracker";
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
2
|
+
import type { JsonRpcMiddleware, MiddlewareContext } from "@metamask/json-rpc-engine/v2";
|
|
3
|
+
import type { Json, JsonRpcRequest } from "@metamask/utils";
|
|
4
4
|
type BlockCacheMiddlewareOptions = {
|
|
5
5
|
blockTracker?: PollingBlockTracker;
|
|
6
6
|
};
|
|
7
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Creates a middleware that caches block-related requests.
|
|
9
|
+
*
|
|
10
|
+
* @param options - The options for the middleware.
|
|
11
|
+
* @param options.blockTracker - The block tracker to use.
|
|
12
|
+
* @returns The block cache middleware.
|
|
13
|
+
*/
|
|
14
|
+
export declare function createBlockCacheMiddleware({ blockTracker, }?: BlockCacheMiddlewareOptions): JsonRpcMiddleware<JsonRpcRequest, Json, MiddlewareContext<{
|
|
15
|
+
skipCache?: boolean;
|
|
16
|
+
}>>;
|
|
8
17
|
export {};
|
|
9
18
|
//# sourceMappingURL=block-cache.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block-cache.d.cts","sourceRoot":"","sources":["../src/block-cache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAoC;
|
|
1
|
+
{"version":3,"file":"block-cache.d.cts","sourceRoot":"","sources":["../src/block-cache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAoC;AACvE,OAAO,KAAK,EACV,iBAAiB,EACjB,iBAAiB,EAClB,qCAAqC;AACtC,OAAO,KAAK,EAAE,IAAI,EAAE,cAAc,EAAE,wBAAwB;AAqB5D,KAAK,2BAA2B,GAAG;IACjC,YAAY,CAAC,EAAE,mBAAmB,CAAC;CACpC,CAAC;AA2GF;;;;;;GAMG;AACH,wBAAgB,0BAA0B,CAAC,EACzC,YAAY,GACb,GAAE,2BAAgC,GAAG,iBAAiB,CACrD,cAAc,EACd,IAAI,EACJ,iBAAiB,CAAC;IAAE,SAAS,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC,CAC3C,CAmFA"}
|
package/dist/block-cache.d.mts
CHANGED
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
import type { PollingBlockTracker } from "@metamask/eth-block-tracker";
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
2
|
+
import type { JsonRpcMiddleware, MiddlewareContext } from "@metamask/json-rpc-engine/v2";
|
|
3
|
+
import type { Json, JsonRpcRequest } from "@metamask/utils";
|
|
4
4
|
type BlockCacheMiddlewareOptions = {
|
|
5
5
|
blockTracker?: PollingBlockTracker;
|
|
6
6
|
};
|
|
7
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Creates a middleware that caches block-related requests.
|
|
9
|
+
*
|
|
10
|
+
* @param options - The options for the middleware.
|
|
11
|
+
* @param options.blockTracker - The block tracker to use.
|
|
12
|
+
* @returns The block cache middleware.
|
|
13
|
+
*/
|
|
14
|
+
export declare function createBlockCacheMiddleware({ blockTracker, }?: BlockCacheMiddlewareOptions): JsonRpcMiddleware<JsonRpcRequest, Json, MiddlewareContext<{
|
|
15
|
+
skipCache?: boolean;
|
|
16
|
+
}>>;
|
|
8
17
|
export {};
|
|
9
18
|
//# sourceMappingURL=block-cache.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block-cache.d.mts","sourceRoot":"","sources":["../src/block-cache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAoC;
|
|
1
|
+
{"version":3,"file":"block-cache.d.mts","sourceRoot":"","sources":["../src/block-cache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAoC;AACvE,OAAO,KAAK,EACV,iBAAiB,EACjB,iBAAiB,EAClB,qCAAqC;AACtC,OAAO,KAAK,EAAE,IAAI,EAAE,cAAc,EAAE,wBAAwB;AAqB5D,KAAK,2BAA2B,GAAG;IACjC,YAAY,CAAC,EAAE,mBAAmB,CAAC;CACpC,CAAC;AA2GF;;;;;;GAMG;AACH,wBAAgB,0BAA0B,CAAC,EACzC,YAAY,GACb,GAAE,2BAAgC,GAAG,iBAAiB,CACrD,cAAc,EACd,IAAI,EACJ,iBAAiB,CAAC;IAAE,SAAS,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC,CAC3C,CAmFA"}
|
package/dist/block-cache.mjs
CHANGED
|
@@ -1,4 +1,15 @@
|
|
|
1
|
-
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
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");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
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");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _BlockCacheStrategy_cache;
|
|
2
13
|
import { projectLogger, createModuleLogger } from "./logging-utils.mjs";
|
|
3
14
|
import { cacheIdentifierForRequest, blockTagForRequest, cacheTypeForMethod, canCache, CacheStrategy } from "./utils/cache.mjs";
|
|
4
15
|
const log = createModuleLogger(projectLogger, 'block-cache');
|
|
@@ -9,15 +20,16 @@ const emptyValues = [undefined, null, '\u003cnil\u003e'];
|
|
|
9
20
|
//
|
|
10
21
|
class BlockCacheStrategy {
|
|
11
22
|
constructor() {
|
|
12
|
-
this
|
|
23
|
+
_BlockCacheStrategy_cache.set(this, void 0);
|
|
24
|
+
__classPrivateFieldSet(this, _BlockCacheStrategy_cache, {}, "f");
|
|
13
25
|
}
|
|
14
26
|
getBlockCache(blockNumberHex) {
|
|
15
27
|
const blockNumber = Number.parseInt(blockNumberHex, 16);
|
|
16
|
-
let blockCache = this
|
|
28
|
+
let blockCache = __classPrivateFieldGet(this, _BlockCacheStrategy_cache, "f")[blockNumber];
|
|
17
29
|
// create new cache if necesary
|
|
18
30
|
if (!blockCache) {
|
|
19
31
|
const newCache = {};
|
|
20
|
-
this
|
|
32
|
+
__classPrivateFieldGet(this, _BlockCacheStrategy_cache, "f")[blockNumber] = newCache;
|
|
21
33
|
blockCache = newCache;
|
|
22
34
|
}
|
|
23
35
|
return blockCache;
|
|
@@ -77,18 +89,24 @@ class BlockCacheStrategy {
|
|
|
77
89
|
clearBefore(oldBlockHex) {
|
|
78
90
|
const oldBlockNumber = Number.parseInt(oldBlockHex, 16);
|
|
79
91
|
// clear old caches
|
|
80
|
-
Object.keys(this
|
|
92
|
+
Object.keys(__classPrivateFieldGet(this, _BlockCacheStrategy_cache, "f"))
|
|
81
93
|
.map(Number)
|
|
82
94
|
.filter((num) => num < oldBlockNumber)
|
|
83
|
-
.forEach((num) => delete this
|
|
95
|
+
.forEach((num) => delete __classPrivateFieldGet(this, _BlockCacheStrategy_cache, "f")[num]);
|
|
84
96
|
}
|
|
85
97
|
}
|
|
98
|
+
_BlockCacheStrategy_cache = new WeakMap();
|
|
99
|
+
/**
|
|
100
|
+
* Creates a middleware that caches block-related requests.
|
|
101
|
+
*
|
|
102
|
+
* @param options - The options for the middleware.
|
|
103
|
+
* @param options.blockTracker - The block tracker to use.
|
|
104
|
+
* @returns The block cache middleware.
|
|
105
|
+
*/
|
|
86
106
|
export function createBlockCacheMiddleware({ blockTracker, } = {}) {
|
|
87
|
-
// validate options
|
|
88
107
|
if (!blockTracker) {
|
|
89
108
|
throw new Error('createBlockCacheMiddleware - No PollingBlockTracker specified');
|
|
90
109
|
}
|
|
91
|
-
// create caching strategies
|
|
92
110
|
const blockCache = new BlockCacheStrategy();
|
|
93
111
|
const strategies = {
|
|
94
112
|
[CacheStrategy.Permanent]: blockCache,
|
|
@@ -96,28 +114,23 @@ export function createBlockCacheMiddleware({ blockTracker, } = {}) {
|
|
|
96
114
|
[CacheStrategy.Fork]: blockCache,
|
|
97
115
|
[CacheStrategy.Never]: undefined,
|
|
98
116
|
};
|
|
99
|
-
return
|
|
100
|
-
|
|
101
|
-
if (req.skipCache) {
|
|
117
|
+
return async ({ request, next, context }) => {
|
|
118
|
+
if (context.get('skipCache')) {
|
|
102
119
|
return next();
|
|
103
120
|
}
|
|
104
|
-
|
|
105
|
-
const type = cacheTypeForMethod(req.method);
|
|
121
|
+
const type = cacheTypeForMethod(request.method);
|
|
106
122
|
const strategy = strategies[type];
|
|
107
|
-
// If there's no strategy in place, pass it down the chain.
|
|
108
123
|
if (!strategy) {
|
|
109
124
|
return next();
|
|
110
125
|
}
|
|
111
|
-
|
|
112
|
-
if (!strategy.canCacheRequest(req)) {
|
|
126
|
+
if (!strategy.canCacheRequest(request)) {
|
|
113
127
|
return next();
|
|
114
128
|
}
|
|
115
|
-
|
|
116
|
-
const requestBlockTag = blockTagForRequest(req);
|
|
129
|
+
const requestBlockTag = blockTagForRequest(request);
|
|
117
130
|
const blockTag = requestBlockTag && typeof requestBlockTag === 'string'
|
|
118
131
|
? requestBlockTag
|
|
119
132
|
: 'latest';
|
|
120
|
-
log('blockTag = %o, req = %o', blockTag,
|
|
133
|
+
log('blockTag = %o, req = %o', blockTag, request);
|
|
121
134
|
// get exact block number
|
|
122
135
|
let requestedBlockNumber;
|
|
123
136
|
if (blockTag === 'earliest') {
|
|
@@ -125,7 +138,6 @@ export function createBlockCacheMiddleware({ blockTracker, } = {}) {
|
|
|
125
138
|
requestedBlockNumber = '0x00';
|
|
126
139
|
}
|
|
127
140
|
else if (blockTag === 'latest') {
|
|
128
|
-
// fetch latest block number
|
|
129
141
|
log('Fetching latest block number to determine cache key');
|
|
130
142
|
const latestBlockNumber = await blockTracker.getLatestBlock();
|
|
131
143
|
// clear all cache before latest block
|
|
@@ -134,28 +146,25 @@ export function createBlockCacheMiddleware({ blockTracker, } = {}) {
|
|
|
134
146
|
requestedBlockNumber = latestBlockNumber;
|
|
135
147
|
}
|
|
136
148
|
else {
|
|
137
|
-
//
|
|
149
|
+
// we have a hex number
|
|
138
150
|
requestedBlockNumber = blockTag;
|
|
139
151
|
}
|
|
140
152
|
// end on a hit, continue on a miss
|
|
141
|
-
const cacheResult = await strategy.get(
|
|
153
|
+
const cacheResult = await strategy.get(request, requestedBlockNumber);
|
|
142
154
|
if (cacheResult === undefined) {
|
|
143
155
|
// cache miss
|
|
144
156
|
// wait for other middleware to handle request
|
|
145
157
|
log('No cache stored under block number %o, carrying request forward', requestedBlockNumber);
|
|
146
|
-
await next();
|
|
158
|
+
const result = await next();
|
|
147
159
|
// add result to cache
|
|
148
160
|
// it's safe to cast res.result as Block, due to runtime type checks
|
|
149
161
|
// performed when strategy.set is called
|
|
150
|
-
log('Populating cache with',
|
|
151
|
-
await strategy.set(
|
|
162
|
+
log('Populating cache with', result);
|
|
163
|
+
await strategy.set(request, requestedBlockNumber, result);
|
|
164
|
+
return result;
|
|
152
165
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
res.result = cacheResult;
|
|
157
|
-
}
|
|
158
|
-
return undefined;
|
|
159
|
-
});
|
|
166
|
+
log('Cache hit, reusing cache result stored under block number %o', requestedBlockNumber);
|
|
167
|
+
return cacheResult;
|
|
168
|
+
};
|
|
160
169
|
}
|
|
161
170
|
//# sourceMappingURL=block-cache.mjs.map
|
package/dist/block-cache.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block-cache.mjs","sourceRoot":"","sources":["../src/block-cache.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,kCAAkC;AAGlE,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,4BAAwB;AASpE,OAAO,EACL,yBAAyB,EACzB,kBAAkB,EAClB,kBAAkB,EAClB,QAAQ,EACR,aAAa,EACd,0BAAsB;AAEvB,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,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import { createAsyncMiddleware } from '@metamask/json-rpc-engine';\nimport type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils';\n\nimport { projectLogger, createModuleLogger } from './logging-utils';\nimport type {\n Block,\n BlockCache,\n // eslint-disable-next-line @typescript-eslint/no-shadow\n Cache,\n JsonRpcCacheMiddleware,\n JsonRpcRequestToCache,\n} from './types';\nimport {\n cacheIdentifierForRequest,\n blockTagForRequest,\n cacheTypeForMethod,\n canCache,\n CacheStrategy,\n} from './utils/cache';\n\nconst log = createModuleLogger(projectLogger, 'block-cache');\n// `<nil>` comes from https://github.com/ethereum/go-ethereum/issues/16925\nconst emptyValues = [undefined, null, '\\u003cnil\\u003e'];\n\ntype BlockCacheMiddlewareOptions = {\n blockTracker?: PollingBlockTracker;\n};\n\n//\n// Cache Strategies\n//\n\nclass BlockCacheStrategy {\n private cache: Cache;\n\n constructor() {\n this.cache = {};\n }\n\n getBlockCache(blockNumberHex: string): BlockCache {\n const blockNumber: number = Number.parseInt(blockNumberHex, 16);\n let blockCache: BlockCache = this.cache[blockNumber];\n // create new cache if necesary\n if (!blockCache) {\n const newCache: BlockCache = {};\n this.cache[blockNumber] = newCache;\n blockCache = newCache;\n }\n return blockCache;\n }\n\n async get(\n request: JsonRpcRequest,\n requestedBlockNumber: string,\n ): Promise<Block | undefined> {\n // lookup block cache\n const blockCache: BlockCache = this.getBlockCache(requestedBlockNumber);\n // lookup payload in block cache\n const identifier: string | null = cacheIdentifierForRequest(request, true);\n return identifier ? blockCache[identifier] : undefined;\n }\n\n async set(\n request: JsonRpcRequest,\n requestedBlockNumber: string,\n result: Block,\n ): Promise<void> {\n // check if we can cached this result\n const canCacheResult: boolean = this.canCacheResult(request, result);\n if (!canCacheResult) {\n return;\n }\n\n // set the value in the cache\n const identifier: string | null = cacheIdentifierForRequest(request, true);\n if (!identifier) {\n return;\n }\n const blockCache: BlockCache = this.getBlockCache(requestedBlockNumber);\n blockCache[identifier] = result;\n }\n\n canCacheRequest(request: JsonRpcRequest): boolean {\n // check request method\n if (!canCache(request.method)) {\n return false;\n }\n // check blockTag\n const blockTag = blockTagForRequest(request);\n\n if (blockTag === 'pending') {\n return false;\n }\n // can be cached\n return true;\n }\n\n canCacheResult(request: JsonRpcRequest, result: Block): boolean {\n // never cache empty values (e.g. undefined)\n if (emptyValues.includes(result as any)) {\n return false;\n }\n\n // check if transactions have block reference before caching\n if (\n request.method &&\n ['eth_getTransactionByHash', 'eth_getTransactionReceipt'].includes(\n request.method,\n )\n ) {\n if (\n !result?.blockHash ||\n result.blockHash ===\n '0x0000000000000000000000000000000000000000000000000000000000000000'\n ) {\n return false;\n }\n }\n // otherwise true\n return true;\n }\n\n // removes all block caches with block number lower than `oldBlockHex`\n clearBefore(oldBlockHex: string): void {\n const oldBlockNumber: number = Number.parseInt(oldBlockHex, 16);\n // clear old caches\n Object.keys(this.cache)\n .map(Number)\n .filter((num) => num < oldBlockNumber)\n .forEach((num) => delete this.cache[num]);\n }\n}\n\nexport function createBlockCacheMiddleware({\n blockTracker,\n}: BlockCacheMiddlewareOptions = {}): JsonRpcCacheMiddleware<\n JsonRpcParams,\n Json\n> {\n // validate options\n if (!blockTracker) {\n throw new Error(\n 'createBlockCacheMiddleware - No PollingBlockTracker specified',\n );\n }\n\n // create caching strategies\n const blockCache: BlockCacheStrategy = new BlockCacheStrategy();\n const strategies: Record<CacheStrategy, BlockCacheStrategy | undefined> = {\n [CacheStrategy.Permanent]: blockCache,\n [CacheStrategy.Block]: blockCache,\n [CacheStrategy.Fork]: blockCache,\n [CacheStrategy.Never]: undefined,\n };\n\n return createAsyncMiddleware(\n async (req: JsonRpcRequestToCache<JsonRpcParams>, res, next) => {\n // allow cach to be skipped if so specified\n if (req.skipCache) {\n return next();\n }\n // check type and matching strategy\n const type = cacheTypeForMethod(req.method);\n const strategy = strategies[type];\n // If there's no strategy in place, pass it down the chain.\n if (!strategy) {\n return next();\n }\n\n // If the strategy can't cache this request, ignore it.\n if (!strategy.canCacheRequest(req)) {\n return next();\n }\n\n // get block reference (number or keyword)\n const requestBlockTag = blockTagForRequest(req);\n const blockTag =\n requestBlockTag && typeof requestBlockTag === 'string'\n ? requestBlockTag\n : 'latest';\n\n log('blockTag = %o, req = %o', blockTag, req);\n\n // get exact block number\n let requestedBlockNumber: string;\n if (blockTag === 'earliest') {\n // this just exists for symmetry with \"latest\"\n requestedBlockNumber = '0x00';\n } else if (blockTag === 'latest') {\n // fetch latest block number\n log('Fetching latest block number to determine cache key');\n const latestBlockNumber = await blockTracker.getLatestBlock();\n // clear all cache before latest block\n log(\n 'Clearing values stored under block numbers before %o',\n latestBlockNumber,\n );\n blockCache.clearBefore(latestBlockNumber);\n requestedBlockNumber = latestBlockNumber;\n } else {\n // We have a hex number\n requestedBlockNumber = blockTag;\n }\n // end on a hit, continue on a miss\n const cacheResult: Block | undefined = await strategy.get(\n req,\n requestedBlockNumber,\n );\n if (cacheResult === undefined) {\n // cache miss\n // wait for other middleware to handle request\n log(\n 'No cache stored under block number %o, carrying request forward',\n requestedBlockNumber,\n );\n await next();\n\n // add result to cache\n // it's safe to cast res.result as Block, due to runtime type checks\n // performed when strategy.set is called\n log('Populating cache with', res);\n await strategy.set(req, requestedBlockNumber, res.result as Block);\n } else {\n // fill in result from cache\n log(\n 'Cache hit, reusing cache result stored under block number %o',\n requestedBlockNumber,\n );\n res.result = cacheResult;\n }\n return undefined;\n },\n );\n}\n"]}
|
|
1
|
+
{"version":3,"file":"block-cache.mjs","sourceRoot":"","sources":["../src/block-cache.ts"],"names":[],"mappings":";;;;;;;;;;;;AAOA,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,4BAAwB;AAOpE,OAAO,EACL,yBAAyB,EACzB,kBAAkB,EAClB,kBAAkB,EAClB,QAAQ,EACR,aAAa,EACd,0BAAsB;AAEvB,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,aAAa,CAAC,CAAC;AAC7D,0EAA0E;AAC1E,MAAM,WAAW,GAAc,CAAC,SAAS,EAAE,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AAU,EAAE;YAC3B,8CAA8C;YAC9C,oBAAoB,GAAG,MAAM,CAAC;SAC/B;aAAM,IAAI,QAAQ,KAAK,QAAQ,EAAE;YAChC,GAAG,CAAC,qDAAqD,CAAC,CAAC;YAC3D,MAAM,iBAAiB,GAAG,MAAM,YAAY,CAAC,cAAc,EAAE,CAAC;YAE9D,sCAAsC;YACtC,GAAG,CACD,sDAAsD,EACtD,iBAAiB,CAClB,CAAC;YACF,UAAU,CAAC,WAAW,CAAC,iBAAiB,CAAC,CAAC;YAC1C,oBAAoB,GAAG,iBAAiB,CAAC;SAC1C;aAAM;YACL,uBAAuB;YACvB,oBAAoB,GAAG,QAAQ,CAAC;SACjC;QAED,mCAAmC;QACnC,MAAM,WAAW,GAAG,MAAM,QAAQ,CAAC,GAAG,CAAC,OAAO,EAAE,oBAAoB,CAAC,CAAC;QACtE,IAAI,WAAW,KAAK,SAAS,EAAE;YAC7B,aAAa;YACb,8CAA8C;YAC9C,GAAG,CACD,iEAAiE,EACjE,oBAAoB,CACrB,CAAC;YACF,MAAM,MAAM,GAAG,MAAM,IAAI,EAAE,CAAC;YAE5B,sBAAsB;YACtB,oEAAoE;YACpE,wCAAwC;YACxC,GAAG,CAAC,uBAAuB,EAAE,MAAM,CAAC,CAAC;YACrC,MAAM,QAAQ,CAAC,GAAG,CAAC,OAAO,EAAE,oBAAoB,EAAE,MAAe,CAAC,CAAC;YACnE,OAAO,MAAM,CAAC;SACf;QACD,GAAG,CACD,8DAA8D,EAC9D,oBAAoB,CACrB,CAAC;QACF,OAAO,WAAW,CAAC;IACrB,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import type { PollingBlockTracker } from '@metamask/eth-block-tracker';\nimport type {\n JsonRpcMiddleware,\n MiddlewareContext,\n} from '@metamask/json-rpc-engine/v2';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\n\nimport { projectLogger, createModuleLogger } from './logging-utils';\nimport type {\n Block,\n BlockCache,\n // eslint-disable-next-line @typescript-eslint/no-shadow\n Cache,\n} from './types';\nimport {\n cacheIdentifierForRequest,\n blockTagForRequest,\n cacheTypeForMethod,\n canCache,\n CacheStrategy,\n} from './utils/cache';\n\nconst log = createModuleLogger(projectLogger, 'block-cache');\n// `<nil>` comes from https://github.com/ethereum/go-ethereum/issues/16925\nconst emptyValues: unknown[] = [undefined, null, '\\u003cnil\\u003e'];\n\ntype BlockCacheMiddlewareOptions = {\n blockTracker?: PollingBlockTracker;\n};\n\n//\n// Cache Strategies\n//\n\nclass BlockCacheStrategy {\n #cache: Cache;\n\n constructor() {\n this.#cache = {};\n }\n\n getBlockCache(blockNumberHex: string): BlockCache {\n const blockNumber: number = Number.parseInt(blockNumberHex, 16);\n let blockCache: BlockCache = this.#cache[blockNumber];\n // create new cache if necesary\n if (!blockCache) {\n const newCache: BlockCache = {};\n this.#cache[blockNumber] = newCache;\n blockCache = newCache;\n }\n return blockCache;\n }\n\n async get(\n request: JsonRpcRequest,\n requestedBlockNumber: string,\n ): Promise<Block | undefined> {\n // lookup block cache\n const blockCache: BlockCache = this.getBlockCache(requestedBlockNumber);\n // lookup payload in block cache\n const identifier: string | null = cacheIdentifierForRequest(request, true);\n return identifier ? blockCache[identifier] : undefined;\n }\n\n async set(\n request: JsonRpcRequest,\n requestedBlockNumber: string,\n result: Block,\n ): Promise<void> {\n // check if we can cached this result\n const canCacheResult: boolean = this.canCacheResult(request, result);\n if (!canCacheResult) {\n return;\n }\n\n // set the value in the cache\n const identifier: string | null = cacheIdentifierForRequest(request, true);\n if (!identifier) {\n return;\n }\n const blockCache: BlockCache = this.getBlockCache(requestedBlockNumber);\n blockCache[identifier] = result;\n }\n\n canCacheRequest(request: JsonRpcRequest): boolean {\n // check request method\n if (!canCache(request.method)) {\n return false;\n }\n // check blockTag\n const blockTag = blockTagForRequest(request);\n\n if (blockTag === 'pending') {\n return false;\n }\n // can be cached\n return true;\n }\n\n canCacheResult(request: JsonRpcRequest, result: Block): boolean {\n // never cache empty values (e.g. undefined)\n if (emptyValues.includes(result)) {\n return false;\n }\n\n // check if transactions have block reference before caching\n if (\n request.method &&\n ['eth_getTransactionByHash', 'eth_getTransactionReceipt'].includes(\n request.method,\n )\n ) {\n if (\n !result?.blockHash ||\n result.blockHash ===\n '0x0000000000000000000000000000000000000000000000000000000000000000'\n ) {\n return false;\n }\n }\n // otherwise true\n return true;\n }\n\n // removes all block caches with block number lower than `oldBlockHex`\n clearBefore(oldBlockHex: string): void {\n const oldBlockNumber: number = Number.parseInt(oldBlockHex, 16);\n // clear old caches\n Object.keys(this.#cache)\n .map(Number)\n .filter((num) => num < oldBlockNumber)\n .forEach((num) => delete this.#cache[num]);\n }\n}\n\n/**\n * Creates a middleware that caches block-related requests.\n *\n * @param options - The options for the middleware.\n * @param options.blockTracker - The block tracker to use.\n * @returns The block cache middleware.\n */\nexport function createBlockCacheMiddleware({\n blockTracker,\n}: BlockCacheMiddlewareOptions = {}): JsonRpcMiddleware<\n JsonRpcRequest,\n Json,\n MiddlewareContext<{ skipCache?: boolean }>\n> {\n if (!blockTracker) {\n throw new Error(\n 'createBlockCacheMiddleware - No PollingBlockTracker specified',\n );\n }\n\n const blockCache: BlockCacheStrategy = new BlockCacheStrategy();\n const strategies: Record<CacheStrategy, BlockCacheStrategy | undefined> = {\n [CacheStrategy.Permanent]: blockCache,\n [CacheStrategy.Block]: blockCache,\n [CacheStrategy.Fork]: blockCache,\n [CacheStrategy.Never]: undefined,\n };\n\n return async ({ request, next, context }) => {\n if (context.get('skipCache')) {\n return next();\n }\n\n const type = cacheTypeForMethod(request.method);\n const strategy = strategies[type];\n if (!strategy) {\n return next();\n }\n\n if (!strategy.canCacheRequest(request)) {\n return next();\n }\n\n const requestBlockTag = blockTagForRequest(request);\n const blockTag =\n requestBlockTag && typeof requestBlockTag === 'string'\n ? requestBlockTag\n : 'latest';\n\n log('blockTag = %o, req = %o', blockTag, request);\n\n // get exact block number\n let requestedBlockNumber: string;\n if (blockTag === 'earliest') {\n // this just exists for symmetry with \"latest\"\n requestedBlockNumber = '0x00';\n } else if (blockTag === 'latest') {\n log('Fetching latest block number to determine cache key');\n const latestBlockNumber = await blockTracker.getLatestBlock();\n\n // clear all cache before latest block\n log(\n 'Clearing values stored under block numbers before %o',\n latestBlockNumber,\n );\n blockCache.clearBefore(latestBlockNumber);\n requestedBlockNumber = latestBlockNumber;\n } else {\n // we have a hex number\n requestedBlockNumber = blockTag;\n }\n\n // end on a hit, continue on a miss\n const cacheResult = await strategy.get(request, requestedBlockNumber);\n if (cacheResult === undefined) {\n // cache miss\n // wait for other middleware to handle request\n log(\n 'No cache stored under block number %o, carrying request forward',\n requestedBlockNumber,\n );\n const result = await next();\n\n // add result to cache\n // it's safe to cast res.result as Block, due to runtime type checks\n // performed when strategy.set is called\n log('Populating cache with', result);\n await strategy.set(request, requestedBlockNumber, result as Block);\n return result;\n }\n log(\n 'Cache hit, reusing cache result stored under block number %o',\n requestedBlockNumber,\n );\n return cacheResult;\n };\n}\n"]}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createBlockRefRewriteMiddleware = void 0;
|
|
4
|
-
const json_rpc_engine_1 = require("@metamask/json-rpc-engine");
|
|
5
4
|
const cache_1 = require("./utils/cache.cjs");
|
|
6
5
|
/**
|
|
7
6
|
* Creates a middleware that rewrites "latest" block references to the known
|
|
@@ -15,13 +14,13 @@ function createBlockRefRewriteMiddleware({ blockTracker, } = {}) {
|
|
|
15
14
|
if (!blockTracker) {
|
|
16
15
|
throw Error('BlockRefRewriteMiddleware - mandatory "blockTracker" option is missing.');
|
|
17
16
|
}
|
|
18
|
-
return
|
|
19
|
-
const blockRefIndex = (0, cache_1.blockTagParamIndex)(
|
|
17
|
+
return async ({ request, next }) => {
|
|
18
|
+
const blockRefIndex = (0, cache_1.blockTagParamIndex)(request.method);
|
|
20
19
|
if (blockRefIndex === undefined) {
|
|
21
20
|
return next();
|
|
22
21
|
}
|
|
23
|
-
const blockRef = Array.isArray(
|
|
24
|
-
?
|
|
22
|
+
const blockRef = Array.isArray(request.params) && request.params[blockRefIndex]
|
|
23
|
+
? request.params[blockRefIndex]
|
|
25
24
|
: // omitted blockRef implies "latest"
|
|
26
25
|
'latest';
|
|
27
26
|
if (blockRef !== 'latest') {
|
|
@@ -29,11 +28,16 @@ function createBlockRefRewriteMiddleware({ blockTracker, } = {}) {
|
|
|
29
28
|
}
|
|
30
29
|
// rewrite blockRef to block-tracker's block number
|
|
31
30
|
const latestBlockNumber = await blockTracker.getLatestBlock();
|
|
32
|
-
if (Array.isArray(
|
|
33
|
-
|
|
31
|
+
if (Array.isArray(request.params)) {
|
|
32
|
+
const params = request.params.slice();
|
|
33
|
+
params[blockRefIndex] = latestBlockNumber;
|
|
34
|
+
return next({
|
|
35
|
+
...request,
|
|
36
|
+
params,
|
|
37
|
+
});
|
|
34
38
|
}
|
|
35
39
|
return next();
|
|
36
|
-
}
|
|
40
|
+
};
|
|
37
41
|
}
|
|
38
42
|
exports.createBlockRefRewriteMiddleware = createBlockRefRewriteMiddleware;
|
|
39
43
|
//# sourceMappingURL=block-ref-rewrite.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block-ref-rewrite.cjs","sourceRoot":"","sources":["../src/block-ref-rewrite.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"block-ref-rewrite.cjs","sourceRoot":"","sources":["../src/block-ref-rewrite.ts"],"names":[],"mappings":";;;AAIA,6CAAmD;AAMnD;;;;;;;GAOG;AACH,SAAgB,+BAA+B,CAAC,EAC9C,YAAY,MACwB,EAAE;IAItC,IAAI,CAAC,YAAY,EAAE;QACjB,MAAM,KAAK,CACT,yEAAyE,CAC1E,CAAC;KACH;IAED,OAAO,KAAK,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE;QACjC,MAAM,aAAa,GAAuB,IAAA,0BAAkB,EAC1D,OAAO,CAAC,MAAM,CACf,CAAC;QACF,IAAI,aAAa,KAAK,SAAS,EAAE;YAC/B,OAAO,IAAI,EAAE,CAAC;SACf;QAED,MAAM,QAAQ,GACZ,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,OAAO,CAAC,MAAM,CAAC,aAAa,CAAC;YAC5D,CAAC,CAAE,OAAO,CAAC,MAAM,CAAC,aAAa,CAAY;YAC3C,CAAC,CAAC,oCAAoC;gBACpC,QAAQ,CAAC;QAEf,IAAI,QAAQ,KAAK,QAAQ,EAAE;YACzB,OAAO,IAAI,EAAE,CAAC;SACf;QAED,mDAAmD;QACnD,MAAM,iBAAiB,GAAG,MAAM,YAAY,CAAC,cAAc,EAAE,CAAC;QAC9D,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;YACjC,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;YACtC,MAAM,CAAC,aAAa,CAAC,GAAG,iBAAiB,CAAC;YAC1C,OAAO,IAAI,CAAC;gBACV,GAAG,OAAO;gBACV,MAAM;aACP,CAAC,CAAC;SACJ;QACD,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC;AA1CD,0EA0CC","sourcesContent":["import type { PollingBlockTracker } from '@metamask/eth-block-tracker';\nimport type { JsonRpcMiddleware } from '@metamask/json-rpc-engine/v2';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\n\nimport { blockTagParamIndex } from './utils/cache';\n\ntype BlockRefRewriteMiddlewareOptions = {\n blockTracker?: PollingBlockTracker;\n};\n\n/**\n * Creates a middleware that rewrites \"latest\" block references to the known\n * latest block number from a block tracker.\n *\n * @param options - The options for the middleware.\n * @param options.blockTracker - The block tracker to use.\n * @returns The middleware.\n */\nexport function createBlockRefRewriteMiddleware({\n blockTracker,\n}: BlockRefRewriteMiddlewareOptions = {}): JsonRpcMiddleware<\n JsonRpcRequest,\n Json\n> {\n if (!blockTracker) {\n throw Error(\n 'BlockRefRewriteMiddleware - mandatory \"blockTracker\" option is missing.',\n );\n }\n\n return async ({ request, next }) => {\n const blockRefIndex: number | undefined = blockTagParamIndex(\n request.method,\n );\n if (blockRefIndex === undefined) {\n return next();\n }\n\n const blockRef: string | undefined =\n Array.isArray(request.params) && request.params[blockRefIndex]\n ? (request.params[blockRefIndex] as string)\n : // omitted blockRef implies \"latest\"\n 'latest';\n\n if (blockRef !== 'latest') {\n return next();\n }\n\n // rewrite blockRef to block-tracker's block number\n const latestBlockNumber = await blockTracker.getLatestBlock();\n if (Array.isArray(request.params)) {\n const params = request.params.slice();\n params[blockRefIndex] = latestBlockNumber;\n return next({\n ...request,\n params,\n });\n }\n return next();\n };\n}\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { PollingBlockTracker } from "@metamask/eth-block-tracker";
|
|
2
|
-
import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine";
|
|
3
|
-
import type { Json,
|
|
2
|
+
import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine/v2";
|
|
3
|
+
import type { Json, JsonRpcRequest } from "@metamask/utils";
|
|
4
4
|
type BlockRefRewriteMiddlewareOptions = {
|
|
5
5
|
blockTracker?: PollingBlockTracker;
|
|
6
6
|
};
|
|
@@ -12,6 +12,6 @@ type BlockRefRewriteMiddlewareOptions = {
|
|
|
12
12
|
* @param options.blockTracker - The block tracker to use.
|
|
13
13
|
* @returns The middleware.
|
|
14
14
|
*/
|
|
15
|
-
export declare function createBlockRefRewriteMiddleware({ blockTracker, }?: BlockRefRewriteMiddlewareOptions): JsonRpcMiddleware<
|
|
15
|
+
export declare function createBlockRefRewriteMiddleware({ blockTracker, }?: BlockRefRewriteMiddlewareOptions): JsonRpcMiddleware<JsonRpcRequest, Json>;
|
|
16
16
|
export {};
|
|
17
17
|
//# sourceMappingURL=block-ref-rewrite.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block-ref-rewrite.d.cts","sourceRoot":"","sources":["../src/block-ref-rewrite.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAoC;AACvE,OAAO,KAAK,EAAE,iBAAiB,EAAE,
|
|
1
|
+
{"version":3,"file":"block-ref-rewrite.d.cts","sourceRoot":"","sources":["../src/block-ref-rewrite.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAoC;AACvE,OAAO,KAAK,EAAE,iBAAiB,EAAE,qCAAqC;AACtE,OAAO,KAAK,EAAE,IAAI,EAAE,cAAc,EAAE,wBAAwB;AAI5D,KAAK,gCAAgC,GAAG;IACtC,YAAY,CAAC,EAAE,mBAAmB,CAAC;CACpC,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,+BAA+B,CAAC,EAC9C,YAAY,GACb,GAAE,gCAAqC,GAAG,iBAAiB,CAC1D,cAAc,EACd,IAAI,CACL,CAqCA"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { PollingBlockTracker } from "@metamask/eth-block-tracker";
|
|
2
|
-
import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine";
|
|
3
|
-
import type { Json,
|
|
2
|
+
import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine/v2";
|
|
3
|
+
import type { Json, JsonRpcRequest } from "@metamask/utils";
|
|
4
4
|
type BlockRefRewriteMiddlewareOptions = {
|
|
5
5
|
blockTracker?: PollingBlockTracker;
|
|
6
6
|
};
|
|
@@ -12,6 +12,6 @@ type BlockRefRewriteMiddlewareOptions = {
|
|
|
12
12
|
* @param options.blockTracker - The block tracker to use.
|
|
13
13
|
* @returns The middleware.
|
|
14
14
|
*/
|
|
15
|
-
export declare function createBlockRefRewriteMiddleware({ blockTracker, }?: BlockRefRewriteMiddlewareOptions): JsonRpcMiddleware<
|
|
15
|
+
export declare function createBlockRefRewriteMiddleware({ blockTracker, }?: BlockRefRewriteMiddlewareOptions): JsonRpcMiddleware<JsonRpcRequest, Json>;
|
|
16
16
|
export {};
|
|
17
17
|
//# sourceMappingURL=block-ref-rewrite.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block-ref-rewrite.d.mts","sourceRoot":"","sources":["../src/block-ref-rewrite.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAoC;AACvE,OAAO,KAAK,EAAE,iBAAiB,EAAE,
|
|
1
|
+
{"version":3,"file":"block-ref-rewrite.d.mts","sourceRoot":"","sources":["../src/block-ref-rewrite.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAoC;AACvE,OAAO,KAAK,EAAE,iBAAiB,EAAE,qCAAqC;AACtE,OAAO,KAAK,EAAE,IAAI,EAAE,cAAc,EAAE,wBAAwB;AAI5D,KAAK,gCAAgC,GAAG;IACtC,YAAY,CAAC,EAAE,mBAAmB,CAAC;CACpC,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,+BAA+B,CAAC,EAC9C,YAAY,GACb,GAAE,gCAAqC,GAAG,iBAAiB,CAC1D,cAAc,EACd,IAAI,CACL,CAqCA"}
|