1
- import React , { useCallback , useMemo } from "react" ;
1
+ import React , { useCallback , useEffect , useMemo } from "react" ;
2
2
3
3
import { useParams } from "react-router-dom" ;
4
4
import { useAccount , usePublicClient } from "wagmi" ;
@@ -36,17 +36,14 @@ const Container = styled.div`
36
36
flex-direction: column;
37
37
` ;
38
38
39
- const ErrorLabel = styled . label `
40
- color: ${ ( { theme } ) => theme . error } ;
41
- ` ;
42
-
43
39
interface IActionButton {
44
40
isSending : boolean ;
45
41
parsedAmount : bigint ;
46
42
action : ActionType ;
47
43
setIsSending : ( arg0 : boolean ) => void ;
48
44
setAmount : ( arg0 : string ) => void ;
49
45
setIsPopupOpen : ( arg0 : boolean ) => void ;
46
+ setErrorMsg : ( msg : string ) => void ;
50
47
}
51
48
52
49
const StakeWithdrawButton : React . FC < IActionButton > = ( {
@@ -55,6 +52,7 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({
55
52
isSending,
56
53
setIsSending,
57
54
setIsPopupOpen,
55
+ setErrorMsg,
58
56
} ) => {
59
57
const { id } = useParams ( ) ;
60
58
const { address } = useAccount ( ) ;
@@ -104,9 +102,11 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({
104
102
} ,
105
103
args : [ klerosCoreAddress [ DEFAULT_CHAIN ] , BigInt ( targetStake ?? 0 ) - BigInt ( allowance ?? 0 ) ] ,
106
104
} ) ;
105
+
107
106
const { writeContractAsync : increaseAllowance } = useWritePnkIncreaseAllowance ( ) ;
107
+
108
108
const handleAllowance = useCallback ( ( ) => {
109
- if ( increaseAllowanceConfig ) {
109
+ if ( increaseAllowanceConfig && publicClient ) {
110
110
setIsSending ( true ) ;
111
111
wrapWithToast ( async ( ) => await increaseAllowance ( increaseAllowanceConfig . request ) , publicClient ) . finally ( ( ) => {
112
112
setIsSending ( false ) ;
@@ -116,16 +116,18 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({
116
116
117
117
const { data : setStakeConfig , error : setStakeError } = useSimulateKlerosCoreSetStake ( {
118
118
query : {
119
- enabled : ! isUndefined ( targetStake ) && ! isUndefined ( id ) && ! isAllowance && parsedAmount !== 0n ,
119
+ enabled :
120
+ ! isUndefined ( targetStake ) && ! isUndefined ( id ) && ! isAllowance && parsedAmount !== 0n && targetStake >= 0n ,
120
121
} ,
121
122
args : [ BigInt ( id ?? 0 ) , targetStake ] ,
122
123
} ) ;
123
124
const { writeContractAsync : setStake } = useWriteKlerosCoreSetStake ( ) ;
125
+
124
126
const handleStake = useCallback ( ( ) => {
125
- if ( setStakeConfig ) {
127
+ if ( setStakeConfig && publicClient ) {
126
128
setIsSending ( true ) ;
127
129
wrapWithToast ( async ( ) => await setStake ( setStakeConfig . request ) , publicClient )
128
- . then ( ( res ) => res . status && setIsPopupOpen ( true ) )
130
+ . then ( ( res ) => setIsPopupOpen ( res . status ) )
129
131
. finally ( ( ) => {
130
132
setIsSending ( false ) ;
131
133
} ) ;
@@ -135,7 +137,7 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({
135
137
const buttonProps = {
136
138
[ ActionType . allowance ] : {
137
139
text : "Allow PNK" ,
138
- checkDisabled : ( ) => ! balance || targetStake ! > balance ,
140
+ checkDisabled : ( ) => ! balance || targetStake > balance ,
139
141
onClick : handleAllowance ,
140
142
} ,
141
143
[ ActionType . stake ] : {
@@ -150,6 +152,12 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({
150
152
} ,
151
153
} ;
152
154
155
+ useEffect ( ( ) => {
156
+ if ( setStakeError ) {
157
+ setErrorMsg ( setStakeError ?. shortMessage ?? setStakeError . message ) ;
158
+ }
159
+ } , [ setStakeError ] ) ;
160
+
153
161
const { text, checkDisabled, onClick } = buttonProps [ isAllowance ? ActionType . allowance : action ] ;
154
162
return (
155
163
< EnsureChain >
@@ -168,7 +176,6 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({
168
176
}
169
177
onClick = { onClick }
170
178
/>
171
- { setStakeError && < ErrorLabel > { setStakeError . message } </ ErrorLabel > }
172
179
</ Container >
173
180
</ EnsureChain >
174
181
) ;
0 commit comments