-
-
Notifications
You must be signed in to change notification settings - Fork 252
feat: Add context parameter to InternalProvider and JsonRpcServer methods
#7061
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
Conversation
922dafc to
36a1cb5
Compare
dd3379c to
86dc3b8
Compare
86dc3b8 to
c0ef5d0
Compare
MiddlewareContext generic to InternalProviderInternalProvider and JsonRpcServer methods
1273941 to
c2632f3
Compare
mcmire
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One question, but overall looks good to me.
| Context extends ContextConstraint = MiddlewareContext, | ||
| > |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Indentation?
| Context extends ContextConstraint = MiddlewareContext, | |
| > | |
| Context extends ContextConstraint = MiddlewareContext, | |
| > |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what Prettier wants (there are no lint warnings or errors in the entire package).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Boo. Fine then 😆
| } | ||
|
|
||
| #coerceRequest(rawRequest: unknown, isRequest: boolean): JsonRpcCall { | ||
| static #coerceRequest(rawRequest: unknown, isRequest: boolean): JsonRpcCall { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not know you could have a static private property 🤔
Thoughts on making it public? Or maybe it doesn't need to be a static property on JsonRpcServer, it can be a function in utils?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, static private properties are neat! I only made this one static because I noticed that I could.
I don't really want to make the method public, because using it "correctly" requires capturing the original id beforehand as we do in handle(). I could make it into a external utility function and not export it if you have a strong preference for that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see, for some reason I thought it was being referenced outside of this class and got confused how that was working.
I don't have a strong preference for this in that case.
| console.log(result); // 'bar' | ||
| ``` | ||
|
|
||
| You can also pass a plain object as a shorthand for a `MiddlewareContext` instance: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice feature :)
mcmire
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
|
Skipping review from @MetaMask/swaps-engineers; we changed a type in a test. |
Explanation
Adds a
MiddlewareContextparameter toInternalProvider.request()andJsonRpcServer.handle(). This is essentially a missing feature from theJsonRpcEngineV2implementation / migration, since callers are no longer able to add non-JSON-RPC properties to request objects and instead need to use thecontextobject.As part of this, permit specifying a plain object as the context to avoid forcing callers to import
MiddlewareContextwhenever they want to make a JSON-RPC call with some particular context. Also opportunistically fixes a bug with the staticisInstancemethods of V2 engine classes, where we wouldn't walk the prototype chain when checking for the symbol property.References
JsonRpcEngineV2#6327Checklist
I've prepared draft pull requests for clients and consumer packages to resolve any breaking changeseth-json-rpc-middlewaretoJsonRpcEngineV2#7065.Note
Adds a context option to
InternalProvider.request()/JsonRpcServer.handle(), enables passing plain-object contexts across the V2 engine, makesPollingBlockTrackercontext-generic, and narrows Network Controller’sProvidertype; updates tests/docs accordingly.HandleOptionswithcontext(acceptsMiddlewareContextor plain object) and forward it throughhandle()andJsonRpcServer.handle().MiddlewareContext(construct from KeyValues object,isInstance) and exportInferKeyValues; add sharedisInstanceutil; minor server refactor (static request coercion).InternalProvidergeneric overContext;request()accepts{ context }and forwards to engine.PollingBlockTrackerwithContext; type provider asInternalProvider<Context>; update CHANGELOG.ProvidertoInternalProvider<MiddlewareContext<{ origin: string; skipCache: boolean } & Record<string, unknown>>>.create-network-client, tests, and helpers; update CHANGELOG.FakeProvider,FakeBlockTracker) and consumers to new generic/context types.@metamask/network-controllerProvider.@metamask/network-controllerto rootpackage.json.Written by Cursor Bugbot for commit c853283. This will update automatically on new commits. Configure here.