@@ -26,15 +26,17 @@ const RaiseDisputeButton: React.FC<IRaiseDisputeButton> = ({ toggleModal, button
26
26
const isBuyer = useMemo ( ( ) => address ?. toLowerCase ( ) === buyer ?. toLowerCase ( ) , [ address , buyer ] ) ;
27
27
const refetchQuery = useQueryRefetch ( ) ;
28
28
29
- const { data : payArbitrationFeeByBuyerConfig } = useSimulateEscrowUniversalPayArbitrationFeeByBuyer ( {
30
- args : [ BigInt ( id ) ] ,
31
- value : arbitrationCost ,
32
- } ) ;
29
+ const { data : payArbitrationFeeByBuyerConfig , error : simulateBuyerError } =
30
+ useSimulateEscrowUniversalPayArbitrationFeeByBuyer ( {
31
+ args : [ BigInt ( id ) ] ,
32
+ value : arbitrationCost ,
33
+ } ) ;
33
34
34
- const { data : payArbitrationFeeBySellerConfig } = useSimulateEscrowUniversalPayArbitrationFeeBySeller ( {
35
- args : [ BigInt ( id ) ] ,
36
- value : arbitrationCost ,
37
- } ) ;
35
+ const { data : payArbitrationFeeBySellerConfig , error : simulateSellerError } =
36
+ useSimulateEscrowUniversalPayArbitrationFeeBySeller ( {
37
+ args : [ BigInt ( id ) ] ,
38
+ value : arbitrationCost ,
39
+ } ) ;
38
40
39
41
const { writeContractAsync : payArbitrationFeeByBuyer } =
40
42
useWriteEscrowUniversalPayArbitrationFeeByBuyer ( payArbitrationFeeByBuyerConfig ) ;
@@ -43,36 +45,72 @@ const RaiseDisputeButton: React.FC<IRaiseDisputeButton> = ({ toggleModal, button
43
45
) ;
44
46
45
47
const handleRaiseDispute = ( ) => {
46
- if ( isBuyer && ! isUndefined ( payArbitrationFeeByBuyer ) ) {
47
- setIsSending ( true ) ;
48
- wrapWithToast ( async ( ) => await payArbitrationFeeByBuyer ( payArbitrationFeeByBuyerConfig . request ) , publicClient )
49
- . then ( ( wrapResult ) => {
50
- if ( wrapResult . status ) {
51
- toggleModal && toggleModal ( ) ;
52
- refetchQuery ( [ [ "refetchOnBlock" , "useTransactionDetailsQuery" ] ] ) ;
53
- } else {
48
+ if ( isBuyer ) {
49
+ if ( simulateBuyerError ) {
50
+ console . log ( { simulateBuyerError} ) ;
51
+ let errorMessage = "An error occurred during simulation." ;
52
+ if ( simulateBuyerError . message . includes ( "insufficient funds" ) ) {
53
+ errorMessage = "You don't have enough balance to raise a dispute." ;
54
+ }
55
+ // Use wrapWithToast to display the error
56
+ wrapWithToast ( ( ) => Promise . reject ( new Error ( errorMessage ) ) , publicClient ) ;
57
+ return ;
58
+ }
59
+
60
+ if ( isUndefined ( payArbitrationFeeByBuyerConfig ) ) {
61
+ console . log ( { payArbitrationFeeByBuyerConfig } ) ;
62
+ console . log ( { simulateBuyerError } ) ;
63
+ wrapWithToast ( ( ) => Promise . reject ( new Error ( "Unable to prepare transaction." ) ) , publicClient ) ;
64
+ return ;
65
+ }
66
+
67
+ if ( ! isUndefined ( payArbitrationFeeByBuyer ) ) {
68
+ setIsSending ( true ) ;
69
+ wrapWithToast ( ( ) => payArbitrationFeeByBuyer ( payArbitrationFeeByBuyerConfig . request ) , publicClient )
70
+ . then ( ( wrapResult ) => {
71
+ setIsSending ( false ) ;
72
+ if ( wrapResult . status ) {
73
+ toggleModal && toggleModal ( ) ;
74
+ refetchQuery ( [ [ "refetchOnBlock" , "useTransactionDetailsQuery" ] ] ) ;
75
+ }
76
+ } )
77
+ . catch ( ( error ) => {
78
+ console . error ( "Error raising dispute as buyer:" , error ) ;
79
+ setIsSending ( false ) ;
80
+ } ) ;
81
+ }
82
+ } else {
83
+ if ( simulateSellerError ) {
84
+ let errorMessage = "An error occurred during simulation." ;
85
+ if ( simulateSellerError . message . includes ( "insufficient funds" ) ) {
86
+ errorMessage = "You don't have enough balance to raise a dispute." ;
87
+ }
88
+ wrapWithToast ( ( ) => Promise . reject ( new Error ( errorMessage ) ) , publicClient ) ;
89
+ return ;
90
+ }
91
+
92
+ if ( isUndefined ( payArbitrationFeeBySellerConfig ) ) {
93
+ console . log ( { payArbitrationFeeBySellerConfig } ) ;
94
+ console . log ( { simulateSellerError } ) ;
95
+ wrapWithToast ( ( ) => Promise . reject ( new Error ( "Unable to prepare transaction." ) ) , publicClient ) ;
96
+ return ;
97
+ }
98
+
99
+ if ( ! isUndefined ( payArbitrationFeeBySeller ) ) {
100
+ setIsSending ( true ) ;
101
+ wrapWithToast ( ( ) => payArbitrationFeeBySeller ( payArbitrationFeeBySellerConfig . request ) , publicClient )
102
+ . then ( ( wrapResult ) => {
54
103
setIsSending ( false ) ;
55
- }
56
- } )
57
- . catch ( ( error ) => {
58
- console . error ( "Error raising dispute as buyer:" , error ) ;
59
- setIsSending ( false ) ;
60
- } ) ;
61
- } else if ( ! isBuyer && ! isUndefined ( payArbitrationFeeBySeller ) ) {
62
- setIsSending ( true ) ;
63
- wrapWithToast ( async ( ) => await payArbitrationFeeBySeller ( payArbitrationFeeBySellerConfig . request ) , publicClient )
64
- . then ( ( wrapResult ) => {
65
- if ( wrapResult . status ) {
66
- toggleModal && toggleModal ( ) ;
67
- refetchQuery ( [ [ "refetchOnBlock" , "useTransactionDetailsQuery" ] ] ) ;
68
- } else {
104
+ if ( wrapResult . status ) {
105
+ toggleModal && toggleModal ( ) ;
106
+ refetchQuery ( [ [ "refetchOnBlock" , "useTransactionDetailsQuery" ] ] ) ;
107
+ }
108
+ } )
109
+ . catch ( ( error ) => {
110
+ console . error ( "Error raising dispute as seller:" , error ) ;
69
111
setIsSending ( false ) ;
70
- }
71
- } )
72
- . catch ( ( error ) => {
73
- console . error ( "Error raising dispute as seller:" , error ) ;
74
- setIsSending ( false ) ;
75
- } ) ;
112
+ } ) ;
113
+ }
76
114
}
77
115
} ;
78
116
0 commit comments