|
| 1 | +/** |
| 2 | + * BroadcastSignModal |
| 3 | + * |
| 4 | + * Modal for signing and broadcasting BTC transaction to Bitcoin network. |
| 5 | + * Opens when user clicks "Sign & Broadcast" button from deposits table. |
| 6 | + * |
| 7 | + * Shows provider steps during broadcast process. |
| 8 | + */ |
| 9 | + |
| 10 | +import { |
| 11 | + Button, |
| 12 | + DialogBody, |
| 13 | + DialogFooter, |
| 14 | + DialogHeader, |
| 15 | + Loader, |
| 16 | + ResponsiveDialog, |
| 17 | + Text, |
| 18 | +} from "@babylonlabs-io/core-ui"; |
| 19 | +import { useState } from "react"; |
| 20 | + |
| 21 | +import { useVaultActivityActions } from "../../../../hooks/useVaultActivityActions"; |
| 22 | +import { usePeginStorage } from "../../../../storage/usePeginStorage"; |
| 23 | +import type { VaultActivity } from "../../../../types/activity"; |
| 24 | + |
| 25 | +interface BroadcastSignModalProps { |
| 26 | + /** Modal open state */ |
| 27 | + open: boolean; |
| 28 | + /** Close handler */ |
| 29 | + onClose: () => void; |
| 30 | + /** The deposit/activity to broadcast for */ |
| 31 | + activity: VaultActivity; |
| 32 | + /** Depositor's ETH address */ |
| 33 | + depositorEthAddress: string; |
| 34 | + /** Success callback - refetch activities and show success modal */ |
| 35 | + onSuccess: () => void; |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Format address for display (first 6 and last 4 characters) |
| 40 | + */ |
| 41 | +function formatAddress(address: string): string { |
| 42 | + if (address.length < 10) return address; |
| 43 | + return `${address.slice(0, 6)}...${address.slice(-4)}`; |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Provider step component |
| 48 | + */ |
| 49 | +function ProviderStep({ |
| 50 | + step, |
| 51 | + providerName, |
| 52 | + providerAddress, |
| 53 | + isActive, |
| 54 | +}: { |
| 55 | + step: number; |
| 56 | + providerName?: string; |
| 57 | + providerAddress: string; |
| 58 | + isActive: boolean; |
| 59 | +}) { |
| 60 | + return ( |
| 61 | + <div className="flex items-start gap-3"> |
| 62 | + <div |
| 63 | + className={`flex h-8 w-8 flex-shrink-0 items-center justify-center rounded-full text-sm font-medium ${ |
| 64 | + isActive |
| 65 | + ? "bg-primary-main text-primary-contrast" |
| 66 | + : "bg-accent-secondary/20 text-accent-secondary" |
| 67 | + }`} |
| 68 | + > |
| 69 | + {isActive ? ( |
| 70 | + <Loader size={16} className="text-primary-contrast" /> |
| 71 | + ) : ( |
| 72 | + step |
| 73 | + )} |
| 74 | + </div> |
| 75 | + <div className="flex-1 pt-1"> |
| 76 | + <Text |
| 77 | + variant="body2" |
| 78 | + className={`text-sm ${isActive ? "font-medium text-accent-primary" : "text-accent-secondary"}`} |
| 79 | + > |
| 80 | + {providerName || formatAddress(providerAddress)} payout |
| 81 | + </Text> |
| 82 | + </div> |
| 83 | + </div> |
| 84 | + ); |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Modal for broadcasting BTC transaction |
| 89 | + * |
| 90 | + * Shows provider steps and handles the broadcast process. |
| 91 | + */ |
| 92 | +export function BroadcastSignModal({ |
| 93 | + open, |
| 94 | + onClose, |
| 95 | + activity, |
| 96 | + depositorEthAddress, |
| 97 | + onSuccess, |
| 98 | +}: BroadcastSignModalProps) { |
| 99 | + const [localBroadcasting, setLocalBroadcasting] = useState(false); |
| 100 | + |
| 101 | + const { broadcasting, broadcastError, handleBroadcast } = |
| 102 | + useVaultActivityActions(); |
| 103 | + |
| 104 | + const { pendingPegins, updatePendingPeginStatus, addPendingPegin } = |
| 105 | + usePeginStorage({ |
| 106 | + ethAddress: depositorEthAddress, |
| 107 | + confirmedPegins: [], |
| 108 | + }); |
| 109 | + |
| 110 | + const pendingPegin = pendingPegins.find((p) => p.id === activity.id); |
| 111 | + |
| 112 | + const handleSign = async () => { |
| 113 | + setLocalBroadcasting(true); |
| 114 | + |
| 115 | + try { |
| 116 | + await handleBroadcast({ |
| 117 | + activityId: activity.id, |
| 118 | + activityAmount: activity.collateral.amount, |
| 119 | + activityProviders: activity.providers, |
| 120 | + connectedAddress: depositorEthAddress, |
| 121 | + pendingPegin, |
| 122 | + updatePendingPeginStatus, |
| 123 | + addPendingPegin, |
| 124 | + onRefetchActivities: () => { |
| 125 | + // Will be called after broadcast |
| 126 | + }, |
| 127 | + onShowSuccessModal: () => { |
| 128 | + setLocalBroadcasting(false); |
| 129 | + onSuccess(); |
| 130 | + }, |
| 131 | + }); |
| 132 | + } catch { |
| 133 | + setLocalBroadcasting(false); |
| 134 | + // Error is already set in the hook |
| 135 | + } |
| 136 | + }; |
| 137 | + |
| 138 | + const isBroadcasting = broadcasting || localBroadcasting; |
| 139 | + |
| 140 | + return ( |
| 141 | + <ResponsiveDialog |
| 142 | + open={open} |
| 143 | + onClose={isBroadcasting ? undefined : onClose} |
| 144 | + > |
| 145 | + <DialogHeader |
| 146 | + title="Sign BTC Transaction" |
| 147 | + onClose={isBroadcasting ? undefined : onClose} |
| 148 | + className="text-accent-primary" |
| 149 | + /> |
| 150 | + |
| 151 | + <DialogBody className="flex flex-col gap-6 px-4 pb-8 pt-4 text-accent-primary sm:px-6"> |
| 152 | + <Text |
| 153 | + variant="body2" |
| 154 | + className="text-sm text-accent-secondary sm:text-base" |
| 155 | + > |
| 156 | + Please sign the Bitcoin transaction to broadcast your deposit to the |
| 157 | + Bitcoin network. |
| 158 | + </Text> |
| 159 | + |
| 160 | + {/* Provider Steps */} |
| 161 | + <div className="flex flex-col gap-4"> |
| 162 | + {activity.providers.map((provider, index) => ( |
| 163 | + <ProviderStep |
| 164 | + key={provider.id} |
| 165 | + step={index + 1} |
| 166 | + providerName={provider.name} |
| 167 | + providerAddress={provider.id} |
| 168 | + isActive={isBroadcasting && index === 0} |
| 169 | + /> |
| 170 | + ))} |
| 171 | + </div> |
| 172 | + |
| 173 | + {/* Error Display */} |
| 174 | + {broadcastError && ( |
| 175 | + <div className="bg-error/10 rounded-lg p-4"> |
| 176 | + <Text variant="body2" className="text-error text-sm"> |
| 177 | + Error: {broadcastError} |
| 178 | + </Text> |
| 179 | + </div> |
| 180 | + )} |
| 181 | + </DialogBody> |
| 182 | + |
| 183 | + <DialogFooter className="flex gap-4 px-4 pb-6 sm:px-6"> |
| 184 | + {!isBroadcasting && ( |
| 185 | + <Button |
| 186 | + variant="outlined" |
| 187 | + color="primary" |
| 188 | + onClick={onClose} |
| 189 | + className="flex-1 text-xs sm:text-base" |
| 190 | + > |
| 191 | + Cancel |
| 192 | + </Button> |
| 193 | + )} |
| 194 | + |
| 195 | + <Button |
| 196 | + disabled={isBroadcasting} |
| 197 | + variant="contained" |
| 198 | + className="flex-1 text-xs sm:text-base" |
| 199 | + onClick={isBroadcasting ? undefined : handleSign} |
| 200 | + > |
| 201 | + {isBroadcasting ? ( |
| 202 | + <Loader size={16} className="text-accent-contrast" /> |
| 203 | + ) : broadcastError ? ( |
| 204 | + "Retry" |
| 205 | + ) : ( |
| 206 | + "Sign & Broadcast" |
| 207 | + )} |
| 208 | + </Button> |
| 209 | + </DialogFooter> |
| 210 | + </ResponsiveDialog> |
| 211 | + ); |
| 212 | +} |
0 commit comments