|
| 1 | +import { |
| 2 | + DelegationV2, |
| 3 | + DelegationV2StakingState, |
| 4 | +} from "@/ui/common/types/delegationsV2"; |
| 5 | + |
| 6 | +import { getExpansionsLocalStorageKey } from "./getExpansionsLocalStorageKey"; |
| 7 | + |
| 8 | +/** |
| 9 | + * Helper function to remove an item from a localStorage record. |
| 10 | + * This centralizes the localStorage record manipulation pattern used throughout the file. |
| 11 | + * |
| 12 | + * @param storageKey - The localStorage key |
| 13 | + * @param itemId - The ID of the item to remove from the record |
| 14 | + */ |
| 15 | +function removeFromLocalStorageRecord( |
| 16 | + storageKey: string, |
| 17 | + itemId: string, |
| 18 | +): void { |
| 19 | + try { |
| 20 | + const data = localStorage.getItem(storageKey); |
| 21 | + if (data) { |
| 22 | + const record = JSON.parse(data); |
| 23 | + if (record[itemId]) { |
| 24 | + delete record[itemId]; |
| 25 | + localStorage.setItem(storageKey, JSON.stringify(record)); |
| 26 | + } |
| 27 | + } |
| 28 | + } catch (error) { |
| 29 | + console.error( |
| 30 | + `Failed to remove item from localStorage key ${storageKey}:`, |
| 31 | + error, |
| 32 | + ); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Mark an expansion as broadcasted by updating its status to INTERMEDIATE_PENDING_BTC_CONFIRMATION. |
| 38 | + * This is called after a verified expansion is successfully signed and broadcasted to Bitcoin. |
| 39 | + * |
| 40 | + * @param expansionTxHashHex - The transaction hash of the expansion to mark as broadcasted |
| 41 | + * @param publicKeyNoCoord - The public key of the wallet |
| 42 | + */ |
| 43 | +export function markExpansionAsBroadcasted( |
| 44 | + expansionTxHashHex: string, |
| 45 | + publicKeyNoCoord: string | undefined, |
| 46 | +): void { |
| 47 | + if (!publicKeyNoCoord) { |
| 48 | + console.warn( |
| 49 | + "Cannot mark expansion as broadcasted: no public key provided", |
| 50 | + ); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + const storageKey = getExpansionsLocalStorageKey(publicKeyNoCoord); |
| 55 | + const statusesKey = `${storageKey}_statuses`; |
| 56 | + |
| 57 | + try { |
| 58 | + // Get existing statuses from localStorage |
| 59 | + const existingStatuses = localStorage.getItem(statusesKey); |
| 60 | + const statuses = existingStatuses ? JSON.parse(existingStatuses) : {}; |
| 61 | + |
| 62 | + // Update the status for this expansion |
| 63 | + statuses[expansionTxHashHex] = |
| 64 | + DelegationV2StakingState.INTERMEDIATE_PENDING_BTC_CONFIRMATION; |
| 65 | + |
| 66 | + // Save back to localStorage |
| 67 | + localStorage.setItem(statusesKey, JSON.stringify(statuses)); |
| 68 | + } catch (error) { |
| 69 | + console.error("Failed to mark expansion as broadcasted:", error); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Get all broadcasted expansions from localStorage. |
| 75 | + * Broadcasted expansions are those with INTERMEDIATE_PENDING_BTC_CONFIRMATION status. |
| 76 | + * |
| 77 | + * @param publicKeyNoCoord - The public key of the wallet |
| 78 | + * @param expansions - The list of expansions to check against localStorage |
| 79 | + * @returns Array of expansions that have been broadcasted |
| 80 | + */ |
| 81 | +export function getBroadcastedExpansions( |
| 82 | + publicKeyNoCoord: string | undefined, |
| 83 | + expansions: DelegationV2[], |
| 84 | +): DelegationV2[] { |
| 85 | + if (!publicKeyNoCoord || !expansions || expansions.length === 0) { |
| 86 | + return []; |
| 87 | + } |
| 88 | + |
| 89 | + const storageKey = getExpansionsLocalStorageKey(publicKeyNoCoord); |
| 90 | + const statusesKey = `${storageKey}_statuses`; |
| 91 | + |
| 92 | + try { |
| 93 | + // Get statuses from localStorage |
| 94 | + const storedStatuses = localStorage.getItem(statusesKey); |
| 95 | + if (!storedStatuses) { |
| 96 | + return []; |
| 97 | + } |
| 98 | + |
| 99 | + const statuses = JSON.parse(storedStatuses); |
| 100 | + |
| 101 | + // Filter expansions to only include those marked as broadcasted |
| 102 | + return expansions.filter( |
| 103 | + (expansion) => |
| 104 | + statuses[expansion.stakingTxHashHex] === |
| 105 | + DelegationV2StakingState.INTERMEDIATE_PENDING_BTC_CONFIRMATION, |
| 106 | + ); |
| 107 | + } catch (error) { |
| 108 | + console.error("Failed to get broadcasted expansions:", error); |
| 109 | + return []; |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * Clean up a broadcasted expansion from localStorage when it becomes ACTIVE. |
| 115 | + * This is called when an expansion transitions from broadcasted to confirmed on-chain. |
| 116 | + * |
| 117 | + * @param expansionTxHashHex - The transaction hash of the expansion to clean up |
| 118 | + * @param publicKeyNoCoord - The public key of the wallet |
| 119 | + */ |
| 120 | +export function cleanupActiveExpansion( |
| 121 | + expansionTxHashHex: string, |
| 122 | + publicKeyNoCoord: string | undefined, |
| 123 | +): void { |
| 124 | + if (!publicKeyNoCoord) { |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + const storageKey = getExpansionsLocalStorageKey(publicKeyNoCoord); |
| 129 | + const pendingKey = `${storageKey}_pending`; |
| 130 | + const statusesKey = `${storageKey}_statuses`; |
| 131 | + |
| 132 | + // Remove from pending delegations |
| 133 | + removeFromLocalStorageRecord(pendingKey, expansionTxHashHex); |
| 134 | + |
| 135 | + // Remove from statuses |
| 136 | + removeFromLocalStorageRecord(statusesKey, expansionTxHashHex); |
| 137 | +} |
| 138 | + |
| 139 | +/** |
| 140 | + * Check if an expansion has been broadcasted. |
| 141 | + * This is a convenience function that checks if the expansion has |
| 142 | + * INTERMEDIATE_PENDING_BTC_CONFIRMATION status in localStorage. |
| 143 | + * |
| 144 | + * @param expansionTxHashHex - The transaction hash to check |
| 145 | + * @param publicKeyNoCoord - The public key of the wallet |
| 146 | + * @returns true if the expansion has been broadcasted, false otherwise |
| 147 | + */ |
| 148 | +export function isExpansionBroadcasted( |
| 149 | + expansionTxHashHex: string, |
| 150 | + publicKeyNoCoord: string | undefined, |
| 151 | +): boolean { |
| 152 | + if (!publicKeyNoCoord) { |
| 153 | + return false; |
| 154 | + } |
| 155 | + |
| 156 | + const storageKey = getExpansionsLocalStorageKey(publicKeyNoCoord); |
| 157 | + const statusesKey = `${storageKey}_statuses`; |
| 158 | + |
| 159 | + try { |
| 160 | + const statusesData = localStorage.getItem(statusesKey); |
| 161 | + if (!statusesData) { |
| 162 | + return false; |
| 163 | + } |
| 164 | + |
| 165 | + const statuses = JSON.parse(statusesData); |
| 166 | + return ( |
| 167 | + statuses[expansionTxHashHex] === |
| 168 | + DelegationV2StakingState.INTERMEDIATE_PENDING_BTC_CONFIRMATION |
| 169 | + ); |
| 170 | + } catch (error) { |
| 171 | + console.error("Failed to check if expansion is broadcasted:", error); |
| 172 | + return false; |
| 173 | + } |
| 174 | +} |
0 commit comments