1
1
import React , { useCallback , useEffect , useMemo , useState } from "react" ;
2
- import styled , { useTheme } from "styled-components" ;
2
+ import styled , { DefaultTheme , useTheme } from "styled-components" ;
3
3
4
4
import { useParams } from "react-router-dom" ;
5
5
import { type TransactionReceipt } from "viem" ;
6
- import { useAccount , usePublicClient } from "wagmi" ;
6
+ import { usePublicClient } from "wagmi" ;
7
7
8
8
import { type _TimelineItem1 , Button } from "@kleros/ui-components-library" ;
9
9
10
10
import { DEFAULT_CHAIN } from "consts/chains" ;
11
- import { REFETCH_INTERVAL } from "consts/index" ;
12
11
import {
13
12
klerosCoreAddress ,
14
13
useSimulateKlerosCoreSetStake ,
15
14
useWriteKlerosCoreSetStake ,
16
- useReadPnkBalanceOf ,
17
15
useSimulatePnkIncreaseAllowance ,
18
16
useWritePnkIncreaseAllowance ,
19
- useReadSortitionModuleGetJurorBalance ,
20
- useReadPnkAllowance ,
21
17
} from "hooks/contracts/generated" ;
22
18
import { useCourtDetails } from "hooks/queries/useCourtDetails" ;
19
+ import { usePnkData } from "hooks/usePNKData" ;
23
20
import { formatETH } from "utils/format" ;
24
21
import { isUndefined } from "utils/index" ;
25
22
import { parseWagmiError } from "utils/parseWagmiError" ;
23
+ import { refetchWithRetry } from "utils/refecthWithRetry" ;
26
24
27
25
import { EnsureChain } from "components/EnsureChain" ;
28
26
@@ -51,35 +49,14 @@ interface IActionButton {
51
49
52
50
const StakeWithdrawButton : React . FC < IActionButton > = ( { amount, parsedAmount, action, setErrorMsg, setAmount } ) => {
53
51
const { id } = useParams ( ) ;
54
- const { address } = useAccount ( ) ;
55
52
const theme = useTheme ( ) ;
56
53
const [ isPopupOpen , setIsPopupOpen ] = useState ( false ) ;
57
54
const [ isSuccess , setIsSuccess ] = useState ( false ) ;
58
55
const [ popupStepsState , setPopupStepsState ] = useState < [ _TimelineItem1 , ..._TimelineItem1 [ ] ] > ( ) ;
59
56
60
57
const { data : courtDetails } = useCourtDetails ( id ) ;
61
- const { data : balance } = useReadPnkBalanceOf ( {
62
- query : {
63
- enabled : ! isUndefined ( address ) ,
64
- refetchInterval : REFETCH_INTERVAL ,
65
- } ,
66
- args : [ address ! ] ,
67
- } ) ;
58
+ const { balance, jurorBalance, allowance, refetchAllowance } = usePnkData ( { courtId : id } ) ;
68
59
69
- const { data : jurorBalance } = useReadSortitionModuleGetJurorBalance ( {
70
- query : {
71
- enabled : ! isUndefined ( address ) ,
72
- refetchInterval : REFETCH_INTERVAL ,
73
- } ,
74
- args : [ address ?? "0x" , BigInt ( id ?? 0 ) ] ,
75
- } ) ;
76
- const { data : allowance , refetch : refetchAllowance } = useReadPnkAllowance ( {
77
- query : {
78
- enabled : ! isUndefined ( address ) ,
79
- refetchInterval : REFETCH_INTERVAL ,
80
- } ,
81
- args : [ address ?? "0x" , klerosCoreAddress [ DEFAULT_CHAIN ] ] ,
82
- } ) ;
83
60
const publicClient = usePublicClient ( ) ;
84
61
85
62
const isStaking = action === ActionType . stake ;
@@ -134,60 +111,40 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({ amount, parsedAmount, ac
134
111
( config ?: typeof setStakeConfig , approvalHash ?: `0x${string } `) => {
135
112
const isWithdraw = action === ActionType . withdraw ;
136
113
const requestData = config ?. request ?? setStakeConfig ?. request ;
114
+ const commonArgs : [ string , DefaultTheme , `0x${string } ` | undefined ] = [ amount , theme , approvalHash ] ;
137
115
138
116
if ( requestData && publicClient ) {
139
117
setPopupStepsState (
140
- getStakeSteps (
141
- isWithdraw ? StakeSteps . WithdrawInitiate : StakeSteps . StakeInitiate ,
142
- amount ,
143
- theme ,
144
- approvalHash
145
- )
118
+ getStakeSteps ( isWithdraw ? StakeSteps . WithdrawInitiate : StakeSteps . StakeInitiate , ...commonArgs )
146
119
) ;
147
120
148
121
setStake ( requestData )
149
122
. then ( async ( hash ) => {
150
123
setPopupStepsState (
151
- getStakeSteps (
152
- isWithdraw ? StakeSteps . WithdrawPending : StakeSteps . StakePending ,
153
- amount ,
154
- theme ,
155
- approvalHash ,
156
- hash
157
- )
124
+ getStakeSteps ( isWithdraw ? StakeSteps . WithdrawPending : StakeSteps . StakePending , ...commonArgs , hash )
158
125
) ;
159
126
await publicClient . waitForTransactionReceipt ( { hash, confirmations : 2 } ) . then ( ( res : TransactionReceipt ) => {
160
127
const status = res . status === "success" ;
161
128
if ( status ) {
162
129
setPopupStepsState (
163
130
getStakeSteps (
164
131
isWithdraw ? StakeSteps . WithdrawConfirmed : StakeSteps . StakeConfirmed ,
165
- amount ,
166
- theme ,
167
- approvalHash ,
132
+ ...commonArgs ,
168
133
hash
169
134
)
170
135
) ;
171
136
setIsSuccess ( true ) ;
172
137
} else
173
138
setPopupStepsState (
174
- getStakeSteps (
175
- isWithdraw ? StakeSteps . WithdrawFailed : StakeSteps . StakeFailed ,
176
- amount ,
177
- theme ,
178
- approvalHash ,
179
- hash
180
- )
139
+ getStakeSteps ( isWithdraw ? StakeSteps . WithdrawFailed : StakeSteps . StakeFailed , ...commonArgs , hash )
181
140
) ;
182
141
} ) ;
183
142
} )
184
143
. catch ( ( err ) => {
185
144
setPopupStepsState (
186
145
getStakeSteps (
187
146
isWithdraw ? StakeSteps . WithdrawFailed : StakeSteps . StakeFailed ,
188
- amount ,
189
- theme ,
190
- approvalHash ,
147
+ ...commonArgs ,
191
148
undefined ,
192
149
err
193
150
)
@@ -201,11 +158,12 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({ amount, parsedAmount, ac
201
158
const handleClick = useCallback ( ( ) => {
202
159
setIsPopupOpen ( true ) ;
203
160
if ( isAllowance && increaseAllowanceConfig && publicClient ) {
204
- setPopupStepsState ( getStakeSteps ( StakeSteps . ApproveInitiate , amount , theme ) ) ;
161
+ const commonArgs : [ string , DefaultTheme ] = [ amount , theme ] ;
162
+ setPopupStepsState ( getStakeSteps ( StakeSteps . ApproveInitiate , ...commonArgs ) ) ;
205
163
206
164
increaseAllowance ( increaseAllowanceConfig . request )
207
165
. then ( async ( hash ) => {
208
- setPopupStepsState ( getStakeSteps ( StakeSteps . ApprovePending , amount , theme , hash ) ) ;
166
+ setPopupStepsState ( getStakeSteps ( StakeSteps . ApprovePending , ... commonArgs , hash ) ) ;
209
167
210
168
await publicClient
211
169
. waitForTransactionReceipt ( { hash, confirmations : 2 } )
@@ -220,8 +178,7 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({ amount, parsedAmount, ac
220
178
setPopupStepsState (
221
179
getStakeSteps (
222
180
StakeSteps . ApproveFailed ,
223
- amount ,
224
- theme ,
181
+ ...commonArgs ,
225
182
hash ,
226
183
undefined ,
227
184
new Error ( "Something went wrong. Please restart the process." )
@@ -230,11 +187,11 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({ amount, parsedAmount, ac
230
187
else {
231
188
handleStake ( refetchData . data , hash ) ;
232
189
}
233
- } else setPopupStepsState ( getStakeSteps ( StakeSteps . ApproveFailed , amount , theme , hash ) ) ;
190
+ } else setPopupStepsState ( getStakeSteps ( StakeSteps . ApproveFailed , ... commonArgs , hash ) ) ;
234
191
} ) ;
235
192
} )
236
193
. catch ( ( err ) => {
237
- setPopupStepsState ( getStakeSteps ( StakeSteps . ApproveFailed , amount , theme , undefined , undefined , err ) ) ;
194
+ setPopupStepsState ( getStakeSteps ( StakeSteps . ApproveFailed , ... commonArgs , undefined , undefined , err ) ) ;
238
195
} ) ;
239
196
} else {
240
197
handleStake ( ) ;
@@ -308,29 +265,4 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({ amount, parsedAmount, ac
308
265
) ;
309
266
} ;
310
267
311
- async function refetchWithRetry < T > ( fn : ( ) => Promise < T > , retryCount = 5 , retryDelay = 2000 ) {
312
- let attempts = 0 ;
313
-
314
- while ( attempts < retryCount ) {
315
- try {
316
- const returnData = await fn ( ) ;
317
-
318
- //@ts -expect-error data does exist
319
- if ( returnData && returnData ?. data !== undefined ) {
320
- return returnData ;
321
- }
322
- } catch ( error ) {
323
- console . error ( `Attempt ${ attempts + 1 } failed with error:` , error ) ;
324
- }
325
-
326
- attempts ++ ;
327
-
328
- if ( attempts >= retryCount ) {
329
- return ;
330
- }
331
-
332
- await new Promise ( ( resolve ) => setTimeout ( resolve , retryDelay ) ) ;
333
- }
334
- return ;
335
- }
336
268
export default StakeWithdrawButton ;
0 commit comments