From 8ec9fe31d7c94244a4add6dda59a356592066711 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Wed, 12 Aug 2020 13:10:41 +0200 Subject: [PATCH] useMemo on payouts (#3400) --- packages/page-staking/src/Payouts/index.tsx | 111 +++++++++++--------- 1 file changed, 62 insertions(+), 49 deletions(-) diff --git a/packages/page-staking/src/Payouts/index.tsx b/packages/page-staking/src/Payouts/index.tsx index 70faf008fa..9e5bc22fcd 100644 --- a/packages/page-staking/src/Payouts/index.tsx +++ b/packages/page-staking/src/Payouts/index.tsx @@ -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): Payo .sort((a, b) => b.available.cmp(a.available)); } +function getAvailable (allRewards: Record | 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('{{days}} days', { replace: { days } }), + value: dayBlocks.div(eraLength).toNumber() + }); + + days = days * 3; + } + + eraSelection.push({ + text: t('Max, {{eras}} eras', { replace: { eras: historyDepth.toNumber() } }), + value: historyDepth.toNumber() + }); + + return eraSelection; + } + + return [{ text: '', value: 0 }]; +} + function Payouts ({ className = '', isInElection, ownValidators }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); - const [{ stashTotal, stashes, validators }, setPayouts] = useState({}); - const [eraSelection, setEraSelection] = useState([{ 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(api.derive.session.eraLength, []); const historyDepth = useCall(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('{{days}} days', { replace: { days } }), - value: dayBlocks.div(eraLength).toNumber() - }); - - days = days * 3; - } - - eraSelection.push({ - text: t('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],