mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Migrate 'localhost' tokens (#9570)
* Delete localhost state from incoming tx controller * Migrate localhost tokens Co-authored-by: Dan Finlay <542863+danfinlay@users.noreply.github.com>
This commit is contained in:
parent
ded43cbcdf
commit
6e8c9aede8
@ -12,6 +12,7 @@ const version = 48
|
|||||||
* 5. Convert transactions metamaskNetworkId to decimal if they are hex
|
* 5. Convert transactions metamaskNetworkId to decimal if they are hex
|
||||||
* 6. Convert address book keys from decimal to hex
|
* 6. Convert address book keys from decimal to hex
|
||||||
* 7. Delete localhost key in IncomingTransactionsController
|
* 7. Delete localhost key in IncomingTransactionsController
|
||||||
|
* 8. Merge 'localhost' tokens into 'rpc' tokens
|
||||||
*/
|
*/
|
||||||
export default {
|
export default {
|
||||||
version,
|
version,
|
||||||
@ -110,6 +111,25 @@ function transformState (state = {}) {
|
|||||||
?.incomingTxLastFetchedBlocksByNetwork
|
?.incomingTxLastFetchedBlocksByNetwork
|
||||||
?.localhost
|
?.localhost
|
||||||
|
|
||||||
|
// 8. Merge 'localhost' tokens into 'rpc' tokens
|
||||||
|
const accountTokens = state.PreferencesController?.accountTokens
|
||||||
|
if (accountTokens) {
|
||||||
|
Object.keys(accountTokens).forEach((account) => {
|
||||||
|
const localhostTokens = accountTokens[account]?.localhost || []
|
||||||
|
|
||||||
|
if (localhostTokens.length > 0) {
|
||||||
|
const rpcTokens = accountTokens[account].rpc || []
|
||||||
|
|
||||||
|
if (rpcTokens.length > 0) {
|
||||||
|
accountTokens[account].rpc = mergeTokenArrays(localhostTokens, rpcTokens)
|
||||||
|
} else {
|
||||||
|
accountTokens[account].rpc = localhostTokens
|
||||||
|
}
|
||||||
|
}
|
||||||
|
delete accountTokens[account]?.localhost
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,3 +189,36 @@ function updateChainIds (networkEntries, chainId) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Merges the two given, non-empty arrays of token objects and returns a new
|
||||||
|
* array.
|
||||||
|
*
|
||||||
|
* @returns {Array<Object>}
|
||||||
|
*/
|
||||||
|
function mergeTokenArrays (localhostTokens, rpcTokens) {
|
||||||
|
const localhostTokensMap = tokenArrayToMap(localhostTokens)
|
||||||
|
const rpcTokensMap = tokenArrayToMap(rpcTokens)
|
||||||
|
|
||||||
|
const mergedTokens = []
|
||||||
|
new Set([
|
||||||
|
...Object.keys(localhostTokensMap),
|
||||||
|
...Object.keys(rpcTokensMap),
|
||||||
|
]).forEach((tokenAddress) => {
|
||||||
|
mergedTokens.push({
|
||||||
|
...localhostTokensMap[tokenAddress],
|
||||||
|
...rpcTokensMap[tokenAddress],
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
return mergedTokens
|
||||||
|
|
||||||
|
function tokenArrayToMap (array) {
|
||||||
|
return array.reduce((map, token) => {
|
||||||
|
if (token?.address && typeof token?.address === 'string') {
|
||||||
|
map[token.address] = token
|
||||||
|
}
|
||||||
|
return map
|
||||||
|
}, {})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -575,4 +575,62 @@ describe('migration #48', function () {
|
|||||||
foo: 'bar',
|
foo: 'bar',
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should merge localhost token list into rpc token list', async function () {
|
||||||
|
const oldStorage = {
|
||||||
|
meta: {},
|
||||||
|
data: {
|
||||||
|
PreferencesController: {
|
||||||
|
accountTokens: {
|
||||||
|
address1: {
|
||||||
|
localhost: [
|
||||||
|
{ address: '1', data1: 'stuff1' },
|
||||||
|
{ address: '2', a: 'X', b: 'B' },
|
||||||
|
],
|
||||||
|
rpc: [
|
||||||
|
{ address: '2', a: 'A', c: 'C' },
|
||||||
|
{ address: '3', data3: 'stuff3' },
|
||||||
|
],
|
||||||
|
foo: [],
|
||||||
|
},
|
||||||
|
address2: {
|
||||||
|
localhost: [],
|
||||||
|
rpc: [],
|
||||||
|
foo: [],
|
||||||
|
},
|
||||||
|
address3: {},
|
||||||
|
},
|
||||||
|
bar: 'baz',
|
||||||
|
},
|
||||||
|
foo: 'bar',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
const newStorage = await migration48.migrate(oldStorage)
|
||||||
|
assert.deepEqual(newStorage.data, {
|
||||||
|
PreferencesController: {
|
||||||
|
accountTokens: {
|
||||||
|
address1: {
|
||||||
|
rpc: [
|
||||||
|
{ address: '1', data1: 'stuff1' },
|
||||||
|
{ address: '2', a: 'A', b: 'B', c: 'C' },
|
||||||
|
{ address: '3', data3: 'stuff3' },
|
||||||
|
],
|
||||||
|
foo: [],
|
||||||
|
},
|
||||||
|
address2: {
|
||||||
|
rpc: [],
|
||||||
|
foo: [],
|
||||||
|
},
|
||||||
|
address3: {},
|
||||||
|
},
|
||||||
|
bar: 'baz',
|
||||||
|
// from other migration
|
||||||
|
frequentRpcListDetail: [{
|
||||||
|
...localhostNetwork,
|
||||||
|
}],
|
||||||
|
},
|
||||||
|
foo: 'bar',
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user