|
2 | 2 | // SPDX-License-Identifier: Apache-2.0
|
3 | 3 |
|
4 | 4 | /* eslint-disable */
|
5 |
| -import { ActiveEraInfo } from '@polkadot/types/interfaces'; |
| 5 | +import {ActiveEraInfo} from '@polkadot/types/interfaces'; |
6 | 6 |
|
7 |
| -import React from 'react'; |
| 7 | +import React, {useEffect, useState} from 'react'; |
8 | 8 |
|
9 | 9 | import { useApi, useCall } from '@polkadot/react-hooks';
|
10 | 10 | import { FormatBalance } from '@polkadot/react-query';
|
11 | 11 | import BN from 'bn.js';
|
12 |
| -import { Icon, Tooltip } from '@polkadot/react-components'; |
| 12 | +import {Icon, Spinner, Tooltip} from '@polkadot/react-components'; |
13 | 13 | import { useTranslation } from '@polkadot/apps/translate';
|
14 | 14 | import { formatBalance } from '@polkadot/util';
|
| 15 | +import {ApiPromise} from "@polkadot/api/promise"; |
15 | 16 |
|
16 | 17 | interface Props {
|
17 | 18 | children?: React.ReactNode;
|
18 | 19 | className?: string;
|
19 | 20 | label?: React.ReactNode;
|
20 | 21 | }
|
21 | 22 |
|
22 |
| -const UNIT = new BN(1_000_000_000_000) |
| 23 | +async function getStakingRewards(api: ApiPromise , idx: number): Promise<BN> { |
| 24 | + const erasp = await api.query.staking.erasStakingPayout(idx); |
| 25 | + const erasStakingPayout = JSON.parse(JSON.stringify(erasp)); |
| 26 | + const keys = await api.query.staking.erasAuthoringPayout.keys(idx); |
| 27 | + |
| 28 | + let totalValue = new BN(0); |
| 29 | + for (const key of keys) { |
| 30 | + const [_, accountId] = key.args; |
| 31 | + const value = await api.query.staking.erasAuthoringPayout(idx, accountId); |
| 32 | + totalValue = totalValue.add(new BN(value.toString())); |
| 33 | + } |
| 34 | + |
| 35 | + return totalValue.add(new BN(erasStakingPayout as string)); |
| 36 | +} |
23 | 37 |
|
24 | 38 | function MainnetReward ({ children, className = '', label }: Props): React.ReactElement<Props> {
|
25 | 39 | const { api } = useApi();
|
26 | 40 | const { t } = useTranslation();
|
| 41 | + const [loading, setLoading] = useState(true) |
| 42 | + const [stakingRewards, setStakingRewards] = useState<BN>(new BN(0)) |
27 | 43 | const activeEraInfo = useCall<ActiveEraInfo>(api.query.staking.activeEra);
|
28 | 44 |
|
29 | 45 | const activeEra = activeEraInfo && (JSON.parse(JSON.stringify(activeEraInfo)).index);
|
30 | 46 | const marketPayout = useCall<any>(api.query.staking.erasMarketPayout, [activeEra]);
|
31 |
| - const stakingRewards = new BN(3011.635871031734).mul(UNIT) |
| 47 | + // const stakingRewards = new BN(3011.635871031734).mul(UNIT) |
32 | 48 | const total = marketPayout && stakingRewards.add(new BN(Number(marketPayout).toString()))
|
33 | 49 |
|
| 50 | + useEffect(() => { |
| 51 | + if (!activeEra) return |
| 52 | + getStakingRewards(api, activeEra as number - 1).then((res: BN) => { |
| 53 | + setLoading(false) |
| 54 | + setStakingRewards(res); |
| 55 | + }) |
| 56 | + }, [activeEra]); |
| 57 | + |
34 | 58 | return (
|
35 | 59 | <div className={className}>
|
36 | 60 | {label || ''}
|
37 |
| - <FormatBalance |
38 |
| - value={total} |
39 |
| - withSi |
40 |
| - label={ |
41 |
| - <Icon |
42 |
| - icon='info-circle' |
43 |
| - tooltip={`mainnet-reward-trigger`} |
44 |
| - /> |
45 |
| - } |
46 |
| - > |
47 |
| - <Tooltip |
48 |
| - text={ |
49 |
| - <> |
50 |
| - <div> |
51 |
| - <div className='faded'>{t('staking payout: {{stakingRewards}}', { |
52 |
| - replace: { |
53 |
| - stakingRewards: formatBalance(stakingRewards) |
54 |
| - } |
55 |
| - })}</div> |
56 |
| - <div className='faded'>{t('market payout: {{marketPayout}}', { |
57 |
| - replace: { |
58 |
| - marketPayout: marketPayout && formatBalance(new BN(Number(marketPayout).toString())) |
59 |
| - } |
60 |
| - })}</div> |
61 |
| - </div> |
62 |
| - </> |
| 61 | + {loading && <Spinner noLabel />} |
| 62 | + {!loading && |
| 63 | + <FormatBalance |
| 64 | + value={total} |
| 65 | + withSi |
| 66 | + label={ |
| 67 | + <Icon |
| 68 | + icon='info-circle' |
| 69 | + tooltip={`mainnet-reward-trigger`} |
| 70 | + /> |
63 | 71 | }
|
64 |
| - trigger={`mainnet-reward-trigger`} |
65 |
| - /> |
66 |
| - </FormatBalance> |
| 72 | + > |
| 73 | + <Tooltip |
| 74 | + text={ |
| 75 | + <> |
| 76 | + <div> |
| 77 | + <div className='faded'>{t('staking payout: {{stakingRewards}}', { |
| 78 | + replace: { |
| 79 | + stakingRewards: formatBalance(stakingRewards) |
| 80 | + } |
| 81 | + })}</div> |
| 82 | + <div className='faded'>{t('market payout: {{marketPayout}}', { |
| 83 | + replace: { |
| 84 | + marketPayout: marketPayout && formatBalance(new BN(Number(marketPayout).toString())) |
| 85 | + } |
| 86 | + })}</div> |
| 87 | + </div> |
| 88 | + </> |
| 89 | + } |
| 90 | + trigger={`mainnet-reward-trigger`} |
| 91 | + /> |
| 92 | + </FormatBalance> |
| 93 | + } |
67 | 94 | {children}
|
68 | 95 | </div>
|
69 | 96 | );
|
|
0 commit comments