feat(2629): validator table - adjust the overstaked amount calculation (#2630)

This commit is contained in:
Sam Keen 2023-01-17 09:01:44 +00:00 committed by GitHub
parent 89f41a4500
commit cd0ed05eb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 14 deletions

View File

@ -209,7 +209,9 @@ export const ValidatorTable = ({
<KeyValueTable data-testid="validator-table-penalties">
<KeyValueTableRow>
<span>{t('OVERSTAKED AMOUNT')}</span>
<span>{overstakedAmount.toString()}</span>
<span>
{formatNumber(toBigNum(overstakedAmount.toNumber(), decimals))}
</span>
</KeyValueTableRow>
<KeyValueTableRow>
<span>{t('OVERSTAKED PENALTY')}</span>
@ -224,7 +226,7 @@ export const ValidatorTable = ({
</span>
</KeyValueTableRow>
<KeyValueTableRow>
<span>{t('PERFORMANCE PENALITY')}</span>
<span>{t('PERFORMANCE PENALTY')}</span>
<span>{getPerformancePenalty(performanceScore)}</span>
</KeyValueTableRow>
<KeyValueTableRow noBorder={true}>

View File

@ -91,19 +91,26 @@ describe('getOverstakingPenalty', () => {
describe('getOverstakedAmount', () => {
it('should return the overstaked amount', () => {
expect(
getOverstakedAmount('0.21', Number(100).toString(), Number(20).toString())
).toEqual(new BigNumber(1));
// If a validator score is 0, any amount staked on the node is considered overstaked
getOverstakedAmount('0', Number(100).toString(), Number(20).toString())
).toEqual(new BigNumber(20));
expect(
getOverstakedAmount('0.22', Number(100).toString(), Number(20).toString())
).toEqual(new BigNumber(2));
getOverstakedAmount('0.05', Number(100).toString(), Number(20).toString())
).toEqual(new BigNumber(15));
expect(
getOverstakedAmount('0.30', Number(100).toString(), Number(20).toString())
getOverstakedAmount('0.1', Number(100).toString(), Number(20).toString())
).toEqual(new BigNumber(10));
expect(
getOverstakedAmount('0.15', Number(100).toString(), Number(20).toString())
).toEqual(new BigNumber(5));
expect(
getOverstakedAmount('0.2', Number(100).toString(), Number(20).toString())
).toEqual(new BigNumber(0));
});
it('should return 0 if the overstaked amount is negative', () => {
expect(
getOverstakedAmount('0.19', Number(100).toString(), Number(20).toString())
getOverstakedAmount('0.8', Number(100).toString(), Number(20).toString())
).toEqual(new BigNumber(0));
});
});

View File

@ -47,14 +47,13 @@ export const getOverstakedAmount = (
totalStake: string,
stakedOnNode: string
) => {
const amount = validatorScore
? new BigNumber(validatorScore)
.times(new BigNumber(totalStake))
.minus(new BigNumber(stakedOnNode))
.dp(2)
const toReturn = validatorScore
? new BigNumber(stakedOnNode).minus(
new BigNumber(validatorScore).times(new BigNumber(totalStake))
)
: new BigNumber(0);
return amount.isNegative() ? new BigNumber(0) : amount;
return toReturn.isNegative() ? new BigNumber(0) : toReturn;
};
export const getOverstakingPenalty = (