Skip to content

Commit

Permalink
Small query cleanups (historic) (#1905)
Browse files Browse the repository at this point in the history
* Small query cleanups (historic)

* Rewards average counter update
  • Loading branch information
jacogr authored Nov 17, 2019
1 parent 8f25962 commit dc58883
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 53 deletions.
6 changes: 4 additions & 2 deletions packages/app-staking/src/Query/Validator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 => {
Expand Down Expand Up @@ -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)) {
Expand All @@ -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 });
Expand Down
7 changes: 2 additions & 5 deletions packages/app-staking/src/useSessionRewards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

Expand Down
33 changes: 0 additions & 33 deletions packages/react-api/src/util/hashes.ts

This file was deleted.

18 changes: 7 additions & 11 deletions packages/react-api/src/util/historic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])
);
}
2 changes: 0 additions & 2 deletions packages/react-api/src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit dc58883

Please sign in to comment.