From dc588838d6171afe792e0d64c1664101f7af08cb Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sun, 17 Nov 2019 22:04:24 +0100 Subject: [PATCH] Small query cleanups (historic) (#1905) * Small query cleanups (historic) * Rewards average counter update --- packages/app-staking/src/Query/Validator.tsx | 6 ++-- .../app-staking/src/useSessionRewards.tsx | 7 ++-- packages/react-api/src/util/hashes.ts | 33 ------------------- packages/react-api/src/util/historic.ts | 18 ++++------ packages/react-api/src/util/index.ts | 2 -- 5 files changed, 13 insertions(+), 53 deletions(-) delete mode 100644 packages/react-api/src/util/hashes.ts diff --git a/packages/app-staking/src/Query/Validator.tsx b/packages/app-staking/src/Query/Validator.tsx index cc9a3dc7e4..26d4cb0e36 100644 --- a/packages/app-staking/src/Query/Validator.tsx +++ b/packages/app-staking/src/Query/Validator.tsx @@ -111,7 +111,7 @@ function Validator ({ className, sessionRewards, t, validatorId }: Props): React const stakeLabels = sessionRewards.map(({ sessionIndex }): string => formatNumber(sessionIndex)); api.isReady.then(async (): Promise => { - const values = await getHistoric(api, 'staking.stakers', [validatorId], hashes); + const values = await getHistoric(api.query.staking.stakers.at, [validatorId], hashes); const stakeChart = extractStake(values, divisor); const splitChart = extractSplit(values, validatorId); const splitMax = splitChart ? Math.min(Math.ceil(splitChart[0].value), 100) : 100; @@ -128,6 +128,7 @@ function Validator ({ className, sessionRewards, t, validatorId }: Props): React const rewardsChart: LineData = [[], [], []]; let total = new BN(0); let lastRewardIndex = 0; + let rewardCount = 0; // we only work from the second position, the first deemed incomplete sessionRewards.forEach(({ blockNumber, reward, sessionIndex, slashes }, index): void => { @@ -157,6 +158,7 @@ function Validator ({ className, sessionRewards, t, validatorId }: Props): React // add this to the total total = total.add(neg).add(pos); + rewardCount++; // if we have a reward here, set the reward index for the next iteration if (reward.gtn(0)) { @@ -169,7 +171,7 @@ function Validator ({ className, sessionRewards, t, validatorId }: Props): React // calculate and format to 3 decimals rewardsChart[0].push(balanceToNumber(neg, divisor)); rewardsChart[1].push(balanceToNumber(pos, divisor)); - rewardsChart[2].push(balanceToNumber(total.divn(index), divisor)); + rewardsChart[2].push(balanceToNumber(total.divn(rewardCount), divisor)); }); setRewardsInfo({ rewardsChart, rewardsLabels }); diff --git a/packages/app-staking/src/useSessionRewards.tsx b/packages/app-staking/src/useSessionRewards.tsx index 834d21e4aa..dccaa8e925 100644 --- a/packages/app-staking/src/useSessionRewards.tsx +++ b/packages/app-staking/src/useSessionRewards.tsx @@ -59,12 +59,9 @@ function mergeResults (sessions: SessionRewards[], newSessions: SessionRewards[] .concat(newSessions) .sort((a, b): number => a.blockNumber.cmp(b.blockNumber)); + // for the first, always use it, otherwise ignore on same sessionIndex return tmp.filter(({ sessionIndex }, index): boolean => - index === 0 - // for the first, always use it - ? true - // if the prev has the same sessionIndex, ignore this one - : !tmp[index - 1].sessionIndex.eq(sessionIndex) + index === 0 || !tmp[index - 1].sessionIndex.eq(sessionIndex) ); } diff --git a/packages/react-api/src/util/hashes.ts b/packages/react-api/src/util/hashes.ts deleted file mode 100644 index 59889f9de9..0000000000 --- a/packages/react-api/src/util/hashes.ts +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2017-2019 @polkadot/react-api authors & contributors -// This software may be modified and distributed under the terms -// of the Apache-2.0 license. See the LICENSE file for details. - -import { Hash } from '@polkadot/types/interfaces'; - -import BN from 'bn.js'; -import ApiPromise from '@polkadot/api/promise'; -import { bnToBn } from '@polkadot/util'; - -export interface HashesOptions { - interval: BN | number; - max: number; - startNumber: BN; -} - -export default async function getHashes (api: ApiPromise, { interval, max, startNumber }: HashesOptions): Promise<[BN, Hash][]> { - const numbers: BN[] = []; - const _interval = bnToBn(interval); - let currentNumber: BN = startNumber; - - while (numbers.length < max && currentNumber.gtn(0)) { - numbers.unshift(currentNumber); - - currentNumber = currentNumber.sub(_interval); - } - - const hashes = await Promise.all( - numbers.map((at): Promise => api.rpc.chain.getBlockHash(at as any)) - ); - - return numbers.map((bn, index): [BN, Hash] => [bn, hashes[index]]); -} diff --git a/packages/react-api/src/util/historic.ts b/packages/react-api/src/util/historic.ts index abced1efb8..21bbf0ea99 100644 --- a/packages/react-api/src/util/historic.ts +++ b/packages/react-api/src/util/historic.ts @@ -5,16 +5,12 @@ import { Hash } from '@polkadot/types/interfaces'; import { Codec } from '@polkadot/types/types'; -import ApiPromise from '@polkadot/api/promise'; +type AtQuery = (hash: string | Uint8Array, ...params: I) => Promise; -export default async function getHistory (api: ApiPromise, endpoint: string, params: any[], hashes: Hash[]): Promise<[Hash, T][]> { - const [mod, fn] = endpoint.split('.'); - const results = await Promise.all(hashes.map((hash): Promise => - api.query[mod][fn].at(hash, ...params) as Promise - )); - - return results.map((value, index): [Hash, T] => [ - hashes[index], - value - ]); +export default async function getHistoric (atQuery: AtQuery, params: I, hashes: Hash[]): Promise<[Hash, T][]> { + return Promise + .all(hashes.map((hash): Promise => atQuery(hash, ...params) as Promise)) + .then((results): [Hash, T][] => + results.map((value, index): [Hash, T] => [hashes[index], value]) + ); } diff --git a/packages/react-api/src/util/index.ts b/packages/react-api/src/util/index.ts index 21801a5e8f..4f916a14f8 100644 --- a/packages/react-api/src/util/index.ts +++ b/packages/react-api/src/util/index.ts @@ -2,14 +2,12 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import getHashes from './hashes'; import getHistoric from './historic'; import intervalObservable from './intervalObservable'; import isEqual from './isEqual'; import triggerChange from './triggerChange'; export { - getHashes, getHistoric, intervalObservable, isEqual,