1
+ import Big from 'big.js' ;
1
2
import { JSX , useCallback , useEffect , useMemo , useState } from 'preact/compat' ;
2
3
import { Signer } from '@polkadot/types/types' ;
3
4
import { ApiPromise } from '@polkadot/api' ;
@@ -25,27 +26,29 @@ import {
25
26
UnstakingDataType ,
26
27
handleTransactionStatus ,
27
28
} from './helpers' ;
28
- import Big from 'big.js' ;
29
29
30
- function CollatorRewards ( ) {
30
+ export function CollatorRewards ( ) {
31
31
const { api, tokenSymbol, ss58Format } = useNodeInfoState ( ) . state ;
32
32
const { walletAccount } = useGlobalState ( ) ;
33
33
const { candidates, estimatedRewards, refreshRewards, createUpdateDelegatorRewardsExtrinsic, unlockUnstaked } =
34
34
useStakingPallet ( ) ;
35
35
36
36
const [ userAvailableBalance , setUserAvailableBalance ] = useState < string > ( '0.00' ) ;
37
+ const [ unstaking , setUnstaking ] = useState < string > ( '0.00' ) ;
38
+ const [ userAvailableBalanceForUnlock , setUserAvailableBalanceForUnlock ] = useState < Big > ( new Big ( 0 ) ) ;
39
+ const [ unlockGasFee , setUnlockGasFee ] = useState < Big > ( new Big ( 0 ) ) ;
40
+
37
41
const [ userStaking , setUserStaking ] = useState < UserStaking > ( ) ;
42
+
38
43
const [ claimDialogVisible , setClaimDialogVisible ] = useState < boolean > ( false ) ;
39
44
const [ submissionPendingRewards , setSubmissionPendingRewards ] = useState ( false ) ;
40
45
const [ , setSubmissionPendingUnlock ] = useState ( false ) ;
41
46
const [ unlockDialogSuccess , setUnlockDialogSuccess ] = useState ( false ) ;
42
- const [ unstaking , setUnstaking ] = useState < string > ( '0.00' ) ;
43
- const [ balanceEnabledForUnlock , setBalanceEnabledForUnlock ] = useState < string > ( '0.00' ) ;
44
- const [ updateEnabled , setUpdateEnabled ] = useState < boolean > ( true ) ;
45
47
const [ unlockDialogVisible , setUnlockDialogVisible ] = useState < boolean > ( false ) ;
46
- const [ tokensTipText , setTokensTipText ] = useState < string > ( 'Locked for 7 days.' ) ;
48
+ const [ updateEnabled , setUpdateEnabled ] = useState < boolean > ( true ) ;
47
49
const [ loadingToken , setLoadingToken ] = useState < boolean > ( true ) ;
48
- const [ unlockGasFee , setUnlockGasFee ] = useState < Big > ( new Big ( 0 ) ) ;
50
+
51
+ const [ tokensTipText , setTokensTipText ] = useState < string > ( 'Locked for 7 days.' ) ;
49
52
50
53
const { getTransactionFee } = useFeePallet ( ) ;
51
54
@@ -85,13 +88,9 @@ function CollatorRewards() {
85
88
setTokensTipText ( tooltipText ) ;
86
89
}
87
90
88
- async function setBalanceForUnlock (
89
- unstakingDataJSON : UnstakingDataType ,
90
- api : ApiPromise ,
91
- setBalanceEnabledForUnlock : StateUpdater < string > ,
92
- ) {
91
+ async function setBalanceForUnlock ( unstakingDataJSON : UnstakingDataType , api : ApiPromise ) {
93
92
const tokensReadyToUnlock = await calculateTokensReadyToUnlock ( unstakingDataJSON , api ) ;
94
- setBalanceEnabledForUnlock ( tokensReadyToUnlock . toString ( ) ) ;
93
+ setUserAvailableBalanceForUnlock ( tokensReadyToUnlock ) ;
95
94
}
96
95
97
96
function setUnstakingTokens ( unstakingData : BTreeMap , setUnstaking : StateUpdater < string > ) {
@@ -106,19 +105,20 @@ function CollatorRewards() {
106
105
if ( ! api || ! walletAccount ) {
107
106
return '0' ;
108
107
}
109
- const { data : balance } = await api . query . system . account ( walletAccount ? .address ) ;
108
+ const { data : balance } = await api . query . system . account ( walletAccount . address ) ;
110
109
setUserAvailableBalance ( balance . free . sub ( balance . frozen ) . toString ( ) ) ;
111
110
} ;
112
111
const fetchUnstaking = async ( ) => {
113
112
if ( ! api || ! walletAccount ) {
114
113
return ;
115
114
}
116
115
117
- const unstakingData = await api . query . parachainStaking . unstaking ( walletAccount ?. address ) ;
116
+ const unstakingData = await api . query . parachainStaking . unstaking ( walletAccount . address ) ;
117
+
118
+ const unstakingDataFormatted = unstakingData . toHuman ( ) as UnstakingDataType ;
118
119
119
- const unstakingDataJSON = unstakingData . toJSON ( ) as UnstakingDataType ;
120
- setTooltipText ( unstakingDataJSON , api , tokenSymbol || '' , setTokensTipText ) ;
121
- setBalanceForUnlock ( unstakingDataJSON , api , setBalanceEnabledForUnlock ) ;
120
+ setTooltipText ( unstakingDataFormatted , api , tokenSymbol || '' , setTokensTipText ) ;
121
+ setBalanceForUnlock ( unstakingDataFormatted , api ) ;
122
122
setUnstakingTokens ( unstakingData , setUnstaking ) ;
123
123
} ;
124
124
@@ -194,7 +194,6 @@ function CollatorRewards() {
194
194
return content ;
195
195
} ;
196
196
197
-
198
197
return (
199
198
< >
200
199
< div className = "flex flex-col mb-8 justify-between md:flex-row " >
@@ -205,7 +204,7 @@ function CollatorRewards() {
205
204
< StakingContent
206
205
userStakingAmount = { userStaking ?. amount }
207
206
onButtonClick = { handleUnlockButtonClick }
208
- balanceEnabledForUnlock = { balanceEnabledForUnlock }
207
+ userAvailableBalanceForUnlock = { userAvailableBalanceForUnlock }
209
208
tokenSymbol = { tokenSymbol as string }
210
209
userAvailableBalance = { userAvailableBalance }
211
210
unstaking = { unstaking }
@@ -239,7 +238,7 @@ function CollatorRewards() {
239
238
gasFee = { unlockGasFee }
240
239
unlockSuccess = { unlockDialogSuccess }
241
240
onUnlock = { handleUnlock }
242
- userStakeBalance = { balanceEnabledForUnlock }
241
+ userAvailableBalanceForUnlock = { userAvailableBalanceForUnlock }
243
242
visible = { unlockDialogVisible }
244
243
onClose = { ( ) => setUnlockDialogVisible ( false ) }
245
244
/>
@@ -252,5 +251,3 @@ function CollatorRewards() {
252
251
</ >
253
252
) ;
254
253
}
255
-
256
- export default CollatorRewards ;
0 commit comments