-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updating tokensChainsCache[chainId].data to object from array in user…
… state (#16535)
- Loading branch information
1 parent
af09521
commit 8f18e04
Showing
3 changed files
with
383 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters