useMemo on payouts (#3400)
This commit is contained in:
parent
20fb4c52de
commit
8ec9fe31d7
@ -2,13 +2,15 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import type { TFunction } from 'i18next';
|
||||
import { DeriveStakerReward } from '@polkadot/api-derive/types';
|
||||
import { StakerState } from '@polkadot/react-hooks/types';
|
||||
import { PayoutStash, PayoutValidator } from './types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import React, { useEffect, useMemo, useState } from 'react';
|
||||
import React, { useMemo, useState } from 'react';
|
||||
import styled from 'styled-components';
|
||||
import { ApiPromise } from '@polkadot/api';
|
||||
import { Button, Table } from '@polkadot/react-components';
|
||||
import { useApi, useCall, useOwnEraRewards } from '@polkadot/react-hooks';
|
||||
import { FormatBalance } from '@polkadot/react-query';
|
||||
@ -99,67 +101,78 @@ function extractStashes (allRewards: Record<string, DeriveStakerReward[]>): Payo
|
||||
.sort((a, b) => b.available.cmp(a.available));
|
||||
}
|
||||
|
||||
function getAvailable (allRewards: Record<string, DeriveStakerReward[]> | null | undefined, stakerPayoutsAfter: BN): Available {
|
||||
if (allRewards) {
|
||||
const stashes = extractStashes(allRewards);
|
||||
const stashTotal = stashes.length
|
||||
? stashes.reduce((total: BN, { available }) => total.add(available), BN_ZERO)
|
||||
: null;
|
||||
|
||||
return {
|
||||
stashTotal,
|
||||
stashes,
|
||||
validators: groupByValidator(allRewards, stakerPayoutsAfter)
|
||||
};
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
function getOptions (api: ApiPromise, eraLength: BN | undefined, historyDepth: BN | undefined, t: TFunction): EraSelection[] {
|
||||
if (eraLength && historyDepth) {
|
||||
const blocksPerDay = DAY_SECS.div(api.consts.babe?.expectedBlockTime || api.consts.timestamp?.minimumPeriod.muln(2) || new BN(6000));
|
||||
const maxBlocks = eraLength.mul(historyDepth);
|
||||
const eraSelection: EraSelection[] = [];
|
||||
let days = 2;
|
||||
|
||||
while (true) {
|
||||
const dayBlocks = blocksPerDay.muln(days);
|
||||
|
||||
if (dayBlocks.gte(maxBlocks)) {
|
||||
break;
|
||||
}
|
||||
|
||||
eraSelection.push({
|
||||
text: t<string>('{{days}} days', { replace: { days } }),
|
||||
value: dayBlocks.div(eraLength).toNumber()
|
||||
});
|
||||
|
||||
days = days * 3;
|
||||
}
|
||||
|
||||
eraSelection.push({
|
||||
text: t<string>('Max, {{eras}} eras', { replace: { eras: historyDepth.toNumber() } }),
|
||||
value: historyDepth.toNumber()
|
||||
});
|
||||
|
||||
return eraSelection;
|
||||
}
|
||||
|
||||
return [{ text: '', value: 0 }];
|
||||
}
|
||||
|
||||
function Payouts ({ className = '', isInElection, ownValidators }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
const { api } = useApi();
|
||||
const [{ stashTotal, stashes, validators }, setPayouts] = useState<Available>({});
|
||||
const [eraSelection, setEraSelection] = useState<EraSelection[]>([{ text: '', value: 0 }]);
|
||||
const [hasOwnValidators] = useState(ownValidators.length !== 0);
|
||||
const [myStashesIndex, setMyStashesIndex] = useState((api.tx.staking.payoutStakers && hasOwnValidators) ? 0 : 1);
|
||||
const [eraSelectionIndex, setEraSelectionIndex] = useState(0);
|
||||
const eraLength = useCall<BN>(api.derive.session.eraLength, []);
|
||||
const historyDepth = useCall<BN>(api.query.staking.historyDepth, []);
|
||||
const stakerPayoutsAfter = useStakerPayouts();
|
||||
const { allRewards, isLoadingRewards } = useOwnEraRewards(eraSelection[eraSelectionIndex].value, myStashesIndex ? undefined : ownValidators);
|
||||
const isDisabled = isInElection || !isFunction(api.tx.utility?.batch);
|
||||
|
||||
useEffect((): void => {
|
||||
if (eraLength && historyDepth) {
|
||||
const blocksPerDay = DAY_SECS.div(api.consts.babe?.expectedBlockTime || api.consts.timestamp?.minimumPeriod.muln(2) || new BN(6000));
|
||||
const maxBlocks = eraLength.mul(historyDepth);
|
||||
const eraSelection: EraSelection[] = [];
|
||||
let days = 2;
|
||||
const eraSelection = useMemo(
|
||||
() => getOptions(api, eraLength, historyDepth, t),
|
||||
[api, eraLength, historyDepth, t]
|
||||
);
|
||||
|
||||
while (true) {
|
||||
const dayBlocks = blocksPerDay.muln(days);
|
||||
const { allRewards, isLoadingRewards } = useOwnEraRewards(eraSelection[eraSelectionIndex].value, myStashesIndex ? undefined : ownValidators);
|
||||
|
||||
if (dayBlocks.gte(maxBlocks)) {
|
||||
break;
|
||||
}
|
||||
|
||||
eraSelection.push({
|
||||
text: t<string>('{{days}} days', { replace: { days } }),
|
||||
value: dayBlocks.div(eraLength).toNumber()
|
||||
});
|
||||
|
||||
days = days * 3;
|
||||
}
|
||||
|
||||
eraSelection.push({
|
||||
text: t<string>('Max, {{eras}} eras', { replace: { eras: historyDepth.toNumber() } }),
|
||||
value: historyDepth.toNumber()
|
||||
});
|
||||
|
||||
setEraSelection(eraSelection);
|
||||
}
|
||||
}, [api, eraLength, historyDepth, t]);
|
||||
|
||||
useEffect((): void => {
|
||||
if (allRewards) {
|
||||
const stashes = extractStashes(allRewards);
|
||||
const stashTotal = stashes.length
|
||||
? stashes.reduce((total: BN, { available }) => total.add(available), BN_ZERO)
|
||||
: null;
|
||||
|
||||
setPayouts({
|
||||
stashTotal,
|
||||
stashes,
|
||||
validators: groupByValidator(allRewards, stakerPayoutsAfter)
|
||||
});
|
||||
} else {
|
||||
setPayouts({});
|
||||
}
|
||||
}, [allRewards, stakerPayoutsAfter]);
|
||||
const { stashTotal, stashes, validators } = useMemo(
|
||||
() => getAvailable(allRewards, stakerPayoutsAfter),
|
||||
[allRewards, stakerPayoutsAfter]
|
||||
);
|
||||
|
||||
const headerStashes = useMemo(() => [
|
||||
[myStashesIndex ? t('payout/stash') : t('overall/validator'), 'start', 2],
|
||||
|
||||
Loading…
Reference in New Issue
Block a user