Skip to content

Commit

Permalink
Fixing incorrectly typed token decimal attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanml committed Mar 17, 2021
1 parent 88ad383 commit c8b5794
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 1 deletion.
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
36 changes: 36 additions & 0 deletions app/scripts/migrations/054.js
Original file line number Diff line number Diff line change
@@ -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;
}
138 changes: 138 additions & 0 deletions app/scripts/migrations/054.test.js
Original file line number Diff line number Diff line change
@@ -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',
},
],
},
});
});
});
1 change: 1 addition & 0 deletions app/scripts/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const migrations = [
require('./051').default,
require('./052').default,
require('./053').default,
require('./054').default,
];

export default migrations;

0 comments on commit c8b5794

Please sign in to comment.