1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 18:00:18 +01:00
metamask-extension/app/scripts/migrations/057.js
Brad Decker 4080ed63a4
Refactor Tx State Manager (#10672)
Co-authored-by: Mark Stacey <markjstacey@gmail.com>
2021-03-30 09:54:05 -05:00

45 lines
1.3 KiB
JavaScript

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;
}