From 791b649fb3569eac94b181fe3f549dab8d4a231c Mon Sep 17 00:00:00 2001 From: "wagmi.x" Date: Sat, 12 Oct 2024 10:57:37 +0800 Subject: [PATCH] feat: add loading --- .../src/Overview/MainnetRewards.tsx | 98 +++++++++---------- 1 file changed, 48 insertions(+), 50 deletions(-) diff --git a/packages/page-staking/src/Overview/MainnetRewards.tsx b/packages/page-staking/src/Overview/MainnetRewards.tsx index 9c692a926..35e186454 100644 --- a/packages/page-staking/src/Overview/MainnetRewards.tsx +++ b/packages/page-staking/src/Overview/MainnetRewards.tsx @@ -2,16 +2,17 @@ // SPDX-License-Identifier: Apache-2.0 /* eslint-disable */ -import { ActiveEraInfo } from '@polkadot/types/interfaces'; +import {ActiveEraInfo} from '@polkadot/types/interfaces'; -import React from 'react'; +import React, {useEffect, useState} from 'react'; import { useApi, useCall } from '@polkadot/react-hooks'; import { FormatBalance } from '@polkadot/react-query'; import BN from 'bn.js'; -import { Icon, Tooltip } from '@polkadot/react-components'; +import {Icon, Spinner, Tooltip} from '@polkadot/react-components'; import { useTranslation } from '@polkadot/apps/translate'; import { formatBalance } from '@polkadot/util'; +import {ApiPromise} from "@polkadot/api/promise"; interface Props { children?: React.ReactNode; @@ -19,80 +20,77 @@ interface Props { label?: React.ReactNode; } -const UNIT = new BN(1_000_000_000_000) - -async function getAllRewards(idx: number): Promise { - const { api } = useApi(); - - // erasStakingPayout +async function getStakingRewards(api: ApiPromise , idx: number): Promise { const erasp = await api.query.staking.erasStakingPayout(idx); - const erasStakingPayout = JSON.parse(JSON.stringify(erasp)); - // console.log(`erasStakingPayout: ${idx}: ${erasStakingPayout}`); - - // erasAuthoringPayout const keys = await api.query.staking.erasAuthoringPayout.keys(idx); let totalValue = new BN(0); - for (const key of keys) { const [_, accountId] = key.args; const value = await api.query.staking.erasAuthoringPayout(idx, accountId); totalValue = totalValue.add(new BN(value.toString())); } - // console.log(`erasAuthoringPayout ${idx}: ${totalValue.toString()}`); - - return totalValue.add(new BN(erasStakingPayout as string)).div(UNIT).toNumber(); + return totalValue.add(new BN(erasStakingPayout as string)); } function MainnetReward ({ children, className = '', label }: Props): React.ReactElement { const { api } = useApi(); const { t } = useTranslation(); + const [loading, setLoading] = useState(true) + const [stakingRewards, setStakingRewards] = useState(new BN(0)) const activeEraInfo = useCall(api.query.staking.activeEra); const activeEra = activeEraInfo && (JSON.parse(JSON.stringify(activeEraInfo)).index); const marketPayout = useCall(api.query.staking.erasMarketPayout, [activeEra]); - const stakingRewards = new BN(3011.635871031734).mul(UNIT) + // const stakingRewards = new BN(3011.635871031734).mul(UNIT) const total = marketPayout && stakingRewards.add(new BN(Number(marketPayout).toString())) - getAllRewards(activeEra as number - 1).then((res) => { - console.log("================> getAllRewards: ", res); - }) + useEffect(() => { + if (!activeEra) return + getStakingRewards(api, activeEra as number - 1).then((res: BN) => { + setLoading(false) + setStakingRewards(res); + }) + }, [activeEra]); return (
{label || ''} - - } - > - -
-
{t('staking payout: {{stakingRewards}}', { - replace: { - stakingRewards: formatBalance(stakingRewards) - } - })}
-
{t('market payout: {{marketPayout}}', { - replace: { - marketPayout: marketPayout && formatBalance(new BN(Number(marketPayout).toString())) - } - })}
-
- + {loading && } + {!loading && + } - trigger={`mainnet-reward-trigger`} - /> - + > + +
+
{t('staking payout: {{stakingRewards}}', { + replace: { + stakingRewards: formatBalance(stakingRewards) + } + })}
+
{t('market payout: {{marketPayout}}', { + replace: { + marketPayout: marketPayout && formatBalance(new BN(Number(marketPayout).toString())) + } + })}
+
+ + } + trigger={`mainnet-reward-trigger`} + /> +
+ } {children}
);