Skip to content
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 1 commit into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/scripts/controllers/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ export default class PreferencesController {
*/
async addToken(rawAddress, symbol, decimals, image) {
const address = normalizeAddress(rawAddress);
const newEntry = { address, symbol, decimals };
const newEntry = { address, symbol, decimals: Number(decimals) };
const { tokens, hiddenTokens } = this.store.getState();
const assetImages = this.getAssetImages();
const updatedHiddenTokens = hiddenTokens.filter(
Expand Down
75 changes: 75 additions & 0 deletions app/scripts/migrations/054.js
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;

Copy link
Contributor

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 migrate tokens and accountTokens: /~https://github.com/MetaMask/metamask-extension/pull/10593/files#diff-0ee44923cc3a183d3803fea2022e1b17f127940f603909b954588cc7c7aa2439R47

And for reference on what those are, see this thread: https://consensys.slack.com/archives/GTQAGKY5V/p1615395521395500?thread_ts=1615332992.380200&cid=GTQAGKY5V

Copy link
Contributor Author

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.

Copy link
Member

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 from accountTokens whenever the user switches accounts or networks.

Copy link
Contributor Author

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

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
}
Loading