// Copyright 2017-2023 @polkadot/app-staking authors & contributors // SPDX-License-Identifier: Apache-2.0 import type { DeriveStakerPrefs } from '@polkadot/api-derive/types'; import type { LineData, Props } from './types'; import React, { useEffect, useMemo, useRef, useState } from 'react'; import { useApi, useCall } from '@polkadot/react-hooks'; import { BN, BN_BILLION } from '@polkadot/util'; import { useTranslation } from '../translate'; import Chart from './Chart'; const MULT = new BN(100 * 100); const COLORS_POINTS = [undefined, '#acacac']; function extractPrefs (labels: string[], prefs: DeriveStakerPrefs[]): LineData { const avgSet = new Array(labels.length); const idxSet = new Array(labels.length); const [total, avgCount] = prefs.reduce(([total, avgCount], { validatorPrefs }) => { const comm = validatorPrefs.commission.unwrap().mul(MULT).div(BN_BILLION).toNumber() / 100; if (comm !== 0) { total += comm; avgCount++; } return [total, avgCount]; }, [0, 0]); prefs.forEach(({ era, validatorPrefs }): void => { const comm = validatorPrefs.commission.unwrap().mul(MULT).div(BN_BILLION).toNumber() / 100; const avg = avgCount > 0 ? Math.ceil(total * 100 / avgCount) / 100 : 0; const index = labels.indexOf(era.toHuman()); if (index !== -1) { avgSet[index] = avg; idxSet[index] = comm; } }); return [idxSet, avgSet]; } function ChartPrefs ({ labels, validatorId }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); const params = useMemo(() => [validatorId, false], [validatorId]); const stakerPrefs = useCall(api.derive.staking.stakerPrefs, params); const [values, setValues] = useState([]); useEffect( () => setValues([]), [validatorId] ); useEffect( () => stakerPrefs && setValues(extractPrefs(labels, stakerPrefs)), [labels, stakerPrefs] ); const legendsRef = useRef([ t('commission'), t('average') ]); return ( ('commission')} labels={labels} legends={legendsRef.current} values={values} /> ); } export default React.memo(ChartPrefs);