From adbf73eabb4cfd7aadd1013ef7970237dfb47dc1 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sat, 13 Feb 2021 19:58:50 +0100 Subject: [PATCH] Adjust calcs --- packages/page-staking/src/useSortedTargets.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/page-staking/src/useSortedTargets.ts b/packages/page-staking/src/useSortedTargets.ts index b52e98db9e..f84aa61d49 100644 --- a/packages/page-staking/src/useSortedTargets.ts +++ b/packages/page-staking/src/useSortedTargets.ts @@ -79,8 +79,8 @@ function sortValidators (list: ValidatorInfo[]): ValidatorInfo[] { ); } -function extractSingle (api: ApiPromise, allAccounts: string[], derive: DeriveStakingElected | DeriveStakingWaiting, favorites: string[], { activeEra, eraLength, lastEra, sessionLength }: LastEra, historyDepth?: BN): [ValidatorInfo[], string[]] { - const nominators: Record = {}; +function extractSingle (api: ApiPromise, allAccounts: string[], derive: DeriveStakingElected | DeriveStakingWaiting, favorites: string[], { activeEra, eraLength, lastEra, sessionLength }: LastEra, historyDepth?: BN): [ValidatorInfo[], Record] { + const nominators: Record = {}; const emptyExposure = api.createType('Exposure'); const earliestEra = historyDepth && lastEra.sub(historyDepth).iadd(BN_ONE); const list = derive.info.map(({ accountId, exposure = emptyExposure, stakingLedger, validatorPrefs }): ValidatorInfo => { @@ -130,7 +130,7 @@ function extractSingle (api: ApiPromise, allAccounts: string[], derive: DeriveSt isNominating: (exposure.others || []).reduce((isNominating, indv): boolean => { const nominator = indv.who.toString(); - nominators[nominator] = true; + nominators[nominator] = (nominators[nominator] || BN_ZERO).add(indv.value.toBn()); return isNominating || allAccounts.includes(nominator); }, allAccounts.includes(key)), @@ -155,7 +155,7 @@ function extractSingle (api: ApiPromise, allAccounts: string[], derive: DeriveSt }; }); - return [list, Object.keys(nominators)]; + return [list, nominators]; } function extractInfo (api: ApiPromise, allAccounts: string[], electedDerive: DeriveStakingElected, waitingDerive: DeriveStakingWaiting, favorites: string[], totalIssuance: BN, lastEraInfo: LastEra, historyDepth?: BN): Partial { @@ -181,9 +181,9 @@ function extractInfo (api: ApiPromise, allAccounts: string[], electedDerive: Der }); // all validators, calc median commission - const minNominated = elected.reduce((min: BN, { minNominated }) => { - return min.isZero() || minNominated.lt(min) - ? minNominated + const minNominated = Object.values(nominators).reduce((min: BN, value) => { + return min.isZero() || value.lt(min) + ? value : min; }, BN_ZERO); const validators = sortValidators(arrayFlatten([elected, waiting])); @@ -206,7 +206,7 @@ function extractInfo (api: ApiPromise, allAccounts: string[], electedDerive: Der lowStaked: activeTotals[0] || BN_ZERO, medianComm, minNominated, - nominators, + nominators: Object.keys(nominators), totalIssuance, totalStaked, validatorIds,