Skip to content

Commit

Permalink
adapt to eip-1193 provider changes (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptodev-2s authored Jul 23, 2024
1 parent 02d7ec0 commit c193b59
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 245 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
"test:watch": "jest --watch"
},
"dependencies": {
"@metamask/eth-block-tracker": "^10.0.0",
"@metamask/eth-json-rpc-provider": "^4.0.0",
"@metamask/eth-block-tracker": "^11.0.0",
"@metamask/eth-json-rpc-provider": "^4.1.0",
"@metamask/eth-sig-util": "^7.0.0",
"@metamask/json-rpc-engine": "^9.0.0",
"@metamask/rpc-errors": "^6.0.0",
Expand Down
8 changes: 2 additions & 6 deletions src/block-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ function createTestSetup() {
describe('block cache', () => {
it('should cache a request and only hit the provider once', async () => {
const { engine, provider, blockTracker } = createTestSetup();
const spy = jest
.spyOn(provider, 'sendAsync')
.mockImplementation((req, cb) => {
cb(undefined, { id: req.id, result: '0x0', jsonrpc: '2.0' });
});
const requestSpy = jest.spyOn(provider, 'request').mockResolvedValue('0x0');
let hitCount = 0;

const hitCountMiddleware = createHitTrackerMiddleware();
Expand Down Expand Up @@ -56,6 +52,6 @@ describe('block cache', () => {
expect(hitCount).toBe(1);
expect(response.result).toBe('0x0');
expect(response2.result).toBe('0x0');
expect(spy).toHaveBeenCalled();
expect(requestSpy).toHaveBeenCalled();
});
});
50 changes: 16 additions & 34 deletions src/block-ref.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
stubProviderRequests,
buildStubForBlockNumberRequest,
buildStubForGenericRequest,
buildFinalMiddlewareWithDefaultResponse,
buildFinalMiddlewareWithDefaultResult,
buildMockParamsWithoutBlockParamAt,
expectProviderRequestNotToHaveBeenMade,
} from '../test/util/helpers';
Expand Down Expand Up @@ -106,11 +106,7 @@ describe('createBlockRefMiddleware', () => {
'0x100',
),
},
response: (req) => ({
id: req.id,
jsonrpc: '2.0' as const,
result: 'something',
}),
result: async () => 'something',
}),
]);

Expand All @@ -120,14 +116,13 @@ describe('createBlockRefMiddleware', () => {
id: 1,
jsonrpc: '2.0',
result: 'something',
error: undefined,
});
},
);
});

it('does not proceed to the next middleware after making a request through the provider', async () => {
const finalMiddleware = buildFinalMiddlewareWithDefaultResponse();
const finalMiddleware = buildFinalMiddlewareWithDefaultResult();

await withTestSetup(
{
Expand Down Expand Up @@ -161,11 +156,7 @@ describe('createBlockRefMiddleware', () => {
'0x100',
),
},
response: (req) => ({
id: req.id,
jsonrpc: '2.0' as const,
result: 'something',
}),
result: async () => 'something',
}),
]);

Expand Down Expand Up @@ -207,11 +198,7 @@ describe('createBlockRefMiddleware', () => {
'0x100',
),
},
response: (req) => ({
id: req.id,
jsonrpc: '2.0' as const,
result: 'something',
}),
result: async () => 'something',
}),
]);

Expand All @@ -221,14 +208,13 @@ describe('createBlockRefMiddleware', () => {
id: 1,
jsonrpc: '2.0',
result: 'something',
error: undefined,
});
},
);
});

it('does not proceed to the next middleware after making a request through the provider', async () => {
const finalMiddleware = buildFinalMiddlewareWithDefaultResponse();
const finalMiddleware = buildFinalMiddlewareWithDefaultResult();

await withTestSetup(
{
Expand Down Expand Up @@ -259,11 +245,7 @@ describe('createBlockRefMiddleware', () => {
'0x100',
),
},
response: (req) => ({
id: req.id,
jsonrpc: '2.0' as const,
result: 'something',
}),
result: async () => 'something',
}),
]);

