Skip to content

Commit 30b0f70

Browse files
authored
feat: add total rewards (#419)
1 parent e4c162f commit 30b0f70

File tree

1 file changed

+61
-34
lines changed

1 file changed

+61
-34
lines changed

packages/page-staking/src/Overview/MainnetRewards.tsx

+61-34
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,95 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
/* eslint-disable */
5-
import { ActiveEraInfo } from '@polkadot/types/interfaces';
5+
import {ActiveEraInfo} from '@polkadot/types/interfaces';
66

7-
import React from 'react';
7+
import React, {useEffect, useState} from 'react';
88

99
import { useApi, useCall } from '@polkadot/react-hooks';
1010
import { FormatBalance } from '@polkadot/react-query';
1111
import BN from 'bn.js';
12-
import { Icon, Tooltip } from '@polkadot/react-components';
12+
import {Icon, Spinner, Tooltip} from '@polkadot/react-components';
1313
import { useTranslation } from '@polkadot/apps/translate';
1414
import { formatBalance } from '@polkadot/util';
15+
import {ApiPromise} from "@polkadot/api/promise";
1516

1617
interface Props {
1718
children?: React.ReactNode;
1819
className?: string;
1920
label?: React.ReactNode;
2021
}
2122

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+
}
2337

2438
function MainnetReward ({ children, className = '', label }: Props): React.ReactElement<Props> {
2539
const { api } = useApi();
2640
const { t } = useTranslation();
41+
const [loading, setLoading] = useState(true)
42+
const [stakingRewards, setStakingRewards] = useState<BN>(new BN(0))
2743
const activeEraInfo = useCall<ActiveEraInfo>(api.query.staking.activeEra);
2844

2945
const activeEra = activeEraInfo && (JSON.parse(JSON.stringify(activeEraInfo)).index);
3046
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)
3248
const total = marketPayout && stakingRewards.add(new BN(Number(marketPayout).toString()))
3349

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+
3458
return (
3559
<div className={className}>
3660
{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+
/>
6371
}
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+
}
6794
{children}
6895
</div>
6996
);

0 commit comments

Comments
 (0)