-
Notifications
You must be signed in to change notification settings - Fork 5k
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
Fixing incorrectly typed token decimal attribute #10666
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { cloneDeep } from 'lodash'; | ||
|
||
const version = 54; | ||
|
||
function isValidDecimals(decimals) { | ||
return ( | ||
typeof decimals === 'number' || | ||
(typeof decimals === 'string' && decimals.match(/^(0x)?\d+$/u)) | ||
); | ||
} | ||
|
||
/** | ||
* Migrates preference tokens with decimals typed as string to number. | ||
* It also removes any tokens with corrupted or inconvertible decimal values. | ||
*/ | ||
export default { | ||
version, | ||
async migrate(originalVersionedData) { | ||
const versionedData = cloneDeep(originalVersionedData); | ||
versionedData.meta.version = version; | ||
const state = versionedData.data; | ||
const newState = transformState(state); | ||
versionedData.data = newState; | ||
return versionedData; | ||
}, | ||
}; | ||
|
||
function transformState(state) { | ||
const newState = state; | ||
|
||
if (!newState.PreferencesController) { | ||
return newState; | ||
} | ||
|
||
const tokens = newState.PreferencesController.tokens || []; | ||
// Filter out any tokens with corrupted decimal values | ||
const validTokens = tokens.filter(({ decimals }) => | ||
isValidDecimals(decimals), | ||
); | ||
for (const token of validTokens) { | ||
// In the case of a decimal value type string, convert to a number. | ||
if (typeof token.decimals === 'string') { | ||
// eslint-disable-next-line radix | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note, per discussion with @Gudahtt we'll let the radix be inferred |
||
token.decimals = parseInt(token.decimals); | ||
} | ||
} | ||
newState.PreferencesController.tokens = validTokens; | ||
|
||
const { accountTokens } = newState.PreferencesController; | ||
if (accountTokens && typeof accountTokens === 'object') { | ||
for (const address of Object.keys(accountTokens)) { | ||
const networkTokens = accountTokens[address]; | ||
if (networkTokens && typeof networkTokens === 'object') { | ||
for (const network of Object.keys(networkTokens)) { | ||
const tokensOnNetwork = networkTokens[network] || []; | ||
// Filter out any tokens with corrupted decimal values | ||
const validTokensOnNetwork = tokensOnNetwork.filter(({ decimals }) => | ||
isValidDecimals(decimals), | ||
); | ||
// In the case of a decimal value type string, convert to a number. | ||
for (const token of validTokensOnNetwork) { | ||
if (typeof token.decimals === 'string') { | ||
// eslint-disable-next-line radix | ||
token.decimals = parseInt(token.decimals); | ||
} | ||
} | ||
networkTokens[network] = validTokensOnNetwork; | ||
} | ||
} | ||
} | ||
} | ||
newState.PreferencesController.accountTokens = accountTokens; | ||
|
||
return newState; | ||
} |
Oops, something went wrong.
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.
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.
A migration of
accountTokens
will also be needed. Check out this PR which is also about to migratetokens
andaccountTokens
: /~https://github.com/MetaMask/metamask-extension/pull/10593/files#diff-0ee44923cc3a183d3803fea2022e1b17f127940f603909b954588cc7c7aa2439R47And for reference on what those are, see this thread: https://consensys.slack.com/archives/GTQAGKY5V/p1615395521395500?thread_ts=1615332992.380200&cid=GTQAGKY5V
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.
Good catch here 👍 I believe having these incorrectly typed in part of
accountTokens
does not produce the UI error in question, but for structural parity they should absolutely be the same.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 see that this is addressed now, but for future reference: it does end up indirectly causing the problem, because
tokens
gets derived fromaccountTokens
whenever the user switches accounts or networks.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.
@Gudahtt - ah, thank you for pointing this out