Expand All @@ -279,7 +261,7 @@ describe('createBlockRefMiddleware', () => {
'if the block param is something other than "latest", like %o',
(blockParam) => {
it('does not make a direct request through the provider', async () => {
const finalMiddleware = buildFinalMiddlewareWithDefaultResponse();
const finalMiddleware = buildFinalMiddlewareWithDefaultResult();

await withTestSetup(
{
Expand All @@ -303,19 +285,19 @@ describe('createBlockRefMiddleware', () => {
blockParam,
),
};
const sendAsyncSpy = stubProviderRequests(provider, [
const requestSpy = stubProviderRequests(provider, [
buildStubForBlockNumberRequest('0x100'),
]);

await engine.handle(request);

expectProviderRequestNotToHaveBeenMade(sendAsyncSpy, request);
expectProviderRequestNotToHaveBeenMade(requestSpy, request);
},
);
});

it('proceeds to the next middleware', async () => {
const finalMiddleware = buildFinalMiddlewareWithDefaultResponse();
const finalMiddleware = buildFinalMiddlewareWithDefaultResult();

await withTestSetup(
{
Expand Down Expand Up @@ -366,7 +348,7 @@ describe('createBlockRefMiddleware', () => {

describe('when the RPC method does not take a block parameter', () => {
it('does not make a direct request through the provider', async () => {
const finalMiddleware = buildFinalMiddlewareWithDefaultResponse();
const finalMiddleware = buildFinalMiddlewareWithDefaultResult();

await withTestSetup(
{
Expand All @@ -387,19 +369,19 @@ describe('createBlockRefMiddleware', () => {
method: 'a_non_block_param_method',
params: ['some value', '0x200'],
};
const sendAsyncSpy = stubProviderRequests(provider, [
const requestSpy = stubProviderRequests(provider, [
buildStubForBlockNumberRequest('0x100'),
]);

await engine.handle(request);

expectProviderRequestNotToHaveBeenMade(sendAsyncSpy, request);
expectProviderRequestNotToHaveBeenMade(requestSpy, request);
},
);
});

it('proceeds to the next middleware', async () => {
const finalMiddleware = buildFinalMiddlewareWithDefaultResponse();
const finalMiddleware = buildFinalMiddlewareWithDefaultResult();

await withTestSetup(
{
Expand Down Expand Up @@ -465,7 +447,7 @@ async function withTestSetup<T>(

const {
middlewareUnderTest,
otherMiddleware = [buildFinalMiddlewareWithDefaultResponse()],
otherMiddleware = [buildFinalMiddlewareWithDefaultResult()],
} = configureMiddleware({ engine, provider, blockTracker });

for (const middleware of [middlewareUnderTest, ...otherMiddleware]) {
Expand Down
15 changes: 3 additions & 12 deletions src/block-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ import type { PollingBlockTracker } from '@metamask/eth-block-tracker';
import type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider';
import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';
import { createAsyncMiddleware } from '@metamask/json-rpc-engine';
import type {
Json,
JsonRpcParams,
PendingJsonRpcResponse,
} from '@metamask/utils';
import type { Json, JsonRpcParams } from '@metamask/utils';
import { klona } from 'klona/full';
import pify from 'pify';

import { projectLogger, createModuleLogger } from './logging-utils';
import type { Block } from './types';
Expand Down Expand Up @@ -68,12 +63,8 @@ export function createBlockRefMiddleware({

// perform child request
log('Performing another request %o', childRequest);
const childRes: PendingJsonRpcResponse<Block> = await pify(
provider.sendAsync,
).call(provider, childRequest);
// copy child response onto original response
res.result = childRes.result;
res.error = childRes.error;
// copy child result onto original response
res.result = await provider.request<JsonRpcParams, Block>(childRequest);

return undefined;
});
Expand Down
23 changes: 7 additions & 16 deletions src/providerAsMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider';
import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';
import {
createAsyncMiddleware,
type JsonRpcMiddleware,
} from '@metamask/json-rpc-engine';
import type {
Json,
JsonRpcParams,
Expand All @@ -9,21 +12,9 @@ import type {
export function providerAsMiddleware(
provider: SafeEventEmitterProvider,
): JsonRpcMiddleware<JsonRpcParams, Json> {
return (req, res, _next, end) => {
// send request to provider
provider.sendAsync(
req,
(err: unknown, providerRes: PendingJsonRpcResponse<any>) => {
// forward any error
if (err instanceof Error) {
return end(err);
}
// copy provider response onto original response
Object.assign(res, providerRes);
return end();
},
);
};
return createAsyncMiddleware(async (req, res) => {
res.result = await provider.request(req);
});
}

export function ethersProviderAsMiddleware(
Expand Down
Loading

0 comments on commit c193b59

Please sign in to comment.