From 6a8d26f411613aff9d75b981581bd00bc9e1ea89 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 3 Sep 2020 23:22:58 +0200 Subject: [PATCH] Cleanup average calcs for targets (#3604) --- packages/page-staking/src/types.ts | 1 + packages/page-staking/src/useSortedTargets.ts | 31 ++++++++++++------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/packages/page-staking/src/types.ts b/packages/page-staking/src/types.ts index 8c3121d3b2..71e9631671 100644 --- a/packages/page-staking/src/types.ts +++ b/packages/page-staking/src/types.ts @@ -47,6 +47,7 @@ export interface ValidatorInfo extends ValidatorInfoRank { bondTotal: Balance; commissionPer: number; hasIdentity: boolean; + isActive: boolean; isCommission: boolean; isElected: boolean; isFavorite: boolean; diff --git a/packages/page-staking/src/useSortedTargets.ts b/packages/page-staking/src/useSortedTargets.ts index 70fde2ea41..2cab234521 100644 --- a/packages/page-staking/src/useSortedTargets.ts +++ b/packages/page-staking/src/useSortedTargets.ts @@ -64,7 +64,7 @@ function sortValidators (list: ValidatorInfo[]): ValidatorInfo[] { ); } -function extractSingle (allAccounts: string[], amount: BN = baseBalance(), { info }: DeriveStakingElected | DeriveStakingWaiting, favorites: string[], perValidatorReward: BN, isElected: boolean): [ValidatorInfo[], string[], BN, BN] { +function extractSingle (allAccounts: string[], amount: BN = baseBalance(), { info }: DeriveStakingElected | DeriveStakingWaiting, favorites: string[], perValidatorReward: BN, isElected: boolean): [ValidatorInfo[], string[]] { const defaultExposure = { others: registry.createType('Vec'), own: registry.createType('Compact'), @@ -74,8 +74,6 @@ function extractSingle (allAccounts: string[], amount: BN = baseBalance(), { inf commission: registry.createType('Compact') }; const nominators: Record = {}; - let totalStaked = BN_ZERO; - let lowStaked = BN_ZERO; const list = info.map(({ accountId, exposure: _exposure, stakingLedger, validatorPrefs }): ValidatorInfo => { const exposure = _exposure || defaultExposure; const prefs = (validatorPrefs as (ValidatorPrefs | ValidatorPrefsTo196)) || defaultPrefs; @@ -103,12 +101,6 @@ function extractSingle (allAccounts: string[], amount: BN = baseBalance(), { inf return isNominating || allAccounts.includes(nominator); }, allAccounts.includes(key)); - totalStaked = totalStaked.add(bondTotal); - - if ((lowStaked.isZero() || bondTotal.lt(lowStaked)) && _exposure && !_exposure.total.isEmpty) { - lowStaked = bondTotal; - } - return { accountId, bondOther: bondTotal.sub(bondOwn), @@ -117,6 +109,7 @@ function extractSingle (allAccounts: string[], amount: BN = baseBalance(), { inf bondTotal, commissionPer: (((prefs as ValidatorPrefs).commission?.unwrap() || BN_ZERO).toNumber() / 10_000_000), hasIdentity: false, + isActive: !skipRewards, isCommission: !!(prefs as ValidatorPrefs).commission, isElected, isFavorite: favorites.includes(key), @@ -137,17 +130,31 @@ function extractSingle (allAccounts: string[], amount: BN = baseBalance(), { inf }; }); - return [list, Object.keys(nominators), totalStaked, lowStaked]; + return [list, Object.keys(nominators)]; } function extractInfo (allAccounts: string[], amount: BN = baseBalance(), electedDerive: DeriveStakingElected, waitingDerive: DeriveStakingWaiting, favorites: string[], lastReward = BN_ONE): Partial { const perValidatorReward = lastReward.divn(electedDerive.info.length); - const [elected, nominators, totalStaked, lowStaked] = extractSingle(allAccounts, amount, electedDerive, favorites, perValidatorReward, true); + const [elected, nominators] = extractSingle(allAccounts, amount, electedDerive, favorites, perValidatorReward, true); const [waiting] = extractSingle(allAccounts, amount, waitingDerive, favorites, perValidatorReward, false); const validators = sortValidators(elected.concat(waiting)); const validatorIds = validators.map(({ accountId }) => accountId.toString()); + const activeTotals = elected + .filter(({ isActive }) => isActive) + .map(({ bondTotal }) => bondTotal) + .sort((a, b) => a.cmp(b)); + const totalStaked = activeTotals.reduce((total: BN, value) => total.iadd(value), new BN(0)); + const avgStaked = totalStaked.divn(activeTotals.length); - return { avgStaked: totalStaked.divn(electedDerive.info.length), lowStaked, nominators, totalStaked, validatorIds, validators }; + // median + // const midIndex = Math.floor(activeTotals.length / 2); + // const avgStaked = activeTotals.length + // ? activeTotals.length % 2 + // ? activeTotals[midIndex] + // : activeTotals[midIndex - 1].add(activeTotals[midIndex]).divn(2) + // : BN_ZERO; + + return { avgStaked, lowStaked: activeTotals[0] || BN_ZERO, nominators, totalStaked, validatorIds, validators }; } const transformEra = {