Skip to content

Commit

Permalink
seatPrice formula fixed (#746)
Browse files Browse the repository at this point in the history
  • Loading branch information
volovyks authored Dec 9, 2021
1 parent 11f3a1c commit 5dd4195
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/validators.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions lib/validators.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions src/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import { CurrentEpochValidatorInfo, NextEpochValidatorInfo } from './providers/p
* @params minimumStakeRatio: minimum stake ratio
* @params protocolVersion: version of the protocol from genesis config
*/
export function findSeatPrice(validators: (CurrentEpochValidatorInfo | NextEpochValidatorInfo)[], maxNumberOfSeats: number, minimumStakeRatio: number, protocolVersion?: number): BN {
export function findSeatPrice(validators: (CurrentEpochValidatorInfo | NextEpochValidatorInfo)[], maxNumberOfSeats: number, minimumStakeRatio: number[], protocolVersion?: number): BN {
if (protocolVersion && protocolVersion < 49) {
return findSeatPriceForProtocolBefore49(validators, maxNumberOfSeats);
}
if (!minimumStakeRatio) {
const deprecate = depd('findSeatPrice(validators, maxNumberOfSeats)');
deprecate('`use `findSeatPrice(validators, maxNumberOfSeats, minimumStakeRatio)` instead');
minimumStakeRatio = 6250; // harcoded minimumStakeRation from 12/7/21
minimumStakeRatio = [1, 6250]; // harcoded minimumStakeRation from 12/7/21
}
return findSeatPriceForProtocolAfter49(validators, maxNumberOfSeats, minimumStakeRatio);
}
Expand Down Expand Up @@ -52,11 +52,14 @@ function findSeatPriceForProtocolBefore49(validators: (CurrentEpochValidatorInfo
}

// nearcore reference: https://github.com/near/nearcore/blob/5a8ae263ec07930cd34d0dcf5bcee250c67c02aa/chain/epoch_manager/src/validator_selection.rs#L308;L315
function findSeatPriceForProtocolAfter49(validators: (CurrentEpochValidatorInfo | NextEpochValidatorInfo)[], maxNumberOfSeats: number, minimumStakeRatio: number): BN {
function findSeatPriceForProtocolAfter49(validators: (CurrentEpochValidatorInfo | NextEpochValidatorInfo)[], maxNumberOfSeats: number, minimumStakeRatio: number[]): BN {
if (minimumStakeRatio.length != 2) {
throw Error('minimumStakeRatio should have 2 elements');
}
const stakes = validators.map(v => new BN(v.stake, 10)).sort((a, b) => a.cmp(b));
const stakesSum = stakes.reduce((a, b) => a.add(b));
if (validators.length < maxNumberOfSeats) {
return stakesSum.div(new BN(minimumStakeRatio));
return stakesSum.mul(new BN(minimumStakeRatio[0])).div(new BN(minimumStakeRatio[1]));
} else {
return stakes[0].add(new BN(1));
}
Expand Down
8 changes: 4 additions & 4 deletions test/validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ const BN = require('bn.js');

test('find seat price', async () => {
expect(nearApi.validators.findSeatPrice(
[{stake: '1000000'}, {stake: '1000000'}, {stake: '100'}], 2, 6250, 49
[{stake: '1000000'}, {stake: '1000000'}, {stake: '100'}], 2, [1, 6250], 49
)).toEqual(new BN('101'));
expect(nearApi.validators.findSeatPrice(
[{stake: '1000000'}, {stake: '1000000'}, {stake: '100'}], 3, 6250
[{stake: '1000000'}, {stake: '1000000'}, {stake: '100'}], 3, [1, 6250]
)).toEqual(new BN('101'));
expect(nearApi.validators.findSeatPrice(
[{stake: '1000000'}, {stake: '1000000'}, {stake: '100'}], 4, 6250, 49
[{stake: '1000000'}, {stake: '1000000'}, {stake: '100'}], 4, [1, 6250], 49
)).toEqual(new BN('320'));
expect(nearApi.validators.findSeatPrice(
[{stake: '1000'}, {stake: '1000'}, {stake: '200'}], 100, 25
[{stake: '1000'}, {stake: '1000'}, {stake: '200'}], 100, [1, 25]
)).toEqual(new BN('88'));
});

Expand Down

0 comments on commit 5dd4195

Please sign in to comment.