Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@near-js/utils

Package Overview
Dependencies
Maintainers
2
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@near-js/utils - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

lib/utils.d.ts

2

lib/constants.d.ts

@@ -1,2 +0,2 @@

export declare const DEFAULT_FUNCTION_CALL_GAS: any;
export declare const DEFAULT_FUNCTION_CALL_GAS: bigint;
//# sourceMappingURL=constants.d.ts.map
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEFAULT_FUNCTION_CALL_GAS = void 0;
const bn_js_1 = __importDefault(require("bn.js"));
// Default amount of gas to be sent with the function calls. Used to pay for the fees

@@ -14,2 +10,2 @@ // incurred while running the contract execution. The unused amount will be refunded back to

// For discussion see https://github.com/nearprotocol/NEPs/issues/67
exports.DEFAULT_FUNCTION_CALL_GAS = new bn_js_1.default('30000000000000');
exports.DEFAULT_FUNCTION_CALL_GAS = BigInt('30000000000000');

@@ -8,3 +8,3 @@ /**

*/
export declare const NEAR_NOMINATION: any;
export declare const NEAR_NOMINATION: bigint;
/**

@@ -11,0 +11,0 @@ * Convert account balance value from internal indivisible units to NEAR. 1 NEAR is defined by {@link NEAR_NOMINATION}.

@@ -7,3 +7,2 @@ "use strict";

exports.baseDecode = exports.baseEncode = exports.parseNearAmount = exports.formatNearAmount = exports.NEAR_NOMINATION = exports.NEAR_NOMINATION_EXP = void 0;
const bn_js_1 = __importDefault(require("bn.js"));
const bs58_1 = __importDefault(require("bs58"));

@@ -17,7 +16,7 @@ /**

*/
exports.NEAR_NOMINATION = new bn_js_1.default('10', 10).pow(new bn_js_1.default(exports.NEAR_NOMINATION_EXP, 10));
exports.NEAR_NOMINATION = BigInt(10) ** BigInt(exports.NEAR_NOMINATION_EXP);
// Pre-calculate offsets used for rounding to different number of digits
const ROUNDING_OFFSETS = [];
const BN10 = new bn_js_1.default(10);
for (let i = 0, offset = new bn_js_1.default(5); i < exports.NEAR_NOMINATION_EXP; i++, offset = offset.mul(BN10)) {
const BN10 = BigInt(10);
for (let i = 0, offset = BigInt(5); i < exports.NEAR_NOMINATION_EXP; i++, offset = offset * BN10) {
ROUNDING_OFFSETS[i] = offset;

@@ -34,3 +33,3 @@ }

function formatNearAmount(balance, fracDigits = exports.NEAR_NOMINATION_EXP) {
const balanceBN = new bn_js_1.default(balance, 10);
let balanceBN = BigInt(balance);
if (fracDigits !== exports.NEAR_NOMINATION_EXP) {

@@ -40,9 +39,11 @@ // Adjust balance for rounding at given number of digits

if (roundingExp > 0) {
balanceBN.iadd(ROUNDING_OFFSETS[roundingExp]);
balanceBN += ROUNDING_OFFSETS[roundingExp];
}
}
balance = balanceBN.toString();
const wholeStr = balance.substring(0, balance.length - exports.NEAR_NOMINATION_EXP) || '0';
const fractionStr = balance.substring(balance.length - exports.NEAR_NOMINATION_EXP)
.padStart(exports.NEAR_NOMINATION_EXP, '0').substring(0, fracDigits);
const wholeStr = balance.substring(0, balance.length - exports.NEAR_NOMINATION_EXP) || "0";
const fractionStr = balance
.substring(balance.length - exports.NEAR_NOMINATION_EXP)
.padStart(exports.NEAR_NOMINATION_EXP, "0")
.substring(0, fracDigits);
return trimTrailingZeroes(`${formatWithCommas(wholeStr)}.${fractionStr}`);

@@ -63,9 +64,9 @@ }

amt = cleanupAmount(amt);
const split = amt.split('.');
const split = amt.split(".");
const wholePart = split[0];
const fracPart = split[1] || '';
const fracPart = split[1] || "";
if (split.length > 2 || fracPart.length > exports.NEAR_NOMINATION_EXP) {
throw new Error(`Cannot parse '${amt}' as NEAR amount`);
}
return trimLeadingZeroes(wholePart + fracPart.padEnd(exports.NEAR_NOMINATION_EXP, '0'));
return trimLeadingZeroes(wholePart + fracPart.padEnd(exports.NEAR_NOMINATION_EXP, "0"));
}

@@ -79,3 +80,3 @@ exports.parseNearAmount = parseNearAmount;

function cleanupAmount(amount) {
return amount.replace(/,/g, '').trim();
return amount.replace(/,/g, "").trim();
}

@@ -88,3 +89,3 @@ /**

function trimTrailingZeroes(value) {
return value.replace(/\.?0*$/, '');
return value.replace(/\.?0*$/, "");
}

@@ -97,5 +98,5 @@ /**

function trimLeadingZeroes(value) {
value = value.replace(/^0+/, '');
if (value === '') {
return '0';
value = value.replace(/^0+/, "");
if (value === "") {
return "0";
}

@@ -112,3 +113,3 @@ return value;

while (pattern.test(value)) {
value = value.replace(pattern, '$1,$2');
value = value.replace(pattern, "$1,$2");
}

@@ -123,3 +124,3 @@ return value;

function baseEncode(value) {
if (typeof value === 'string') {
if (typeof value === "string") {
const bytes = [];

@@ -126,0 +127,0 @@ for (let c = 0; c < value.length; c++) {

@@ -8,2 +8,3 @@ export * from './constants';

export * from './logger';
export * from './utils';
//# sourceMappingURL=index.d.ts.map

@@ -24,1 +24,2 @@ "use strict";

__exportStar(require("./logger"), exports);
__exportStar(require("./utils"), exports);
import { CurrentEpochValidatorInfo, NextEpochValidatorInfo } from '@near-js/types';
import BN from 'bn.js';
/** Finds seat price given validators stakes and number of seats.

@@ -10,3 +9,3 @@ * Calculation follow the spec: https://nomicon.io/Economics/README.html#validator-selection

*/
export declare function findSeatPrice(validators: (CurrentEpochValidatorInfo | NextEpochValidatorInfo)[], maxNumberOfSeats: number, minimumStakeRatio: number[], protocolVersion?: number): BN;
export declare function findSeatPrice(validators: (CurrentEpochValidatorInfo | NextEpochValidatorInfo)[], maxNumberOfSeats: number, minimumStakeRatio: number[], protocolVersion?: number): bigint;
export interface ChangedValidatorInfo {

@@ -13,0 +12,0 @@ current: CurrentEpochValidatorInfo;

@@ -7,4 +7,4 @@ "use strict";

exports.diffEpochValidators = exports.findSeatPrice = void 0;
const bn_js_1 = __importDefault(require("bn.js"));
const depd_1 = __importDefault(require("depd"));
const utils_1 = require("./utils");
/** Finds seat price given validators stakes and number of seats.

@@ -30,17 +30,17 @@ * Calculation follow the spec: https://nomicon.io/Economics/README.html#validator-selection

function findSeatPriceForProtocolBefore49(validators, numSeats) {
const stakes = validators.map(v => new bn_js_1.default(v.stake, 10)).sort((a, b) => a.cmp(b));
const num = new bn_js_1.default(numSeats);
const stakesSum = stakes.reduce((a, b) => a.add(b));
if (stakesSum.lt(num)) {
const stakes = validators.map(v => BigInt(v.stake)).sort(utils_1.sortBigIntAsc);
const num = BigInt(numSeats);
const stakesSum = stakes.reduce((a, b) => a + b);
if (stakesSum < num) {
throw new Error('Stakes are below seats');
}
// assert stakesSum >= numSeats
let left = new bn_js_1.default(1), right = stakesSum.add(new bn_js_1.default(1));
while (!left.eq(right.sub(new bn_js_1.default(1)))) {
const mid = left.add(right).div(new bn_js_1.default(2));
let left = BigInt(1), right = stakesSum + BigInt(1);
while (left !== right - BigInt(1)) {
const mid = (left + right) / BigInt(2);
let found = false;
let currentSum = new bn_js_1.default(0);
let currentSum = BigInt(0);
for (let i = 0; i < stakes.length; ++i) {
currentSum = currentSum.add(stakes[i].div(mid));
if (currentSum.gte(num)) {
currentSum = currentSum + (stakes[i] / mid);
if (currentSum >= num) {
left = mid;

@@ -62,9 +62,9 @@ found = true;

}
const stakes = validators.map(v => new bn_js_1.default(v.stake, 10)).sort((a, b) => a.cmp(b));
const stakesSum = stakes.reduce((a, b) => a.add(b));
const stakes = validators.map(v => BigInt(v.stake)).sort(utils_1.sortBigIntAsc);
const stakesSum = stakes.reduce((a, b) => a + b);
if (validators.length < maxNumberOfSeats) {
return stakesSum.mul(new bn_js_1.default(minimumStakeRatio[0])).div(new bn_js_1.default(minimumStakeRatio[1]));
return stakesSum * BigInt(minimumStakeRatio[0]) / BigInt(minimumStakeRatio[1]);
}
else {
return stakes[0].add(new bn_js_1.default(1));
return stakes[0] + BigInt(1);
}

@@ -71,0 +71,0 @@ }

{
"name": "@near-js/utils",
"version": "0.1.0",
"version": "0.2.0",
"description": "Common methods and constants for the NEAR API JavaScript client",

@@ -10,7 +10,6 @@ "main": "lib/index.js",

"dependencies": {
"bn.js": "5.2.1",
"bs58": "4.0.0",
"depd": "2.0.0",
"mustache": "4.0.0",
"@near-js/types": "0.0.4"
"@near-js/types": "0.1.0"
},

@@ -17,0 +16,0 @@ "devDependencies": {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc