Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace bn.js with BigInt #1223

Merged
merged 11 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .changeset/eight-owls-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
"near-api-js": major
"@near-js/accounts": minor
"@near-js/transactions": minor
"@near-js/types": minor
"@near-js/utils": minor
"@near-js/biometric-ed25519": patch
"@near-js/cookbook": patch
"@near-js/crypto": patch
"@near-js/providers": patch
"@near-js/wallet-account": patch
---

Replace bn.js by BigInt.
1 change: 1 addition & 0 deletions .eslintrc.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ globals:
jasmine: true
window: false
fail: true
BigInt: true
85 changes: 42 additions & 43 deletions packages/accounts/package.json
Original file line number Diff line number Diff line change
@@ -1,45 +1,44 @@
{
"name": "@near-js/accounts",
"version": "1.0.4",
"description": "Classes encapsulating account-specific functionality",
"main": "lib/index.js",
"scripts": {
"build": "pnpm compile",
"compile": "tsc -p tsconfig.json",
"lint:js": "eslint -c ../../.eslintrc.js.yml test/**/*.js --no-eslintrc",
"lint:js:fix": "eslint -c ../../.eslintrc.js.yml test/**/*.js --no-eslintrc --fix",
"lint:ts": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts --no-eslintrc",
"lint:ts:fix": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts --no-eslintrc --fix",
"test": "jest test"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@near-js/crypto": "workspace:*",
"@near-js/providers": "workspace:*",
"@near-js/signers": "workspace:*",
"@near-js/transactions": "workspace:*",
"@near-js/types": "workspace:*",
"@near-js/utils": "workspace:*",
"bn.js": "5.2.1",
"borsh": "1.0.0",
"depd": "2.0.0",
"is-my-json-valid": "^2.20.6",
"lru_map": "0.4.1",
"near-abi": "0.1.1"
},
"devDependencies": {
"@near-js/keystores": "workspace:*",
"@types/node": "18.11.18",
"bs58": "4.0.0",
"jest": "26.0.1",
"near-hello": "0.5.1",
"near-workspaces": "3.4.0",
"ts-jest": "26.5.6",
"typescript": "4.9.4"
},
"files": [
"lib"
]
"name": "@near-js/accounts",
"version": "1.0.4",
"description": "Classes encapsulating account-specific functionality",
"main": "lib/index.js",
"scripts": {
"build": "pnpm compile",
"compile": "tsc -p tsconfig.json",
"lint:js": "eslint -c ../../.eslintrc.js.yml test/**/*.js --no-eslintrc",
"lint:js:fix": "eslint -c ../../.eslintrc.js.yml test/**/*.js --no-eslintrc --fix",
"lint:ts": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts --no-eslintrc",
"lint:ts:fix": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts --no-eslintrc --fix",
"test": "jest test"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@near-js/crypto": "workspace:*",
"@near-js/providers": "workspace:*",
"@near-js/signers": "workspace:*",
"@near-js/transactions": "workspace:*",
"@near-js/types": "workspace:*",
"@near-js/utils": "workspace:*",
"borsh": "1.0.0",
"depd": "2.0.0",
"is-my-json-valid": "^2.20.6",
"lru_map": "0.4.1",
"near-abi": "0.1.1"
},
"devDependencies": {
"@near-js/keystores": "workspace:*",
"@types/node": "18.11.18",
"bs58": "4.0.0",
"jest": "26.0.1",
"near-hello": "0.5.1",
"near-workspaces": "3.4.0",
"ts-jest": "26.5.6",
"typescript": "4.9.4"
},
"files": [
"lib"
]
}
49 changes: 24 additions & 25 deletions packages/accounts/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import {
printTxOutcomeLogs,
printTxOutcomeLogsAndFailures,
} from '@near-js/utils';
import BN from 'bn.js';

import { Connection } from './connection';

