Updating tokensChainsCache[chainId].data to object from array in user state (#16535)

feature/default_network_editable
Niranjana Binoy 2 years ago committed by GitHub
parent af09521a69
commit 8f18e04b97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 59
      app/scripts/migrations/077.js
  2. 322
      app/scripts/migrations/077.test.js
  3. 2
      app/scripts/migrations/index.js

@ -0,0 +1,59 @@
import { cloneDeep } from 'lodash';
const version = 77;
/**
* Prior to token detection v2 the data property in tokensChainsCache was an array,
* in v2 we changes that to an object. In this migration we are converting the data as array to object.
*/
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 TokenListController = state?.TokenListController || {};
const { tokensChainsCache } = TokenListController;
let dataCache;
let dataObject;
// eslint-disable-next-line
for (const chainId in tokensChainsCache) {
dataCache = tokensChainsCache[chainId].data;
dataObject = {};
// if the data is array conver that to object
if (Array.isArray(dataCache)) {
for (const token of dataCache) {
dataObject[token.address] = token;
}
} else if (
Object.keys(dataCache)[0].toLowerCase() !==
dataCache[Object.keys(dataCache)[0]].address.toLowerCase()
) {
// for the users who already updated to the recent version
// and the dataCache is already an object keyed with 0,1,2,3 etc
// eslint-disable-next-line
for (const tokenAddress in dataCache) {
dataObject[dataCache[tokenAddress].address] = dataCache[tokenAddress];
}
}
tokensChainsCache[chainId].data =
Object.keys(dataObject).length > 0 ? dataObject : dataCache;
}
TokenListController.tokensChainsCache = tokensChainsCache;
return {
...state,
TokenListController: {
...TokenListController,
},
};
}

