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

Align types with @metamask/eth-json-rpc-provider #21

Merged
merged 1 commit into from
Nov 9, 2023

Conversation

mcmire
Copy link

@mcmire mcmire commented Nov 9, 2023

Currently, passing an instance of SafeEventEmitterProvider to the EthQuery constructor produces a type error. This is happening because the Provider type that this package defines allows the params property of the JSON-RPC request object to be anything, whereas in the SafeEventEmitterProvider interface, it has to be JSON-compatible. In other words, the provider that this package expects isn't compliant with a "proper" provider.

To fix this, this commit manually copies the Json and JsonRpcParams types from @metamask/utils to avoid a direct dependency. This is a temporary solution to prevent this type error from spreading within the core codebase. We will investigate a more overarching solution later.

See: MetaMask/core#1823

Currently, passing an instance of SafeEventEmitterProvider to the
EthQuery constructor produces a type error. This is happening because
the `Provider` type that this package defines allows the `params`
property of the JSON-RPC request object to be anything, whereas in the
SafeEventEmitterProvider interface, it has to be JSON-compatible.
In other words, the provider that this package expects isn't compliant
with a "proper" provider.

To fix this, this commit manually copies the `Json` and `JsonRpcParams`
types from `@metamask/utils` to avoid a direct dependency. This is a
temporary solution to prevent this type error from spreading within
the `core` codebase. We will investigate a more overarching solution
later.
@mcmire mcmire requested a review from a team as a code owner November 9, 2023 20:52
Copy link

@MajorLift MajorLift left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a good temporary solution!

@mcmire mcmire merged commit 4f1dc24 into main Nov 9, 2023
11 checks passed
@mcmire mcmire deleted the align-with-eth-json-rpc-provider branch November 9, 2023 21:51
MajorLift added a commit to MetaMask/core that referenced this pull request Nov 15, 2023
## Explanation

Fixes `// @ts-expect-error Mock eth query does not fulfill type
requirements` annotations throughout core repo.

## References

- Builds upon
  - MetaMask/eth-query#21
  - #2028
- Related #1823
- Closes #2036

## Changelog

N/A

## 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
MajorLift added a commit to MetaMask/ppom-validator that referenced this pull request Feb 15, 2024
## Explanation

Reduces repo-wide `any` count to 1 (excluding test files).

- Types `provider` variables using the `Provider` type from `@metamask/network-controller`.
  - The `Provider` type is now safe to use, with its recent alignment issues resolved as of MetaMask/eth-query#21.
  - See also MetaMask/core#2028).
- Fixes JSON-RPC typing for provider requests.
  - Installs `@metamask/utils` as a dependency.
- Types `PPOM` variables with provisional skeleton type. 
- Fixes for controller-messenger typing.
- Fixes for misc. `any` usage

## References

- Closes #144

## Changelog

### Changed
- Add `@metamask/utils` ^8.3.0 as a dependency. ([#89](#89))

### Removed
- **BREAKING:** `NetworkControllerStateChangeEvent` is removed from the `PPOMControllerEvents` union type. ([#89](#89))

### Fixed
- **BREAKING**: `PPOMController` class constructor option types are narrowed from `any`. ([#89](#89))
  - The constructor expects `provider` to be the `Provider` type from `@metamask/network-controller` (equivalent to `SafeEventEmitterProvider` from `@metamask/eth-json-rpc-provider`).
  - The constructor expects `onPreferencesChange` to be `(callback: (preferencesState: { securityAlertsEnabled: boolean } & Record<string, Json>) => void) => void`.
- **BREAKING:** The `UsePPOM` type replaces `any` with the `PPOM` type in its definition. ([#89](#89))
- **BREAKING:** When a `PPOM` class instance makes JSON-RPC requests to the provider, the `params` type is now widened from `Record<string, unknown>` to `JsonRpcParams`, and the response type is narrowed from `any` to `JsonRpcSuccess<Json>`. ([#89](#89))

## Commits

* Linter fixes

* Install `@metamask/utils` as dependency

* Add entries to depcheck ignore to fix yarn depcheck error

* Fix JSON RPC types for provider requests

* Fix controller-messenger typing

* Explicitly enumerate package-level exports from `ppom-controller`

* Type `provider` variables with `SafeEventEmitterProvider` from network-controller

* Type `ppom` variables with provisional skeleton type

* Fix various `any` usage and typos

* Add TODO for fixing `any`

* Improve error response typing for `#jsonRpcRequest`

* Replace null check for `oldestChainId` with non-null assertion

* Replace null check for `this.#ppom` with non-null assertion

* Replace unnecessary null fallback with non-null assertion

* Linter fix

* Record CHANGELOG

* Remove changelog entries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants