|
| 1 | +import { isSolanaChainId } from '@metamask/bridge-controller'; |
| 2 | +import { type BridgeStatusControllerState } from '@metamask/bridge-status-controller'; |
| 3 | +import { cloneDeep } from 'lodash'; |
| 4 | + |
| 5 | +export const version = 174; |
| 6 | + |
| 7 | +/** |
| 8 | + * This migration fixes txHistory entries that were added with keys that were not valid |
| 9 | + * Solana tx signatures. |
| 10 | + * |
| 11 | + * For existing txs with invalid keys, the migration will remove the txHistory entry and add |
| 12 | + * it back with a valid key. |
| 13 | + * |
| 14 | + * @param originalVersionedData - Versioned MetaMask extension state, exactly what we persist to dist. |
| 15 | + * @param originalVersionedData.meta - State metadata. |
| 16 | + * @param originalVersionedData.meta.version - The current state version. |
| 17 | + * @param originalVersionedData.data - The persisted MetaMask state, keyed by controller. |
| 18 | + * @returns Updated versioned MetaMask extension state. |
| 19 | + */ |
| 20 | +export async function migrate(originalVersionedData: { |
| 21 | + meta: { version: number }; |
| 22 | + data: Record<string, unknown>; |
| 23 | +}) { |
| 24 | + const versionedData = cloneDeep(originalVersionedData); |
| 25 | + versionedData.meta.version = version; |
| 26 | + versionedData.data = transformState(versionedData.data); |
| 27 | + return versionedData; |
| 28 | +} |
| 29 | + |
| 30 | +function transformState(state: Record<string, unknown>) { |
| 31 | + const bridgeStatusControllerState = state?.BridgeStatusController; |
| 32 | + |
| 33 | + if ( |
| 34 | + !bridgeStatusControllerState || |
| 35 | + typeof bridgeStatusControllerState !== 'object' |
| 36 | + ) { |
| 37 | + return state; |
| 38 | + } |
| 39 | + |
| 40 | + const { txHistory } = |
| 41 | + bridgeStatusControllerState as BridgeStatusControllerState; |
| 42 | + |
| 43 | + if (!txHistory || Object.keys(txHistory).length === 0) { |
| 44 | + return state; |
| 45 | + } |
| 46 | + |
| 47 | + const newTxHistory = Object.entries< |
| 48 | + BridgeStatusControllerState['txHistory'][string] |
| 49 | + >(txHistory as unknown as BridgeStatusControllerState['txHistory']).reduce< |
| 50 | + Record<string, BridgeStatusControllerState['txHistory'][string]> |
| 51 | + >((acc, [key, historyItem]) => { |
| 52 | + // Check if src chain is solana |
| 53 | + const srcChainId = |
| 54 | + historyItem.status?.srcChain?.chainId ?? historyItem.quote?.srcChainId; |
| 55 | + const isSolanaTx = isSolanaChainId(srcChainId); |
| 56 | + // If solana tx, use the src chain tx hash as the key |
| 57 | + const newKey = isSolanaTx ? historyItem.status?.srcChain?.txHash : key; |
| 58 | + |
| 59 | + return { |
| 60 | + ...acc, |
| 61 | + [newKey ?? key]: { ...historyItem }, |
| 62 | + }; |
| 63 | + }, {}); |
| 64 | + |
| 65 | + const newState = { |
| 66 | + ...state, |
| 67 | + BridgeStatusController: { |
| 68 | + ...bridgeStatusControllerState, |
| 69 | + txHistory: newTxHistory, |
| 70 | + }, |
| 71 | + }; |
| 72 | + |
| 73 | + return newState; |
| 74 | +} |
0 commit comments