@@ -9,68 +9,86 @@ import {
9
9
Text ,
10
10
} from "@babylonlabs-io/core-ui" ;
11
11
import { useEffect , useState } from "react" ;
12
+ import type { Hex } from "viem" ;
13
+ import { useMintAndBorrow } from "../../hooks/useMintAndBorrow" ;
12
14
13
15
interface BorrowSignModalProps {
14
16
open : boolean ;
15
17
onClose : ( ) => void ;
16
18
onSuccess : ( ) => void ;
17
19
borrowAmount ?: number ;
18
20
collateralAmount ?: string ;
21
+ /** Pegin transaction hash (vault ID) */
22
+ pegInTxHash ?: Hex ;
19
23
}
20
24
21
25
/**
22
- * BorrowSignModal - Multi-step signing modal for borrow flow
23
- *
24
- * Shows 3 steps that auto-progress with 2-second delays:
25
- * 1. Mint vaultBTC
26
- * 2. Borrow Transaction
27
- * 3. Confirming on Ethereum
28
- *
29
- * After all steps complete, triggers onSuccess callback to show success modal.
30
- *
31
- * Note: This is hardcoded for UI demonstration only.
32
- * Real implementation would integrate with wallet signing.
26
+ * BorrowSignModal - Transaction signing modal for borrow flow
27
+ *
28
+ * The mintAndBorrow transaction atomically:
29
+ * 1. Mints vaultBTC from the pegin
30
+ * 2. Deposits vaultBTC as collateral to Morpho
31
+ * 3. Borrows USDC against the collateral
32
+ *
33
+ * All three steps happen in a single Ethereum transaction.
33
34
*/
34
35
export function BorrowSignModal ( {
35
36
open,
36
37
onClose,
37
38
onSuccess,
39
+ borrowAmount,
40
+ pegInTxHash,
38
41
} : BorrowSignModalProps ) {
39
- const [ currentStep , setCurrentStep ] = useState ( 1 ) ;
40
- const [ processing , setProcessing ] = useState ( false ) ;
41
-
42
- // Auto-progress through steps with 2-second delays (hardcoded for UI demo)
43
- useEffect ( ( ) => {
44
- if ( ! open || currentStep > 3 ) return ;
45
-
46
- setProcessing ( true ) ;
47
-
48
- const timer = setTimeout ( ( ) => {
49
- if ( currentStep === 3 ) {
50
- // Last step complete, trigger success modal
51
- setProcessing ( false ) ;
52
- onSuccess ( ) ;
53
- } else {
54
- // Move to next step
55
- setCurrentStep ( ( prev ) => prev + 1 ) ;
56
- }
57
- } , 2000 ) ; // 2 second delay per step
58
-
59
- return ( ) => clearTimeout ( timer ) ;
60
- } , [ open , currentStep , onSuccess ] ) ;
42
+ const [ transactionStarted , setTransactionStarted ] = useState ( false ) ;
43
+ const { executeMintAndBorrow, isLoading, error } = useMintAndBorrow ( ) ;
61
44
62
45
// Reset state when modal closes
63
46
useEffect ( ( ) => {
64
47
if ( ! open ) {
65
- setCurrentStep ( 1 ) ;
66
- setProcessing ( false ) ;
48
+ setTransactionStarted ( false ) ;
67
49
}
68
50
} , [ open ] ) ;
69
51
70
- const handleSign = ( ) => {
71
- // In real implementation, this would trigger wallet signing
72
- // For now, auto-progression handles everything
73
- setProcessing ( true ) ;
52
+ // Show error in console if transaction fails
53
+ useEffect ( ( ) => {
54
+ if ( error ) {
55
+ console . error ( '[BorrowSignModal] Transaction error:' , error ) ;
56
+ // TODO: Show error in UI
57
+ }
58
+ } , [ error ] ) ;
59
+
60
+ const handleSign = async ( ) => {
61
+ if ( ! pegInTxHash || ! borrowAmount ) {
62
+ console . error ( '[BorrowSignModal] Missing required data:' , { pegInTxHash, borrowAmount } ) ;
63
+ return ;
64
+ }
65
+
66
+ // Convert USDC amount to bigint with 6 decimals
67
+ const borrowAmountBigInt = BigInt ( Math . floor ( borrowAmount * 1_000_000 ) ) ;
68
+
69
+ console . log ( '[BorrowSignModal] Starting transaction:' , {
70
+ pegInTxHash,
71
+ borrowAmount,
72
+ borrowAmountBigInt : borrowAmountBigInt . toString ( ) ,
73
+ } ) ;
74
+
75
+ setTransactionStarted ( true ) ;
76
+
77
+ try {
78
+ const result = await executeMintAndBorrow ( {
79
+ pegInTxHash,
80
+ borrowAmount : borrowAmountBigInt ,
81
+ } ) ;
82
+
83
+ if ( result ) {
84
+ // Success - trigger success modal
85
+ onSuccess ( ) ;
86
+ }
87
+ } catch ( err ) {
88
+ console . error ( '[BorrowSignModal] Transaction failed:' , err ) ;
89
+ setTransactionStarted ( false ) ;
90
+ // Keep modal open to show error
91
+ }
74
92
} ;
75
93
76
94
return (
@@ -83,20 +101,20 @@ export function BorrowSignModal({
83
101
84
102
< DialogBody className = "flex flex-col gap-4 px-4 pb-8 pt-4 text-accent-primary sm:px-6" >
85
103
< Text variant = "body1" className = "text-sm text-accent-secondary sm:text-base" >
86
- Follow the steps below. Your wallet will prompt you when action is needed .
104
+ Sign the transaction in your wallet to mint vaultBTC and borrow USDC atomically .
87
105
</ Text >
88
106
89
107
< div className = "flex flex-col items-start gap-4 py-4" >
90
- < Step step = { 1 } currentStep = { currentStep } >
91
- Mint vaultBTC
92
- </ Step >
93
- < Step step = { 2 } currentStep = { currentStep } >
94
- Borrow Transaction
95
- </ Step >
96
- < Step step = { 3 } currentStep = { currentStep } >
97
- Confirming on Ethereum
108
+ < Step step = { 1 } currentStep = { transactionStarted || isLoading ? 1 : 0 } >
109
+ Mint vaultBTC & Borrow USDC
98
110
</ Step >
99
111
</ div >
112
+
113
+ { error && (
114
+ < Text variant = "body2" className = "text-error-main text-sm" >
115
+ { error }
116
+ </ Text >
117
+ ) }
100
118
</ DialogBody >
101
119
102
120
< DialogFooter className = "flex gap-4" >
@@ -105,17 +123,18 @@ export function BorrowSignModal({
105
123
color = "primary"
106
124
onClick = { onClose }
107
125
className = "flex-1 text-xs sm:text-base"
126
+ disabled = { isLoading }
108
127
>
109
128
Cancel
110
129
</ Button >
111
130
112
131
< Button
113
- disabled = { processing || currentStep > 3 }
132
+ disabled = { isLoading || ! pegInTxHash || ! borrowAmount }
114
133
variant = "contained"
115
134
className = "flex-1 text-xs sm:text-base"
116
135
onClick = { handleSign }
117
136
>
118
- { processing ? (
137
+ { isLoading ? (
119
138
< Loader size = { 16 } className = "text-accent-contrast" />
120
139
) : (
121
140
"Sign"
0 commit comments