2021-02-04 19:15:23 +01:00
|
|
|
import { cloneDeep } from 'lodash';
|
2023-01-18 15:47:29 +01:00
|
|
|
import { TransactionStatus } from '../../../shared/constants/transaction';
|
2018-11-17 06:27:01 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export default function failTxsThat(version, reason, condition) {
|
2018-11-17 06:27:01 +01:00
|
|
|
return function (originalVersionedData) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const versionedData = cloneDeep(originalVersionedData);
|
|
|
|
versionedData.meta.version = version;
|
2018-11-17 06:27:01 +01:00
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
const state = versionedData.data;
|
|
|
|
const newState = transformState(state, condition, reason);
|
|
|
|
versionedData.data = newState;
|
2018-11-17 06:27:01 +01:00
|
|
|
} catch (err) {
|
2021-02-04 19:15:23 +01:00
|
|
|
console.warn(`MetaMask Migration #${version}${err.stack}`);
|
2018-11-17 06:27:01 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return Promise.resolve(versionedData);
|
|
|
|
};
|
2018-11-17 06:27:01 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function transformState(state, condition, reason) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const newState = state;
|
|
|
|
const { TransactionController } = newState;
|
2018-11-17 06:27:01 +01:00
|
|
|
if (TransactionController && TransactionController.transactions) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { transactions } = TransactionController;
|
2018-11-17 06:27:01 +01:00
|
|
|
|
|
|
|
newState.TransactionController.transactions = transactions.map((txMeta) => {
|
|
|
|
if (!condition(txMeta)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return txMeta;
|
2018-11-17 06:27:01 +01:00
|
|
|
}
|
|
|
|
|
2023-01-18 15:47:29 +01:00
|
|
|
txMeta.status = TransactionStatus.failed;
|
2018-11-17 06:27:01 +01:00
|
|
|
txMeta.err = {
|
|
|
|
message: reason,
|
|
|
|
note: `Tx automatically failed by migration because ${reason}`,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-11-17 06:27:01 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return txMeta;
|
|
|
|
});
|
2018-11-17 06:27:01 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return newState;
|
2018-11-17 06:27:01 +01:00
|
|
|
}
|