Expand Down Expand Up @@ -106,9 +105,9 @@ export interface FunctionCallOptions {
*/
args?: object;
/** max amount of gas that method call can use */
gas?: BN;
gas?: bigint;
/** amount of NEAR (in yoctoNEAR) to send together with the call */
attachedDeposit?: BN;
attachedDeposit?: bigint;
/**
* Convert input arguments into bytes array.
*/
Expand Down Expand Up @@ -145,7 +144,7 @@ interface StakedBalance {
interface ActiveDelegatedStakeBalance {
stakedValidators: StakedBalance[];
failedValidators: StakedBalance[];
total: BN | string;
total: bigint | string;
}

interface SignedDelegateOptions {
Expand Down Expand Up @@ -202,7 +201,7 @@ export class Account {
const block = await this.connection.provider.block({ finality: 'final' });
const blockHash = block.header.hash;

const nonce = accessKey.nonce.add(new BN(1));
const nonce = accessKey.nonce + BigInt(1);
return await signTransaction(
receiverId, nonce, actions, baseDecode(blockHash), this.connection.signer, this.accountId, this.connection.networkId
);
Expand Down Expand Up @@ -297,10 +296,10 @@ export class Account {
finality: 'optimistic'
});

// store nonce as BN to preserve precision on big number
// store nonce as BigInt to preserve precision on big number
const accessKey = {
...rawAccessKey,
nonce: new BN(rawAccessKey.nonce),
nonce: BigInt(rawAccessKey.nonce || 0)
gagdiez marked this conversation as resolved.
Show resolved Hide resolved
};
// this function can be called multiple times and retrieve the same access key
// this checks to see if the access key was already retrieved and cached while
Expand Down Expand Up @@ -329,7 +328,7 @@ export class Account {
* @param data The compiled contract code
* @param amount of NEAR to transfer to the created contract account. Transfer enough to pay for storage https://docs.near.org/docs/concepts/storage-staking
*/
async createAndDeployContract(contractId: string, publicKey: string | PublicKey, data: Uint8Array, amount: BN): Promise<Account> {
async createAndDeployContract(contractId: string, publicKey: string | PublicKey, data: Uint8Array, amount: bigint): Promise<Account> {
const accessKey = fullAccessKey();
await this.signAndSendTransaction({
receiverId: contractId,
Expand All @@ -343,7 +342,7 @@ export class Account {
* @param receiverId NEAR account receiving Ⓝ
* @param amount Amount to send in yoctoⓃ
*/
async sendMoney(receiverId: string, amount: BN): Promise<FinalExecutionOutcome> {
async sendMoney(receiverId: string, amount: bigint): Promise<FinalExecutionOutcome> {
return this.signAndSendTransaction({
receiverId,
actions: [transfer(amount)]
Expand All @@ -354,7 +353,7 @@ export class Account {
* @param newAccountId NEAR account name to be created
* @param publicKey A public key created from the masterAccount
*/
async createAccount(newAccountId: string, publicKey: string | PublicKey, amount: BN): Promise<FinalExecutionOutcome> {
async createAccount(newAccountId: string, publicKey: string | PublicKey, amount: bigint): Promise<FinalExecutionOutcome> {
const accessKey = fullAccessKey();
return this.signAndSendTransaction({
receiverId: newAccountId,
Expand Down Expand Up @@ -431,7 +430,7 @@ export class Account {
* @param methodNames The method names on the contract that should be allowed to be called. Pass null for no method names and '' or [] for any method names.
* @param amount Payment in yoctoⓃ that is sent to the contract during this function call
*/
async addKey(publicKey: string | PublicKey, contractId?: string, methodNames?: string | string[], amount?: BN): Promise<FinalExecutionOutcome> {
async addKey(publicKey: string | PublicKey, contractId?: string, methodNames?: string | string[], amount?: bigint): Promise<FinalExecutionOutcome> {
if (!methodNames) {
methodNames = [];
}
Expand Down Expand Up @@ -467,7 +466,7 @@ export class Account {
* @param publicKey The public key for the account that's staking
* @param amount The account to stake in yoctoⓃ
*/
async stake(publicKey: string | PublicKey, amount: BN): Promise<FinalExecutionOutcome> {
async stake(publicKey: string | PublicKey, amount: bigint): Promise<FinalExecutionOutcome> {
return this.signAndSendTransaction({
receiverId: this.accountId,
actions: [stake(amount, PublicKey.from(publicKey))]
Expand All @@ -493,8 +492,8 @@ export class Account {

const delegateAction = buildDelegateAction({
actions,
maxBlockHeight: new BN(header.height).add(new BN(blockHeightTtl)),
nonce: new BN(accessKey.nonce).add(new BN(1)),
maxBlockHeight: BigInt(header.height) + BigInt(blockHeightTtl),
nonce: BigInt(accessKey.nonce) + BigInt(1),
publicKey,
receiverId,
senderId: this.accountId,
Expand Down Expand Up @@ -611,8 +610,8 @@ export class Account {
account_id: this.accountId,
finality: 'optimistic'
});
// Replace raw nonce into a new BN
return response?.keys?.map((key) => ({ ...key, access_key: { ...key.access_key, nonce: new BN(key.access_key.nonce) } }));
// Replace raw nonce into a new BigInt
return response?.keys?.map((key) => ({ ...key, access_key: { ...key.access_key, nonce: BigInt(key.access_key.nonce) } }));
}

/**
Expand Down Expand Up @@ -643,11 +642,11 @@ export class Account {
const protocolConfig = await this.connection.provider.experimental_protocolConfig({ finality: 'final' });
const state = await this.state();

const costPerByte = new BN(protocolConfig.runtime_config.storage_amount_per_byte);
const stateStaked = new BN(state.storage_usage).mul(costPerByte);
const staked = new BN(state.locked);
const totalBalance = new BN(state.amount).add(staked);
const availableBalance = totalBalance.sub(BN.max(staked, stateStaked));
const costPerByte = BigInt(protocolConfig.runtime_config.storage_amount_per_byte);
const stateStaked = BigInt(state.storage_usage) * costPerByte;
const staked = BigInt(state.locked);
const totalBalance = BigInt(state.amount) + staked;
const availableBalance = totalBalance - (staked > stateStaked ? staked : stateStaked);

return {
total: totalBalance.toString(),
Expand Down Expand Up @@ -699,12 +698,12 @@ export class Account {
const summary = results.reduce((result, state, index) => {
const validatorId = uniquePools[index];
if (state.status === 'fulfilled') {
const currentBN = new BN(state.value);
if (!currentBN.isZero()) {
const currentBN = BigInt(state.value);
if (currentBN !== BigInt(0)) {
return {
...result,
stakedValidators: [...result.stakedValidators, { validatorId, amount: currentBN.toString() }],
total: result.total.add(currentBN),
total: result.total + currentBN,
};
}
}
Expand All @@ -716,7 +715,7 @@ export class Account {
}
return result;
},
{ stakedValidators: [], failedValidators: [], total: new BN(0) });
{ stakedValidators: [], failedValidators: [], total: BigInt(0) });

return {
...summary,
Expand Down
3 changes: 1 addition & 2 deletions packages/accounts/src/account_2fa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { FinalExecutionOutcome, TypedError, FunctionCallPermissionView } from '@
import { fetchJson } from '@near-js/providers';
import { actionCreators } from '@near-js/transactions';
import { Logger } from '@near-js/utils'
import BN from 'bn.js';

import { SignAndSendTransactionOptions } from './account';
import { AccountMultisig } from './account_multisig';
Expand Down Expand Up @@ -159,7 +158,7 @@ export class Account2FA extends AccountMultisig {
const currentAccountStateKeys = currentAccountState.map(({ key }) => key.toString('base64'));
return currentAccountState.length ? [
deployContract(cleanupContractBytes),
functionCall('clean', { keys: currentAccountStateKeys }, MULTISIG_GAS, new BN('0'))
functionCall('clean', { keys: currentAccountStateKeys }, MULTISIG_GAS, BigInt('0'))
] : [];
}

Expand Down
5 changes: 2 additions & 3 deletions packages/accounts/src/account_creator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { PublicKey } from '@near-js/crypto';
import { fetchJson } from '@near-js/providers';
import BN from 'bn.js';

import { Connection } from './connection';
import { Account } from './account';
Expand All @@ -14,9 +13,9 @@ export abstract class AccountCreator {

export class LocalAccountCreator extends AccountCreator {
readonly masterAccount: Account;
readonly initialBalance: BN;
readonly initialBalance: bigint;

constructor(masterAccount: Account, initialBalance: BN) {
constructor(masterAccount: Account, initialBalance: bigint) {
super();
this.masterAccount = masterAccount;
this.initialBalance = initialBalance;
Expand Down
7 changes: 3 additions & 4 deletions packages/accounts/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { parseNearAmount } from '@near-js/utils';
import BN from 'bn.js';

export const MULTISIG_STORAGE_KEY = '__multisigRequest';
export const MULTISIG_ALLOWANCE = new BN(parseNearAmount('1'));
export const MULTISIG_ALLOWANCE = BigInt(parseNearAmount('1'));
// TODO: Different gas value for different requests (can reduce gas usage dramatically)
export const MULTISIG_GAS = new BN('100000000000000');
export const MULTISIG_DEPOSIT = new BN('0');
export const MULTISIG_GAS = BigInt('100000000000000');
export const MULTISIG_DEPOSIT = BigInt('0');
export const MULTISIG_CHANGE_METHODS = ['add_request', 'add_request_and_confirm', 'delete_request', 'confirm'];
export const MULTISIG_CONFIRM_METHODS = ['confirm'];
Loading
Loading