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

Fix provider type errors in NetworkController and its downstream packages #1823

Closed
MajorLift opened this issue Oct 11, 2023 · 3 comments · Fixed by #2028
Closed

Fix provider type errors in NetworkController and its downstream packages #1823

MajorLift opened this issue Oct 11, 2023 · 3 comments · Fixed by #2028
Assignees
Labels
bug Something isn't working team-wallet-framework

Comments

@MajorLift
Copy link
Contributor

MajorLift commented Oct 11, 2023

Explanation

Tasks

Provider type alignment

  • ProxyWithAccessibleTarget<SafeEventEmitterProvider> (@metamask/network-controller) vs. Provider (@metamask/eth-query)
 error TS2345: Argument of type 'ProxyWithAccessibleTarget<SafeEventEmitterProvider>' is not assignable to parameter of type 'Provider<JsonRpcParams, Json>'.
  Types of property 'sendAsync' are incompatible.
    Type '(req: JsonRpcRequest<JsonRpcParams>, callback: (error: unknown, providerRes?: any) => void) => void' is not assignable to type '<Params, Result>(payload: SendAsyncPayload<Params>, callback: ProviderSendAsyncCallback<Result>) => void'.
      Types of parameters 'req' and 'payload' are incompatible.
        Type 'SendAsyncPayload<Params>' is not assignable to type 'JsonRpcRequest<JsonRpcParams>'.
  • SafeEventEmitterProvider
  • ProviderProxy
  • FakeProvider

References

@MajorLift MajorLift self-assigned this Oct 11, 2023
@MajorLift MajorLift added bug Something isn't working team-wallet-framework labels Oct 11, 2023
@MajorLift MajorLift changed the title Fix provider, middleware type errors in NetworkController and downstream packages Fix provider type errors in NetworkController and its downstream packages Nov 8, 2023
@mcmire mcmire self-assigned this Nov 9, 2023
MajorLift added a commit that referenced this issue 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 MajorLift reopened this Nov 20, 2023
@mcmire
Copy link
Contributor

mcmire commented Nov 20, 2023

What's left on this ticket?

@MajorLift
Copy link
Contributor Author

MajorLift commented Nov 20, 2023

@mcmire I'm still seeing the following error in this PR MetaMask/smart-transactions-controller#237, even after updating @metamask/eth-query and @metamask/network-controller to the latest versions.

src/SmartTransactionsController.ts:334:31 - error TS2345: Argument of type 'ProxyWithAccessibleTarget<SafeEventEmitterProvider>' is not assignable to parameter of type 'Provider'.
  Types of property 'sendAsync' are incompatible.
    Type '(req: JsonRpcRequest<JsonRpcParams>, callback: (error: unknown, providerRes?: any) => void) => void' is not assignable to type '<Params, Result>(payload: SendAsyncPayload<Params>, callback: ProviderSendAsyncCallback<Result>) => void'.
      Types of parameters 'req' and 'payload' are incompatible.
        Type 'SendAsyncPayload<Params>' is not assignable to type 'JsonRpcRequest<JsonRpcParams>'.
          Type 'SendAsyncPayload<Params>' is not assignable to type '{ params?: ((Record<string, Json> | Json[]) & ExactOptionalGuard) | undefined; id: string | number | null; method: string; jsonrpc: "2.0"; }'.
            Types of property 'params' are incompatible.
              Type 'Params' is not assignable to type '((Record<string, Json> | Json[]) & ExactOptionalGuard) | undefined'.
                Type 'Params' is not assignable to type 'Json[] & ExactOptionalGuard'.
                  Type 'Params' is not assignable to type 'Json[]'.

334       ethQuery = new EthQuery(networkClient.provider);

I think we need to make the following updates to SafeEventEmitterProvider in @metamask/eth-json-rpc-provider to make it align with the @metamask/utils updates.

diff --git a/packages/eth-json-rpc-provider/src/safe-event-emitter-provider.ts b/packages/eth-json-rpc-provider/src/safe-event-emitter-provider.ts
index fbf2db3ae..db9298cf0 100644
--- a/packages/eth-json-rpc-provider/src/safe-event-emitter-provider.ts
+++ b/packages/eth-json-rpc-provider/src/safe-event-emitter-provider.ts
@@ -1,6 +1,11 @@
 import type { JsonRpcEngine } from '@metamask/json-rpc-engine';
 import SafeEventEmitter from '@metamask/safe-event-emitter';
-import type { JsonRpcRequest } from '@metamask/utils';
+import type {
+  Json,
+  JsonRpcParams,
+  JsonRpcRequest,
+  JsonRpcResponse,
+} from '@metamask/utils';
 
 /**
  * An Ethereum provider.
@@ -34,9 +39,12 @@ export class SafeEventEmitterProvider extends SafeEventEmitter {
    * @param req - The request to send.
    * @param callback - A function that is called upon the success or failure of the request.
    */
-  sendAsync = (
-    req: JsonRpcRequest,
-    callback: (error: unknown, providerRes?: any) => void,
+  sendAsync = <
+    Params extends JsonRpcParams = JsonRpcParams,
+    Result extends Json = Json,
+  >(
+    req: JsonRpcRequest<Params>,
+    callback: (error: unknown, providerRes?: JsonRpcResponse<Result>) => void,
   ) => {
     this.#engine.handle(req, callback);
   };
@@ -51,9 +59,12 @@ export class SafeEventEmitterProvider extends SafeEventEmitter {
    * @param req - The request to send.
    * @param callback - A function that is called upon the success or failure of the request.
    */
-  send = (
-    req: JsonRpcRequest,
-    callback: (error: unknown, providerRes?: any) => void,
+  send = <
+    Params extends JsonRpcParams = JsonRpcParams,
+    Result extends Json = Json,
+  >(
+    req: JsonRpcRequest<Params>,
+    callback: (error: unknown, providerRes?: JsonRpcResponse<Result>) => void,
   ) => {
     if (typeof callback !== 'function') {
       throw new Error('Must provide callback to "send" method.');

@MajorLift
Copy link
Contributor Author

MajorLift commented Nov 22, 2023

The above is caused by the @metamask/eth-query version bump in controller-utils (#2028) not being released yet. The issue will be resolved by the next release that includes the unreleased changes in all of @metamask/eth-query's downstream packages in core.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working team-wallet-framework
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants