Skip to content

Commit

Permalink
chore: nitpicks
Browse files Browse the repository at this point in the history
  • Loading branch information
gtsonevv committed Jan 5, 2024
1 parent 49a17f4 commit 818fc25
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/transactions/src/create_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import { Action } from './actions';
import { Transaction } from './schema';

export function createTransaction(signerId: string, publicKey: PublicKey, receiverId: string, nonce: bigint | string | number, actions: Action[], blockHash: Uint8Array): Transaction {
const bigIntNonce = typeof nonce === 'string' || typeof nonce === 'number' ? BigInt(nonce) : nonce;
return new Transaction({ signerId, publicKey, nonce: bigIntNonce, receiverId, actions, blockHash });
const txNonce = typeof nonce === 'bigint' ? nonce : BigInt(nonce);
return new Transaction({ signerId, publicKey, nonce: txNonce, receiverId, actions, blockHash });
}
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './logging';
export * from './provider';
export * from './validators';
export * from './logger';
export * from './utils';
3 changes: 3 additions & 0 deletions packages/utils/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function sortBigIntAsc(a: bigint, b: bigint) {
return (a < b ? -1 : a > b ? 1 : 0)
}
7 changes: 4 additions & 3 deletions packages/utils/src/validators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CurrentEpochValidatorInfo, NextEpochValidatorInfo } from '@near-js/types';
import depd from 'depd';
import { sortBigIntAsc } from './utils';

/** Finds seat price given validators stakes and number of seats.
* Calculation follow the spec: https://nomicon.io/Economics/README.html#validator-selection
Expand All @@ -21,7 +22,7 @@ export function findSeatPrice(validators: (CurrentEpochValidatorInfo | NextEpoch
}

function findSeatPriceForProtocolBefore49(validators: (CurrentEpochValidatorInfo | NextEpochValidatorInfo)[], numSeats: number): bigint {
const stakes = validators.map(v => BigInt(v.stake)).sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
const stakes = validators.map(v => BigInt(v.stake)).sort(sortBigIntAsc);
const num = BigInt(numSeats);
const stakesSum = stakes.reduce((a, b) => a + b);
if (stakesSum < num) {
Expand All @@ -34,7 +35,7 @@ function findSeatPriceForProtocolBefore49(validators: (CurrentEpochValidatorInfo
let found = false;
let currentSum = BigInt(0);
for (let i = 0; i < stakes.length; ++i) {
currentSum = currentSum + stakes[i] / mid;
currentSum = currentSum + (stakes[i] / mid);
if (currentSum >= num) {
left = mid;
found = true;
Expand All @@ -53,7 +54,7 @@ function findSeatPriceForProtocolAfter49(validators: (CurrentEpochValidatorInfo
if (minimumStakeRatio.length != 2) {
throw Error('minimumStakeRatio should have 2 elements');
}
const stakes = validators.map(v => BigInt(v.stake)).sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
const stakes = validators.map(v => BigInt(v.stake)).sort(sortBigIntAsc);
const stakesSum = stakes.reduce((a, b) => a + b);
if (validators.length < maxNumberOfSeats) {
return stakesSum * BigInt(minimumStakeRatio[0]) / BigInt(minimumStakeRatio[1]);
Expand Down

0 comments on commit 818fc25

Please sign in to comment.