1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-29 15:50:28 +01:00
metamask-extension/app/scripts/migrations/057.js

45 lines
1.3 KiB
JavaScript
Raw Normal View History

import { cloneDeep, keyBy } from 'lodash';
import createId from '../../../shared/modules/random-id';
const version = 57;
/**
* replace 'incomingTxLastFetchedBlocksByNetwork' with 'incomingTxLastFetchedBlockByChainId'
*/
export default {
version,
async migrate(originalVersionedData) {
const versionedData = cloneDeep(originalVersionedData);
versionedData.meta.version = version;
const state = versionedData.data;
versionedData.data = transformState(state);
return versionedData;
},
};
function transformState(state) {
if (
state?.TransactionController?.transactions &&
Array.isArray(state.TransactionController.transactions) &&
!state.TransactionController.transactions.some(
(item) =>
typeof item !== 'object' || typeof item.txParams === 'undefined',
)
) {
state.TransactionController.transactions = keyBy(
state.TransactionController.transactions,
// In case for some reason any of a user's transactions do not have an id
// generate a new one for the transaction.
(tx) => {
if (typeof tx.id === 'undefined' || tx.id === null) {
// This mutates the item in the array, so will result in a change to
// the state.
tx.id = createId();
}
return tx.id;
},
);
}
return state;
}