Skip to content

Commit 7413027

Browse files
authored
fix display Staking Unlock (days, and balance)
1 parent 47165d6 commit 7413027

File tree

8 files changed

+53
-245
lines changed

8 files changed

+53
-245
lines changed

src/pages/collators/CollatorRewards.tsx

Lines changed: 0 additions & 194 deletions
This file was deleted.

src/pages/collators/CollatorRewards/CollatorRewards.tsx

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Big from 'big.js';
12
import { JSX, useCallback, useEffect, useMemo, useState } from 'preact/compat';
23
import { Signer } from '@polkadot/types/types';
34
import { ApiPromise } from '@polkadot/api';
@@ -25,27 +26,29 @@ import {
2526
UnstakingDataType,
2627
handleTransactionStatus,
2728
} from './helpers';
28-
import Big from 'big.js';
2929

30-
function CollatorRewards() {
30+
export function CollatorRewards() {
3131
const { api, tokenSymbol, ss58Format } = useNodeInfoState().state;
3232
const { walletAccount } = useGlobalState();
3333
const { candidates, estimatedRewards, refreshRewards, createUpdateDelegatorRewardsExtrinsic, unlockUnstaked } =
3434
useStakingPallet();
3535

3636
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+
3741
const [userStaking, setUserStaking] = useState<UserStaking>();
42+
3843
const [claimDialogVisible, setClaimDialogVisible] = useState<boolean>(false);
3944
const [submissionPendingRewards, setSubmissionPendingRewards] = useState(false);
4045
const [, setSubmissionPendingUnlock] = useState(false);
4146
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);
4547
const [unlockDialogVisible, setUnlockDialogVisible] = useState<boolean>(false);
46-
const [tokensTipText, setTokensTipText] = useState<string>('Locked for 7 days.');
48+
const [updateEnabled, setUpdateEnabled] = useState<boolean>(true);
4749
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.');
4952

5053
const { getTransactionFee } = useFeePallet();
5154

@@ -85,13 +88,9 @@ function CollatorRewards() {
8588
setTokensTipText(tooltipText);
8689
}
8790

88-
async function setBalanceForUnlock(
89-
unstakingDataJSON: UnstakingDataType,
90-
api: ApiPromise,
91-
setBalanceEnabledForUnlock: StateUpdater<string>,
92-
) {
91+
async function setBalanceForUnlock(unstakingDataJSON: UnstakingDataType, api: ApiPromise) {
9392
const tokensReadyToUnlock = await calculateTokensReadyToUnlock(unstakingDataJSON, api);
94-
setBalanceEnabledForUnlock(tokensReadyToUnlock.toString());
93+
setUserAvailableBalanceForUnlock(tokensReadyToUnlock);
9594
}
9695

9796
function setUnstakingTokens(unstakingData: BTreeMap, setUnstaking: StateUpdater<string>) {
@@ -106,19 +105,20 @@ function CollatorRewards() {
106105
if (!api || !walletAccount) {
107106
return '0';
108107
}
109-
const { data: balance } = await api.query.system.account(walletAccount?.address);
108+
const { data: balance } = await api.query.system.account(walletAccount.address);
110109
setUserAvailableBalance(balance.free.sub(balance.frozen).toString());
111110
};
112111
const fetchUnstaking = async () => {
113112
if (!api || !walletAccount) {
114113
return;
115114
}
116115

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;
118119

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);
122122
setUnstakingTokens(unstakingData, setUnstaking);
123123
};
124124

@@ -194,7 +194,6 @@ function CollatorRewards() {
194194
return content;
195195
};
196196

197-
198197
return (
199198
<>
200199
<div className="flex flex-col mb-8 justify-between md:flex-row ">
@@ -205,7 +204,7 @@ function CollatorRewards() {
205204
<StakingContent
206205
userStakingAmount={userStaking?.amount}
207206
onButtonClick={handleUnlockButtonClick}
208-
balanceEnabledForUnlock={balanceEnabledForUnlock}
207+
userAvailableBalanceForUnlock={userAvailableBalanceForUnlock}
209208
tokenSymbol={tokenSymbol as string}
210209
userAvailableBalance={userAvailableBalance}
211210
unstaking={unstaking}
@@ -239,7 +238,7 @@ function CollatorRewards() {
239238
gasFee={unlockGasFee}
240239
unlockSuccess={unlockDialogSuccess}
241240
onUnlock={handleUnlock}
242-
userStakeBalance={balanceEnabledForUnlock}
241+
userAvailableBalanceForUnlock={userAvailableBalanceForUnlock}
243242
visible={unlockDialogVisible}
244243
onClose={() => setUnlockDialogVisible(false)}
245244
/>
@@ -252,5 +251,3 @@ function CollatorRewards() {
252251
</>
253252
);
254253
}
255-
256-
export default CollatorRewards;

src/pages/collators/CollatorRewards/StakingContent/StakingContent.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1+
import Big from 'big.js';
12
import { ExclamationCircleIcon } from '@heroicons/react/24/outline';
23

34
import StakedIcon from '../../../../assets/collators-staked-icon';
45
import { nativeToFormatMetric } from '../../../../shared/parseNumbers/metric';
56

67
interface StakingContentProps {
78
onButtonClick: () => void;
8-
balanceEnabledForUnlock: string;
9-
tokenSymbol: string;
109
userStakingAmount?: string;
1110
userAvailableBalance: string;
11+
userAvailableBalanceForUnlock: Big;
12+
tokenSymbol: string;
1213
unstaking: string;
1314
tokensTipText: string;
1415
}
1516

1617
export const StakingContent: React.FC<StakingContentProps> = ({
1718
onButtonClick,
18-
balanceEnabledForUnlock,
19-
tokenSymbol,
2019
userStakingAmount = '0.00',
2120
userAvailableBalance,
21+
userAvailableBalanceForUnlock,
22+
tokenSymbol,
2223
unstaking,
2324
tokensTipText,
2425
}) => (
@@ -46,7 +47,7 @@ export const StakingContent: React.FC<StakingContentProps> = ({
4647
) : null}
4748
</div>
4849
<button
49-
disabled={!Number(balanceEnabledForUnlock)}
50+
disabled={!userAvailableBalanceForUnlock.toNumber()}
5051
className="btn btn-primary btn-unlock min-h-fit max-h-10 w-full m-auto px-8"
5152
onClick={onButtonClick}
5253
>

src/pages/collators/CollatorRewards/helpers.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import { StateUpdater } from 'preact/hooks';
33
import { EventRecord, ExtrinsicStatus } from '@polkadot/types/interfaces';
44

55
import { ShowToast, ToastMessage } from '../../../shared/showToast';
6-
import { nativeToFormatMetric } from '../../../shared/parseNumbers/metric';
6+
import { nativeToFormatMetric, sanitizeNative } from '../../../shared/parseNumbers/metric';
77
import { BLOCK_TIME_SEC, MINUTE_IN_MILLISECONDS, SECONDS_IN_A_DAY } from '../../../shared/constants';
88
import { getErrors } from '../../../helpers/substrate';
9+
import Big from 'big.js';
910

10-
async function calculateDaysLeft(blockNumber: string, api: ApiPromise) {
11+
async function calculateDaysLeft(blockNumber: string | number, api: ApiPromise) {
1112
const currentBlockNumber = await api.derive.chain.bestNumber();
1213
const blocksLeft = Number(blockNumber) - Number(currentBlockNumber);
1314
const daysLeft = Math.round((blocksLeft * BLOCK_TIME_SEC) / SECONDS_IN_A_DAY);
@@ -28,7 +29,6 @@ export async function generateUnstakingTooltipText(
2829
): Promise<string> {
2930
const tooltipTextPromises = Object.entries(unstakingData).map(async ([blockNumber, value]) => {
3031
const daysLeft = await calculateDaysLeft(blockNumber, api);
31-
3232
if (areTokensReadyToUnlock(daysLeft)) {
3333
return `${nativeToFormatMetric(value, tokenSymbol)} ready to be unlocked.\n`;
3434
} else {
@@ -43,19 +43,19 @@ export async function generateUnstakingTooltipText(
4343
return tooltipText;
4444
}
4545

46-
export async function calculateTokensReadyToUnlock(unstakingData: UnstakingDataType, api: ApiPromise): Promise<number> {
46+
export async function calculateTokensReadyToUnlock(unstakingData: UnstakingDataType, api: ApiPromise): Promise<Big> {
4747
const tokensReadyToUnlockPromises = Object.entries(unstakingData).map(async ([blockNumber, value]) => {
4848
const daysLeft = await calculateDaysLeft(blockNumber, api);
4949

5050
if (areTokensReadyToUnlock(daysLeft)) {
51-
return Number(value);
51+
return sanitizeNative(value);
5252
}
5353

54-
return 0;
54+
return new Big(0);
5555
});
5656

5757
const tokensReadyToUnlockValues = await Promise.all(tokensReadyToUnlockPromises);
58-
const tokensReadyToUnlock = tokensReadyToUnlockValues.reduce((total, value) => total + value, 0);
58+
const tokensReadyToUnlock = tokensReadyToUnlockValues.reduce((total, value) => total.add(value), new Big(0));
5959

6060
return tokensReadyToUnlock;
6161
}

0 commit comments

Comments
 (0)