-
-
Notifications
You must be signed in to change notification settings - Fork 183
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
Add eth-query types #1266
Add eth-query types #1266
Conversation
c8fa5bc
to
4881a29
Compare
621e59f
to
e9c594c
Compare
e9c594c
to
617611d
Compare
8d8a93b
to
3fc91b6
Compare
Type definitions have been added for the `eth-query` package. These were copied from the type definitions used in the extension. The addition of these types revealed various invalid assumptions we've made throughout the codebase, requiring the addition of runtime checks. Mostly this consists of throwing an error whenever someone tries to use the `ethQuery` instance before it has been set. In most cases, this would only introduce an error in code paths that would already throw an error. The one exception to that is the gas fee controller, which used to get `ethQuery` upon construction. It has been updated to wait until the `providerConfigChange` event before attempting to fetch that, since it's never defined upon construction in practice anyway. There are two places in the network controller where these types revealed that we're not validating the network response to the `net_version` and `eth_getBlockByNumber` calls made during network lookup. A comment has been left about validating this in a future PR. Closes #1204
3fc91b6
to
96b6e78
Compare
Co-authored-by: Maarten Zuidhoorn <maarten@zuidhoorn.com>
packages/network-controller/tests/provider-api-tests/helpers.ts
Outdated
Show resolved
Hide resolved
cc2e4e9
to
601a61a
Compare
Co-authored-by: Maarten Zuidhoorn <maarten@zuidhoorn.com>
@@ -346,9 +346,6 @@ export class GasFeeController extends BaseControllerV2< | |||
'NetworkController:getProviderConfig', | |||
); | |||
this.currentChainId = providerConfig.chainId; | |||
this.ethQuery = this.messagingSystem.call( |
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.
In retrospect, my assumption here wasn't entirely true; in practice this likely was set upon construction. Just not in the tests. So this is breaking after all. We'll need to fix this before the next release.
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.
Maybe the simplest fix would be to update getEthQuery
to return EthQuery | undefined
, and restore this line.
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.
Okay, fixed via #1284.
if (error) { | ||
reject(error); | ||
return; | ||
} | ||
resolve(result); | ||
}; | ||
|
||
if (typeof ethQuery[method] === 'function') { | ||
if ( | ||
hasProperty(ethQuery, method) && |
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 seems to have introduced a bug; the methods in question are on the ethQuery
prototype, so this is returning false
when this condition used to resolve to true
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.
Right, that makes sense.
The `query` function was updated in #1266 to use `hasProperty` has part of an effect to improve the types being used. Unfortunately this had the unexpected affected of breaking this condition because `hasProperty` doesn't look up the prototype chain, but the method we were checking for was on the prototype. `hasProperty` is only meant to be used with plain objects. The condition has been updated to use `in`, and the tests have been updated to use a more realistic EthQuery mock that has methods as prototypes.
The `query` function was updated in #1266 to use `hasProperty` has part of an effect to improve the types being used. Unfortunately this had the unexpected affected of breaking this condition because `hasProperty` doesn't look up the prototype chain, but the method we were checking for was on the prototype. `hasProperty` is only meant to be used with plain objects. The condition has been updated to use `in`, and the tests have been updated to use a more realistic EthQuery mock that has methods as prototypes. To fix various type errors, an old inlined EthQueryLike type has been replaced by the real EthQuery type. This required adding `eth-query` as a dependency to the controller utils package.
The `query` function was updated in #1266 to use `hasProperty` has part of an effect to improve the types being used. Unfortunately this had the unexpected affected of breaking this condition because `hasProperty` doesn't look up the prototype chain, but the method we were checking for was on the prototype. `hasProperty` is only meant to be used with plain objects. The condition has been updated to use `in`, and the tests have been updated to use a more realistic EthQuery mock that has methods as prototypes. To fix various type errors, an old inlined EthQueryLike type has been replaced by the real EthQuery type. This required adding `eth-query` as a dependency to the controller utils package.
Type definitions have been added for the `eth-query` package. These were copied from the type definitions used in the extension. The addition of these types revealed various invalid assumptions we've made throughout the codebase, requiring the addition of runtime checks. Mostly this consists of throwing an error whenever someone tries to use the `ethQuery` instance before it has been set. In most cases, this would only introduce an error in code paths that would already throw an error. The one exception to that is the gas fee controller, which used to get `ethQuery` upon construction. It has been updated to wait until the `providerConfigChange` event before attempting to fetch that, since it's never defined upon construction in practice anyway. There are two places in the network controller where these types revealed that we're not validating the network response to the `net_version` and `eth_getBlockByNumber` calls made during network lookup. A comment has been left about validating this in a future PR. Co-authored-by: Maarten Zuidhoorn <maarten@zuidhoorn.com>
The `query` function was updated in #1266 to use `hasProperty` has part of an effect to improve the types being used. Unfortunately this had the unexpected affected of breaking this condition because `hasProperty` doesn't look up the prototype chain, but the method we were checking for was on the prototype. `hasProperty` is only meant to be used with plain objects. The condition has been updated to use `in`, and the tests have been updated to use a more realistic EthQuery mock that has methods as prototypes. To fix various type errors, an old inlined EthQueryLike type has been replaced by the real EthQuery type. This required adding `eth-query` as a dependency to the controller utils package.
Type definitions have been added for the `eth-query` package. These were copied from the type definitions used in the extension. The addition of these types revealed various invalid assumptions we've made throughout the codebase, requiring the addition of runtime checks. Mostly this consists of throwing an error whenever someone tries to use the `ethQuery` instance before it has been set. In most cases, this would only introduce an error in code paths that would already throw an error. The one exception to that is the gas fee controller, which used to get `ethQuery` upon construction. It has been updated to wait until the `providerConfigChange` event before attempting to fetch that, since it's never defined upon construction in practice anyway. There are two places in the network controller where these types revealed that we're not validating the network response to the `net_version` and `eth_getBlockByNumber` calls made during network lookup. A comment has been left about validating this in a future PR. Co-authored-by: Maarten Zuidhoorn <maarten@zuidhoorn.com>
The `query` function was updated in #1266 to use `hasProperty` has part of an effect to improve the types being used. Unfortunately this had the unexpected affected of breaking this condition because `hasProperty` doesn't look up the prototype chain, but the method we were checking for was on the prototype. `hasProperty` is only meant to be used with plain objects. The condition has been updated to use `in`, and the tests have been updated to use a more realistic EthQuery mock that has methods as prototypes. To fix various type errors, an old inlined EthQueryLike type has been replaced by the real EthQuery type. This required adding `eth-query` as a dependency to the controller utils package.
Description
Type definitions have been added for the
eth-query
package. These were copied from the type definitions used in the extension.The addition of these types revealed various invalid assumptions we've made throughout the codebase, requiring the addition of runtime checks. Mostly this consists of throwing an error whenever someone tries to use the
ethQuery
instance before it has been set. In most cases, this would only introduce an error in code paths that would already throw an error.The one exception to that is the gas fee controller, which used to get
ethQuery
upon construction. It has been updated to wait until theproviderConfigChange
event before attempting to fetch that, since it's never defined upon construction in practice anyway.There are two places in the network controller where these types revealed that we're not validating the network response to the
net_version
andeth_getBlockByNumber
calls made during network lookup. A comment has been left about validating this in a future PR.Changes
@metamask/assets-controllers
EthQuery
type definition for theprovider
config property@metamask/controller-utils
query
function has improved type checks for theethQuery
argumentethQuery
parameter. In that circumstance this would have thrown an error at runtime anyway. Effectively this should be non-breaking for any usage that isn't already broken.@metamask/gas-fee-controller
NetworkController:getEthQuery
is no longer called in the constructorethQuery
instance gets updated upon theproviderConfigChange
event. This means that theethQuery
instance won't get set until after provider initialization or the next network switch.@metamask/network-controller
Provider
type has been updated to better reflect the provider type returned by the network controllerany
. Now it returns a type that mostly matches the provider returned (some semi-internal properties are omitted)ProviderProxy
type as well, which wraps theProvider
typeNetworkController:getEthQuery
will now throw an error ifethQuery
is not set, rather than returningundefined
References
Closes #1204
Checklist