-
-
Notifications
You must be signed in to change notification settings - Fork 187
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
Prevent redundant token rate updates #3647
Conversation
The token rates controller fetches rates for certain collections of tokens obtained via the tokens controller. If any of these collections change, then new rates need to be fetched. Unfortunately, because the check to determine whether these collections have changed is simplistic, new rates are fetched every single time the tokens controller state is changed. To address this, we can consider two things: - The token rates controller only uses the addresses of tokens in these collections to fetch rates, and it also normalizes those addresses when updating state. - These collections of tokens may contain duplicates. So to reduce the number of times rates are fetched and to optimize each request, this commit updates the tokens state callback to assemble the aforementioned token collections into a deduplicated, normalized list of token addresses, and then it checks to see if this list has any changes to know whether or not to re-fetch rates.
4867c37
to
a20836b
Compare
@@ -482,7 +490,7 @@ export class TokenRatesController extends PollingControllerV1< | |||
(obj, [tokenContractAddress, tokenPrice]) => { | |||
return { | |||
...obj, | |||
[toChecksumHexAddress(tokenContractAddress)]: tokenPrice.value, | |||
[tokenContractAddress]: tokenPrice.value, |
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.
By the time we get here, tokenContractAddress
is already normalized (thanks to #getTokenAddresses
).
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.
Looks good!
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!
return [ | ||
...new Set( | ||
[...tokens, ...detectedTokens].map((token) => | ||
toHex(toChecksumHexAddress(token.address)), |
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.
is the toHex
call redundant alongside toChecksumHexAddress
?
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.
It's not — unfortunately toChecksumHexAddress
(located in controller-utils
) doesn't use a type predicate to assert that its return value is a Hex
. toHex
(which is from @metamask/utils
) does, though. So it's redundant in the sense that it doesn't do anything, but it does convert this to a Hex
.
Explanation
The token rates controller fetches rates for certain collections of tokens obtained via the tokens controller. If any of these collections change, then new rates need to be fetched.
Unfortunately, because the check to determine whether these collections have changed is simplistic, new rates are fetched every single time the tokens controller state is changed.
To address this, we can consider two things:
So to reduce the number of times rates are fetched and to optimize each request, this commit updates the tokens state callback to assemble the aforementioned token collections into a deduplicated, normalized list of token addresses, and then it checks to see if this list has any changes to know whether or not to re-fetch rates.
References
Fixes #3605.
Changelog
@metamask/assets-controllers
Checklist