@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
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { createAsyncMiddleware } from "@metamask/json-rpc-engine";
|
|
2
1
|
import { blockTagParamIndex } from "./utils/cache.mjs";
|
|
3
2
|
/**
|
|
4
3
|
* Creates a middleware that rewrites "latest" block references to the known
|
|
@@ -12,13 +11,13 @@ export function createBlockRefRewriteMiddleware({ blockTracker, } = {}) {
|
|
|
12
11
|
if (!blockTracker) {
|
|
13
12
|
throw Error('BlockRefRewriteMiddleware - mandatory "blockTracker" option is missing.');
|
|
14
13
|
}
|
|
15
|
-
return
|
|
16
|
-
const blockRefIndex = blockTagParamIndex(
|
|
14
|
+
return async ({ request, next }) => {
|
|
15
|
+
const blockRefIndex = blockTagParamIndex(request.method);
|
|
17
16
|
if (blockRefIndex === undefined) {
|
|
18
17
|
return next();
|
|
19
18
|
}
|
|
20
|
-
const blockRef = Array.isArray(
|
|
21
|
-
?
|
|
19
|
+
const blockRef = Array.isArray(request.params) && request.params[blockRefIndex]
|
|
20
|
+
? request.params[blockRefIndex]
|
|
22
21
|
: // omitted blockRef implies "latest"
|
|
23
22
|
'latest';
|
|
24
23
|
if (blockRef !== 'latest') {
|
|
@@ -26,10 +25,15 @@ export function createBlockRefRewriteMiddleware({ blockTracker, } = {}) {
|
|
|
26
25
|
}
|
|
27
26
|
// rewrite blockRef to block-tracker's block number
|
|
28
27
|
const latestBlockNumber = await blockTracker.getLatestBlock();
|
|
29
|
-
if (Array.isArray(
|
|
30
|
-
|
|
28
|
+
if (Array.isArray(request.params)) {
|
|
29
|
+
const params = request.params.slice();
|
|
30
|
+
params[blockRefIndex] = latestBlockNumber;
|
|
31
|
+
return next({
|
|
32
|
+
...request,
|
|
33
|
+
params,
|
|
34
|
+
});
|
|
31
35
|
}
|
|
32
36
|
return next();
|
|
33
|
-
}
|
|
37
|
+
};
|
|
34
38
|
}
|
|
35
39
|
//# sourceMappingURL=block-ref-rewrite.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block-ref-rewrite.mjs","sourceRoot":"","sources":["../src/block-ref-rewrite.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"block-ref-rewrite.mjs","sourceRoot":"","sources":["../src/block-ref-rewrite.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,kBAAkB,EAAE,0BAAsB;AAMnD;;;;;;;GAOG;AACH,MAAM,UAAU,+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,kBAAkB,CAC1D,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","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"]}
|
package/dist/block-ref.cjs
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createBlockRefMiddleware = void 0;
|
|
4
|
-
const
|
|
5
|
-
const full_1 = require("klona/full");
|
|
4
|
+
const klona_1 = require("klona");
|
|
6
5
|
const logging_utils_1 = require("./logging-utils.cjs");
|
|
7
6
|
const cache_1 = require("./utils/cache.cjs");
|
|
8
7
|
const log = (0, logging_utils_1.createModuleLogger)(logging_utils_1.projectLogger, 'block-ref');
|
|
8
|
+
/**
|
|
9
|
+
* Creates a middleware that rewrites "latest" block references to the known
|
|
10
|
+
* latest block number from a block tracker.
|
|
11
|
+
*
|
|
12
|
+
* @param options - The options for the middleware.
|
|
13
|
+
* @param options.provider - The provider to use.
|
|
14
|
+
* @param options.blockTracker - The block tracker to use.
|
|
15
|
+
* @returns The middleware.
|
|
16
|
+
*/
|
|
9
17
|
function createBlockRefMiddleware({ provider, blockTracker, } = {}) {
|
|
10
18
|
if (!provider) {
|
|
11
19
|
throw Error('BlockRefMiddleware - mandatory "provider" option is missing.');
|
|
@@ -13,14 +21,14 @@ function createBlockRefMiddleware({ provider, blockTracker, } = {}) {
|
|
|
13
21
|
if (!blockTracker) {
|
|
14
22
|
throw Error('BlockRefMiddleware - mandatory "blockTracker" option is missing.');
|
|
15
23
|
}
|
|
16
|
-
return
|
|
17
|
-
const blockRefIndex = (0, cache_1.blockTagParamIndex)(
|
|
24
|
+
return async ({ request, next }) => {
|
|
25
|
+
const blockRefIndex = (0, cache_1.blockTagParamIndex)(request.method);
|
|
18
26
|
// skip if method does not include blockRef
|
|
19
27
|
if (blockRefIndex === undefined) {
|
|
20
28
|
return next();
|
|
21
29
|
}
|
|
22
|
-
const blockRef = Array.isArray(
|
|
23
|
-
? (
|
|
30
|
+
const blockRef = Array.isArray(request.params)
|
|
31
|
+
? (request.params[blockRefIndex] ?? 'latest')
|
|
24
32
|
: 'latest';
|
|
25
33
|
// skip if not "latest"
|
|
26
34
|
if (blockRef !== 'latest') {
|
|
@@ -31,16 +39,15 @@ function createBlockRefMiddleware({ provider, blockTracker, } = {}) {
|
|
|
31
39
|
const latestBlockNumber = await blockTracker.getLatestBlock();
|
|
32
40
|
log(`blockRef is "latest", setting param ${blockRefIndex} to latest block ${latestBlockNumber}`);
|
|
33
41
|
// create child request with specific block-ref
|
|
34
|
-
const childRequest = (0,
|
|
42
|
+
const childRequest = (0, klona_1.klona)(request);
|
|
35
43
|
if (Array.isArray(childRequest.params)) {
|
|
36
44
|
childRequest.params[blockRefIndex] = latestBlockNumber;
|
|
37
45
|
}
|
|
38
46
|
// perform child request
|
|
39
47
|
log('Performing another request %o', childRequest);
|
|
40
48
|
// copy child result onto original response
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
});
|
|
49
|
+
return await provider.request(childRequest);
|
|
50
|
+
};
|
|
44
51
|
}
|
|
45
52
|
exports.createBlockRefMiddleware = createBlockRefMiddleware;
|
|
46
53
|
//# sourceMappingURL=block-ref.cjs.map
|
package/dist/block-ref.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block-ref.cjs","sourceRoot":"","sources":["../src/block-ref.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"block-ref.cjs","sourceRoot":"","sources":["../src/block-ref.ts"],"names":[],"mappings":";;;AAIA,iCAA8B;AAE9B,uDAAoE;AAEpE,6CAAmD;AAOnD,MAAM,GAAG,GAAG,IAAA,kCAAkB,EAAC,6BAAa,EAAE,WAAW,CAAC,CAAC;AAE3D;;;;;;;;GAQG;AACH,SAAgB,wBAAwB,CAAC,EACvC,QAAQ,EACR,YAAY,MACiB,EAAE;IAC/B,IAAI,CAAC,QAAQ,EAAE;QACb,MAAM,KAAK,CAAC,8DAA8D,CAAC,CAAC;KAC7E;IAED,IAAI,CAAC,YAAY,EAAE;QACjB,MAAM,KAAK,CACT,kEAAkE,CACnE,CAAC;KACH;IAED,OAAO,KAAK,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE;QACjC,MAAM,aAAa,GAAG,IAAA,0BAAkB,EAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QAEzD,2CAA2C;QAC3C,IAAI,aAAa,KAAK,SAAS,EAAE;YAC/B,OAAO,IAAI,EAAE,CAAC;SACf;QAED,MAAM,QAAQ,GAAG,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC;YAC5C,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,QAAQ,CAAC;YAC7C,CAAC,CAAC,QAAQ,CAAC;QAEb,uBAAuB;QACvB,IAAI,QAAQ,KAAK,QAAQ,EAAE;YACzB,GAAG,CAAC,oDAAoD,CAAC,CAAC;YAC1D,OAAO,IAAI,EAAE,CAAC;SACf;QAED,sBAAsB;QACtB,MAAM,iBAAiB,GAAG,MAAM,YAAY,CAAC,cAAc,EAAE,CAAC;QAC9D,GAAG,CACD,uCAAuC,aAAa,oBAAoB,iBAAiB,EAAE,CAC5F,CAAC;QAEF,+CAA+C;QAC/C,MAAM,YAAY,GAAG,IAAA,aAAK,EAAC,OAAO,CAAC,CAAC;QAEpC,IAAI,KAAK,CAAC,OAAO,CAAC,YAAY,CAAC,MAAM,CAAC,EAAE;YACtC,YAAY,CAAC,MAAM,CAAC,aAAa,CAAC,GAAG,iBAAiB,CAAC;SACxD;QAED,wBAAwB;QACxB,GAAG,CAAC,+BAA+B,EAAE,YAAY,CAAC,CAAC;QACnD,2CAA2C;QAC3C,OAAO,MAAM,QAAQ,CAAC,OAAO,CAAuB,YAAY,CAAC,CAAC;IACpE,CAAC,CAAC;AACJ,CAAC;AAlDD,4DAkDC","sourcesContent":["import type { PollingBlockTracker } from '@metamask/eth-block-tracker';\nimport type { InternalProvider } from '@metamask/eth-json-rpc-provider';\nimport type { JsonRpcMiddleware } from '@metamask/json-rpc-engine/v2';\nimport type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils';\nimport { klona } from 'klona';\n\nimport { projectLogger, createModuleLogger } from './logging-utils';\nimport type { Block } from './types';\nimport { blockTagParamIndex } from './utils/cache';\n\ntype BlockRefMiddlewareOptions = {\n blockTracker?: PollingBlockTracker;\n provider?: InternalProvider;\n};\n\nconst log = createModuleLogger(projectLogger, 'block-ref');\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.provider - The provider to use.\n * @param options.blockTracker - The block tracker to use.\n * @returns The middleware.\n */\nexport function createBlockRefMiddleware({\n provider,\n blockTracker,\n}: BlockRefMiddlewareOptions = {}): JsonRpcMiddleware<JsonRpcRequest, Json> {\n if (!provider) {\n throw Error('BlockRefMiddleware - mandatory \"provider\" option is missing.');\n }\n\n if (!blockTracker) {\n throw Error(\n 'BlockRefMiddleware - mandatory \"blockTracker\" option is missing.',\n );\n }\n\n return async ({ request, next }) => {\n const blockRefIndex = blockTagParamIndex(request.method);\n\n // skip if method does not include blockRef\n if (blockRefIndex === undefined) {\n return next();\n }\n\n const blockRef = Array.isArray(request.params)\n ? (request.params[blockRefIndex] ?? 'latest')\n : 'latest';\n\n // skip if not \"latest\"\n if (blockRef !== 'latest') {\n log('blockRef is not \"latest\", carrying request forward');\n return next();\n }\n\n // lookup latest block\n const latestBlockNumber = await blockTracker.getLatestBlock();\n log(\n `blockRef is \"latest\", setting param ${blockRefIndex} to latest block ${latestBlockNumber}`,\n );\n\n // create child request with specific block-ref\n const childRequest = klona(request);\n\n if (Array.isArray(childRequest.params)) {\n childRequest.params[blockRefIndex] = latestBlockNumber;\n }\n\n // perform child request\n log('Performing another request %o', childRequest);\n // copy child result onto original response\n return await provider.request<JsonRpcParams, Block>(childRequest);\n };\n}\n"]}
|
package/dist/block-ref.d.cts
CHANGED
|
@@ -1,11 +1,20 @@
|
|
|
1
1
|
import type { PollingBlockTracker } from "@metamask/eth-block-tracker";
|
|
2
2
|
import type { InternalProvider } from "@metamask/eth-json-rpc-provider";
|
|
3
|
-
import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine";
|
|
4
|
-
import type { Json,
|
|
3
|
+
import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine/v2";
|
|
4
|
+
import type { Json, JsonRpcRequest } from "@metamask/utils";
|
|
5
5
|
type BlockRefMiddlewareOptions = {
|
|
6
6
|
blockTracker?: PollingBlockTracker;
|
|
7
7
|
provider?: InternalProvider;
|
|
8
8
|
};
|
|
9
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Creates a middleware that rewrites "latest" block references to the known
|
|
11
|
+
* latest block number from a block tracker.
|
|
12
|
+
*
|
|
13
|
+
* @param options - The options for the middleware.
|
|
14
|
+
* @param options.provider - The provider to use.
|
|
15
|
+
* @param options.blockTracker - The block tracker to use.
|
|
16
|
+
* @returns The middleware.
|
|
17
|
+
*/
|
|
18
|
+
export declare function createBlockRefMiddleware({ provider, blockTracker, }?: BlockRefMiddlewareOptions): JsonRpcMiddleware<JsonRpcRequest, Json>;
|
|
10
19
|
export {};
|
|
11
20
|
//# sourceMappingURL=block-ref.d.cts.map
|
package/dist/block-ref.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block-ref.d.cts","sourceRoot":"","sources":["../src/block-ref.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAoC;AACvE,OAAO,KAAK,EAAE,gBAAgB,EAAE,wCAAwC;AACxE,OAAO,KAAK,EAAE,iBAAiB,EAAE,
|
|
1
|
+
{"version":3,"file":"block-ref.d.cts","sourceRoot":"","sources":["../src/block-ref.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAoC;AACvE,OAAO,KAAK,EAAE,gBAAgB,EAAE,wCAAwC;AACxE,OAAO,KAAK,EAAE,iBAAiB,EAAE,qCAAqC;AACtE,OAAO,KAAK,EAAE,IAAI,EAAiB,cAAc,EAAE,wBAAwB;AAO3E,KAAK,yBAAyB,GAAG;IAC/B,YAAY,CAAC,EAAE,mBAAmB,CAAC;IACnC,QAAQ,CAAC,EAAE,gBAAgB,CAAC;CAC7B,CAAC;AAIF;;;;;;;;GAQG;AACH,wBAAgB,wBAAwB,CAAC,EACvC,QAAQ,EACR,YAAY,GACb,GAAE,yBAA8B,GAAG,iBAAiB,CAAC,cAAc,EAAE,IAAI,CAAC,CA+C1E"}
|
package/dist/block-ref.d.mts
CHANGED
|
@@ -1,11 +1,20 @@
|
|
|
1
1
|
import type { PollingBlockTracker } from "@metamask/eth-block-tracker";
|
|
2
2
|
import type { InternalProvider } from "@metamask/eth-json-rpc-provider";
|
|
3
|
-
import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine";
|
|
4
|
-
import type { Json,
|
|
3
|
+
import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine/v2";
|
|
4
|
+
import type { Json, JsonRpcRequest } from "@metamask/utils";
|
|
5
5
|
type BlockRefMiddlewareOptions = {
|
|
6
6
|
blockTracker?: PollingBlockTracker;
|
|
7
7
|
provider?: InternalProvider;
|
|
8
8
|
};
|
|
9
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Creates a middleware that rewrites "latest" block references to the known
|
|
11
|
+
* latest block number from a block tracker.
|
|
12
|
+
*
|
|
13
|
+
* @param options - The options for the middleware.
|
|
14
|
+
* @param options.provider - The provider to use.
|
|
15
|
+
* @param options.blockTracker - The block tracker to use.
|
|
16
|
+
* @returns The middleware.
|
|
17
|
+
*/
|
|
18
|
+
export declare function createBlockRefMiddleware({ provider, blockTracker, }?: BlockRefMiddlewareOptions): JsonRpcMiddleware<JsonRpcRequest, Json>;
|
|
10
19
|
export {};
|
|
11
20
|
//# sourceMappingURL=block-ref.d.mts.map
|
package/dist/block-ref.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block-ref.d.mts","sourceRoot":"","sources":["../src/block-ref.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAoC;AACvE,OAAO,KAAK,EAAE,gBAAgB,EAAE,wCAAwC;AACxE,OAAO,KAAK,EAAE,iBAAiB,EAAE,
|
|
1
|
+
{"version":3,"file":"block-ref.d.mts","sourceRoot":"","sources":["../src/block-ref.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAoC;AACvE,OAAO,KAAK,EAAE,gBAAgB,EAAE,wCAAwC;AACxE,OAAO,KAAK,EAAE,iBAAiB,EAAE,qCAAqC;AACtE,OAAO,KAAK,EAAE,IAAI,EAAiB,cAAc,EAAE,wBAAwB;AAO3E,KAAK,yBAAyB,GAAG;IAC/B,YAAY,CAAC,EAAE,mBAAmB,CAAC;IACnC,QAAQ,CAAC,EAAE,gBAAgB,CAAC;CAC7B,CAAC;AAIF;;;;;;;;GAQG;AACH,wBAAgB,wBAAwB,CAAC,EACvC,QAAQ,EACR,YAAY,GACb,GAAE,yBAA8B,GAAG,iBAAiB,CAAC,cAAc,EAAE,IAAI,CAAC,CA+C1E"}
|
package/dist/block-ref.mjs
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { klona } from "klona/full";
|
|
1
|
+
import { klona } from "klona";
|
|
3
2
|
import { projectLogger, createModuleLogger } from "./logging-utils.mjs";
|
|
4
3
|
import { blockTagParamIndex } from "./utils/cache.mjs";
|
|
5
4
|
const log = createModuleLogger(projectLogger, 'block-ref');
|
|
5
|
+
/**
|
|
6
|
+
* Creates a middleware that rewrites "latest" block references to the known
|
|
7
|
+
* latest block number from a block tracker.
|
|
8
|
+
*
|
|
9
|
+
* @param options - The options for the middleware.
|
|
10
|
+
* @param options.provider - The provider to use.
|
|
11
|
+
* @param options.blockTracker - The block tracker to use.
|
|
12
|
+
* @returns The middleware.
|
|
13
|
+
*/
|
|
6
14
|
export function createBlockRefMiddleware({ provider, blockTracker, } = {}) {
|
|
7
15
|
if (!provider) {
|
|
8
16
|
throw Error('BlockRefMiddleware - mandatory "provider" option is missing.');
|
|
@@ -10,14 +18,14 @@ export function createBlockRefMiddleware({ provider, blockTracker, } = {}) {
|
|
|
10
18
|
if (!blockTracker) {
|
|
11
19
|
throw Error('BlockRefMiddleware - mandatory "blockTracker" option is missing.');
|
|
12
20
|
}
|
|
13
|
-
return
|
|
14
|
-
const blockRefIndex = blockTagParamIndex(
|
|
21
|
+
return async ({ request, next }) => {
|
|
22
|
+
const blockRefIndex = blockTagParamIndex(request.method);
|
|
15
23
|
// skip if method does not include blockRef
|
|
16
24
|
if (blockRefIndex === undefined) {
|
|
17
25
|
return next();
|
|
18
26
|
}
|
|
19
|
-
const blockRef = Array.isArray(
|
|
20
|
-
? (
|
|
27
|
+
const blockRef = Array.isArray(request.params)
|
|
28
|
+
? (request.params[blockRefIndex] ?? 'latest')
|
|
21
29
|
: 'latest';
|
|
22
30
|
// skip if not "latest"
|
|
23
31
|
if (blockRef !== 'latest') {
|
|
@@ -28,15 +36,14 @@ export function createBlockRefMiddleware({ provider, blockTracker, } = {}) {
|
|
|
28
36
|
const latestBlockNumber = await blockTracker.getLatestBlock();
|
|
29
37
|
log(`blockRef is "latest", setting param ${blockRefIndex} to latest block ${latestBlockNumber}`);
|
|
30
38
|
// create child request with specific block-ref
|
|
31
|
-
const childRequest = klona(
|
|
39
|
+
const childRequest = klona(request);
|
|
32
40
|
if (Array.isArray(childRequest.params)) {
|
|
33
41
|
childRequest.params[blockRefIndex] = latestBlockNumber;
|
|
34
42
|
}
|
|
35
43
|
// perform child request
|
|
36
44
|
log('Performing another request %o', childRequest);
|
|
37
45
|
// copy child result onto original response
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
});
|
|
46
|
+
return await provider.request(childRequest);
|
|
47
|
+
};
|
|
41
48
|
}
|
|
42
49
|
//# sourceMappingURL=block-ref.mjs.map
|
package/dist/block-ref.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block-ref.mjs","sourceRoot":"","sources":["../src/block-ref.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"block-ref.mjs","sourceRoot":"","sources":["../src/block-ref.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,EAAE,cAAc;AAE9B,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,4BAAwB;AAEpE,OAAO,EAAE,kBAAkB,EAAE,0BAAsB;AAOnD,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,WAAW,CAAC,CAAC;AAE3D;;;;;;;;GAQG;AACH,MAAM,UAAU,wBAAwB,CAAC,EACvC,QAAQ,EACR,YAAY,MACiB,EAAE;IAC/B,IAAI,CAAC,QAAQ,EAAE;QACb,MAAM,KAAK,CAAC,8DAA8D,CAAC,CAAC;KAC7E;IAED,IAAI,CAAC,YAAY,EAAE;QACjB,MAAM,KAAK,CACT,kEAAkE,CACnE,CAAC;KACH;IAED,OAAO,KAAK,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE;QACjC,MAAM,aAAa,GAAG,kBAAkB,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QAEzD,2CAA2C;QAC3C,IAAI,aAAa,KAAK,SAAS,EAAE;YAC/B,OAAO,IAAI,EAAE,CAAC;SACf;QAED,MAAM,QAAQ,GAAG,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC;YAC5C,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,QAAQ,CAAC;YAC7C,CAAC,CAAC,QAAQ,CAAC;QAEb,uBAAuB;QACvB,IAAI,QAAQ,KAAK,QAAQ,EAAE;YACzB,GAAG,CAAC,oDAAoD,CAAC,CAAC;YAC1D,OAAO,IAAI,EAAE,CAAC;SACf;QAED,sBAAsB;QACtB,MAAM,iBAAiB,GAAG,MAAM,YAAY,CAAC,cAAc,EAAE,CAAC;QAC9D,GAAG,CACD,uCAAuC,aAAa,oBAAoB,iBAAiB,EAAE,CAC5F,CAAC;QAEF,+CAA+C;QAC/C,MAAM,YAAY,GAAG,KAAK,CAAC,OAAO,CAAC,CAAC;QAEpC,IAAI,KAAK,CAAC,OAAO,CAAC,YAAY,CAAC,MAAM,CAAC,EAAE;YACtC,YAAY,CAAC,MAAM,CAAC,aAAa,CAAC,GAAG,iBAAiB,CAAC;SACxD;QAED,wBAAwB;QACxB,GAAG,CAAC,+BAA+B,EAAE,YAAY,CAAC,CAAC;QACnD,2CAA2C;QAC3C,OAAO,MAAM,QAAQ,CAAC,OAAO,CAAuB,YAAY,CAAC,CAAC;IACpE,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import type { PollingBlockTracker } from '@metamask/eth-block-tracker';\nimport type { InternalProvider } from '@metamask/eth-json-rpc-provider';\nimport type { JsonRpcMiddleware } from '@metamask/json-rpc-engine/v2';\nimport type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils';\nimport { klona } from 'klona';\n\nimport { projectLogger, createModuleLogger } from './logging-utils';\nimport type { Block } from './types';\nimport { blockTagParamIndex } from './utils/cache';\n\ntype BlockRefMiddlewareOptions = {\n blockTracker?: PollingBlockTracker;\n provider?: InternalProvider;\n};\n\nconst log = createModuleLogger(projectLogger, 'block-ref');\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.provider - The provider to use.\n * @param options.blockTracker - The block tracker to use.\n * @returns The middleware.\n */\nexport function createBlockRefMiddleware({\n provider,\n blockTracker,\n}: BlockRefMiddlewareOptions = {}): JsonRpcMiddleware<JsonRpcRequest, Json> {\n if (!provider) {\n throw Error('BlockRefMiddleware - mandatory \"provider\" option is missing.');\n }\n\n if (!blockTracker) {\n throw Error(\n 'BlockRefMiddleware - mandatory \"blockTracker\" option is missing.',\n );\n }\n\n return async ({ request, next }) => {\n const blockRefIndex = blockTagParamIndex(request.method);\n\n // skip if method does not include blockRef\n if (blockRefIndex === undefined) {\n return next();\n }\n\n const blockRef = Array.isArray(request.params)\n ? (request.params[blockRefIndex] ?? 'latest')\n : 'latest';\n\n // skip if not \"latest\"\n if (blockRef !== 'latest') {\n log('blockRef is not \"latest\", carrying request forward');\n return next();\n }\n\n // lookup latest block\n const latestBlockNumber = await blockTracker.getLatestBlock();\n log(\n `blockRef is \"latest\", setting param ${blockRefIndex} to latest block ${latestBlockNumber}`,\n );\n\n // create child request with specific block-ref\n const childRequest = klona(request);\n\n if (Array.isArray(childRequest.params)) {\n childRequest.params[blockRefIndex] = latestBlockNumber;\n }\n\n // perform child request\n log('Performing another request %o', childRequest);\n // copy child result onto original response\n return await provider.request<JsonRpcParams, Block>(childRequest);\n };\n}\n"]}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createBlockTrackerInspectorMiddleware = void 0;
|
|
4
|
-
const json_rpc_engine_1 = require("@metamask/json-rpc-engine");
|
|
5
4
|
const utils_1 = require("@metamask/utils");
|
|
6
5
|
const logging_utils_1 = require("./logging-utils.cjs");
|
|
7
6
|
const log = (0, logging_utils_1.createModuleLogger)(logging_utils_1.projectLogger, 'block-tracker-inspector');
|
|
@@ -18,39 +17,43 @@ const futureBlockRefRequests = [
|
|
|
18
17
|
* @returns The middleware.
|
|
19
18
|
*/
|
|
20
19
|
function createBlockTrackerInspectorMiddleware({ blockTracker, }) {
|
|
21
|
-
return
|
|
22
|
-
if (!futureBlockRefRequests.includes(
|
|
20
|
+
return async ({ request, next }) => {
|
|
21
|
+
if (!futureBlockRefRequests.includes(request.method)) {
|
|
23
22
|
return next();
|
|
24
23
|
}
|
|
25
|
-
await next();
|
|
26
|
-
const responseBlockNumber = getResultBlockNumber(
|
|
27
|
-
if (
|
|
28
|
-
|
|
24
|
+
const result = await next();
|
|
25
|
+
const responseBlockNumber = getResultBlockNumber(result);
|
|
26
|
+
if (responseBlockNumber) {
|
|
27
|
+
log('res.result.blockNumber exists, proceeding. res = %o', result);
|
|
28
|
+
// If number is higher, suggest block-tracker check for a new block
|
|
29
|
+
const blockNumber = Number.parseInt(responseBlockNumber, 16);
|
|
30
|
+
const currentBlockNumber = Number.parseInt(
|
|
31
|
+
// Typecast: If getCurrentBlock returns null, currentBlockNumber will be NaN, which is fine.
|
|
32
|
+
blockTracker.getCurrentBlock(), 16);
|
|
33
|
+
if (blockNumber > currentBlockNumber) {
|
|
34
|
+
log('blockNumber from response is greater than current block number, refreshing current block number');
|
|
35
|
+
await blockTracker.checkForLatestBlock();
|
|
36
|
+
}
|
|
29
37
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const blockNumber = Number.parseInt(responseBlockNumber, 16);
|
|
33
|
-
const currentBlockNumber = Number.parseInt(
|
|
34
|
-
// Typecast: If getCurrentBlock returns null, currentBlockNumber will be NaN, which is fine.
|
|
35
|
-
blockTracker.getCurrentBlock(), 16);
|
|
36
|
-
if (blockNumber > currentBlockNumber) {
|
|
37
|
-
log('blockNumber from response is greater than current block number, refreshing current block number');
|
|
38
|
-
await blockTracker.checkForLatestBlock();
|
|
39
|
-
}
|
|
40
|
-
return undefined;
|
|
41
|
-
});
|
|
38
|
+
return result;
|
|
39
|
+
};
|
|
42
40
|
}
|
|
43
41
|
exports.createBlockTrackerInspectorMiddleware = createBlockTrackerInspectorMiddleware;
|
|
44
|
-
|
|
45
|
-
|
|
42
|
+
/**
|
|
43
|
+
* Extracts the block number from the result.
|
|
44
|
+
*
|
|
45
|
+
* @param result - The result to extract the block number from.
|
|
46
|
+
* @returns The block number, or undefined if the result is not an object with a
|
|
47
|
+
* `blockNumber` property.
|
|
48
|
+
*/
|
|
49
|
+
function getResultBlockNumber(result) {
|
|
46
50
|
if (!result ||
|
|
47
51
|
typeof result !== 'object' ||
|
|
48
52
|
!(0, utils_1.hasProperty)(result, 'blockNumber')) {
|
|
49
53
|
return undefined;
|
|
50
54
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
return undefined;
|
|
55
|
+
return typeof result.blockNumber === 'string'
|
|
56
|
+
? result.blockNumber
|
|
57
|
+
: undefined;
|
|
55
58
|
}
|
|
56
59
|
//# sourceMappingURL=block-tracker-inspector.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block-tracker-inspector.cjs","sourceRoot":"","sources":["../src/block-tracker-inspector.ts"],"names":[],"mappings":";;;AAEA
|
|
1
|
+
{"version":3,"file":"block-tracker-inspector.cjs","sourceRoot":"","sources":["../src/block-tracker-inspector.ts"],"names":[],"mappings":";;;AAEA,2CAA8C;AAG9C,uDAAoE;AAEpE,MAAM,GAAG,GAAG,IAAA,kCAAkB,EAAC,6BAAa,EAAE,yBAAyB,CAAC,CAAC;AACzE,MAAM,sBAAsB,GAAsB;IAChD,0BAA0B;IAC1B,2BAA2B;CAC5B,CAAC;AAEF;;;;;;;GAOG;AACH,SAAgB,qCAAqC,CAAC,EACpD,YAAY,GAGb;IACC,OAAO,KAAK,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE;QACjC,IAAI,CAAC,sBAAsB,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;YACpD,OAAO,IAAI,EAAE,CAAC;SACf;QACD,MAAM,MAAM,GAAG,MAAM,IAAI,EAAE,CAAC;QAE5B,MAAM,mBAAmB,GAAG,oBAAoB,CAAC,MAAM,CAAC,CAAC;QACzD,IAAI,mBAAmB,EAAE;YACvB,GAAG,CAAC,qDAAqD,EAAE,MAAM,CAAC,CAAC;YAEnE,mEAAmE;YACnE,MAAM,WAAW,GAAW,MAAM,CAAC,QAAQ,CAAC,mBAAmB,EAAE,EAAE,CAAC,CAAC;YACrE,MAAM,kBAAkB,GAAW,MAAM,CAAC,QAAQ;YAChD,4FAA4F;YAC5F,YAAY,CAAC,eAAe,EAAY,EACxC,EAAE,CACH,CAAC;YAEF,IAAI,WAAW,GAAG,kBAAkB,EAAE;gBACpC,GAAG,CACD,iGAAiG,CAClG,CAAC;gBACF,MAAM,YAAY,CAAC,mBAAmB,EAAE,CAAC;aAC1C;SACF;QACD,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC;AAhCD,sFAgCC;AAED;;;;;;GAMG;AACH,SAAS,oBAAoB,CAC3B,MAAkC;IAElC,IACE,CAAC,MAAM;QACP,OAAO,MAAM,KAAK,QAAQ;QAC1B,CAAC,IAAA,mBAAW,EAAC,MAAM,EAAE,aAAa,CAAC,EACnC;QACA,OAAO,SAAS,CAAC;KAClB;IAED,OAAO,OAAO,MAAM,CAAC,WAAW,KAAK,QAAQ;QAC3C,CAAC,CAAC,MAAM,CAAC,WAAW;QACpB,CAAC,CAAC,SAAS,CAAC;AAChB,CAAC","sourcesContent":["import type { PollingBlockTracker } from '@metamask/eth-block-tracker';\nimport type { JsonRpcMiddleware } from '@metamask/json-rpc-engine/v2';\nimport { hasProperty } from '@metamask/utils';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\n\nimport { projectLogger, createModuleLogger } from './logging-utils';\n\nconst log = createModuleLogger(projectLogger, 'block-tracker-inspector');\nconst futureBlockRefRequests: readonly string[] = [\n 'eth_getTransactionByHash',\n 'eth_getTransactionReceipt',\n];\n\n/**\n * Creates a middleware that checks whether response block references are higher than the current block.\n * If the block reference is higher, the middleware will make the block tracker check for a new block.\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 createBlockTrackerInspectorMiddleware({\n blockTracker,\n}: {\n blockTracker: PollingBlockTracker;\n}): JsonRpcMiddleware<JsonRpcRequest, Json> {\n return async ({ request, next }) => {\n if (!futureBlockRefRequests.includes(request.method)) {\n return next();\n }\n const result = await next();\n\n const responseBlockNumber = getResultBlockNumber(result);\n if (responseBlockNumber) {\n log('res.result.blockNumber exists, proceeding. res = %o', result);\n\n // If number is higher, suggest block-tracker check for a new block\n const blockNumber: number = Number.parseInt(responseBlockNumber, 16);\n const currentBlockNumber: number = Number.parseInt(\n // Typecast: If getCurrentBlock returns null, currentBlockNumber will be NaN, which is fine.\n blockTracker.getCurrentBlock() as string,\n 16,\n );\n\n if (blockNumber > currentBlockNumber) {\n log(\n 'blockNumber from response is greater than current block number, refreshing current block number',\n );\n await blockTracker.checkForLatestBlock();\n }\n }\n return result;\n };\n}\n\n/**\n * Extracts the block number from the result.\n *\n * @param result - The result to extract the block number from.\n * @returns The block number, or undefined if the result is not an object with a\n * `blockNumber` property.\n */\nfunction getResultBlockNumber(\n result: Readonly<Json> | undefined,\n): string | undefined {\n if (\n !result ||\n typeof result !== 'object' ||\n !hasProperty(result, 'blockNumber')\n ) {\n return undefined;\n }\n\n return typeof result.blockNumber === 'string'\n ? result.blockNumber\n : undefined;\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
|
/**
|
|
5
5
|
* Creates a middleware that checks whether response block references are higher than the current block.
|
|
6
6
|
* If the block reference is higher, the middleware will make the block tracker check for a new block.
|
|
@@ -11,5 +11,5 @@ import type { Json, JsonRpcParams } from "@metamask/utils";
|
|
|
11
11
|
*/
|
|
12
12
|
export declare function createBlockTrackerInspectorMiddleware({ blockTracker, }: {
|
|
13
13
|
blockTracker: PollingBlockTracker;
|
|
14
|
-
}): JsonRpcMiddleware<
|
|
14
|
+
}): JsonRpcMiddleware<JsonRpcRequest, Json>;
|
|
15
15
|
//# sourceMappingURL=block-tracker-inspector.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block-tracker-inspector.d.cts","sourceRoot":"","sources":["../src/block-tracker-inspector.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAoC;AACvE,OAAO,KAAK,EAAE,iBAAiB,EAAE,
|
|
1
|
+
{"version":3,"file":"block-tracker-inspector.d.cts","sourceRoot":"","sources":["../src/block-tracker-inspector.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAoC;AACvE,OAAO,KAAK,EAAE,iBAAiB,EAAE,qCAAqC;AAEtE,OAAO,KAAK,EAAE,IAAI,EAAE,cAAc,EAAE,wBAAwB;AAU5D;;;;;;;GAOG;AACH,wBAAgB,qCAAqC,CAAC,EACpD,YAAY,GACb,EAAE;IACD,YAAY,EAAE,mBAAmB,CAAC;CACnC,GAAG,iBAAiB,CAAC,cAAc,EAAE,IAAI,CAAC,CA4B1C"}
|
|
@@ -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
|
/**
|
|
5
5
|
* Creates a middleware that checks whether response block references are higher than the current block.
|
|
6
6
|
* If the block reference is higher, the middleware will make the block tracker check for a new block.
|
|
@@ -11,5 +11,5 @@ import type { Json, JsonRpcParams } from "@metamask/utils";
|
|
|
11
11
|
*/
|
|
12
12
|
export declare function createBlockTrackerInspectorMiddleware({ blockTracker, }: {
|
|
13
13
|
blockTracker: PollingBlockTracker;
|
|
14
|
-
}): JsonRpcMiddleware<
|
|
14
|
+
}): JsonRpcMiddleware<JsonRpcRequest, Json>;
|
|
15
15
|
//# sourceMappingURL=block-tracker-inspector.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block-tracker-inspector.d.mts","sourceRoot":"","sources":["../src/block-tracker-inspector.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAoC;AACvE,OAAO,KAAK,EAAE,iBAAiB,EAAE,
|
|
1
|
+
{"version":3,"file":"block-tracker-inspector.d.mts","sourceRoot":"","sources":["../src/block-tracker-inspector.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAoC;AACvE,OAAO,KAAK,EAAE,iBAAiB,EAAE,qCAAqC;AAEtE,OAAO,KAAK,EAAE,IAAI,EAAE,cAAc,EAAE,wBAAwB;AAU5D;;;;;;;GAOG;AACH,wBAAgB,qCAAqC,CAAC,EACpD,YAAY,GACb,EAAE;IACD,YAAY,EAAE,mBAAmB,CAAC;CACnC,GAAG,iBAAiB,CAAC,cAAc,EAAE,IAAI,CAAC,CA4B1C"}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { createAsyncMiddleware } from "@metamask/json-rpc-engine";
|
|
2
1
|
import { hasProperty } from "@metamask/utils";
|
|
3
2
|
import { projectLogger, createModuleLogger } from "./logging-utils.mjs";
|
|
4
3
|
const log = createModuleLogger(projectLogger, 'block-tracker-inspector');
|
|
@@ -15,38 +14,42 @@ const futureBlockRefRequests = [
|
|
|
15
14
|
* @returns The middleware.
|
|
16
15
|
*/
|
|
17
16
|
export function createBlockTrackerInspectorMiddleware({ blockTracker, }) {
|
|
18
|
-
return
|
|
19
|
-
if (!futureBlockRefRequests.includes(
|
|
17
|
+
return async ({ request, next }) => {
|
|
18
|
+
if (!futureBlockRefRequests.includes(request.method)) {
|
|
20
19
|
return next();
|
|
21
20
|
}
|
|
22
|
-
await next();
|
|
23
|
-
const responseBlockNumber = getResultBlockNumber(
|
|
24
|
-
if (
|
|
25
|
-
|
|
21
|
+
const result = await next();
|
|
22
|
+
const responseBlockNumber = getResultBlockNumber(result);
|
|
23
|
+
if (responseBlockNumber) {
|
|
24
|
+
log('res.result.blockNumber exists, proceeding. res = %o', result);
|
|
25
|
+
// If number is higher, suggest block-tracker check for a new block
|
|
26
|
+
const blockNumber = Number.parseInt(responseBlockNumber, 16);
|
|
27
|
+
const currentBlockNumber = Number.parseInt(
|
|
28
|
+
// Typecast: If getCurrentBlock returns null, currentBlockNumber will be NaN, which is fine.
|
|
29
|
+
blockTracker.getCurrentBlock(), 16);
|
|
30
|
+
if (blockNumber > currentBlockNumber) {
|
|
31
|
+
log('blockNumber from response is greater than current block number, refreshing current block number');
|
|
32
|
+
await blockTracker.checkForLatestBlock();
|
|
33
|
+
}
|
|
26
34
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const blockNumber = Number.parseInt(responseBlockNumber, 16);
|
|
30
|
-
const currentBlockNumber = Number.parseInt(
|
|
31
|
-
// Typecast: If getCurrentBlock returns null, currentBlockNumber will be NaN, which is fine.
|
|
32
|
-
blockTracker.getCurrentBlock(), 16);
|
|
33
|
-
if (blockNumber > currentBlockNumber) {
|
|
34
|
-
log('blockNumber from response is greater than current block number, refreshing current block number');
|
|
35
|
-
await blockTracker.checkForLatestBlock();
|
|
36
|
-
}
|
|
37
|
-
return undefined;
|
|
38
|
-
});
|
|
35
|
+
return result;
|
|
36
|
+
};
|
|
39
37
|
}
|
|
40
|
-
|
|
41
|
-
|
|
38
|
+
/**
|
|
39
|
+
* Extracts the block number from the result.
|
|
40
|
+
*
|
|
41
|
+
* @param result - The result to extract the block number from.
|
|
42
|
+
* @returns The block number, or undefined if the result is not an object with a
|
|
43
|
+
* `blockNumber` property.
|
|
44
|
+
*/
|
|
45
|
+
function getResultBlockNumber(result) {
|
|
42
46
|
if (!result ||
|
|
43
47
|
typeof result !== 'object' ||
|
|
44
48
|
!hasProperty(result, 'blockNumber')) {
|
|
45
49
|
return undefined;
|
|
46
50
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
return undefined;
|
|
51
|
+
return typeof result.blockNumber === 'string'
|
|
52
|
+
? result.blockNumber
|
|
53
|
+
: undefined;
|
|
51
54
|
}
|
|
52
55
|
//# sourceMappingURL=block-tracker-inspector.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block-tracker-inspector.mjs","sourceRoot":"","sources":["../src/block-tracker-inspector.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"block-tracker-inspector.mjs","sourceRoot":"","sources":["../src/block-tracker-inspector.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,wBAAwB;AAG9C,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,4BAAwB;AAEpE,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,yBAAyB,CAAC,CAAC;AACzE,MAAM,sBAAsB,GAAsB;IAChD,0BAA0B;IAC1B,2BAA2B;CAC5B,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,UAAU,qCAAqC,CAAC,EACpD,YAAY,GAGb;IACC,OAAO,KAAK,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE;QACjC,IAAI,CAAC,sBAAsB,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;YACpD,OAAO,IAAI,EAAE,CAAC;SACf;QACD,MAAM,MAAM,GAAG,MAAM,IAAI,EAAE,CAAC;QAE5B,MAAM,mBAAmB,GAAG,oBAAoB,CAAC,MAAM,CAAC,CAAC;QACzD,IAAI,mBAAmB,EAAE;YACvB,GAAG,CAAC,qDAAqD,EAAE,MAAM,CAAC,CAAC;YAEnE,mEAAmE;YACnE,MAAM,WAAW,GAAW,MAAM,CAAC,QAAQ,CAAC,mBAAmB,EAAE,EAAE,CAAC,CAAC;YACrE,MAAM,kBAAkB,GAAW,MAAM,CAAC,QAAQ;YAChD,4FAA4F;YAC5F,YAAY,CAAC,eAAe,EAAY,EACxC,EAAE,CACH,CAAC;YAEF,IAAI,WAAW,GAAG,kBAAkB,EAAE;gBACpC,GAAG,CACD,iGAAiG,CAClG,CAAC;gBACF,MAAM,YAAY,CAAC,mBAAmB,EAAE,CAAC;aAC1C;SACF;QACD,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,SAAS,oBAAoB,CAC3B,MAAkC;IAElC,IACE,CAAC,MAAM;QACP,OAAO,MAAM,KAAK,QAAQ;QAC1B,CAAC,WAAW,CAAC,MAAM,EAAE,aAAa,CAAC,EACnC;QACA,OAAO,SAAS,CAAC;KAClB;IAED,OAAO,OAAO,MAAM,CAAC,WAAW,KAAK,QAAQ;QAC3C,CAAC,CAAC,MAAM,CAAC,WAAW;QACpB,CAAC,CAAC,SAAS,CAAC;AAChB,CAAC","sourcesContent":["import type { PollingBlockTracker } from '@metamask/eth-block-tracker';\nimport type { JsonRpcMiddleware } from '@metamask/json-rpc-engine/v2';\nimport { hasProperty } from '@metamask/utils';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\n\nimport { projectLogger, createModuleLogger } from './logging-utils';\n\nconst log = createModuleLogger(projectLogger, 'block-tracker-inspector');\nconst futureBlockRefRequests: readonly string[] = [\n 'eth_getTransactionByHash',\n 'eth_getTransactionReceipt',\n];\n\n/**\n * Creates a middleware that checks whether response block references are higher than the current block.\n * If the block reference is higher, the middleware will make the block tracker check for a new block.\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 createBlockTrackerInspectorMiddleware({\n blockTracker,\n}: {\n blockTracker: PollingBlockTracker;\n}): JsonRpcMiddleware<JsonRpcRequest, Json> {\n return async ({ request, next }) => {\n if (!futureBlockRefRequests.includes(request.method)) {\n return next();\n }\n const result = await next();\n\n const responseBlockNumber = getResultBlockNumber(result);\n if (responseBlockNumber) {\n log('res.result.blockNumber exists, proceeding. res = %o', result);\n\n // If number is higher, suggest block-tracker check for a new block\n const blockNumber: number = Number.parseInt(responseBlockNumber, 16);\n const currentBlockNumber: number = Number.parseInt(\n // Typecast: If getCurrentBlock returns null, currentBlockNumber will be NaN, which is fine.\n blockTracker.getCurrentBlock() as string,\n 16,\n );\n\n if (blockNumber > currentBlockNumber) {\n log(\n 'blockNumber from response is greater than current block number, refreshing current block number',\n );\n await blockTracker.checkForLatestBlock();\n }\n }\n return result;\n };\n}\n\n/**\n * Extracts the block number from the result.\n *\n * @param result - The result to extract the block number from.\n * @returns The block number, or undefined if the result is not an object with a\n * `blockNumber` property.\n */\nfunction getResultBlockNumber(\n result: Readonly<Json> | undefined,\n): string | undefined {\n if (\n !result ||\n typeof result !== 'object' ||\n !hasProperty(result, 'blockNumber')\n ) {\n return undefined;\n }\n\n return typeof result.blockNumber === 'string'\n ? result.blockNumber\n : undefined;\n}\n"]}
|
package/dist/fetch.cjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createFetchMiddleware = void 0;
|
|
4
|
-
const json_rpc_engine_1 = require("@metamask/json-rpc-engine");
|
|
5
4
|
const rpc_errors_1 = require("@metamask/rpc-errors");
|
|
5
|
+
const klona_1 = require("klona");
|
|
6
6
|
/**
|
|
7
7
|
* Creates middleware for sending a JSON-RPC request through the given RPC
|
|
8
8
|
* service.
|
|
@@ -16,18 +16,14 @@ const rpc_errors_1 = require("@metamask/rpc-errors");
|
|
|
16
16
|
* @returns The fetch middleware.
|
|
17
17
|
*/
|
|
18
18
|
function createFetchMiddleware({ rpcService, options = {}, }) {
|
|
19
|
-
return
|
|
19
|
+
return async ({ request, context }) => {
|
|
20
|
+
const origin = context.get('origin');
|
|
20
21
|
const headers = 'originHttpHeaderKey' in options &&
|
|
21
22
|
options.originHttpHeaderKey !== undefined &&
|
|
22
|
-
|
|
23
|
-
? { [options.originHttpHeaderKey]:
|
|
23
|
+
origin !== undefined
|
|
24
|
+
? { [options.originHttpHeaderKey]: origin }
|
|
24
25
|
: {};
|
|
25
|
-
const jsonRpcResponse = await rpcService.request({
|
|
26
|
-
id: req.id,
|
|
27
|
-
jsonrpc: req.jsonrpc,
|
|
28
|
-
method: req.method,
|
|
29
|
-
params: req.params,
|
|
30
|
-
}, {
|
|
26
|
+
const jsonRpcResponse = await rpcService.request((0, klona_1.klona)(request), {
|
|
31
27
|
headers,
|
|
32
28
|
});
|
|
33
29
|
// NOTE: We intentionally do not test to see if `jsonRpcResponse.error` is
|
|
@@ -41,8 +37,8 @@ function createFetchMiddleware({ rpcService, options = {}, }) {
|
|
|
41
37
|
}
|
|
42
38
|
// Discard the `id` and `jsonrpc` fields in the response body
|
|
43
39
|
// (the JSON-RPC engine will fill those in)
|
|
44
|
-
|
|
45
|
-
}
|
|
40
|
+
return jsonRpcResponse.result;
|
|
41
|
+
};
|
|
46
42
|
}
|
|
47
43
|
exports.createFetchMiddleware = createFetchMiddleware;
|
|
48
44
|
//# sourceMappingURL=fetch.cjs.map
|
package/dist/fetch.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.cjs","sourceRoot":"","sources":["../src/fetch.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"fetch.cjs","sourceRoot":"","sources":["../src/fetch.ts"],"names":[],"mappings":";;;AAIA,qDAAiD;AAEjD,iCAA8B;AAI9B;;;;;;;;;;;GAWG;AACH,SAAgB,qBAAqB,CAAC,EACpC,UAAU,EACV,OAAO,GAAG,EAAE,GAMb;IAKC,OAAO,KAAK,EAAE,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE;QACpC,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QACrC,MAAM,OAAO,GACX,qBAAqB,IAAI,OAAO;YAChC,OAAO,CAAC,mBAAmB,KAAK,SAAS;YACzC,MAAM,KAAK,SAAS;YAClB,CAAC,CAAC,EAAE,CAAC,OAAO,CAAC,mBAAmB,CAAC,EAAE,MAAM,EAAE;YAC3C,CAAC,CAAC,EAAE,CAAC;QAET,MAAM,eAAe,GAAG,MAAM,UAAU,CAAC,OAAO,CAAC,IAAA,aAAK,EAAC,OAAO,CAAC,EAAE;YAC/D,OAAO;SACR,CAAC,CAAC;QAEH,0EAA0E;QAC1E,4CAA4C;QAC5C,sEAAsE;QACtE,uEAAuE;QACvE,IAAI,OAAO,IAAI,eAAe,EAAE;YAC9B,MAAM,sBAAS,CAAC,QAAQ,CAAC;gBACvB,IAAI,EAAE,eAAe,CAAC,KAAK;aAC5B,CAAC,CAAC;SACJ;QAED,6DAA6D;QAC7D,2CAA2C;QAC3C,OAAO,eAAe,CAAC,MAAM,CAAC;IAChC,CAAC,CAAC;AACJ,CAAC;AAxCD,sDAwCC","sourcesContent":["import type {\n JsonRpcMiddleware,\n MiddlewareContext,\n} from '@metamask/json-rpc-engine/v2';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\nimport { klona } from 'klona';\n\nimport type { AbstractRpcServiceLike } from './types';\n\n/**\n * Creates middleware for sending a JSON-RPC request through the given RPC\n * service.\n *\n * @param args - The arguments to this function.\n * @param args.rpcService - The RPC service to use.\n * @param args.options - Options.\n * @param args.options.originHttpHeaderKey - If provided, the origin field for\n * each JSON-RPC request will be attached to each outgoing fetch request under\n * this header.\n * @returns The fetch middleware.\n */\nexport function createFetchMiddleware({\n rpcService,\n options = {},\n}: {\n rpcService: AbstractRpcServiceLike;\n options?: {\n originHttpHeaderKey?: string;\n };\n}): JsonRpcMiddleware<\n JsonRpcRequest,\n Json,\n MiddlewareContext<{ origin: string }>\n> {\n return async ({ request, context }) => {\n const origin = context.get('origin');\n const headers =\n 'originHttpHeaderKey' in options &&\n options.originHttpHeaderKey !== undefined &&\n origin !== undefined\n ? { [options.originHttpHeaderKey]: origin }\n : {};\n\n const jsonRpcResponse = await rpcService.request(klona(request), {\n headers,\n });\n\n // NOTE: We intentionally do not test to see if `jsonRpcResponse.error` is\n // strictly a JSON-RPC error response as per\n // <https://www.jsonrpc.org/specification#error_object> to account for\n // Ganache returning error objects with extra properties such as `name`\n if ('error' in jsonRpcResponse) {\n throw rpcErrors.internal({\n data: jsonRpcResponse.error,\n });\n }\n\n // Discard the `id` and `jsonrpc` fields in the response body\n // (the JSON-RPC engine will fill those in)\n return jsonRpcResponse.result;\n };\n}\n"]}
|
package/dist/fetch.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine";
|
|
2
|
-
import type { Json,
|
|
1
|
+
import type { JsonRpcMiddleware, MiddlewareContext } from "@metamask/json-rpc-engine/v2";
|
|
2
|
+
import type { Json, JsonRpcRequest } from "@metamask/utils";
|
|
3
3
|
import type { AbstractRpcServiceLike } from "./types.cjs";
|
|
4
4
|
/**
|
|
5
5
|
* Creates middleware for sending a JSON-RPC request through the given RPC
|
|
@@ -18,5 +18,7 @@ export declare function createFetchMiddleware({ rpcService, options, }: {
|
|
|
18
18
|
options?: {
|
|
19
19
|
originHttpHeaderKey?: string;
|
|
20
20
|
};
|
|
21
|
-
}): JsonRpcMiddleware<
|
|
21
|
+
}): JsonRpcMiddleware<JsonRpcRequest, Json, MiddlewareContext<{
|
|
22
|
+
origin: string;
|
|
23
|
+
}>>;
|
|
22
24
|
//# sourceMappingURL=fetch.d.cts.map
|