Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adapt to eip-1193 provider changes #317

Merged
merged 17 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"dependencies": {
"@metamask/eth-block-tracker": "^10.0.0",
"@metamask/eth-json-rpc-provider": "^4.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();
});
});
32 changes: 8 additions & 24 deletions src/block-ref.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,7 @@ describe('createBlockRefMiddleware', () => {
'0x100',
),
},
response: (req) => ({
id: req.id,
jsonrpc: '2.0' as const,
result: 'something',
}),
response: () => 'something',
}),
]);

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

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

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

Expand Down Expand Up @@ -303,13 +287,13 @@ describe('createBlockRefMiddleware', () => {
blockParam,
),
};
const sendAsyncSpy = stubProviderRequests(provider, [
const requestSpy = stubProviderRequests(provider, [
buildStubForBlockNumberRequest('0x100'),
]);

await engine.handle(request);

expectProviderRequestNotToHaveBeenMade(sendAsyncSpy, request);
expectProviderRequestNotToHaveBeenMade(requestSpy, request);
},
);
});
Expand Down Expand Up @@ -387,13 +371,13 @@ 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);
},
);
});
Expand Down
20 changes: 9 additions & 11 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,15 @@ 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;
cryptodev-2s marked this conversation as resolved.
Show resolved Hide resolved
try {
const childResult = await provider.request<JsonRpcParams, Block>(
childRequest,
);
res.result = childResult;
} catch (error: any) {
cryptodev-2s marked this conversation as resolved.
Show resolved Hide resolved
res.error = error;
}

return undefined;
});
Expand Down
22 changes: 9 additions & 13 deletions src/providerAsMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@ export function providerAsMiddleware(
provider: SafeEventEmitterProvider,
): JsonRpcMiddleware<JsonRpcParams, Json> {
return (req, res, _next, end) => {
cryptodev-2s marked this conversation as resolved.
Show resolved Hide resolved
// send request to provider
provider.sendAsync(
req,
(err: unknown, providerRes: PendingJsonRpcResponse<any>) => {
// forward any error
if (err instanceof Error) {
cryptodev-2s marked this conversation as resolved.
Show resolved Hide resolved
return end(err);
}
// copy provider response onto original response
Object.assign(res, providerRes);
return end();
},
);
provider
.request(req)
.then((result) => {
res.result = result;
cryptodev-2s marked this conversation as resolved.
Show resolved Hide resolved
end();
})
.catch((error) => {
end(error);
});
};
}

Expand Down
Loading
Loading