Skip to content

Commit

Permalink
Merge branch 'develop' into init-changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gudahtt authored Nov 18, 2022
2 parents 7038e6a + 8f18e04 commit 7d5c09a
Show file tree
Hide file tree
Showing 3 changed files with 383 additions and 0 deletions.
59 changes: 59 additions & 0 deletions app/scripts/migrations/077.js
Original file line number Diff line number Diff line change
@@ -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,
},
};
}
322 changes: 322 additions & 0 deletions app/scripts/migrations/077.test.js
Original file line number Diff line number Diff line change
@@ -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,
},
},
},
},
},
},
});
});
});
2 changes: 2 additions & 0 deletions app/scripts/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -157,6 +158,7 @@ const migrations = [
m074,
m075,
m076,
m077,
];

export default migrations;

0 comments on commit 7d5c09a

Please sign in to comment.