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

View File

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

View File

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