Skip to content

Commit

Permalink
feat: make SafeEventEmitterProvider compatible with eth req libraries (
Browse files Browse the repository at this point in the history
…#4422)

## Explanation

- Aligning SafeEventEmitterProvider to EIP-1193 by adding a `request`
method and deprecating `sendAsync` (which aren't part of the spec)
- Ensuring that `request` does not require `id` and `jsonrpc` to be set,
filling them in if they are missing

## References

Fixes #4095 

## Changelog

<!--
If you're making any consumer-facing changes, list those changes here as
if you were updating a changelog, using the template below as a guide.

(CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or
FIXED. For security-related issues, follow the Security Advisory
process.)

Please take care to name the exact pieces of the API you've added or
changed (e.g. types, interfaces, functions, or methods).

If there are any breaking changes, make sure to offer a solution for
consumers to follow once they upgrade to the changes.

Finally, if you're only making changes to development scripts or tests,
you may replace the template below with "None".
-->

### `@metamask/eth-json-rpc-provider`

- **ADDED**: request method type compatible with ethereum request
libraries.
- **CHANGED**: markes sendAsync as `deprecated` for usage in favor of
`request`.

## Checklist

- [ ] I've updated the test suite for new or updated code as appropriate
- [ ] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [ ] I've highlighted breaking changes using the "BREAKING" category
above as appropriate
  • Loading branch information
cryptodev-2s authored Jul 8, 2024
1 parent fa23228 commit bea117b
Show file tree
Hide file tree
Showing 9 changed files with 649 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ export class AssetsContractController extends BaseControllerV1<
throw new Error(MISSING_PROVIDER_ERROR);
}

// @ts-expect-error TODO: remove this annotation once the `Eip1193Provider` class is released
return new Web3Provider(provider);
}

Expand Down
8 changes: 7 additions & 1 deletion packages/eth-json-rpc-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,19 @@
},
"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"
"@metamask/utils": "^8.3.0",
"uuid": "^8.3.2"
},
"devDependencies": {
"@ethersproject/providers": "^5.7.0",
"@metamask/auto-changelog": "^3.4.4",
"@metamask/eth-query": "^4.0.0",
"@metamask/ethjs-query": "^0.5.3",
"@types/jest": "^27.4.1",
"deepmerge": "^4.2.2",
"ethers": "^6.12.0",
"jest": "^27.5.1",
"jest-it-up": "^2.0.2",
"ts-jest": "^27.1.4",
Expand Down
23 changes: 13 additions & 10 deletions packages/eth-json-rpc-provider/src/provider-from-engine.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
import { promisify } from 'util';
import { providerErrors } from '@metamask/rpc-errors';

import { providerFromEngine } from './provider-from-engine';

Expand All @@ -11,33 +11,36 @@ describe('providerFromEngine', () => {
end();
});
const provider = providerFromEngine(engine);
const promisifiedSendAsync = promisify(provider.sendAsync);
const exampleRequest = {
id: 1,
jsonrpc: '2.0' as const,
method: 'test',
};

const response = await promisifiedSendAsync(exampleRequest);
const response = await provider.request(exampleRequest);

expect(response.result).toBe(42);
expect(response).toBe(42);
});

it('handle a failed request', async () => {
const engine = new JsonRpcEngine();
engine.push((_req, _res, _next, _end) => {
throw new Error('Test error');
engine.push((_req, _res, _next, end) => {
end(
providerErrors.custom({
code: 1001,
message: 'Test error',
}),
);
});
const provider = providerFromEngine(engine);
const promisifiedSendAsync = promisify(provider.sendAsync);
const exampleRequest = {
id: 1,
jsonrpc: '2.0' as const,
method: 'test',
};

await expect(async () =>
promisifiedSendAsync(exampleRequest),
).rejects.toThrow('Test error');
await expect(async () => provider.request(exampleRequest)).rejects.toThrow(
'Test error',
);
});
});
26 changes: 14 additions & 12 deletions packages/eth-json-rpc-provider/src/provider-from-middleware.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';
import { promisify } from 'util';
import { providerErrors } from '@metamask/rpc-errors';

import { providerFromMiddleware } from './provider-from-middleware';

Expand All @@ -12,32 +12,34 @@ describe('providerFromMiddleware', () => {
end();
};
const provider = providerFromMiddleware(middleware);
const promisifiedSendAsync = promisify(provider.sendAsync);
const exampleRequest = {
id: 1,
jsonrpc: '2.0' as const,
method: 'test',
};

const response = await promisifiedSendAsync(exampleRequest);
const response = await provider.request(exampleRequest);

expect(response.result).toBe(42);
expect(response).toBe(42);
});

it('handle a failed request', async () => {
const middleware = () => {
throw new Error('Test error');
};
const provider = providerFromMiddleware(middleware);
const promisifiedSendAsync = promisify(provider.sendAsync);
const provider = providerFromMiddleware((_req, _res, _next, end) => {
end(
providerErrors.custom({
code: 1001,
message: 'Test error',
}),
);
});
const exampleRequest = {
id: 1,
jsonrpc: '2.0' as const,
method: 'test',
};

await expect(async () =>
promisifiedSendAsync(exampleRequest),
).rejects.toThrow('Test error');
await expect(async () => provider.request(exampleRequest)).rejects.toThrow(
'Test error',
);
});
});
Loading

0 comments on commit bea117b

Please sign in to comment.