From 2c26f35189130bfb169351e2582d6897d78e4cf1 Mon Sep 17 00:00:00 2001 From: Kanthesha Devaramane Date: Wed, 3 Jul 2024 23:09:44 +0100 Subject: [PATCH 1/6] adapt to eip-1193 provider changes --- src/PollingBlockTracker.test.ts | 90 +++++++++++++------------------ src/PollingBlockTracker.ts | 7 ++- src/SubscribeBlockTracker.test.ts | 8 +-- src/SubscribeBlockTracker.ts | 25 +++------ tests/withBlockTracker.ts | 61 +++++++++++---------- 5 files changed, 85 insertions(+), 106 deletions(-) diff --git a/src/PollingBlockTracker.test.ts b/src/PollingBlockTracker.test.ts index a83d43ac..a1364ecc 100644 --- a/src/PollingBlockTracker.test.ts +++ b/src/PollingBlockTracker.test.ts @@ -197,20 +197,17 @@ describe('PollingBlockTracker', () => { await withPollingBlockTracker( { blockTracker: { setSkipCacheFlag: true } }, async ({ provider, blockTracker }) => { - jest.spyOn(provider, 'sendAsync'); + jest.spyOn(provider, 'request'); await blockTracker.getLatestBlock(); - expect(provider.sendAsync).toHaveBeenCalledWith( - { - jsonrpc: '2.0' as const, - id: expect.any(Number), - method: 'eth_blockNumber' as const, - params: [], - skipCache: true, - }, - expect.any(Function), - ); + expect(provider.request).toHaveBeenCalledWith({ + jsonrpc: '2.0' as const, + id: expect.any(Number), + method: 'eth_blockNumber' as const, + params: [], + skipCache: true, + }); }, ); }); @@ -219,14 +216,12 @@ describe('PollingBlockTracker', () => { recordCallsToSetTimeout(); await withPollingBlockTracker(async ({ provider, blockTracker }) => { - const sendAsyncSpy = jest.spyOn(provider, 'sendAsync'); + const requestSpy = jest.spyOn(provider, 'request'); await blockTracker.getLatestBlock(); await blockTracker.getLatestBlock(); - const requestsForLatestBlock = sendAsyncSpy.mock.calls.filter( - (args) => { - return args[0].method === 'eth_blockNumber'; - }, - ); + const requestsForLatestBlock = requestSpy.mock.calls.filter((args) => { + return args[0].method === 'eth_blockNumber'; + }); expect(requestsForLatestBlock).toHaveLength(1); }); }); @@ -259,7 +254,7 @@ describe('PollingBlockTracker', () => { blockTracker: blockTrackerOptions, }, async ({ provider, blockTracker }) => { - const sendAsyncSpy = jest.spyOn(provider, 'sendAsync'); + const requestSpy = jest.spyOn(provider, 'request'); await blockTracker.getLatestBlock(); // When the block tracker stops, there may be two `setTimeout`s in // play: one to go to the next iteration of the block tracker @@ -269,7 +264,7 @@ describe('PollingBlockTracker', () => { blockTrackerOptions.blockResetDuration, ); await blockTracker.getLatestBlock(); - const requestsForLatestBlock = sendAsyncSpy.mock.calls.filter( + const requestsForLatestBlock = requestSpy.mock.calls.filter( (args) => { return args[0].method === 'eth_blockNumber'; }, @@ -731,20 +726,17 @@ describe('PollingBlockTracker', () => { await withPollingBlockTracker( { blockTracker: { setSkipCacheFlag: true } }, async ({ provider, blockTracker }) => { - jest.spyOn(provider, 'sendAsync'); + jest.spyOn(provider, 'request'); await blockTracker.checkForLatestBlock(); - expect(provider.sendAsync).toHaveBeenCalledWith( - { - jsonrpc: '2.0' as const, - id: expect.any(Number), - method: 'eth_blockNumber' as const, - params: [], - skipCache: true, - }, - expect.any(Function), - ); + expect(provider.request).toHaveBeenCalledWith({ + jsonrpc: '2.0' as const, + id: expect.any(Number), + method: 'eth_blockNumber' as const, + params: [], + skipCache: true, + }); }, ); }); @@ -1246,22 +1238,19 @@ describe('PollingBlockTracker', () => { await withPollingBlockTracker( { blockTracker: { setSkipCacheFlag: true } }, async ({ provider, blockTracker }) => { - jest.spyOn(provider, 'sendAsync'); + jest.spyOn(provider, 'request'); await new Promise((resolve) => { blockTracker[methodToAddListener]('latest', resolve); }); - expect(provider.sendAsync).toHaveBeenCalledWith( - { - jsonrpc: '2.0' as const, - id: expect.any(Number), - method: 'eth_blockNumber' as const, - params: [], - skipCache: true, - }, - expect.any(Function), - ); + expect(provider.request).toHaveBeenCalledWith({ + jsonrpc: '2.0' as const, + id: expect.any(Number), + method: 'eth_blockNumber' as const, + params: [], + skipCache: true, + }); }, ); }); @@ -2007,22 +1996,19 @@ describe('PollingBlockTracker', () => { await withPollingBlockTracker( { blockTracker: { setSkipCacheFlag: true } }, async ({ provider, blockTracker }) => { - jest.spyOn(provider, 'sendAsync'); + jest.spyOn(provider, 'request'); await new Promise((resolve) => { blockTracker[methodToAddListener]('sync', resolve); }); - expect(provider.sendAsync).toHaveBeenCalledWith( - { - jsonrpc: '2.0' as const, - id: expect.any(Number), - method: 'eth_blockNumber' as const, - params: [], - skipCache: true, - }, - expect.any(Function), - ); + expect(provider.request).toHaveBeenCalledWith({ + jsonrpc: '2.0' as const, + id: expect.any(Number), + method: 'eth_blockNumber' as const, + params: [], + skipCache: true, + }); }, ); }); diff --git a/src/PollingBlockTracker.ts b/src/PollingBlockTracker.ts index 12c82a47..f720e689 100644 --- a/src/PollingBlockTracker.ts +++ b/src/PollingBlockTracker.ts @@ -2,7 +2,6 @@ import type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider'; import SafeEventEmitter from '@metamask/safe-event-emitter'; import type { JsonRpcRequest } from '@metamask/utils'; import getCreateRandomId from 'json-rpc-random-id'; -import pify from 'pify'; import type { BlockTracker } from './BlockTracker'; import { projectLogger, createModuleLogger } from './logging-utils'; @@ -272,14 +271,14 @@ export class PollingBlockTracker } log('Making request', req); - const res = await pify((cb) => this._provider.sendAsync(req, cb))(); + const res = await this._provider.request(req); log('Got response', res); - if (res.error) { + if ('error' in res) { throw new Error( `PollingBlockTracker - encountered error fetching block:\n${res.error.message}`, ); } - return res.result; + return res.result as string; } /** diff --git a/src/SubscribeBlockTracker.test.ts b/src/SubscribeBlockTracker.test.ts index 47b69911..3318d801 100644 --- a/src/SubscribeBlockTracker.test.ts +++ b/src/SubscribeBlockTracker.test.ts @@ -184,10 +184,10 @@ describe('SubscribeBlockTracker', () => { recordCallsToSetTimeout(); await withSubscribeBlockTracker(async ({ provider, blockTracker }) => { - const sendAsyncSpy = jest.spyOn(provider, 'sendAsync'); + const requestSpy = jest.spyOn(provider, 'request'); await blockTracker[methodToGetLatestBlock](); await blockTracker[methodToGetLatestBlock](); - const requestsForLatestBlock = sendAsyncSpy.mock.calls.filter( + const requestsForLatestBlock = requestSpy.mock.calls.filter( (args) => { return args[0].method === 'eth_blockNumber'; }, @@ -248,7 +248,7 @@ describe('SubscribeBlockTracker', () => { blockTracker: blockTrackerOptions, }, async ({ provider, blockTracker }) => { - const sendAsyncSpy = jest.spyOn(provider, 'sendAsync'); + const requestSpy = jest.spyOn(provider, 'request'); await blockTracker[methodToGetLatestBlock](); // For PollingBlockTracker, there are possibly multiple // `setTimeout`s in play at this point. For SubscribeBlockTracker @@ -258,7 +258,7 @@ describe('SubscribeBlockTracker', () => { blockTrackerOptions.blockResetDuration, ); await blockTracker[methodToGetLatestBlock](); - const requestsForLatestBlock = sendAsyncSpy.mock.calls.filter( + const requestsForLatestBlock = requestSpy.mock.calls.filter( (args) => { return args[0].method === 'eth_blockNumber'; }, diff --git a/src/SubscribeBlockTracker.ts b/src/SubscribeBlockTracker.ts index d0ac90b8..3c46ba8f 100644 --- a/src/SubscribeBlockTracker.ts +++ b/src/SubscribeBlockTracker.ts @@ -256,23 +256,14 @@ export class SubscribeBlockTracker } private async _call(method: string, ...params: Json[]): Promise { - return new Promise((resolve, reject) => { - this._provider.sendAsync( - { - id: createRandomId(), - method, - params, - jsonrpc: '2.0', - }, - (err, res) => { - if (err) { - reject(err); - } else { - resolve((res as JsonRpcSuccess).result); - } - }, - ); - }); + const res = (await this._provider.request({ + id: createRandomId(), + method, + params, + jsonrpc: '2.0', + })) as JsonRpcSuccess; + + return res.result; } private _handleSubData( diff --git a/tests/withBlockTracker.ts b/tests/withBlockTracker.ts index e7e1b05d..f6c31ac4 100644 --- a/tests/withBlockTracker.ts +++ b/tests/withBlockTracker.ts @@ -1,7 +1,11 @@ import { providerFromEngine } from '@metamask/eth-json-rpc-provider'; -import type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider'; +import type { + // Eip1193Request, + SafeEventEmitterProvider, +} from '@metamask/eth-json-rpc-provider'; import { JsonRpcEngine } from '@metamask/json-rpc-engine'; -import type { JsonRpcRequest, JsonRpcResponse } from '@metamask/utils'; +import type { Json, JsonRpcResponse } from '@metamask/utils'; +// import { request } from 'http'; import util from 'util'; import type { @@ -32,21 +36,21 @@ type WithSubscribeBlockTrackerCallback = (args: { /** * An object that allows specifying the behavior of a specific invocation of - * `sendAsync`. The `methodName` always identifies the stub, but the behavior - * may be specified multiple ways: `sendAsync` can either return a promise or + * `request`. The `methodName` always identifies the stub, but the behavior + * may be specified multiple ways: `request` can either return a promise or * throw an error, and if it returns a promise, that promise can either be * resolved with a response object or reject with an error. * * @property methodName - The RPC method to which this stub will be matched. - * @property response - Instructs `sendAsync` to return a promise that resolves + * @property response - Instructs `request` to return a promise that resolves * with a response object. * @property response.result - Specifies a successful response, with this as the * `result`. * @property response.error - Specifies an error response, with this as the * `error`. - * @property implementation - Allows overriding `sendAsync` entirely. Useful if + * @property implementation - Allows overriding `request` entirely. Useful if * you want it to throw an error. - * @property error - Instructs `sendAsync` to return a promise that rejects with + * @property error - Instructs `request` to return a promise that rejects with * this error. */ type FakeProviderStub = @@ -67,7 +71,7 @@ type FakeProviderStub = * The set of options that a new instance of FakeProvider takes. * * @property stubs - A set of objects that allow specifying the behavior - * of specific invocations of `sendAsync` matching a `methodName`. + * of specific invocations of `request` matching a `methodName`. */ interface FakeProviderOptions { stubs?: FakeProviderStub[]; @@ -79,7 +83,7 @@ interface FakeProviderOptions { * * @param options - The options. * @param options.stubs - A set of objects that allow specifying the behavior - * of specific invocations of `sendAsync` matching a `methodName`. + * of specific invocations of `request` matching a `methodName`. * @returns The fake provider. */ function getFakeProvider({ @@ -119,14 +123,11 @@ function getFakeProvider({ const provider = providerFromEngine(new JsonRpcEngine()); jest - .spyOn(provider, 'sendAsync') + .spyOn(provider, 'request') .mockImplementation( - ( - request: JsonRpcRequest, - callback: (err: unknown, response?: JsonRpcResponse) => void, - ) => { + async (eip1193Request): Promise> => { const index = stubs.findIndex( - (stub) => stub.methodName === request.method, + (stub) => stub.methodName === eip1193Request.method, ); if (index !== -1) { @@ -136,35 +137,37 @@ function getFakeProvider({ stub.implementation(); } else if ('response' in stub) { if ('result' in stub.response) { - callback(null, { + return { jsonrpc: '2.0', id: 1, result: stub.response.result, - }); + } as JsonRpcResponse; } else if ('error' in stub.response) { - callback(null, { + return { jsonrpc: '2.0', id: 1, error: { code: -999, message: stub.response.error, }, - }); + } as JsonRpcResponse; } } else if ('error' in stub) { - callback(new Error(stub.error)); + throw new Error(stub.error); } - return; + return { + jsonrpc: '2.0', + id: 1, + result: null, + } as JsonRpcResponse; } - callback( - new Error( - `Could not find any stubs matching "${request.method}". Perhaps they've already been called?\n\n` + - 'The original set of stubs were:\n\n' + - `${util.inspect(originalStubs, { depth: null })}\n\n` + - 'Current set of stubs:\n\n' + - `${util.inspect(stubs, { depth: null })}\n\n`, - ), + throw new Error( + `Could not find any stubs matching "${eip1193Request.method}". Perhaps they've already been called?\n\n` + + 'The original set of stubs were:\n\n' + + `${util.inspect(originalStubs, { depth: null })}\n\n` + + 'Current set of stubs:\n\n' + + `${util.inspect(stubs, { depth: null })}\n\n`, ); }, ); From 46b2261dcbd464f957de777ca64a171c57462cb8 Mon Sep 17 00:00:00 2001 From: Kanthesha Devaramane Date: Tue, 9 Jul 2024 17:48:13 +0100 Subject: [PATCH 2/6] fixing the eip 1193 request response impact --- src/PollingBlockTracker.test.ts | 813 ++++-------------------------- src/PollingBlockTracker.ts | 9 +- src/SubscribeBlockTracker.test.ts | 612 ++-------------------- src/SubscribeBlockTracker.ts | 12 +- tests/withBlockTracker.ts | 88 ++-- 5 files changed, 188 insertions(+), 1346 deletions(-) diff --git a/src/PollingBlockTracker.test.ts b/src/PollingBlockTracker.test.ts index a1364ecc..db16fbb4 100644 --- a/src/PollingBlockTracker.test.ts +++ b/src/PollingBlockTracker.test.ts @@ -66,9 +66,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -117,9 +115,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -177,9 +173,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -239,15 +233,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_blockNumber', - response: { - result: '0x1', - }, + response: '0x1', }, ], }, @@ -275,45 +265,6 @@ describe('PollingBlockTracker', () => { }); METHODS_TO_ADD_LISTENER.forEach((methodToAddListener) => { - it(`should emit the "error" event (added via \`${methodToAddListener}\`) and should not throw if the request for the latest block number returns an error response`, async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withPollingBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_blockNumber', - response: { - error: 'boom', - }, - }, - { - methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, - }, - ], - }, - }, - async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); - - const promiseForLatestBlock = blockTracker.getLatestBlock(); - - const caughtError = await promiseForCaughtError; - expect(caughtError.message).toMatch( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: PollingBlockTracker - encountered error fetching block:\nboom/u, - ); - const latestBlock = await promiseForLatestBlock; - expect(latestBlock).toBe('0x0'); - }, - ); - }); - it(`should emit the "error" event (added via \`${methodToAddListener}\`) and should not throw if, while making the request for the latest block number, the provider throws an Error`, async () => { recordCallsToSetTimeout({ numAutomaticCalls: 1 }); @@ -329,9 +280,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -368,9 +317,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -405,9 +352,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -430,41 +375,6 @@ describe('PollingBlockTracker', () => { }); }); - it('should log an error if the request for the latest block number returns an error response and there is nothing listening to "error"', async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withPollingBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_blockNumber', - response: { - error: 'boom', - }, - }, - ], - }, - }, - async ({ blockTracker }) => { - jest.spyOn(console, 'error').mockImplementation(EMPTY_FUNCTION); - - blockTracker.getLatestBlock(); - await new Promise((resolve) => { - blockTracker.on('_waitingForNextIteration', resolve); - }); - - expect(console.error).toHaveBeenCalledWith( - expect.objectContaining({ - message: expect.stringMatching( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: PollingBlockTracker - encountered error fetching block:\nboom/u, - ), - }), - ); - }, - ); - }); - it('should log an error if, while making a request for the latest block number, the provider throws an Error and there is nothing listening to "error"', async () => { recordCallsToSetTimeout({ numAutomaticCalls: 1 }); @@ -577,9 +487,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -605,9 +513,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -641,15 +547,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_blockNumber', - response: { - result: '0x1', - }, + response: '0x1', }, ], }, @@ -682,9 +584,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -705,9 +605,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -741,31 +639,6 @@ describe('PollingBlockTracker', () => { ); }); - it(`should not emit the "error" event, but should throw instead if the request for the latest block number returns an error response`, async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withPollingBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_blockNumber', - response: { - error: 'boom', - }, - }, - ], - }, - }, - async ({ blockTracker }) => { - const promiseForLatestBlock = blockTracker.checkForLatestBlock(); - await expect(promiseForLatestBlock).rejects.toThrow( - 'PollingBlockTracker - encountered error fetching block:\nboom', - ); - }, - ); - }); - it(`should not emit the "error" event, but should throw instead, if, while making the request for the latest block number, the provider rejects with an error`, async () => { recordCallsToSetTimeout({ numAutomaticCalls: 1 }); const thrownError = new Error('boom'); @@ -780,9 +653,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -807,9 +678,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -842,15 +711,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_blockNumber', - response: { - result: '0x1', - }, + response: '0x1', }, ], }, @@ -874,15 +739,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_blockNumber', - response: { - result: '0x1', - }, + response: '0x1', }, ], }, @@ -906,15 +767,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x1', - }, + response: '0x1', }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -938,15 +795,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x1', - }, + response: '0x1', }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -973,15 +826,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_blockNumber', - response: { - result: '0x1', - }, + response: '0x1', }, ], }, @@ -1005,15 +854,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_blockNumber', - response: { - result: '0x1', - }, + response: '0x1', }, ], }, @@ -1037,15 +882,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x1', - }, + response: '0x1', }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -1069,15 +910,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x1', - }, + response: '0x1', }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -1116,9 +953,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -1141,9 +976,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -1172,15 +1005,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_blockNumber', - response: { - result: '0x1', - }, + response: '0x1', }, ], }, @@ -1255,47 +1084,6 @@ describe('PollingBlockTracker', () => { ); }); - it(`should emit the "error" event and should not kill the block tracker if the request for the latest block number returns an error response`, async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withPollingBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_blockNumber', - response: { - error: 'boom', - }, - }, - { - methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, - }, - ], - }, - }, - async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); - - const promiseForLatestBlock = new Promise((resolve) => { - blockTracker[methodToAddListener]('latest', resolve); - }); - - const caughtError = await promiseForCaughtError; - expect(caughtError.message).toMatch( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: PollingBlockTracker - encountered error fetching block:\nboom/u, - ); - const latestBlock = await promiseForLatestBlock; - expect(latestBlock).toBe('0x0'); - }, - ); - }); - it(`should emit the "error" event and should not kill the block tracker if, while making the request for the latest block number, the provider throws an Error`, async () => { recordCallsToSetTimeout({ numAutomaticCalls: 1 }); @@ -1311,9 +1099,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -1352,9 +1138,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -1391,9 +1175,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -1417,47 +1199,6 @@ describe('PollingBlockTracker', () => { ); }); - it('should log an error if the request for the latest block number returns an error response and there is nothing listening to "error"', async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withPollingBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_blockNumber', - response: { - error: 'boom', - }, - }, - { - methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, - }, - ], - }, - }, - async ({ blockTracker }) => { - jest.spyOn(console, 'error').mockImplementation(EMPTY_FUNCTION); - - blockTracker[methodToAddListener]('latest', EMPTY_FUNCTION); - await new Promise((resolve) => { - blockTracker.on('_waitingForNextIteration', resolve); - }); - - expect(console.error).toHaveBeenCalledWith( - expect.objectContaining({ - message: expect.stringMatching( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: PollingBlockTracker - encountered error fetching block:\nboom/u, - ), - }), - ); - }, - ); - }); - it('should log an error if, while making a request for the latest block number, the provider throws an Error and there is nothing listening to "error"', async () => { recordCallsToSetTimeout({ numAutomaticCalls: 1 }); @@ -1473,9 +1214,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -1514,9 +1253,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -1553,9 +1290,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -1596,15 +1331,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_blockNumber', - response: { - result: '0x1', - }, + response: '0x1', }, ], }, @@ -1640,15 +1371,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x1', - }, + response: '0x1', }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -1684,15 +1411,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -1731,15 +1454,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_blockNumber', - response: { - result: '0x1', - }, + response: '0x1', }, ], }, @@ -1775,15 +1494,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x1', - }, + response: '0x1', }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -1819,15 +1534,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -1874,9 +1585,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -1899,9 +1608,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -1930,15 +1637,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_blockNumber', - response: { - result: '0x1', - }, + response: '0x1', }, ], }, @@ -2013,47 +1716,6 @@ describe('PollingBlockTracker', () => { ); }); - it(`should emit the "error" event and should not kill the block tracker if the request for the latest block number returns an error response`, async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withPollingBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_blockNumber', - response: { - error: 'boom', - }, - }, - { - methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, - }, - ], - }, - }, - async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); - - const promiseForSync = new Promise((resolve) => { - blockTracker[methodToAddListener]('sync', resolve); - }); - - const caughtError = await promiseForCaughtError; - expect(caughtError.message).toMatch( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: PollingBlockTracker - encountered error fetching block:\nboom/u, - ); - const sync = await promiseForSync; - expect(sync).toStrictEqual({ oldBlock: null, newBlock: '0x0' }); - }, - ); - }); - it(`should emit the "error" event and should not kill the block tracker if, while making the request for the latest block number, the provider throws an Error`, async () => { recordCallsToSetTimeout({ numAutomaticCalls: 1 }); @@ -2069,9 +1731,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -2110,9 +1770,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -2149,9 +1807,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -2175,41 +1831,6 @@ describe('PollingBlockTracker', () => { ); }); - it('should log an error if the request for the latest block number returns an error response and there is nothing listening to "error"', async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withPollingBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_blockNumber', - response: { - error: 'boom', - }, - }, - ], - }, - }, - async ({ blockTracker }) => { - jest.spyOn(console, 'error').mockImplementation(EMPTY_FUNCTION); - - blockTracker[methodToAddListener]('sync', EMPTY_FUNCTION); - await new Promise((resolve) => { - blockTracker.on('_waitingForNextIteration', resolve); - }); - - expect(console.error).toHaveBeenCalledWith( - expect.objectContaining({ - message: expect.stringMatching( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: PollingBlockTracker - encountered error fetching block:\nboom/u, - ), - }), - ); - }, - ); - }); - it('should log an error if, while making a request for the latest block number, the provider throws an Error and there is nothing listening to "error"', async () => { recordCallsToSetTimeout({ numAutomaticCalls: 1 }); @@ -2330,15 +1951,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_blockNumber', - response: { - result: '0x1', - }, + response: '0x1', }, ], }, @@ -2374,15 +1991,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x1', - }, + response: '0x1', }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -2417,15 +2030,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -2463,15 +2072,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_blockNumber', - response: { - result: '0x1', - }, + response: '0x1', }, ], }, @@ -2507,15 +2112,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x1', - }, + response: '0x1', }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -2551,15 +2152,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -2605,9 +2202,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -2662,9 +2257,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -2710,21 +2303,15 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_blockNumber', - response: { - result: '0x1', - }, + response: '0x1', }, { methodName: 'eth_blockNumber', - response: { - result: '0x2', - }, + response: '0x2', }, ], }, @@ -2791,9 +2378,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -2882,15 +2467,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_blockNumber', - response: { - result: '0x1', - }, + response: '0x1', }, ], }, @@ -2927,9 +2508,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -2954,47 +2533,6 @@ describe('PollingBlockTracker', () => { }); METHODS_TO_ADD_LISTENER.forEach((methodToAddListener) => { - it(`should emit the "error" event (added via \`${methodToAddListener}\`) and should not throw if the request for the latest block number returns an error response`, async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withPollingBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_blockNumber', - response: { - error: 'boom', - }, - }, - { - methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, - }, - ], - }, - }, - async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); - - const promiseForLatestBlock = new Promise((resolve) => { - blockTracker.once('latest', resolve); - }); - - const caughtError = await promiseForCaughtError; - expect(caughtError.message).toMatch( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: PollingBlockTracker - encountered error fetching block:\nboom/u, - ); - const latestBlock = await promiseForLatestBlock; - expect(latestBlock).toBe('0x0'); - }, - ); - }); - it(`should emit the "error" event (added via \`${methodToAddListener}\`) and should not throw if, while making the request for the latest block number, the provider throws an Error`, async () => { recordCallsToSetTimeout({ numAutomaticCalls: 1 }); @@ -3010,9 +2548,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -3051,9 +2587,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -3090,9 +2624,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -3117,47 +2649,6 @@ describe('PollingBlockTracker', () => { }); }); - it('should log an error if the request for the latest block number returns an error response and there is nothing listening to "error"', async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withPollingBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_blockNumber', - response: { - error: 'boom', - }, - }, - { - methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, - }, - ], - }, - }, - async ({ blockTracker }) => { - jest.spyOn(console, 'error').mockImplementation(EMPTY_FUNCTION); - - blockTracker.once('latest', EMPTY_FUNCTION); - await new Promise((resolve) => { - blockTracker.on('_waitingForNextIteration', resolve); - }); - - expect(console.error).toHaveBeenCalledWith( - expect.objectContaining({ - message: expect.stringMatching( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: PollingBlockTracker - encountered error fetching block:\nboom/u, - ), - }), - ); - }, - ); - }); - it('should log an error if, while making a request for the latest block number, the provider throws an Error and there is nothing listening to "error"', async () => { recordCallsToSetTimeout({ numAutomaticCalls: 1 }); @@ -3173,9 +2664,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -3214,9 +2703,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -3253,9 +2740,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -3310,9 +2795,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -3337,47 +2820,6 @@ describe('PollingBlockTracker', () => { }); METHODS_TO_ADD_LISTENER.forEach((methodToAddListener) => { - it(`should emit the "error" event (added via \`${methodToAddListener}\`) and should not throw if the request for the latest block number returns an error response`, async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withPollingBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_blockNumber', - response: { - error: 'boom', - }, - }, - { - methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, - }, - ], - }, - }, - async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); - - const promiseForSync = new Promise((resolve) => { - blockTracker.once('sync', resolve); - }); - - const caughtError = await promiseForCaughtError; - expect(caughtError.message).toMatch( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: PollingBlockTracker - encountered error fetching block:\nboom/u, - ); - const sync = await promiseForSync; - expect(sync).toStrictEqual({ oldBlock: null, newBlock: '0x0' }); - }, - ); - }); - it(`should emit the "error" event (added via \`${methodToAddListener}\`) and should not throw if, while making the request for the latest block number, the provider throws an Error`, async () => { recordCallsToSetTimeout({ numAutomaticCalls: 1 }); @@ -3393,9 +2835,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -3434,9 +2874,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -3473,9 +2911,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -3500,47 +2936,6 @@ describe('PollingBlockTracker', () => { }); }); - it('should log an error if the request for the latest block number returns an error response and there is nothing listening to "error"', async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withPollingBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_blockNumber', - response: { - error: 'boom', - }, - }, - { - methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, - }, - ], - }, - }, - async ({ blockTracker }) => { - jest.spyOn(console, 'error').mockImplementation(EMPTY_FUNCTION); - - blockTracker.once('sync', EMPTY_FUNCTION); - await new Promise((resolve) => { - blockTracker.on('_waitingForNextIteration', resolve); - }); - - expect(console.error).toHaveBeenCalledWith( - expect.objectContaining({ - message: expect.stringMatching( - /^PollingBlockTracker - encountered an error while attempting to update latest block:\nError: PollingBlockTracker - encountered error fetching block:\nboom/u, - ), - }), - ); - }, - ); - }); - it('should log an error if, while making a request for the latest block number, the provider throws an Error and there is nothing listening to "error"', async () => { recordCallsToSetTimeout({ numAutomaticCalls: 1 }); @@ -3556,9 +2951,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -3597,9 +2990,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -3636,9 +3027,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -3688,9 +3077,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -3733,9 +3120,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -3792,9 +3177,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, diff --git a/src/PollingBlockTracker.ts b/src/PollingBlockTracker.ts index f720e689..3e7b231c 100644 --- a/src/PollingBlockTracker.ts +++ b/src/PollingBlockTracker.ts @@ -271,14 +271,7 @@ export class PollingBlockTracker } log('Making request', req); - const res = await this._provider.request(req); - log('Got response', res); - if ('error' in res) { - throw new Error( - `PollingBlockTracker - encountered error fetching block:\n${res.error.message}`, - ); - } - return res.result as string; + return this._provider.request(req); } /** diff --git a/src/SubscribeBlockTracker.test.ts b/src/SubscribeBlockTracker.test.ts index 3318d801..a3af6d2c 100644 --- a/src/SubscribeBlockTracker.test.ts +++ b/src/SubscribeBlockTracker.test.ts @@ -70,9 +70,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -106,9 +104,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -164,9 +160,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -209,39 +203,27 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_subscribe', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_unsubscribe', - response: { - result: true, - }, + response: true, }, { methodName: 'eth_blockNumber', - response: { - result: '0x1', - }, + response: '0x1', }, { methodName: 'eth_subscribe', - response: { - result: '0x1', - }, + response: '0x1', }, { methodName: 'eth_unsubscribe', - response: { - result: true, - }, + response: true, }, ], }, @@ -269,37 +251,6 @@ describe('SubscribeBlockTracker', () => { }); METHODS_TO_ADD_LISTENER.forEach((methodToAddListener) => { - it(`should not emit the "error" event (added via \`${methodToAddListener}\`) and should resolve with undefined if the request for the latest block number returns an error response`, async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withSubscribeBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_blockNumber', - response: { - error: 'boom', - }, - }, - ], - }, - }, - async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); - - const promiseForLatestBlock = - blockTracker[methodToGetLatestBlock](); - - await expect(promiseForCaughtError).toNeverResolve(); - const latestBlock = await promiseForLatestBlock; - expect(latestBlock).toBeUndefined(); - }, - ); - }); - it(`should emit the "error" event (added via \`${methodToAddListener}\`) and should never resolve if, while making the request for the latest block number, the provider throws an Error`, async () => { recordCallsToSetTimeout({ numAutomaticCalls: 1 }); const thrownError = new Error('boom'); @@ -393,37 +344,6 @@ describe('SubscribeBlockTracker', () => { ); }); - it(`should not emit the "error" event (added via \`${methodToAddListener}\`) and should still resolve with the latest block number if the request to subscribe returns an error response`, async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withSubscribeBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_subscribe', - response: { - error: 'boom', - }, - }, - ], - }, - }, - async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); - - const promiseForLatestBlock = - blockTracker[methodToGetLatestBlock](); - - await expect(promiseForCaughtError).toNeverResolve(); - const latestBlockNumber = await promiseForLatestBlock; - expect(latestBlockNumber).toBe('0x0'); - }, - ); - }); - it(`should emit the "error" event (added via \`${methodToAddListener}\`) and should never resolve if, while making the request to subscribe, the provider throws an Error`, async () => { recordCallsToSetTimeout({ numAutomaticCalls: 1 }); const thrownError = new Error('boom'); @@ -517,34 +437,6 @@ describe('SubscribeBlockTracker', () => { ); }); - it(`should not emit the "error" event (added via \`${methodToAddListener}\`) if the request to unsubscribe returns an error response`, async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withSubscribeBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_unsubscribe', - response: { - error: 'boom', - }, - }, - ], - }, - }, - async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); - - await blockTracker[methodToGetLatestBlock](); - - await expect(promiseForCaughtError).toNeverResolve(); - }, - ); - }); - it(`should emit the "error" event (added via \`${methodToAddListener}\`) if, while making the request to unsubscribe, the provider throws an Error`, async () => { recordCallsToSetTimeout({ numAutomaticCalls: 1 }); const thrownError = new Error('boom'); @@ -642,9 +534,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -670,9 +560,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -719,9 +607,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -744,9 +630,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -770,15 +654,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_subscribe', - response: { - result: '0x64', - }, + response: '0x64', }, ], }, @@ -822,15 +702,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_subscribe', - response: { - result: '0x64', - }, + response: '0x64', }, ], }, @@ -887,38 +763,6 @@ describe('SubscribeBlockTracker', () => { }); }); - it(`should not emit the "error" event and should emit "latest" with undefined if the request for the latest block number returns an error response`, async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withSubscribeBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_blockNumber', - response: { - error: 'boom', - }, - }, - ], - }, - }, - async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); - - const promiseForLatestBlock = new Promise((resolve) => { - blockTracker[methodToAddListener]('latest', resolve); - }); - - await expect(promiseForCaughtError).toNeverResolve(); - const latestBlock = await promiseForLatestBlock; - expect(latestBlock).toBeUndefined(); - }, - ); - }); - it(`should emit the "error" event and should not emit "latest" if, while making the request for the latest block number, the provider throws an Error`, async () => { recordCallsToSetTimeout({ numAutomaticCalls: 1 }); @@ -996,9 +840,7 @@ describe('SubscribeBlockTracker', () => { }, { methodName: 'eth_subscribe', - response: { - result: '0x64', - }, + response: '0x64', }, ], }, @@ -1019,43 +861,6 @@ describe('SubscribeBlockTracker', () => { ); }); - it(`should not emit the "error" event and should still emit "latest" with the latest block number if the request to subscribe returns an error response`, async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withSubscribeBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, - }, - { - methodName: 'eth_subscribe', - response: { - error: 'boom', - }, - }, - ], - }, - }, - async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); - - const latestBlockNumber = await new Promise((resolve) => { - blockTracker[methodToAddListener]('latest', resolve); - }); - - await expect(promiseForCaughtError).toNeverResolve(); - expect(latestBlockNumber).toBe('0x0'); - }, - ); - }); - it(`should emit the "error" event and should not emit "latest" if, while making the request to subscribe, the provider throws an Error`, async () => { recordCallsToSetTimeout({ numAutomaticCalls: 1 }); @@ -1165,15 +970,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_subscribe', - response: { - result: '0x64', - }, + response: '0x64', }, ], }, @@ -1219,15 +1020,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x1', - }, + response: '0x1', }, { methodName: 'eth_subscribe', - response: { - result: '0x64', - }, + response: '0x64', }, ], }, @@ -1272,15 +1069,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_subscribe', - response: { - result: '0x64', - }, + response: '0x64', }, ], }, @@ -1328,15 +1121,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_subscribe', - response: { - result: '0x64', - }, + response: '0x64', }, ], }, @@ -1382,15 +1171,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x1', - }, + response: '0x1', }, { methodName: 'eth_subscribe', - response: { - result: '0x64', - }, + response: '0x64', }, ], }, @@ -1435,15 +1220,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_subscribe', - response: { - result: '0x64', - }, + response: '0x64', }, ], }, @@ -1501,9 +1282,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -1526,9 +1305,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -1552,15 +1329,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_subscribe', - response: { - result: '0x64', - }, + response: '0x64', }, ], }, @@ -1603,15 +1376,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_subscribe', - response: { - result: '0x64', - }, + response: '0x64', }, ], }, @@ -1667,41 +1436,6 @@ describe('SubscribeBlockTracker', () => { }); }); - it(`should not emit the "error" event and should emit "sync" with undefined in place of the latest block if the request for the latest block number returns an error response`, async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withSubscribeBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_blockNumber', - response: { - error: 'boom', - }, - }, - ], - }, - }, - async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); - - const promiseForSync = new Promise((resolve) => { - blockTracker[methodToAddListener]('sync', resolve); - }); - - await expect(promiseForCaughtError).toNeverResolve(); - const sync = await promiseForSync; - expect(sync).toStrictEqual({ - oldBlock: null, - newBlock: undefined, - }); - }, - ); - }); - it(`should emit the "error" event and should not emit "sync" if, while making a request for the latest block number, the provider throws an Error`, async () => { recordCallsToSetTimeout({ numAutomaticCalls: 1 }); @@ -1796,41 +1530,6 @@ describe('SubscribeBlockTracker', () => { ); }); - it(`should not emit the "error" event and should still emit "sync" with the latest block number if the request to subscribe returns an error response`, async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withSubscribeBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_subscribe', - response: { - error: 'boom', - }, - }, - ], - }, - }, - async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); - - const promiseForSync = new Promise((resolve) => { - blockTracker[methodToAddListener]('sync', resolve); - }); - - await expect(promiseForCaughtError).toNeverResolve(); - const sync = await promiseForSync; - expect(sync).toStrictEqual({ - oldBlock: null, - newBlock: '0x0', - }); - }, - ); - }); - it(`should emit the "error" event and should not emit "sync" if, while making the request to subscribe, the provider throws an Error`, async () => { recordCallsToSetTimeout({ numAutomaticCalls: 1 }); @@ -1940,15 +1639,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_subscribe', - response: { - result: '0x64', - }, + response: '0x64', }, ], }, @@ -1994,15 +1689,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x1', - }, + response: '0x1', }, { methodName: 'eth_subscribe', - response: { - result: '0x64', - }, + response: '0x64', }, ], }, @@ -2046,15 +1737,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_subscribe', - response: { - result: '0x64', - }, + response: '0x64', }, ], }, @@ -2101,15 +1788,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_subscribe', - response: { - result: '0x64', - }, + response: '0x64', }, ], }, @@ -2155,15 +1838,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x1', - }, + response: '0x1', }, { methodName: 'eth_subscribe', - response: { - result: '0x64', - }, + response: '0x64', }, ], }, @@ -2208,15 +1887,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, { methodName: 'eth_subscribe', - response: { - result: '0x64', - }, + response: '0x64', }, ], }, @@ -2273,9 +1948,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -2330,9 +2003,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -2541,9 +2212,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -2773,9 +2442,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -2800,38 +2467,6 @@ describe('SubscribeBlockTracker', () => { }); METHODS_TO_ADD_LISTENER.forEach((methodToAddListener) => { - it(`should not emit the "error" event (added via \`${methodToAddListener}\`) and should emit "latest" with undefined if the request for the latest block number returns an error response`, async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withSubscribeBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_blockNumber', - response: { - error: 'boom', - }, - }, - ], - }, - }, - async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); - - const promiseForLatestBlock = new Promise((resolve) => { - blockTracker.once('latest', resolve); - }); - - await expect(promiseForCaughtError).toNeverResolve(); - const latestBlockNumber = await promiseForLatestBlock; - expect(latestBlockNumber).toBeUndefined(); - }, - ); - }); - it(`should emit the "error" event (added via \`${methodToAddListener}\`) and should not emit "latest" if, while making the request for the latest block number, the provider throws an Error`, async () => { recordCallsToSetTimeout({ numAutomaticCalls: 1 }); const thrownError = new Error('boom'); @@ -2928,37 +2563,6 @@ describe('SubscribeBlockTracker', () => { ); }); - it(`should not emit the "error" event (added via \`${methodToAddListener}\`) and should still emit "latest" if the request to subscribe returns an error response`, async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withSubscribeBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_subscribe', - response: { - error: 'boom', - }, - }, - ], - }, - }, - async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); - - const latestBlockNumber = await new Promise((resolve) => { - blockTracker[methodToAddListener]('latest', resolve); - }); - - await expect(promiseForCaughtError).toNeverResolve(); - expect(latestBlockNumber).toBe('0x0'); - }, - ); - }); - it(`should emit the "error" event (added via \`${methodToAddListener}\`) and should not emit "latest" if, while making the request to subscribe, the provider throws an Error`, async () => { recordCallsToSetTimeout({ numAutomaticCalls: 1 }); const thrownError = new Error('boom'); @@ -3055,36 +2659,6 @@ describe('SubscribeBlockTracker', () => { ); }); - it(`should not emit the "error" event (added via \`${methodToAddListener}\`) if the request to unsubscribe returns an error response`, async () => { - recordCallsToSetTimeout(); - - await withSubscribeBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_unsubscribe', - response: { - error: 'boom', - }, - }, - ], - }, - }, - async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); - - await new Promise((resolve) => { - blockTracker.once('latest', resolve); - }); - - await expect(promiseForCaughtError).toNeverResolve(); - }, - ); - }); - it(`should emit the "error" event (added via \`${methodToAddListener}\`) if, while making the request to unsubscribe, the provider throws an Error`, async () => { recordCallsToSetTimeout(); @@ -3208,9 +2782,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -3235,41 +2807,6 @@ describe('SubscribeBlockTracker', () => { }); METHODS_TO_ADD_LISTENER.forEach((methodToAddListener) => { - it(`should not emit the "error" event (added via \`${methodToAddListener}\`) and should emit "sync" with undefined in place of the latest block number if the request for the latest block number returns an error response`, async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withSubscribeBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_blockNumber', - response: { - error: 'boom', - }, - }, - ], - }, - }, - async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); - - const promiseForSync = new Promise((resolve) => { - blockTracker.once('sync', resolve); - }); - - await expect(promiseForCaughtError).toNeverResolve(); - const sync = await promiseForSync; - expect(sync).toStrictEqual({ - oldBlock: null, - newBlock: undefined, - }); - }, - ); - }); - it(`should emit the "error" event (added via \`${methodToAddListener}\`) and should not emit "sync" if, while making the request for the latest block number, the provider throws an Error`, async () => { recordCallsToSetTimeout({ numAutomaticCalls: 1 }); const thrownError = new Error('boom'); @@ -3366,37 +2903,6 @@ describe('SubscribeBlockTracker', () => { ); }); - it(`should not emit the "error" event (added via \`${methodToAddListener}\`) and should still emit "sync" if the request to subscribe returns an error response`, async () => { - recordCallsToSetTimeout({ numAutomaticCalls: 1 }); - - await withSubscribeBlockTracker( - { - provider: { - stubs: [ - { - methodName: 'eth_subscribe', - response: { - error: 'boom', - }, - }, - ], - }, - }, - async ({ blockTracker }) => { - const promiseForCaughtError = new Promise((resolve) => { - blockTracker[methodToAddListener]('error', resolve); - }); - - const sync = await new Promise((resolve) => { - blockTracker[methodToAddListener]('sync', resolve); - }); - - await expect(promiseForCaughtError).toNeverResolve(); - expect(sync).toStrictEqual({ oldBlock: null, newBlock: '0x0' }); - }, - ); - }); - it(`should emit the "error" event (added via \`${methodToAddListener}\`) and should not emit "sync" if, while making the request to subscribe, the provider throws an Error`, async () => { recordCallsToSetTimeout({ numAutomaticCalls: 1 }); const thrownError = new Error('boom'); @@ -3611,9 +3117,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -3656,9 +3160,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, @@ -3714,9 +3216,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }, ], }, diff --git a/src/SubscribeBlockTracker.ts b/src/SubscribeBlockTracker.ts index 3c46ba8f..d81a8ea9 100644 --- a/src/SubscribeBlockTracker.ts +++ b/src/SubscribeBlockTracker.ts @@ -1,10 +1,6 @@ import type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider'; import SafeEventEmitter from '@metamask/safe-event-emitter'; -import type { - Json, - JsonRpcNotification, - JsonRpcSuccess, -} from '@metamask/utils'; +import type { Json, JsonRpcNotification } from '@metamask/utils'; import getCreateRandomId from 'json-rpc-random-id'; import type { BlockTracker } from './BlockTracker'; @@ -256,14 +252,12 @@ export class SubscribeBlockTracker } private async _call(method: string, ...params: Json[]): Promise { - const res = (await this._provider.request({ + return this._provider.request({ id: createRandomId(), method, params, jsonrpc: '2.0', - })) as JsonRpcSuccess; - - return res.result; + }); } private _handleSubData( diff --git a/tests/withBlockTracker.ts b/tests/withBlockTracker.ts index f6c31ac4..a5d32edb 100644 --- a/tests/withBlockTracker.ts +++ b/tests/withBlockTracker.ts @@ -4,8 +4,7 @@ import type { SafeEventEmitterProvider, } from '@metamask/eth-json-rpc-provider'; import { JsonRpcEngine } from '@metamask/json-rpc-engine'; -import type { Json, JsonRpcResponse } from '@metamask/utils'; -// import { request } from 'http'; +import type { Json } from '@metamask/utils'; import util from 'util'; import type { @@ -56,7 +55,7 @@ type WithSubscribeBlockTrackerCallback = (args: { type FakeProviderStub = | { methodName: string; - response: { result: any } | { error: string }; + response: any; } | { methodName: string; @@ -97,80 +96,53 @@ function getFakeProvider({ if (!stubs.some((stub) => stub.methodName === 'eth_blockNumber')) { stubs.push({ methodName: 'eth_blockNumber', - response: { - result: '0x0', - }, + response: '0x0', }); } if (!stubs.some((stub) => stub.methodName === 'eth_subscribe')) { stubs.push({ methodName: 'eth_subscribe', - response: { - result: '0x0', - }, + response: '0x0', }); } if (!stubs.some((stub) => stub.methodName === 'eth_unsubscribe')) { stubs.push({ methodName: 'eth_unsubscribe', - response: { - result: true, - }, + response: true, }); } const provider = providerFromEngine(new JsonRpcEngine()); jest .spyOn(provider, 'request') - .mockImplementation( - async (eip1193Request): Promise> => { - const index = stubs.findIndex( - (stub) => stub.methodName === eip1193Request.method, - ); - - if (index !== -1) { - const stub = stubs[index]; - stubs.splice(index, 1); - if ('implementation' in stub) { - stub.implementation(); - } else if ('response' in stub) { - if ('result' in stub.response) { - return { - jsonrpc: '2.0', - id: 1, - result: stub.response.result, - } as JsonRpcResponse; - } else if ('error' in stub.response) { - return { - jsonrpc: '2.0', - id: 1, - error: { - code: -999, - message: stub.response.error, - }, - } as JsonRpcResponse; - } - } else if ('error' in stub) { - throw new Error(stub.error); - } - return { - jsonrpc: '2.0', - id: 1, - result: null, - } as JsonRpcResponse; + .mockImplementation(async (eip1193Request): Promise => { + const index = stubs.findIndex( + (stub) => stub.methodName === eip1193Request.method, + ); + + if (index !== -1) { + const stub = stubs[index]; + stubs.splice(index, 1); + if ('implementation' in stub) { + stub.implementation(); + } else if ('response' in stub) { + return stub.response; + } else if ('error' in stub) { + throw new Error(stub.error); } - - throw new Error( - `Could not find any stubs matching "${eip1193Request.method}". Perhaps they've already been called?\n\n` + - 'The original set of stubs were:\n\n' + - `${util.inspect(originalStubs, { depth: null })}\n\n` + - 'Current set of stubs:\n\n' + - `${util.inspect(stubs, { depth: null })}\n\n`, - ); - }, - ); + return null; + } + + throw new Error( + `Could not find any stubs matching "${eip1193Request.method}". Perhaps they've already been called?\n\n` + + 'The original set of stubs were:\n\n' + + `${util.inspect(originalStubs, { depth: null })}\n\n` + + 'Current set of stubs:\n\n' + + `${util.inspect(stubs, { depth: null })}\n\n`, + ); + }); return provider; } From 5b41ce9511e77c356959bd68623b65dc708dd52d Mon Sep 17 00:00:00 2001 From: Kanthesha Devaramane Date: Wed, 10 Jul 2024 18:23:06 +0100 Subject: [PATCH 3/6] bumped eth-json-rpc-provider --- package.json | 2 +- yarn.lock | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index a110e937..b6ebeed3 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "test:watch": "jest --watch" }, "dependencies": { - "@metamask/eth-json-rpc-provider": "^4.0.0", + "@metamask/eth-json-rpc-provider": "^4.1.0", "@metamask/safe-event-emitter": "^3.0.0", "@metamask/utils": "^8.1.0", "json-rpc-random-id": "^1.0.1", diff --git a/yarn.lock b/yarn.lock index f8a93d35..4f8134c1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -700,14 +700,16 @@ resolved "https://registry.yarnpkg.com/@metamask/eslint-config/-/eslint-config-12.2.0.tgz#6cefc8331e4a34d26ae951882437371ecfe4e3c4" integrity sha512-BurYsht8MKdhvW2itUPPF8NkAhYtDdsCGHTSY7EzVvlmGP4jc9XrRZyfNwlt0zhB6MCMjHB1uNWwchtX7vBFjw== -"@metamask/eth-json-rpc-provider@^4.0.0": - version "4.0.0" - resolved "https://registry.yarnpkg.com/@metamask/eth-json-rpc-provider/-/eth-json-rpc-provider-4.0.0.tgz#0ab54548fceda1829f313f2f8ec072ef91cce687" - integrity sha512-HB/I5eNsS67rE5C+px5zASyEuAoK/UFeWe4c4FIm2U4zMo7Y2EED1p10A4zHRHjpctObHdvNDcZQbfu2gHcqsQ== +"@metamask/eth-json-rpc-provider@^4.1.0": + version "4.1.0" + resolved "https://registry.yarnpkg.com/@metamask/eth-json-rpc-provider/-/eth-json-rpc-provider-4.1.0.tgz#4191de86c9ace77035135522c42a67a65ab366d0" + integrity sha512-c8aVSZbtJBqFeQFN/4uQz5DsJFsjcwyITXwEOnuqJ1B0dQWu3EWE/VtKjyIMJDmfyvbU2Ihi7SXwRcgCkALI6A== dependencies: "@metamask/json-rpc-engine" "^9.0.0" + "@metamask/rpc-errors" "^6.2.1" "@metamask/safe-event-emitter" "^3.0.0" "@metamask/utils" "^8.3.0" + uuid "^8.3.2" "@metamask/json-rpc-engine@^9.0.0": version "9.0.0" @@ -4197,6 +4199,11 @@ util-deprecate@^1.0.1: resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf" integrity sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw== +uuid@^8.3.2: + version "8.3.2" + resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2" + integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg== + uuid@^9.0.1: version "9.0.1" resolved "https://registry.yarnpkg.com/uuid/-/uuid-9.0.1.tgz#e188d4c8853cc722220392c424cd637f32293f30" From fc2b1c443f3ab9b5c2d28deff4887394364aff4f Mon Sep 17 00:00:00 2001 From: Kanthesha Devaramane Date: Thu, 11 Jul 2024 14:48:25 +0100 Subject: [PATCH 4/6] response renamed to result, retain result logging --- src/PollingBlockTracker.test.ts | 196 +++++++++++++++--------------- src/PollingBlockTracker.ts | 4 +- src/SubscribeBlockTracker.test.ts | 116 +++++++++--------- tests/withBlockTracker.ts | 12 +- 4 files changed, 165 insertions(+), 163 deletions(-) diff --git a/src/PollingBlockTracker.test.ts b/src/PollingBlockTracker.test.ts index db16fbb4..7a598db2 100644 --- a/src/PollingBlockTracker.test.ts +++ b/src/PollingBlockTracker.test.ts @@ -66,7 +66,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -115,7 +115,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -173,7 +173,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -233,11 +233,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_blockNumber', - response: '0x1', + result: '0x1', }, ], }, @@ -280,7 +280,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -317,7 +317,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -352,7 +352,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -487,7 +487,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -513,7 +513,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -547,11 +547,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_blockNumber', - response: '0x1', + result: '0x1', }, ], }, @@ -584,7 +584,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -605,7 +605,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -653,7 +653,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -678,7 +678,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -711,11 +711,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_blockNumber', - response: '0x1', + result: '0x1', }, ], }, @@ -739,11 +739,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_blockNumber', - response: '0x1', + result: '0x1', }, ], }, @@ -767,11 +767,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x1', + result: '0x1', }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -795,11 +795,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x1', + result: '0x1', }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -826,11 +826,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_blockNumber', - response: '0x1', + result: '0x1', }, ], }, @@ -854,11 +854,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_blockNumber', - response: '0x1', + result: '0x1', }, ], }, @@ -882,11 +882,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x1', + result: '0x1', }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -910,11 +910,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x1', + result: '0x1', }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -953,7 +953,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -976,7 +976,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -1005,11 +1005,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_blockNumber', - response: '0x1', + result: '0x1', }, ], }, @@ -1099,7 +1099,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -1138,7 +1138,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -1175,7 +1175,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -1214,7 +1214,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -1253,7 +1253,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -1290,7 +1290,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -1331,11 +1331,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_blockNumber', - response: '0x1', + result: '0x1', }, ], }, @@ -1371,11 +1371,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x1', + result: '0x1', }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -1411,11 +1411,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -1454,11 +1454,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_blockNumber', - response: '0x1', + result: '0x1', }, ], }, @@ -1494,11 +1494,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x1', + result: '0x1', }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -1534,11 +1534,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -1585,7 +1585,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -1608,7 +1608,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -1637,11 +1637,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_blockNumber', - response: '0x1', + result: '0x1', }, ], }, @@ -1731,7 +1731,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -1770,7 +1770,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -1807,7 +1807,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -1951,11 +1951,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_blockNumber', - response: '0x1', + result: '0x1', }, ], }, @@ -1991,11 +1991,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x1', + result: '0x1', }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -2030,11 +2030,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -2072,11 +2072,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_blockNumber', - response: '0x1', + result: '0x1', }, ], }, @@ -2112,11 +2112,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x1', + result: '0x1', }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -2152,11 +2152,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -2202,7 +2202,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -2257,7 +2257,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -2303,15 +2303,15 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_blockNumber', - response: '0x1', + result: '0x1', }, { methodName: 'eth_blockNumber', - response: '0x2', + result: '0x2', }, ], }, @@ -2378,7 +2378,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -2467,11 +2467,11 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_blockNumber', - response: '0x1', + result: '0x1', }, ], }, @@ -2508,7 +2508,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -2548,7 +2548,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -2587,7 +2587,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -2624,7 +2624,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -2664,7 +2664,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -2703,7 +2703,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -2740,7 +2740,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -2795,7 +2795,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -2835,7 +2835,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -2874,7 +2874,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -2911,7 +2911,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -2951,7 +2951,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -2990,7 +2990,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -3027,7 +3027,7 @@ describe('PollingBlockTracker', () => { }, { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -3077,7 +3077,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -3120,7 +3120,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -3177,7 +3177,7 @@ describe('PollingBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, diff --git a/src/PollingBlockTracker.ts b/src/PollingBlockTracker.ts index 3e7b231c..7f5d044a 100644 --- a/src/PollingBlockTracker.ts +++ b/src/PollingBlockTracker.ts @@ -271,7 +271,9 @@ export class PollingBlockTracker } log('Making request', req); - return this._provider.request(req); + const result = await this._provider.request(req); + log('Got result', result); + return result as unknown as string; } /** diff --git a/src/SubscribeBlockTracker.test.ts b/src/SubscribeBlockTracker.test.ts index a3af6d2c..6d78f230 100644 --- a/src/SubscribeBlockTracker.test.ts +++ b/src/SubscribeBlockTracker.test.ts @@ -70,7 +70,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -104,7 +104,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -160,7 +160,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -203,27 +203,27 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_subscribe', - response: '0x0', + result: '0x0', }, { methodName: 'eth_unsubscribe', - response: true, + result: true, }, { methodName: 'eth_blockNumber', - response: '0x1', + result: '0x1', }, { methodName: 'eth_subscribe', - response: '0x1', + result: '0x1', }, { methodName: 'eth_unsubscribe', - response: true, + result: true, }, ], }, @@ -534,7 +534,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -560,7 +560,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -607,7 +607,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -630,7 +630,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -654,11 +654,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_subscribe', - response: '0x64', + result: '0x64', }, ], }, @@ -702,11 +702,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_subscribe', - response: '0x64', + result: '0x64', }, ], }, @@ -840,7 +840,7 @@ describe('SubscribeBlockTracker', () => { }, { methodName: 'eth_subscribe', - response: '0x64', + result: '0x64', }, ], }, @@ -970,11 +970,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_subscribe', - response: '0x64', + result: '0x64', }, ], }, @@ -1020,11 +1020,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x1', + result: '0x1', }, { methodName: 'eth_subscribe', - response: '0x64', + result: '0x64', }, ], }, @@ -1069,11 +1069,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_subscribe', - response: '0x64', + result: '0x64', }, ], }, @@ -1121,11 +1121,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_subscribe', - response: '0x64', + result: '0x64', }, ], }, @@ -1171,11 +1171,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x1', + result: '0x1', }, { methodName: 'eth_subscribe', - response: '0x64', + result: '0x64', }, ], }, @@ -1220,11 +1220,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_subscribe', - response: '0x64', + result: '0x64', }, ], }, @@ -1282,7 +1282,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -1305,7 +1305,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -1329,11 +1329,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_subscribe', - response: '0x64', + result: '0x64', }, ], }, @@ -1376,11 +1376,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_subscribe', - response: '0x64', + result: '0x64', }, ], }, @@ -1639,11 +1639,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_subscribe', - response: '0x64', + result: '0x64', }, ], }, @@ -1689,11 +1689,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x1', + result: '0x1', }, { methodName: 'eth_subscribe', - response: '0x64', + result: '0x64', }, ], }, @@ -1737,11 +1737,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_subscribe', - response: '0x64', + result: '0x64', }, ], }, @@ -1788,11 +1788,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_subscribe', - response: '0x64', + result: '0x64', }, ], }, @@ -1838,11 +1838,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x1', + result: '0x1', }, { methodName: 'eth_subscribe', - response: '0x64', + result: '0x64', }, ], }, @@ -1887,11 +1887,11 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, { methodName: 'eth_subscribe', - response: '0x64', + result: '0x64', }, ], }, @@ -1948,7 +1948,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -2003,7 +2003,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -2044,7 +2044,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_unsubscribe', - response: { + result: { error: 'boom', }, }, @@ -2212,7 +2212,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -2253,7 +2253,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_unsubscribe', - response: { + result: { error: 'boom', }, }, @@ -2442,7 +2442,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -2782,7 +2782,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -3117,7 +3117,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -3160,7 +3160,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, @@ -3216,7 +3216,7 @@ describe('SubscribeBlockTracker', () => { stubs: [ { methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }, ], }, diff --git a/tests/withBlockTracker.ts b/tests/withBlockTracker.ts index a5d32edb..5248866b 100644 --- a/tests/withBlockTracker.ts +++ b/tests/withBlockTracker.ts @@ -55,7 +55,7 @@ type WithSubscribeBlockTrackerCallback = (args: { type FakeProviderStub = | { methodName: string; - response: any; + result: any; } | { methodName: string; @@ -96,21 +96,21 @@ function getFakeProvider({ if (!stubs.some((stub) => stub.methodName === 'eth_blockNumber')) { stubs.push({ methodName: 'eth_blockNumber', - response: '0x0', + result: '0x0', }); } if (!stubs.some((stub) => stub.methodName === 'eth_subscribe')) { stubs.push({ methodName: 'eth_subscribe', - response: '0x0', + result: '0x0', }); } if (!stubs.some((stub) => stub.methodName === 'eth_unsubscribe')) { stubs.push({ methodName: 'eth_unsubscribe', - response: true, + result: true, }); } @@ -127,8 +127,8 @@ function getFakeProvider({ stubs.splice(index, 1); if ('implementation' in stub) { stub.implementation(); - } else if ('response' in stub) { - return stub.response; + } else if ('result' in stub) { + return stub.result; } else if ('error' in stub) { throw new Error(stub.error); } From 59dca3fdf7fc1d9a9c54f403e9e835a2a68f85f8 Mon Sep 17 00:00:00 2001 From: Kanthesha Devaramane Date: Thu, 11 Jul 2024 22:28:26 +0100 Subject: [PATCH 5/6] include try catch --- src/PollingBlockTracker.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/PollingBlockTracker.ts b/src/PollingBlockTracker.ts index 7f5d044a..8ab89b99 100644 --- a/src/PollingBlockTracker.ts +++ b/src/PollingBlockTracker.ts @@ -1,6 +1,6 @@ import type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider'; import SafeEventEmitter from '@metamask/safe-event-emitter'; -import type { JsonRpcRequest } from '@metamask/utils'; +import { getErrorMessage, type JsonRpcRequest } from '@metamask/utils'; import getCreateRandomId from 'json-rpc-random-id'; import type { BlockTracker } from './BlockTracker'; @@ -271,9 +271,14 @@ export class PollingBlockTracker } log('Making request', req); - const result = await this._provider.request(req); - log('Got result', result); - return result as unknown as string; + try { + const result = await this._provider.request<[], string>(req); + log('Got result', result); + return result; + } catch (error) { + log('Encountered error fetching block', getErrorMessage(error)); + throw error; + } } /** From 5f47e865bec3efdd3153622afc75ab7c6db72e0f Mon Sep 17 00:00:00 2001 From: Kanthesha Devaramane Date: Fri, 12 Jul 2024 10:41:45 +0100 Subject: [PATCH 6/6] update the jsdoc of type FakeProviderStub --- tests/withBlockTracker.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/tests/withBlockTracker.ts b/tests/withBlockTracker.ts index 5248866b..1e945e80 100644 --- a/tests/withBlockTracker.ts +++ b/tests/withBlockTracker.ts @@ -36,17 +36,11 @@ type WithSubscribeBlockTrackerCallback = (args: { /** * An object that allows specifying the behavior of a specific invocation of * `request`. The `methodName` always identifies the stub, but the behavior - * may be specified multiple ways: `request` can either return a promise or - * throw an error, and if it returns a promise, that promise can either be - * resolved with a response object or reject with an error. + * may be specified multiple ways: `request` can either return a result + * or reject with an error. * * @property methodName - The RPC method to which this stub will be matched. - * @property response - Instructs `request` to return a promise that resolves - * with a response object. - * @property response.result - Specifies a successful response, with this as the - * `result`. - * @property response.error - Specifies an error response, with this as the - * `error`. + * @property result - Instructs `request` to return a result. * @property implementation - Allows overriding `request` entirely. Useful if * you want it to throw an error. * @property error - Instructs `request` to return a promise that rejects with