Integrate TokensController (#11552)
* Integrate controllers/tokensController * address rebase issues * small cleanup * addressing feedback * more feedbackfeature/default_network_editable
parent
ad7d85b04e
commit
490d3b8d40
@ -0,0 +1,78 @@ |
||||
import { cloneDeep } from 'lodash'; |
||||
|
||||
const version = 63; |
||||
|
||||
/** |
||||
* Moves token state from preferences controller to TokensController |
||||
*/ |
||||
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 accountTokens = state?.PreferencesController?.accountTokens; |
||||
const accountHiddenTokens = state?.PreferencesController?.accountHiddenTokens; |
||||
|
||||
const newAllTokens = {}; |
||||
if (accountTokens) { |
||||
Object.keys(accountTokens).forEach((accountAddress) => { |
||||
Object.keys(accountTokens[accountAddress]).forEach((chainId) => { |
||||
const tokensArray = accountTokens[accountAddress][chainId]; |
||||
if (newAllTokens[chainId] === undefined) { |
||||
newAllTokens[chainId] = { [accountAddress]: tokensArray }; |
||||
} else { |
||||
newAllTokens[chainId] = { |
||||
...newAllTokens[chainId], |
||||
[accountAddress]: tokensArray, |
||||
}; |
||||
} |
||||
}); |
||||
}); |
||||
} |
||||
|
||||
const newAllIgnoredTokens = {}; |
||||
if (accountHiddenTokens) { |
||||
Object.keys(accountHiddenTokens).forEach((accountAddress) => { |
||||
Object.keys(accountHiddenTokens[accountAddress]).forEach((chainId) => { |
||||
const ignoredTokensArray = accountHiddenTokens[accountAddress][chainId]; |
||||
if (newAllIgnoredTokens[chainId] === undefined) { |
||||
newAllIgnoredTokens[chainId] = { |
||||
[accountAddress]: ignoredTokensArray, |
||||
}; |
||||
} else { |
||||
newAllIgnoredTokens[chainId] = { |
||||
...newAllIgnoredTokens[chainId], |
||||
[accountAddress]: ignoredTokensArray, |
||||
}; |
||||
} |
||||
}); |
||||
}); |
||||
} |
||||
|
||||
if (state.TokensController) { |
||||
state.TokensController.allTokens = newAllTokens; |
||||
state.TokensController.allIgnoredTokens = newAllIgnoredTokens; |
||||
} else { |
||||
state.TokensController = { |
||||
allTokens: newAllTokens, |
||||
allIgnoredTokens: newAllIgnoredTokens, |
||||
}; |
||||
} |
||||
|
||||
delete state?.PreferencesController?.accountHiddenTokens; |
||||
delete state?.PreferencesController?.accountTokens; |
||||
delete state?.PreferencesController?.assetImages; |
||||
delete state?.PreferencesController?.hiddenTokens; |
||||
delete state?.PreferencesController?.tokens; |
||||
delete state?.PreferencesController?.suggestedTokens; |
||||
|
||||
return state; |
||||
} |
@ -0,0 +1,251 @@ |
||||
import { strict as assert } from 'assert'; |
||||
import migration63 from './063'; |
||||
|
||||
describe('migration #63', function () { |
||||
it('should update the version metadata', async function () { |
||||
const oldStorage = { |
||||
meta: { |
||||
version: 62, |
||||
}, |
||||
data: {}, |
||||
}; |
||||
|
||||
const newStorage = await migration63.migrate(oldStorage); |
||||
assert.deepEqual(newStorage.meta, { |
||||
version: 63, |
||||
}); |
||||
}); |
||||
|
||||
it('should move accountTokens data from PreferencesController to TokensController allTokens field and rotate structure from [accountAddress][chainId] to [chainId][accountAddress]', async function () { |
||||
const oldAccountTokens = { |
||||
'0x00000000000': { |
||||
'0x1': [ |
||||
{ |
||||
address: '0x6b175474e89094c44da98b954eedeac495271d0f', |
||||
decimals: 18, |
||||
isERC721: false, |
||||
symbol: 'DAI', |
||||
}, |
||||
{ |
||||
address: '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984', |
||||
decimals: 18, |
||||
isERC721: false, |
||||
symbol: 'UNI', |
||||
}, |
||||
], |
||||
'0x89': [ |
||||
{ |
||||
address: '0x70d1f773a9f81c852087b77f6ae6d3032b02d2ab', |
||||
decimals: 18, |
||||
isERC721: false, |
||||
symbol: 'LINK', |
||||
}, |
||||
{ |
||||
address: '0xc2132d05d31c914a87c6611c10748aeb04b58e8f', |
||||
decimals: 6, |
||||
isERC721: false, |
||||
symbol: 'USDT', |
||||
}, |
||||
], |
||||
}, |
||||
'0x1111111111': { |
||||
'0x1': [ |
||||
{ |
||||
address: '0x6b175474e89094c44da98b954eedeac495271d0f', |
||||
decimals: 18, |
||||
isERC721: false, |
||||
symbol: 'FAI', |
||||
}, |
||||
{ |
||||
address: '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984', |
||||
decimals: 18, |
||||
isERC721: false, |
||||
symbol: 'PUNI', |
||||
}, |
||||
], |
||||
'0x89': [ |
||||
{ |
||||
address: '0x70d1f773a9f81c852087b77f6ae6d3032b02d2ab', |
||||
decimals: 18, |
||||
isERC721: false, |
||||
symbol: 'SLINK', |
||||
}, |
||||
{ |
||||
address: '0xc2132d05d31c914a87c6611c10748aeb04b58e8f', |
||||
decimals: 6, |
||||
isERC721: false, |
||||
symbol: 'USDC', |
||||
}, |
||||
], |
||||
}, |
||||
}; |
||||
|
||||
const expectedTokens = { |
||||
'0x1': { |
||||
'0x00000000000': [ |
||||
{ |
||||
address: '0x6b175474e89094c44da98b954eedeac495271d0f', |
||||
decimals: 18, |
||||
isERC721: false, |
||||
symbol: 'DAI', |
||||
}, |
||||
{ |
||||
address: '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984', |
||||
decimals: 18, |
||||
isERC721: false, |
||||
symbol: 'UNI', |
||||
}, |
||||
], |
||||
'0x1111111111': [ |
||||
{ |
||||
address: '0x6b175474e89094c44da98b954eedeac495271d0f', |
||||
decimals: 18, |
||||
isERC721: false, |
||||
symbol: 'FAI', |
||||
}, |
||||
{ |
||||
address: '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984', |
||||
decimals: 18, |
||||
isERC721: false, |
||||
symbol: 'PUNI', |
||||
}, |
||||
], |
||||
}, |
||||
'0x89': { |
||||
'0x00000000000': [ |
||||
{ |
||||
address: '0x70d1f773a9f81c852087b77f6ae6d3032b02d2ab', |
||||
decimals: 18, |
||||
isERC721: false, |
||||
symbol: 'LINK', |
||||
}, |
||||
{ |
||||
address: '0xc2132d05d31c914a87c6611c10748aeb04b58e8f', |
||||
decimals: 6, |
||||
isERC721: false, |
||||
symbol: 'USDT', |
||||
}, |
||||
], |
||||
'0x1111111111': [ |
||||
{ |
||||
address: '0x70d1f773a9f81c852087b77f6ae6d3032b02d2ab', |
||||
decimals: 18, |
||||
isERC721: false, |
||||
symbol: 'SLINK', |
||||
}, |
||||
{ |
||||
address: '0xc2132d05d31c914a87c6611c10748aeb04b58e8f', |
||||
decimals: 6, |
||||
isERC721: false, |
||||
symbol: 'USDC', |
||||
}, |
||||
], |
||||
}, |
||||
}; |
||||
|
||||
const oldStorage = { |
||||
meta: {}, |
||||
data: { |
||||
PreferencesController: { |
||||
completedOnboarding: true, |
||||
dismissSeedBackUpReminder: false, |
||||
accountTokens: oldAccountTokens, |
||||
}, |
||||
}, |
||||
}; |
||||
|
||||
const newStorage = await migration63.migrate(oldStorage); |
||||
assert.deepStrictEqual(newStorage.data, { |
||||
TokensController: { |
||||
allTokens: expectedTokens, |
||||
allIgnoredTokens: {}, |
||||
}, |
||||
PreferencesController: { |
||||
completedOnboarding: true, |
||||
dismissSeedBackUpReminder: false, |
||||
}, |
||||
}); |
||||
}); |
||||
|
||||
it('should move accountHiddenTokens data from PreferencesController to TokensController allIgnoredTokens field and rotate structure from [accountAddress][chainId] to [chainId][accountAddress]', async function () { |
||||
const oldStorage = { |
||||
meta: {}, |
||||
data: { |
||||
PreferencesController: { |
||||
completedOnboarding: true, |
||||
dismissSeedBackUpReminder: false, |
||||
accountTokens: {}, |
||||
accountHiddenTokens: { |
||||
'0x1111111111': { |
||||
'0x1': ['0x000000000000'], |
||||
'0x89': ['0x11111111111'], |
||||
}, |
||||
'0x222222': { |
||||
'0x4': ['0x000011112222'], |
||||
}, |
||||
'0x333333': { |
||||
'0x5': ['0x000022223333'], |
||||
'0x1': ['0x000033333344'], |
||||
}, |
||||
}, |
||||
}, |
||||
}, |
||||
}; |
||||
|
||||
const newStorage = await migration63.migrate(oldStorage); |
||||
assert.deepStrictEqual(newStorage.data, { |
||||
TokensController: { |
||||
allTokens: {}, |
||||
allIgnoredTokens: { |
||||
'0x1': { |
||||
'0x1111111111': ['0x000000000000'], |
||||
'0x333333': ['0x000033333344'], |
||||
}, |
||||
'0x89': { |
||||
'0x1111111111': ['0x11111111111'], |
||||
}, |
||||
'0x4': { |
||||
'0x222222': ['0x000011112222'], |
||||
}, |
||||
'0x5': { |
||||
'0x333333': ['0x000022223333'], |
||||
}, |
||||
}, |
||||
}, |
||||
PreferencesController: { |
||||
completedOnboarding: true, |
||||
dismissSeedBackUpReminder: false, |
||||
}, |
||||
}); |
||||
}); |
||||
|
||||
it('should should remove all token related state from the preferences controller', async function () { |
||||
const oldStorage = { |
||||
meta: {}, |
||||
data: { |
||||
PreferencesController: { |
||||
completedOnboarding: true, |
||||
dismissSeedBackUpReminder: false, |
||||
accountTokens: {}, |
||||
accountHiddenTokens: {}, |
||||
tokens: {}, |
||||
hiddenTokens: {}, |
||||
assetImages: {}, |
||||
suggestedTokens: {}, |
||||
}, |
||||
}, |
||||
}; |
||||
|
||||
const newStorage = await migration63.migrate(oldStorage); |
||||
assert.deepStrictEqual(newStorage.data, { |
||||
PreferencesController: { |
||||
completedOnboarding: true, |
||||
dismissSeedBackUpReminder: false, |
||||
}, |
||||
TokensController: { |
||||
allTokens: {}, |
||||
allIgnoredTokens: {}, |
||||
}, |
||||
}); |
||||
}); |
||||
}); |
@ -1,150 +1,165 @@ |
||||
{ |
||||
"data": { |
||||
"AlertController": { |
||||
"alertEnabledness": { |
||||
"unconnectedAccount": true, |
||||
"web3ShimUsage": true |
||||
}, |
||||
"unconnectedAccountAlertShownOrigins": {}, |
||||
"web3ShimUsageOrigins": {} |
||||
}, |
||||
"AppStateController": { |
||||
"mkrMigrationReminderTimestamp": null |
||||
"connectedStatusPopoverHasBeenShown": true, |
||||
"defaultHomeActiveTabName": null, |
||||
"recoveryPhraseReminderHasBeenShown": true, |
||||
"recoveryPhraseReminderLastShown": 1627317428214 |
||||
}, |
||||
"CachedBalancesController": { |
||||
"cachedBalances": { |
||||
"4": {} |
||||
"0x4": { |
||||
"0x5cfe73b6021e818b776b421b1c4db2474086a7e1": "0x0" |
||||
} |
||||
} |
||||
}, |
||||
"CurrencyController": { |
||||
"conversionDate": 1575697244.188, |
||||
"conversionRate": 149.61, |
||||
"conversionDate": 1626907353.891, |
||||
"conversionRate": 1968.5, |
||||
"currentCurrency": "usd", |
||||
"nativeCurrency": "ETH" |
||||
"nativeCurrency": "ETH", |
||||
"pendingCurrentCurrency": null, |
||||
"pendingNativeCurrency": null, |
||||
"usdConversionRate": 1968.5 |
||||
}, |
||||
"IncomingTransactionsController": { |
||||
"incomingTransactions": {}, |
||||
"incomingTxLastFetchedBlocksByNetwork": { |
||||
"goerli": null, |
||||
"kovan": null, |
||||
"mainnet": null, |
||||
"rinkeby": 5570536 |
||||
"incomingTxLastFetchedBlockByChainId": { |
||||
"0x1": null, |
||||
"0x2a": null, |
||||
"0x3": null, |
||||
"0x4": 8977934, |
||||
"0x5": null |
||||
} |
||||
}, |
||||
"KeyringController": { |
||||
"vault": "{\"data\":\"s6TpYjlUNsn7ifhEFTkuDGBUM1GyOlPrim7JSjtfIxgTt8/6MiXgiR/CtFfR4dWW2xhq85/NGIBYEeWrZThGdKGarBzeIqBfLFhw9n509jprzJ0zc2Rf+9HVFGLw+xxC4xPxgCS0IIWeAJQ+XtGcHmn0UZXriXm8Ja4kdlow6SWinB7sr/WM3R0+frYs4WgllkwggDf2/Tv6VHygvLnhtzp6hIJFyTjh+l/KnyJTyZW1TkZhDaNDzX3SCOHT\",\"iv\":\"FbeHDAW5afeWNORfNJBR0Q==\",\"salt\":\"TxZ+WbCW6891C9LK/hbMAoUsSEW1E8pyGLVBU6x5KR8=\"}" |
||||
}, |
||||
"MetaMetricsController": { |
||||
"metaMetricsId": "0xff3e952b9f5a27ffcab42b0b4abf689e77dcc1f9f441871dc962d622b089fb51", |
||||
"participateInMetaMetrics": true |
||||
}, |
||||
"NetworkController": { |
||||
"network": "1337", |
||||
"networkDetails": { |
||||
"EIPS": {} |
||||
}, |
||||
"previousProviderStore": { |
||||
"chainId": "0x4", |
||||
"ticker": "ETH", |
||||
"type": "rinkeby" |
||||
}, |
||||
"provider": { |
||||
"chainId": "0x539", |
||||
"nickname": "Localhost 8545", |
||||
"rpcPrefs": {}, |
||||
"rpcUrl": "http://localhost:8545", |
||||
"chainId": "0x539", |
||||
"ticker": "ETH", |
||||
"type": "rpc" |
||||
} |
||||
}, |
||||
"NotificationController": { |
||||
"notifications": { |
||||
"1": { |
||||
"isShown": true |
||||
}, |
||||
"3": { |
||||
"isShown": true |
||||
}, |
||||
"5": { |
||||
"isShown": true |
||||
}, |
||||
"6": { |
||||
"isShown": true |
||||
} |
||||
} |
||||
"notifications": {} |
||||
}, |
||||
"OnboardingController": { |
||||
"onboardingTabs": {}, |
||||
"seedPhraseBackedUp": false |
||||
"seedPhraseBackedUp": true |
||||
}, |
||||
"PermissionsMetadata": { |
||||
"domainMetadata": { |
||||
"metamask.github.io": { |
||||
"icon": null, |
||||
"name": "M E T A M A S K M E S H T E S T" |
||||
} |
||||
}, |
||||
"permissionsHistory": {}, |
||||
"permissionsLog": [ |
||||
{ |
||||
"id": 746677923, |
||||
"method": "eth_accounts", |
||||
"methodType": "restricted", |
||||
"origin": "metamask.github.io", |
||||
"request": { |
||||
"id": 746677923, |
||||
"jsonrpc": "2.0", |
||||
"method": "eth_accounts", |
||||
"origin": "metamask.github.io", |
||||
"params": [] |
||||
}, |
||||
"requestTime": 1575697241368, |
||||
"response": { |
||||
"id": 746677923, |
||||
"jsonrpc": "2.0", |
||||
"result": [] |
||||
}, |
||||
"responseTime": 1575697241370, |
||||
"success": true |
||||
} |
||||
] |
||||
"PermissionsController": { |
||||
"domains": {}, |
||||
"permissionsDescriptions": {}, |
||||
"permissionsRequests": [] |
||||
}, |
||||
"PreferencesController": { |
||||
"accountTokens": { |
||||
"0x5cfe73b6021e818b776b421b1c4db2474086a7e1": { |
||||
"0x539": [ |
||||
{ |
||||
"address": "0x86002be4cdd922de1ccb831582bf99284b99ac12", |
||||
"symbol": "TST", |
||||
"decimals": 4 |
||||
} |
||||
], |
||||
"rinkeby": [], |
||||
"ropsten": [] |
||||
} |
||||
}, |
||||
"assetImages": {}, |
||||
"completedOnboarding": true, |
||||
"currentLocale": "en", |
||||
"dismissSeedBackUpReminder": true, |
||||
"featureFlags": { |
||||
"showIncomingTransactions": true, |
||||
"transactionTime": false |
||||
"showIncomingTransactions": true |
||||
}, |
||||
"firstTimeFlowType": "create", |
||||
"firstTimeFlowType": "import", |
||||
"forgottenPassword": false, |
||||
"frequentRpcListDetail": [], |
||||
"frequentRpcListDetail": [ |
||||
{ |
||||
"chainId": "0x539", |
||||
"nickname": "Localhost 8545", |
||||
"rpcPrefs": {}, |
||||
"rpcUrl": "http://localhost:8545", |
||||
"ticker": "ETH" |
||||
} |
||||
], |
||||
"identities": { |
||||
"0x5cfe73b6021e818b776b421b1c4db2474086a7e1": { |
||||
"address": "0x5cfe73b6021e818b776b421b1c4db2474086a7e1", |
||||
"lastSelected": 1626907346643, |
||||
"name": "Account 1" |
||||
} |
||||
}, |
||||
"infuraBlocked": false, |
||||
"ipfsGateway": "dweb.link", |
||||
"knownMethodData": {}, |
||||
"lostIdentities": {}, |
||||
"metaMetricsId": null, |
||||
"participateInMetaMetrics": false, |
||||
"preferences": { |
||||
"hideZeroBalanceTokens": false, |
||||
"showFiatInTestnets": false, |
||||
"useNativeCurrencyAsPrimaryCurrency": true |
||||
}, |
||||
"selectedAddress": "0x5cfe73b6021e818b776b421b1c4db2474086a7e1", |
||||
"suggestedTokens": {}, |
||||
"useBlockie": false, |
||||
"useLedgerLive": false, |
||||
"useNonceField": false, |
||||
"usePhishDetect": true, |
||||
"useStaticTokenList": false |
||||
}, |
||||
"TokenListController": { |
||||
"tokenList": {}, |
||||
"tokensChainsCache": {} |
||||
}, |
||||
"TokensController": { |
||||
"allTokens": { |
||||
"0x539": { |
||||
"0x5cfe73b6021e818b776b421b1c4db2474086a7e1": [ |
||||
{ |
||||
"address": "0x86002be4cdd922de1ccb831582bf99284b99ac12", |
||||
"decimals": 4, |
||||
"image": null, |
||||
"isERC721": false, |
||||
"symbol": "TST" |
||||
} |
||||
] |
||||
} |
||||
}, |
||||
"ignoredTokens": [], |
||||
"suggestedAssets": [], |
||||
"allIgnoredTokens": {}, |
||||
"tokens": [ |
||||
{ |
||||
"address": "0x86002be4cdd922de1ccb831582bf99284b99ac12", |
||||
"symbol": "TST", |
||||
"decimals": 4 |
||||
"decimals": 4, |
||||
"image": null, |
||||
"isERC721": false, |
||||
"symbol": "TST" |
||||
} |
||||
], |
||||
"useBlockie": false, |
||||
"useNonceField": false, |
||||
"usePhishDetect": true |
||||
] |
||||
}, |
||||
"TransactionController": { |
||||
"transactions": {} |
||||
}, |
||||
"config": {}, |
||||
"firstTimeInfo": { |
||||
"date": 1575697234195, |
||||
"version": "7.7.0" |
||||
"date": 1626907328205, |
||||
"version": "9.8.1" |
||||
} |
||||
}, |
||||
"meta": { |
||||
"version": 40 |
||||
"version": 63 |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue