Add validator total to staker rewards (#2129)

* Add validator total to staker rewards

* yarn rewrite
This commit is contained in:
Jaco Greeff
2020-04-02 16:16:10 +02:00
committed by GitHub
parent 8ef983169f
commit 8213fa7cb9
4 changed files with 376 additions and 361 deletions
+1 -1
View File
@@ -28,7 +28,7 @@
"@babel/register": "^7.9.0",
"@babel/runtime": "^7.9.2",
"@polkadot/dev": "^0.52.4",
"@polkadot/ts": "^0.3.14",
"@polkadot/ts": "^0.3.15",
"@polkadot/typegen": "workspace:packages/typegen",
"@types/jest": "^25.1.4",
"copyfiles": "^2.2.0"
@@ -3,8 +3,7 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Balance } from '@polkadot/types/interfaces';
import { DeriveEraPoints, DeriveEraPrefs, DeriveEraRewards, DeriveEraValPrefs, DeriveStakerExposure, DeriveStakerReward } from '../types';
import { DeriveEraPoints, DeriveEraPrefs, DeriveEraRewards, DeriveEraValPrefs, DeriveStakerExposure, DeriveStakerReward, DeriveStakerRewardValidator } from '../types';
import BN from 'bn.js';
import { Observable, combineLatest } from 'rxjs';
@@ -19,10 +18,10 @@ const COMM_DIV = new BN(1_000_000_000);
function parseRewards (api: ApiInterfaceRx, stakerId: string, [erasPoints, erasPrefs, erasRewards, exposures]: Result): DeriveStakerReward[] {
return exposures.map(({ era, isEmpty, isValidator, nominating, validators: eraValidators }): DeriveStakerReward => {
const { eraPoints, validators: allValPoints } = erasPoints.find((p) => p.era.eq(era)) || { eraPoints: new BN(0), validators: {} };
const { eraReward } = erasRewards.find((r) => r.era.eq(era)) || { eraReward: ZERO };
const { eraPoints, validators: allValPoints } = erasPoints.find((p) => p.era.eq(era)) || { eraPoints: ZERO, validators: {} };
const { eraReward } = erasRewards.find((r) => r.era.eq(era)) || { eraReward: api.registry.createType('Balance') };
const { validators: allValPrefs } = erasPrefs.find((p) => p.era.eq(era)) || { validators: {} as DeriveEraValPrefs };
const validators: Record<string, Balance> = {};
const validators: Record<string, DeriveStakerRewardValidator> = {};
let total = ZERO;
Object.entries(eraValidators).forEach(([validatorId, exposure]): void => {
@@ -50,11 +49,15 @@ function parseRewards (api: ApiInterfaceRx, stakerId: string, [erasPoints, erasP
total = total.add(value);
}
validators[validatorId] = api.registry.createType('Balance', value);
validators[validatorId] = {
total: api.registry.createType('Balance', avail),
value: api.registry.createType('Balance', value)
};
});
return {
era,
eraReward,
isEmpty,
isValidator,
nominating,
+7 -1
View File
@@ -67,13 +67,19 @@ export interface DeriveStakerExposure {
validators: DeriveEraValidatorExposure;
}
export interface DeriveStakerRewardValidator {
total: Balance;
value: Balance;
}
export interface DeriveStakerReward {
era: EraIndex;
eraReward: Balance;
isEmpty: boolean;
isValidator: boolean;
nominating: [string, number][];
validators: Record<string, Balance>;
total: Balance;
validators: Record<string, DeriveStakerRewardValidator>;
}
export interface DeriveStakerSlashes {
+359 -353
View File
File diff suppressed because it is too large Load Diff