Correct use of useTokenTracker in viewQuote to ensure token data is not disrupted by faulty token in user account #10456
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes the following bug which exists on develop and prod:
eth-token-tracker
tries to call either thesymbol
,balance
orbalanceOf
method on the token contract. For example, add0x7a250d5630b4cf539739df2c5dacb4c659f2488d
and use a symbol name like "Test"7. You will arrive at the view quote screen and see a message that says "You need 5 more UNI to complete this swap", even though you have exactly 5 UNI in your account. Instead you should see a "You need ## more ETH to complete this swap"
Cause of this bug:
The
useTokenTracker
hook is how we get all balance information for users tokens. TheuseTokenTracker
hook gets it balances via theeth-token-tracker
library. That library receives data on all of a users tokens, and attempts to fetch balance data for all of them within aPromise.all
call. If an error is thrown when attempting to fetch the balance data for any single token, it ultimately causes theuseTokenTracker
to return an empty array of token balances, even if balances for token other than the erroneous one would have succeeded.If the user arrived at the view quote screen, and the above happened AND they had insufficient ETH for the token swap, then they see an error that says they have insufficient tokens. The
useTokenTracker
has no data on the token, so the view-quote screen logic evaluates the users balance of the token to be 0. Meanwhile, because the the user has insufficient ETH, theswaps.state.balanceError
property is set to true. This causes the logic that setsinsufficientTokens
to evaluate to true, andtokenBalanceNeeded
to evaluate to the amount of tokens being sent.Solution:
This PR solves the issue in two ways, each of which would work on their own, but together they handle possible erroneous cases that could arise in the future as well.
First, the second parameter passed to
useTokenTracker
in view-quote is set to true. This is theincludeFailedTokens
parameter. It ensures that balances for all tokens that can be successfully retrieved are returned, even if attempting to get the balance of one or more tokens encounters and error. (This functionality was added in #9896). This alone prevents the bug as described above as it ensures that the token balance reflects the user's actual token balance and is not incorrectly defaulted to0
. As a result, the user will now correctly see an eth balance error (instead of a token balance error).Second, if for whatever reason the
useTokenTracker
fails to get the balance for the currently selected token, we should not assume the balance is0
and then show the user an error message that suggests that. Instead we should tell the user that we were unable to get information on their token balance, while also disabling submission of swaps in this case. AtokenBalanceUnavailable
variable and associated error message has been added to handle this case.