1
1
import React , { useMemo , useState } from "react" ;
2
2
import { Button } from "@kleros/ui-components-library" ;
3
- import { useAccount , usePublicClient } from "wagmi" ;
3
+ import { useAccount , useBalance , usePublicClient } from "wagmi" ;
4
4
import {
5
5
useWriteEscrowUniversalPayArbitrationFeeByBuyer ,
6
6
useWriteEscrowUniversalPayArbitrationFeeBySeller ,
@@ -11,6 +11,8 @@ import { isUndefined } from "utils/index";
11
11
import { wrapWithToast } from "utils/wrapWithToast" ;
12
12
import { useTransactionDetailsContext } from "context/TransactionDetailsContext" ;
13
13
import { useQueryRefetch } from "hooks/useQueryRefetch" ;
14
+ import ClosedCircleIcon from "components/StyledIcons/ClosedCircleIcon" ;
15
+ import { ErrorButtonMessage } from "pages/NewTransaction/NavigationButtons/DepositPaymentButton" ;
14
16
15
17
interface IRaiseDisputeButton {
16
18
toggleModal ?: ( ) => void ;
@@ -25,29 +27,33 @@ const RaiseDisputeButton: React.FC<IRaiseDisputeButton> = ({ toggleModal, button
25
27
const { buyer, id } = useTransactionDetailsContext ( ) ;
26
28
const isBuyer = useMemo ( ( ) => address ?. toLowerCase ( ) === buyer ?. toLowerCase ( ) , [ address , buyer ] ) ;
27
29
const refetchQuery = useQueryRefetch ( ) ;
30
+ const { data : balanceData } = useBalance ( {
31
+ address : address as `0x${string } ` | undefined ,
32
+ } ) ;
28
33
29
- const { data : payArbitrationFeeByBuyerConfig , isLoading : isPreparingBuyerConfig } =
30
- useSimulateEscrowUniversalPayArbitrationFeeByBuyer ( {
31
- query : {
32
- enabled : isBuyer ,
33
- } ,
34
- args : [ BigInt ( id ) ] ,
35
- value : arbitrationCost ,
36
- } ) ;
34
+ const insufficientBalance = parseFloat ( arbitrationCost . toString ( ) ) > parseFloat ( balanceData ?. value . toString ( ) || "0" ) ;
37
35
38
- const { data : payArbitrationFeeBySellerConfig , isLoading : isPreparingSellerConfig } =
39
- useSimulateEscrowUniversalPayArbitrationFeeBySeller ( {
40
- query : {
41
- enabled : ! isBuyer ,
42
- } ,
43
- args : [ BigInt ( id ) ] ,
44
- value : arbitrationCost ,
45
- } ) ;
36
+ const { data : payArbitrationFeeByBuyerConfig } = useSimulateEscrowUniversalPayArbitrationFeeByBuyer ( {
37
+ query : {
38
+ enabled : isBuyer ,
39
+ } ,
40
+ args : [ BigInt ( id ) ] ,
41
+ value : arbitrationCost ,
42
+ } ) ;
43
+
44
+ const { data : payArbitrationFeeBySellerConfig } = useSimulateEscrowUniversalPayArbitrationFeeBySeller ( {
45
+ query : {
46
+ enabled : ! isBuyer ,
47
+ } ,
48
+ args : [ BigInt ( id ) ] ,
49
+ value : arbitrationCost ,
50
+ } ) ;
46
51
47
52
const { writeContractAsync : payArbitrationFeeByBuyer } =
48
53
useWriteEscrowUniversalPayArbitrationFeeByBuyer ( payArbitrationFeeByBuyerConfig ) ;
49
- const { writeContractAsync : payArbitrationFeeBySeller } =
50
- useWriteEscrowUniversalPayArbitrationFeeBySeller ( payArbitrationFeeBySellerConfig ) ;
54
+ const { writeContractAsync : payArbitrationFeeBySeller } = useWriteEscrowUniversalPayArbitrationFeeBySeller (
55
+ payArbitrationFeeBySellerConfig
56
+ ) ;
51
57
52
58
const handleRaiseDispute = ( ) => {
53
59
if ( isBuyer && ! isUndefined ( payArbitrationFeeByBuyer ) ) {
@@ -84,12 +90,19 @@ const RaiseDisputeButton: React.FC<IRaiseDisputeButton> = ({ toggleModal, button
84
90
} ;
85
91
86
92
return (
87
- < Button
88
- isLoading = { isSending || isPreparingBuyerConfig || isPreparingSellerConfig }
89
- disabled = { isSending || isPreparingBuyerConfig || isPreparingSellerConfig }
90
- text = { buttonText }
91
- onClick = { handleRaiseDispute }
92
- />
93
+ < div >
94
+ < Button
95
+ isLoading = { isSending }
96
+ disabled = { isSending || insufficientBalance }
97
+ text = { buttonText }
98
+ onClick = { handleRaiseDispute }
99
+ />
100
+ { insufficientBalance && (
101
+ < ErrorButtonMessage >
102
+ < ClosedCircleIcon /> Insufficient balance
103
+ </ ErrorButtonMessage >
104
+ ) }
105
+ </ div >
93
106
) ;
94
107
} ;
95
108
0 commit comments