From 5fefc9fdeaf9f615c835d66ce50897af4cb92e0f Mon Sep 17 00:00:00 2001 From: Mark Stacey Date: Fri, 10 Feb 2023 21:45:16 -0330 Subject: [PATCH] Remove `providerFromEngine` and `providerFromMiddleware` The two provider construction functions have been removed. These functions are instead availabile from the new package `eth-json-rpc-provider`. --- package.json | 2 +- src/block-cache.test.ts | 3 ++- src/block-ref.test.ts | 8 +++----- src/block-ref.ts | 3 ++- src/index.ts | 3 --- src/providerAsMiddleware.ts | 7 ++++--- src/providerFromEngine.ts | 32 -------------------------------- src/providerFromMiddleware.ts | 12 ------------ src/retryOnEmpty.test.ts | 8 +++----- src/retryOnEmpty.ts | 3 ++- src/types.ts | 22 +--------------------- test/util/helpers.ts | 2 +- yarn.lock | 12 +++++++++++- 13 files changed, 30 insertions(+), 87 deletions(-) delete mode 100644 src/providerFromEngine.ts delete mode 100644 src/providerFromMiddleware.ts diff --git a/package.json b/package.json index 5fa92662..7977ef92 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,6 @@ }, "dependencies": { "@metamask/eth-sig-util": "^5.0.0", - "@metamask/safe-event-emitter": "^2.0.0", "@metamask/utils": "^3.0.3", "clone": "^2.1.1", "eth-block-tracker": "^6.1.0", @@ -46,6 +45,7 @@ "@metamask/eslint-config-jest": "^8.0.0", "@metamask/eslint-config-nodejs": "^8.0.0", "@metamask/eslint-config-typescript": "^8.0.0", + "@metamask/eth-json-rpc-provider": "^1.0.0", "@types/btoa": "^1.2.3", "@types/clone": "^2.1.0", "@types/jest": "^27.4.1", diff --git a/src/block-cache.test.ts b/src/block-cache.test.ts index d154417f..2b0e4f94 100644 --- a/src/block-cache.test.ts +++ b/src/block-cache.test.ts @@ -1,8 +1,9 @@ import { PollingBlockTracker, Provider } from 'eth-block-tracker'; import { JsonRpcEngine } from 'json-rpc-engine'; import pify from 'pify'; +import { providerFromEngine } from '@metamask/eth-json-rpc-provider'; import createHitTrackerMiddleware from '../test/util/createHitTrackerMiddleware'; -import { providerFromEngine, createBlockCacheMiddleware } from '.'; +import { createBlockCacheMiddleware } from '.'; function createTestSetup() { // raw data source diff --git a/src/block-ref.test.ts b/src/block-ref.test.ts index ac87ffea..c49ac28f 100644 --- a/src/block-ref.test.ts +++ b/src/block-ref.test.ts @@ -1,5 +1,7 @@ import { PollingBlockTracker, Provider } from 'eth-block-tracker'; import { JsonRpcEngine, JsonRpcMiddleware } from 'json-rpc-engine'; +import { providerFromEngine } from '@metamask/eth-json-rpc-provider'; +import type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider'; import { buildMockParamsWithBlockParamAt, stubProviderRequests, @@ -9,11 +11,7 @@ import { buildMockParamsWithoutBlockParamAt, expectProviderRequestNotToHaveBeenMade, } from '../test/util/helpers'; -import { - SafeEventEmitterProvider, - providerFromEngine, - createBlockRefMiddleware, -} from '.'; +import { createBlockRefMiddleware } from '.'; /** * Objects used in each test. diff --git a/src/block-ref.ts b/src/block-ref.ts index 18089410..a9aa96fb 100644 --- a/src/block-ref.ts +++ b/src/block-ref.ts @@ -6,9 +6,10 @@ import { } from 'json-rpc-engine'; import clone from 'clone'; import pify from 'pify'; +import type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider'; import { projectLogger, createModuleLogger } from './logging-utils'; import { blockTagParamIndex } from './utils/cache'; -import type { Block, SafeEventEmitterProvider } from './types'; +import type { Block } from './types'; interface BlockRefMiddlewareOptions { blockTracker?: PollingBlockTracker; diff --git a/src/index.ts b/src/index.ts index 5acaf99e..80efdcd0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,8 +5,5 @@ export * from './block-tracker-inspector'; export * from './fetch'; export * from './inflight-cache'; export * from './providerAsMiddleware'; -export * from './providerFromEngine'; -export * from './providerFromMiddleware'; export * from './retryOnEmpty'; -export type { SafeEventEmitterProvider } from './types'; export * from './wallet'; diff --git a/src/providerAsMiddleware.ts b/src/providerAsMiddleware.ts index 153a1774..00c6bd17 100644 --- a/src/providerAsMiddleware.ts +++ b/src/providerAsMiddleware.ts @@ -1,5 +1,5 @@ import { JsonRpcMiddleware, PendingJsonRpcResponse } from 'json-rpc-engine'; -import type { SafeEventEmitterProvider } from './types'; +import type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider'; export function providerAsMiddleware( provider: SafeEventEmitterProvider, @@ -28,10 +28,11 @@ export function ethersProviderAsMiddleware( // send request to provider provider.send( req, - (err: Error, providerRes: PendingJsonRpcResponse) => { + (err: unknown, providerRes: PendingJsonRpcResponse) => { // forward any error if (err) { - return end(err); + // TODO: Fix this type in `json-rpc-engine` + return end(err as Error); } // copy provider response onto original response Object.assign(res, providerRes); diff --git a/src/providerFromEngine.ts b/src/providerFromEngine.ts deleted file mode 100644 index e2e80b3d..00000000 --- a/src/providerFromEngine.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { JsonRpcEngine, JsonRpcRequest } from 'json-rpc-engine'; -import SafeEventEmitter from '@metamask/safe-event-emitter'; -import type { SafeEventEmitterProvider } from './types'; - -export function providerFromEngine( - engine: JsonRpcEngine, -): SafeEventEmitterProvider { - const provider: SafeEventEmitterProvider = - new SafeEventEmitter() as SafeEventEmitterProvider; - // handle both rpc send methods - provider.sendAsync = (req, cb) => { - engine.handle(req, cb); - }; - - provider.send = ( - req: JsonRpcRequest, - callback: (error: any, providerRes: any) => void, - ) => { - if (typeof callback !== 'function') { - throw new Error('Must provide callback to "send" method.'); - } - engine.handle(req, callback); - }; - - // forward notifications - if (engine.on) { - engine.on('notification', (message: string) => { - provider.emit('data', null, message); - }); - } - return provider; -} diff --git a/src/providerFromMiddleware.ts b/src/providerFromMiddleware.ts deleted file mode 100644 index 8361ba91..00000000 --- a/src/providerFromMiddleware.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { JsonRpcEngine, JsonRpcMiddleware } from 'json-rpc-engine'; -import type { SafeEventEmitterProvider } from './types'; -import { providerFromEngine } from './providerFromEngine'; - -export function providerFromMiddleware( - middleware: JsonRpcMiddleware, -): SafeEventEmitterProvider { - const engine: JsonRpcEngine = new JsonRpcEngine(); - engine.push(middleware); - const provider: SafeEventEmitterProvider = providerFromEngine(engine); - return provider; -} diff --git a/src/retryOnEmpty.test.ts b/src/retryOnEmpty.test.ts index ec9c3cc9..42add7e5 100644 --- a/src/retryOnEmpty.test.ts +++ b/src/retryOnEmpty.test.ts @@ -4,6 +4,8 @@ import { JsonRpcMiddleware, JsonRpcRequest, } from 'json-rpc-engine'; +import { providerFromEngine } from '@metamask/eth-json-rpc-provider'; +import type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider'; import { buildFinalMiddlewareWithDefaultResponse, buildMockParamsWithBlockParamAt, @@ -15,11 +17,7 @@ import { requestMatches, stubProviderRequests, } from '../test/util/helpers'; -import { - providerFromEngine, - createRetryOnEmptyMiddleware, - SafeEventEmitterProvider, -} from '.'; +import { createRetryOnEmptyMiddleware } from '.'; /** * Objects used in each test. diff --git a/src/retryOnEmpty.ts b/src/retryOnEmpty.ts index 4873d300..14300402 100644 --- a/src/retryOnEmpty.ts +++ b/src/retryOnEmpty.ts @@ -5,10 +5,11 @@ import { JsonRpcMiddleware, PendingJsonRpcResponse, } from 'json-rpc-engine'; +import type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider'; import pify from 'pify'; import { projectLogger, createModuleLogger } from './logging-utils'; import { blockTagParamIndex } from './utils/cache'; -import { Block, SafeEventEmitterProvider } from './types'; +import { Block } from './types'; // // RetryOnEmptyMiddleware will retry any request with an empty response that has diff --git a/src/types.ts b/src/types.ts index d3afc9c9..005de8cd 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,9 +1,4 @@ -import { - JsonRpcMiddleware, - JsonRpcRequest, - JsonRpcResponse, -} from 'json-rpc-engine'; -import SafeEventEmitter from '@metamask/safe-event-emitter'; +import { JsonRpcMiddleware, JsonRpcRequest } from 'json-rpc-engine'; export interface JsonRpcRequestToCache extends JsonRpcRequest { skipCache?: boolean; @@ -23,18 +18,3 @@ export type Block = Record; export type BlockCache = Record; export type Cache = Record; - -export type SendAsyncCallBack = ( - err: unknown, - providerRes: JsonRpcResponse, -) => void; - -export type SendCallBack = ( - err: any, - providerRes: JsonRpcResponse, -) => void; - -export interface SafeEventEmitterProvider extends SafeEventEmitter { - sendAsync: (req: JsonRpcRequest, cb: SendAsyncCallBack) => void; - send: (req: JsonRpcRequest, callback: SendCallBack) => void; -} diff --git a/test/util/helpers.ts b/test/util/helpers.ts index b58a1a2c..91c1892b 100644 --- a/test/util/helpers.ts +++ b/test/util/helpers.ts @@ -5,7 +5,7 @@ import { JsonRpcResponse, JsonRpcMiddleware, } from 'json-rpc-engine'; -import { SafeEventEmitterProvider } from '../../src'; +import type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider'; /** * An object that can be used to assign a canned response to a request made via diff --git a/yarn.lock b/yarn.lock index a2029954..82ff78ad 100644 --- a/yarn.lock +++ b/yarn.lock @@ -856,8 +856,8 @@ __metadata: "@metamask/eslint-config-jest": ^8.0.0 "@metamask/eslint-config-nodejs": ^8.0.0 "@metamask/eslint-config-typescript": ^8.0.0 + "@metamask/eth-json-rpc-provider": ^1.0.0 "@metamask/eth-sig-util": ^5.0.0 - "@metamask/safe-event-emitter": ^2.0.0 "@metamask/utils": ^3.0.3 "@types/btoa": ^1.2.3 "@types/clone": ^2.1.0 @@ -889,6 +889,16 @@ __metadata: languageName: unknown linkType: soft +"@metamask/eth-json-rpc-provider@npm:^1.0.0": + version: 1.0.0 + resolution: "@metamask/eth-json-rpc-provider@npm:1.0.0" + dependencies: + "@metamask/safe-event-emitter": ^2.0.0 + json-rpc-engine: ^6.1.0 + checksum: 27865d84d90030db1a9e5a66bc0b0ae079706fb7be635ec1e9bd4f64771e819aae78f0a026c6629d3a1a2eb277fcd51977315c049c47a70df1dd95d1d4106982 + languageName: node + linkType: hard + "@metamask/eth-sig-util@npm:^5.0.0": version: 5.0.2 resolution: "@metamask/eth-sig-util@npm:5.0.2"