Skip to content

Commit

Permalink
deps(approval-controller): move to @metamask/rpc-errors (#1743)
Browse files Browse the repository at this point in the history
## Explanation

This replaces obsolete `eth-rpc-errors` with `@metamask/rpc-errors` in
`@metamask/approval-controller`. This should be coupled with #1639 and
can be merged before or after.

## References

#### Broken out from
- #1731 

#### Blocking
- #1724 

#### Related
- #1690

## Changelog


### `@metamask/approval-controller`

- **Changed**: Replaced `eth-rpc-errors` with `@metamask/rpc-errors`

### `@metamask/controller-utils`

- **Fixed**: Removed unused dependency `eth-rpc-errors`

## Checklist

- [x] I've updated the test suite for new or updated code as appropriate
- [x] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [x] I've highlighted breaking changes using the "BREAKING" category
above as appropriate
  • Loading branch information
legobeat authored and MajorLift committed Oct 11, 2023
1 parent 699f94f commit f9085d8
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/approval-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
},
"dependencies": {
"@metamask/base-controller": "^3.2.2",
"@metamask/rpc-errors": "^6.0.0",
"@metamask/utils": "^8.1.0",
"eth-rpc-errors": "^4.0.2",
"immer": "^9.0.6",
"nanoid": "^3.1.31"
},
Expand Down
16 changes: 8 additions & 8 deletions packages/approval-controller/src/ApprovalController.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable jest/expect-expect */

import { ControllerMessenger } from '@metamask/base-controller';
import { errorCodes, EthereumRpcError } from 'eth-rpc-errors';
import { errorCodes, JsonRpcError } from '@metamask/rpc-errors';

import type {
ApprovalControllerActions,
Expand Down Expand Up @@ -652,7 +652,7 @@ describe('approval controller', () => {
approvalController.reject('2', new Error('foo'));
expect(approvalController.getTotalApprovalCount()).toBe(2);

approvalController.clear(new EthereumRpcError(1, 'clear'));
approvalController.clear(new JsonRpcError(1, 'clear'));
expect(approvalController.getTotalApprovalCount()).toBe(0);
});

Expand All @@ -677,7 +677,7 @@ describe('approval controller', () => {
approvalController.reject('2', new Error('foo'));
expect(approvalController.getTotalApprovalCount()).toBe(1);

approvalController.clear(new EthereumRpcError(1, 'clear'));
approvalController.clear(new JsonRpcError(1, 'clear'));
expect(approvalController.getTotalApprovalCount()).toBe(0);
});
});
Expand Down Expand Up @@ -1042,7 +1042,7 @@ describe('approval controller', () => {
describe('clear', () => {
it('does nothing if state is already empty', () => {
expect(() =>
approvalController.clear(new EthereumRpcError(1, 'clear')),
approvalController.clear(new JsonRpcError(1, 'clear')),
).not.toThrow();
});

Expand All @@ -1057,7 +1057,7 @@ describe('approval controller', () => {
.add({ id: 'foo3', origin: 'fizz.buzz', type: 'myType' })
.catch((_error) => undefined);

approvalController.clear(new EthereumRpcError(1, 'clear'));
approvalController.clear(new JsonRpcError(1, 'clear'));

expect(
approvalController.state[PENDING_APPROVALS_STORE_KEY],
Expand All @@ -1072,16 +1072,16 @@ describe('approval controller', () => {
type: 'myType',
});

approvalController.clear(new EthereumRpcError(1000, 'foo'));
approvalController.clear(new JsonRpcError(1000, 'foo'));
await expect(rejectPromise).rejects.toThrow(
new EthereumRpcError(1000, 'foo'),
new JsonRpcError(1000, 'foo'),
);
});

it('does not clear approval flows', async () => {
approvalController.startFlow();

approvalController.clear(new EthereumRpcError(1, 'clear'));
approvalController.clear(new JsonRpcError(1, 'clear'));

expect(approvalController.state[APPROVAL_FLOWS_STORE_KEY]).toHaveLength(
1,
Expand Down
14 changes: 7 additions & 7 deletions packages/approval-controller/src/ApprovalController.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { RestrictedControllerMessenger } from '@metamask/base-controller';
import { BaseControllerV2 } from '@metamask/base-controller';
import type { JsonRpcError, DataWithOptionalCause } from '@metamask/rpc-errors';
import { rpcErrors } from '@metamask/rpc-errors';
import type { Json, OptionalField } from '@metamask/utils';
import type { EthereumRpcError } from 'eth-rpc-errors';
import { ethErrors } from 'eth-rpc-errors';
import type { Patch } from 'immer';
import { nanoid } from 'nanoid';

Expand Down Expand Up @@ -256,7 +256,7 @@ export type GetApprovalsState = {

export type ClearApprovalRequests = {
type: `${typeof controllerName}:clearRequests`;
handler: (error: EthereumRpcError<unknown>) => void;
handler: (error: JsonRpcError<DataWithOptionalCause>) => void;
};

export type AddApprovalRequest = {
Expand Down Expand Up @@ -719,10 +719,10 @@ export class ApprovalController extends BaseControllerV2<
/**
* Rejects and deletes all approval requests.
*
* @param rejectionError - The EthereumRpcError to reject the approval
* @param rejectionError - The JsonRpcError to reject the approval
* requests with.
*/
clear(rejectionError: EthereumRpcError<unknown>): void {
clear(rejectionError: JsonRpcError<DataWithOptionalCause>): void {
for (const id of this.#approvals.keys()) {
this.reject(id, rejectionError);
}
Expand Down Expand Up @@ -878,7 +878,7 @@ export class ApprovalController extends BaseControllerV2<
!this.#typesExcludedFromRateLimiting.includes(type) &&
this.has({ origin, type })
) {
throw ethErrors.rpc.resourceUnavailable(
throw rpcErrors.resourceUnavailable(
getAlreadyPendingMessage(origin, type),
);
}
Expand Down Expand Up @@ -937,7 +937,7 @@ export class ApprovalController extends BaseControllerV2<
}

if (errorMessage) {
throw ethErrors.rpc.internal(errorMessage);
throw rpcErrors.internal(errorMessage);
}
}

Expand Down
1 change: 0 additions & 1 deletion packages/controller-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"@metamask/utils": "^8.1.0",
"@spruceid/siwe-parser": "1.1.3",
"eth-ens-namehash": "^2.0.8",
"eth-rpc-errors": "^4.0.2",
"ethereumjs-util": "^7.0.10",
"ethjs-unit": "^0.1.6",
"fast-deep-equal": "^3.1.3"
Expand Down
3 changes: 1 addition & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1369,10 +1369,10 @@ __metadata:
dependencies:
"@metamask/auto-changelog": ^3.1.0
"@metamask/base-controller": ^3.2.2
"@metamask/rpc-errors": ^6.0.0
"@metamask/utils": ^8.1.0
"@types/jest": ^27.4.1
deepmerge: ^4.2.2
eth-rpc-errors: ^4.0.2
immer: ^9.0.6
jest: ^27.5.1
nanoid: ^3.1.31
Expand Down Expand Up @@ -1506,7 +1506,6 @@ __metadata:
"@types/jest": ^27.4.1
deepmerge: ^4.2.2
eth-ens-namehash: ^2.0.8
eth-rpc-errors: ^4.0.2
ethereumjs-util: ^7.0.10
ethjs-unit: ^0.1.6
fast-deep-equal: ^3.1.3
Expand Down

0 comments on commit f9085d8

Please sign in to comment.