@ -0,0 +1,322 @@
import migration77 from './077';
describe('migration #77', () => {
it('should update the version metadata', async () => {
const oldStorage = {
meta: {
version: 76,
},
};
const newStorage = await migration77.migrate(oldStorage);
expect(newStorage.meta).toStrictEqual({
version: 77,
});
});
it('should change the data from array to object for a single network', async () => {
const oldStorage = {
meta: {
version: 76,
},
data: {
TokenListController: {
tokenList: {
'0x514910771af9ca656af840dff83e8264ecf986ca': {
address: '0x514910771af9ca656af840dff83e8264ecf986ca',
symbol: 'LINK',
decimals: 18,
},
},
tokensChainsCache: {
1: {
timestamp: 1234,
data: [
{
address: '0x514910771af9ca656af840dff83e8264ecf986ca',
symbol: 'LINK',
decimals: 18,
},
{
address: '0xc00e94cb662c3520282e6f5717214004a7f26888',
symbol: 'COMP',
decimals: 18,
},
],
},
},
},
},
};
const newStorage = await migration77.migrate(oldStorage);
expect(newStorage).toStrictEqual({
meta: {
version: 77,
},
data: {
TokenListController: {
tokenList: {
'0x514910771af9ca656af840dff83e8264ecf986ca': {
address: '0x514910771af9ca656af840dff83e8264ecf986ca',
symbol: 'LINK',
decimals: 18,
},
},
tokensChainsCache: {
1: {
timestamp: 1234,
data: {
'0x514910771af9ca656af840dff83e8264ecf986ca': {
address: '0x514910771af9ca656af840dff83e8264ecf986ca',
symbol: 'LINK',
decimals: 18,
},
'0xc00e94cb662c3520282e6f5717214004a7f26888': {
address: '0xc00e94cb662c3520282e6f5717214004a7f26888',
symbol: 'COMP',
decimals: 18,
},
},
},
},
},
},
});
});
it('should change the data from array to object for a multiple networks', async () => {
const oldStorage = {
meta: {
version: 76,
},
data: {
TokenListController: {
tokenList: {
'0x514910771af9ca656af840dff83e8264ecf986ca': {
address: '0x514910771af9ca656af840dff83e8264ecf986ca',
symbol: 'LINK',
decimals: 18,
},
},
tokensChainsCache: {
1: {
timestamp: 1234,
data: [
{
address: '0x514910771af9ca656af840dff83e8264ecf986ca',
symbol: 'LINK',
decimals: 18,
},
{
address: '0xc00e94cb662c3520282e6f5717214004a7f26888',
symbol: 'COMP',
decimals: 18,
},
],
},
56: {
timestamp: 1324,
data: [
{
address: '0x3ee2200efb3400fabb9aacf31297cbdd1d435d47',
symbol: 'ADA',
decimals: 18,
},
{
address: '0x928e55dab735aa8260af3cedada18b5f70c72f1b',
symbol: 'FRONT',
decimals: 18,
},
],
},
},
},
},
};
const newStorage = await migration77.migrate(oldStorage);
expect(newStorage).toStrictEqual({
meta: {
version: 77,
},
data: {
TokenListController: {
tokenList: {
'0x514910771af9ca656af840dff83e8264ecf986ca': {
address: '0x514910771af9ca656af840dff83e8264ecf986ca',
symbol: 'LINK',
decimals: 18,
},
},
tokensChainsCache: {
1: {
timestamp: 1234,
data: {
'0x514910771af9ca656af840dff83e8264ecf986ca': {
address: '0x514910771af9ca656af840dff83e8264ecf986ca',
symbol: 'LINK',
decimals: 18,
},
'0xc00e94cb662c3520282e6f5717214004a7f26888': {
address: '0xc00e94cb662c3520282e6f5717214004a7f26888',
symbol: 'COMP',
decimals: 18,
},
},
},
56: {
timestamp: 1324,
data: {
'0x3ee2200efb3400fabb9aacf31297cbdd1d435d47': {
address: '0x3ee2200efb3400fabb9aacf31297cbdd1d435d47',
symbol: 'ADA',
decimals: 18,
},
'0x928e55dab735aa8260af3cedada18b5f70c72f1b': {
address: '0x928e55dab735aa8260af3cedada18b5f70c72f1b',
symbol: 'FRONT',
decimals: 18,
},
},
},
},
},
},
});
});
it('should not change anything if the data is already an object', async () => {
const oldStorage = {
meta: {
version: 76,
},
data: {
TokenListController: {
tokenList: {
'0x514910771af9ca656af840dff83e8264ecf986ca': {
address: '0x514910771af9ca656af840dff83e8264ecf986ca',
symbol: 'LINK',
decimals: 18,
},
},
tokensChainsCache: {
1: {
timestamp: 1234,
data: {
'0x514910771af9ca656af840dff83e8264ecf986ca': {
address: '0x514910771af9ca656af840dff83e8264ecf986ca',
symbol: 'LINK',
decimals: 18,
},
'0xc00e94cb662c3520282e6f5717214004a7f26888': {
address: '0xc00e94cb662c3520282e6f5717214004a7f26888',
symbol: 'COMP',
decimals: 18,
},
},
},
},
},
},
};
const newStorage = await migration77.migrate(oldStorage);
expect(newStorage).toStrictEqual({
meta: {
version: 77,
},
data: {
TokenListController: {
tokenList: {
'0x514910771af9ca656af840dff83e8264ecf986ca': {
address: '0x514910771af9ca656af840dff83e8264ecf986ca',
symbol: 'LINK',
decimals: 18,
},
},
tokensChainsCache: {
1: {
timestamp: 1234,
data: {
'0x514910771af9ca656af840dff83e8264ecf986ca': {
address: '0x514910771af9ca656af840dff83e8264ecf986ca',
symbol: 'LINK',
decimals: 18,
},
'0xc00e94cb662c3520282e6f5717214004a7f26888': {
address: '0xc00e94cb662c3520282e6f5717214004a7f26888',
symbol: 'COMP',
decimals: 18,
},
},
},
},
},
},
});
});
it('should correct the address keys if the object is keyed wrong', async () => {
const oldStorage = {
meta: {
version: 76,
},
data: {
TokenListController: {
tokenList: {
'0x514910771af9ca656af840dff83e8264ecf986ca': {
address: '0x514910771af9ca656af840dff83e8264ecf986ca',
symbol: 'LINK',
decimals: 18,
},
},
tokensChainsCache: {
1: {
timestamp: 1234,
data: {
0: {
address: '0x514910771af9ca656af840dff83e8264ecf986ca',
symbol: 'LINK',
decimals: 18,
},
1: {
address: '0xc00e94cb662c3520282e6f5717214004a7f26888',
symbol: 'COMP',
decimals: 18,
},
},
},
},
},
},
};
const newStorage = await migration77.migrate(oldStorage);
expect(newStorage).toStrictEqual({
meta: {
version: 77,
},
data: {
TokenListController: {
tokenList: {
'0x514910771af9ca656af840dff83e8264ecf986ca': {
address: '0x514910771af9ca656af840dff83e8264ecf986ca',
symbol: 'LINK',
decimals: 18,
},
},
tokensChainsCache: {
1: {
timestamp: 1234,
data: {
'0x514910771af9ca656af840dff83e8264ecf986ca': {
address: '0x514910771af9ca656af840dff83e8264ecf986ca',
symbol: 'LINK',
decimals: 18,
},
'0xc00e94cb662c3520282e6f5717214004a7f26888': {
address: '0xc00e94cb662c3520282e6f5717214004a7f26888',
symbol: 'COMP',
decimals: 18,
},
},
},
},
},
},
});
});
});

@ -80,6 +80,7 @@ import m073 from './073';
import m074 from './074';
import m075 from './075';
import m076 from './076';
import m077 from './077';
const migrations = [
m002,
@ -157,6 +158,7 @@ const migrations = [
m074,
m075,
m076,
m077,
];
export default migrations;

Loading…
Cancel
Save