Skip to content

Commit

Permalink
Merge pull request #24 from Cardinal-Cryptography/A0-1101
Browse files Browse the repository at this point in the history
A0-1101 Make validators APRs equal to each other (with an accuracy up to commission)
  • Loading branch information
Marcin-Radecki authored Jul 28, 2022
2 parents a32d1ca + f258e0d commit 5f09a9a
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions packages/page-staking/src/useSortedTargets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { SortedTargets, TargetSortBy, ValidatorInfo } from './types';
import { useMemo } from 'react';

import { createNamedHook, useAccounts, useApi, useCall, useCallMulti, useInflation } from '@polkadot/react-hooks';
import { arrayFlatten, BN, BN_HUNDRED, BN_MAX_INTEGER, BN_ONE, BN_ZERO } from '@polkadot/util';
import { arrayFlatten, BN, BN_ONE, BN_ZERO } from '@polkadot/util';

interface LastEra {
activeEra: BN;
Expand Down Expand Up @@ -173,19 +173,15 @@ function extractSingle (api: ApiPromise, allAccounts: string[], derive: DeriveSt
}

function addReturns (inflation: Inflation, baseInfo: Partial<SortedTargets>): Partial<SortedTargets> {
const avgStaked = baseInfo.avgStaked;
const validators = baseInfo.validators;

if (!validators) {
return baseInfo;
}

avgStaked && !avgStaked.isZero() && validators.forEach((v): void => {
validators.forEach((v): void => {
if (!v.skipRewards && v.withReturns) {
const adjusted = avgStaked.mul(BN_HUNDRED).imuln(inflation.stakedReturn).div(v.bondTotal);

// in some cases, we may have overflows... protect against those
v.stakedReturn = (adjusted.gt(BN_MAX_INTEGER) ? BN_MAX_INTEGER : adjusted).toNumber() / BN_HUNDRED.toNumber();
v.stakedReturn = inflation.stakedReturn;
v.stakedReturnCmp = v.stakedReturn * (100 - v.commissionPer) / 100;
}
});
Expand Down

0 comments on commit 5f09a9a

Please sign in to comment.