Small query cleanups (historic) (#1905)

* Small query cleanups (historic)

* Rewards average counter update
This commit is contained in:
Jaco Greeff 2019-11-17 22:04:24 +01:00 committed by GitHub
parent 8f25962458
commit dc588838d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 53 deletions

View File

@ -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<void> => {
const values = await getHistoric<Exposure>(api, 'staking.stakers', [validatorId], hashes);
const values = await getHistoric<Exposure>(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 });

View File

@ -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)
);
}

View File

@ -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<Hash> => api.rpc.chain.getBlockHash(at as any))
);
return numbers.map((bn, index): [BN, Hash] => [bn, hashes[index]]);
}

View File

@ -5,16 +5,12 @@
import { Hash } from '@polkadot/types/interfaces';
import { Codec } from '@polkadot/types/types';
import ApiPromise from '@polkadot/api/promise';
type AtQuery <I extends any[]> = (hash: string | Uint8Array, ...params: I) => Promise<Codec>;
export default async function getHistory <T extends Codec> (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<T> =>
api.query[mod][fn].at(hash, ...params) as Promise<T>
));
return results.map((value, index): [Hash, T] => [
hashes[index],
value
]);
export default async function getHistoric <T extends Codec, I extends any[] = any[]> (atQuery: AtQuery<I>, params: I, hashes: Hash[]): Promise<[Hash, T][]> {
return Promise
.all(hashes.map((hash): Promise<T> => atQuery(hash, ...params) as Promise<T>))
.then((results): [Hash, T][] =>
results.map((value, index): [Hash, T] => [hashes[index], value])
);
}

View File

@ -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,