From c8b5794c2cac4951b3d5b2b5ba2ac2ce15fad07f Mon Sep 17 00:00:00 2001 From: ryanml Date: Tue, 16 Mar 2021 23:28:55 -0700 Subject: [PATCH] Fixing incorrectly typed token decimal attribute --- app/scripts/controllers/preferences.js | 2 +- app/scripts/migrations/054.js | 36 +++++++ app/scripts/migrations/054.test.js | 138 +++++++++++++++++++++++++ app/scripts/migrations/index.js | 1 + 4 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 app/scripts/migrations/054.js create mode 100644 app/scripts/migrations/054.test.js diff --git a/app/scripts/controllers/preferences.js b/app/scripts/controllers/preferences.js index 240aaaa1855d..fba7a400a4a4 100644 --- a/app/scripts/controllers/preferences.js +++ b/app/scripts/controllers/preferences.js @@ -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( diff --git a/app/scripts/migrations/054.js b/app/scripts/migrations/054.js new file mode 100644 index 000000000000..04014eebb16e --- /dev/null +++ b/app/scripts/migrations/054.js @@ -0,0 +1,36 @@ +import { cloneDeep } from 'lodash'; + +const version = 54; + +/** + * Migrates preference tokens with 0 decimals typed as 'string' to 'number' + */ +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) { + const { tokens } = newState.PreferencesController; + if (Array.isArray(tokens)) { + for (const token of tokens) { + if (token.decimals === '0') { + token.decimals = Number('0'); + } + } + } + newState.PreferencesController.tokens = tokens; + } + + return newState; +} diff --git a/app/scripts/migrations/054.test.js b/app/scripts/migrations/054.test.js new file mode 100644 index 000000000000..ab88a1691d10 --- /dev/null +++ b/app/scripts/migrations/054.test.js @@ -0,0 +1,138 @@ +import { strict as assert } from 'assert'; +import migration54 from './054'; + +describe('migration #54', function () { + it('should update the version metadata', async function () { + const oldStorage = { + meta: { + version: 53, + }, + data: {}, + }; + + const newStorage = await migration54.migrate(oldStorage); + assert.deepEqual(newStorage.meta, { + version: 54, + }); + }); + + it('should retype instance of 0 decimal values to numbers', async function () { + const oldStorage = { + meta: {}, + data: { + PreferencesController: { + tokens: [ + { + address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', + decimals: '0', + symbol: 'CK', + }, + { + address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', + decimals: 18, + symbol: 'BAT', + }, + { + address: '0x514910771af9ca656af840dff83e8264ecf986ca', + decimals: 18, + symbol: 'LINK', + }, + { + address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', + decimals: '0', + symbol: 'SOR', + }, + ], + }, + }, + }; + + const newStorage = await migration54.migrate(oldStorage); + assert.deepEqual(newStorage.data, { + PreferencesController: { + tokens: [ + { + address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', + decimals: 0, + symbol: 'CK', + }, + { + address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', + decimals: 18, + symbol: 'BAT', + }, + { + address: '0x514910771af9ca656af840dff83e8264ecf986ca', + decimals: 18, + symbol: 'LINK', + }, + { + address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', + decimals: 0, + symbol: 'SOR', + }, + ], + }, + }); + }); + + it('should do nothing if all decimal value typings are correct', async function () { + const oldStorage = { + meta: {}, + data: { + PreferencesController: { + tokens: [ + { + address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', + decimals: 0, + symbol: 'CK', + }, + { + address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', + decimals: 18, + symbol: 'BAT', + }, + { + address: '0x514910771af9ca656af840dff83e8264ecf986ca', + decimals: 18, + symbol: 'LINK', + }, + { + address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', + decimals: 0, + symbol: 'SOR', + }, + ], + }, + }, + }; + + const newStorage = await migration54.migrate(oldStorage); + assert.deepEqual(newStorage.data, { + PreferencesController: { + tokens: [ + { + address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', + decimals: 0, + symbol: 'CK', + }, + { + address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', + decimals: 18, + symbol: 'BAT', + }, + { + address: '0x514910771af9ca656af840dff83e8264ecf986ca', + decimals: 18, + symbol: 'LINK', + }, + { + address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', + decimals: 0, + symbol: 'SOR', + }, + ], + }, + }); + }); +}); diff --git a/app/scripts/migrations/index.js b/app/scripts/migrations/index.js index b304102024c4..00565e4d8823 100644 --- a/app/scripts/migrations/index.js +++ b/app/scripts/migrations/index.js @@ -58,6 +58,7 @@ const migrations = [ require('./051').default, require('./052').default, require('./053').default, + require('./054').default, ]; export default migrations;