Skip to content

Commit

Permalink
refactor: bigint literals
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-haynes committed Jun 21, 2024
1 parent 25402be commit 232d940
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 40 deletions.
8 changes: 4 additions & 4 deletions packages/accounts/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export class Account implements IntoConnection {
const block = await this.connection.provider.block({ finality: 'final' });
const blockHash = block.header.hash;

const nonce = accessKey.nonce + BigInt(1);
const nonce = accessKey.nonce + 1n;
return await signTransaction(
receiverId, nonce, actions, baseDecode(blockHash), this.connection.signer, this.accountId, this.connection.networkId
);
Expand Down Expand Up @@ -444,7 +444,7 @@ export class Account implements IntoConnection {
const delegateAction = buildDelegateAction({
actions,
maxBlockHeight: BigInt(header.height) + BigInt(blockHeightTtl),
nonce: BigInt(accessKey.nonce) + BigInt(1),
nonce: BigInt(accessKey.nonce) + 1n,
publicKey,
receiverId,
senderId: this.accountId,
Expand Down Expand Up @@ -610,7 +610,7 @@ export class Account implements IntoConnection {
const validatorId = uniquePools[index];
if (state.status === 'fulfilled') {
const currentBN = BigInt(state.value);
if (currentBN !== BigInt(0)) {
if (currentBN !== 0n) {
return {
...result,
stakedValidators: [...result.stakedValidators, { validatorId, amount: currentBN.toString() }],
Expand All @@ -626,7 +626,7 @@ export class Account implements IntoConnection {
}
return result;
},
{ stakedValidators: [], failedValidators: [], total: BigInt(0) });
{ stakedValidators: [], failedValidators: [], total: 0n });

return {
...summary,
Expand Down
2 changes: 1 addition & 1 deletion packages/accounts/src/account_2fa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,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, BigInt('0'))
functionCall('clean', { keys: currentAccountStateKeys }, MULTISIG_GAS, 0n)
] : [];
}

Expand Down
4 changes: 2 additions & 2 deletions packages/accounts/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { parseNearAmount } from '@near-js/utils';
export const MULTISIG_STORAGE_KEY = '__multisigRequest';
export const MULTISIG_ALLOWANCE = BigInt(parseNearAmount('1'));
// TODO: Different gas value for different requests (can reduce gas usage dramatically)
export const MULTISIG_GAS = BigInt('100000000000000');
export const MULTISIG_DEPOSIT = BigInt('0');
export const MULTISIG_GAS = 100000000000000n;
export const MULTISIG_DEPOSIT = 0n;
export const MULTISIG_CHANGE_METHODS = ['add_request', 'add_request_and_confirm', 'delete_request', 'confirm'];
export const MULTISIG_CONFIRM_METHODS = ['confirm'];
8 changes: 4 additions & 4 deletions packages/accounts/src/local-view-execution/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,21 +298,21 @@ export class Runtime {
const result = this.storageRead(key_len, key_ptr);

if (result == null) {
return BigInt(0);
return 0n;
}

this.registers[register_id] = result;
return BigInt(1);
return 1n;
}

private hasStorageKey (key_len: bigint, key_ptr: bigint): bigint {
const result = this.storageRead(key_len, key_ptr);

if (result == null) {
return BigInt(0);
return 0n;
}

return BigInt(1);
return 1n;
}

private getHostImports() {
Expand Down
4 changes: 2 additions & 2 deletions packages/accounts/test/providers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('providers', () => {
test('txStatus with string hash and buffer hash', async () => {
const sender = await createAccount(near);
const receiver = await createAccount(near);
const outcome = await sender.sendMoney(receiver.accountId, BigInt('1'));
const outcome = await sender.sendMoney(receiver.accountId, 1n);
const responseWithString = await provider.txStatus(outcome.transaction.hash, sender.accountId);
const responseWithUint8Array = await provider.txStatus(base58.decode(outcome.transaction.hash), sender.accountId);
expect(responseWithString).toMatchObject(outcome);
Expand All @@ -29,7 +29,7 @@ describe('providers', () => {
test('txStatusReciept with string hash and buffer hash', async () => {
const sender = await createAccount(near);
const receiver = await createAccount(near);
const outcome = await sender.sendMoney(receiver.accountId, BigInt('1'));
const outcome = await sender.sendMoney(receiver.accountId, 1n);
const reciepts = await provider.sendJsonRpc('EXPERIMENTAL_tx_status', [outcome.transaction.hash, sender.accountId]);

const responseWithString = await provider.txStatusReceipts(outcome.transaction.hash, sender.accountId);
Expand Down
8 changes: 4 additions & 4 deletions packages/transactions/src/action_creators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ export function stringifyJsonOrBytes(args: any): Buffer {
function functionCall(
methodName: string,
args: Uint8Array | object,
gas = BigInt(0),
deposit = BigInt(0),
gas = 0n,
deposit = 0n,
stringify = stringifyJsonOrBytes,
jsContract = false
): Action {
Expand All @@ -124,7 +124,7 @@ function functionCall(
* @param deposit The amount to be deposited along with the transfer. Default: 0.
* @returns A new action for transferring funds.
*/
function transfer(deposit = BigInt(0)): Action {
function transfer(deposit = 0n): Action {
return new Action({ transfer: new Transfer({ deposit }) });
}

Expand All @@ -134,7 +134,7 @@ function transfer(deposit = BigInt(0)): Action {
* @param publicKey The public key associated with the staking action.
* @returns A new action for staking tokens.
*/
function stake(stake = BigInt(0), publicKey: PublicKey): Action {
function stake(stake = 0n, publicKey: PublicKey): Action {
return new Action({ stake: new Stake({ stake, publicKey }) });
}

Expand Down
2 changes: 1 addition & 1 deletion packages/transactions/test/serialize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ describe('serialize and deserialize on different types of nonce', () => {
'test.near',
PublicKey.fromString('Anu7LYDfpLtkP7E16LT9imXF694BdQaa9ufVkQiwTQxC'),
'whatever.near',
BigInt(1),
1n,
actions,
blockHash);
const serialized = encodeTransaction(transaction);
Expand Down
12 changes: 6 additions & 6 deletions packages/transactions/test/transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ const { functionCall } = actionCreators;

test('functionCall with already serialized args', () => {
const serializedArgs = Buffer.from('{}');
const action = functionCall('methodName', serializedArgs, BigInt(1), BigInt(2));
const action = functionCall('methodName', serializedArgs, 1n, 2n);
expect(action).toMatchObject({
functionCall: {
methodName: 'methodName',
args: serializedArgs,
gas: BigInt(1),
deposit: BigInt(2)
gas: 1n,
deposit: 2n
}
});
});

test('functionCall with non-serialized args', () => {
const serializedArgs = Buffer.from('{}');
const action = functionCall('methodName', {}, BigInt(1), BigInt(2));
const action = functionCall('methodName', {}, 1n, 2n);
expect(action).toMatchObject({
functionCall: {
methodName: 'methodName',
args: serializedArgs,
gas: BigInt(1),
deposit: BigInt(2)
gas: 1n,
deposit: 2n
}
});
});
2 changes: 1 addition & 1 deletion packages/utils/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
// Due to protocol changes that charge upfront for the maximum possible gas price inflation due to
// full blocks, the price of max_prepaid_gas is decreased to `300 * 10**12`.
// For discussion see https://github.com/nearprotocol/NEPs/issues/67
export const DEFAULT_FUNCTION_CALL_GAS = BigInt('30000000000000');
export const DEFAULT_FUNCTION_CALL_GAS = 30000000000000n;
6 changes: 3 additions & 3 deletions packages/utils/src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ export const NEAR_NOMINATION_EXP = 24;
/**
* Number of indivisible units in one NEAR. Derived from {@link NEAR_NOMINATION_EXP}.
*/
export const NEAR_NOMINATION = BigInt(10) ** BigInt(NEAR_NOMINATION_EXP);
export const NEAR_NOMINATION = 10n ** BigInt(NEAR_NOMINATION_EXP);

// Pre-calculate offsets used for rounding to different number of digits
const ROUNDING_OFFSETS: bigint[] = [];
const BN10 = BigInt(10);
const BN10 = 10n;
for (
let i = 0, offset = BigInt(5);
let i = 0, offset = 5n;
i < NEAR_NOMINATION_EXP;
i++, offset = offset * BN10
) {
Expand Down
10 changes: 5 additions & 5 deletions packages/utils/src/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ function findSeatPriceForProtocolBefore49(validators: (CurrentEpochValidatorInfo
throw new Error('Stakes are below seats');
}
// assert stakesSum >= numSeats
let left = BigInt(1), right = stakesSum + BigInt(1);
while (left !== right - BigInt(1)) {
const mid = (left + right) / BigInt(2);
let left = 1n, right = stakesSum + 1n;
while (left !== right - 1n) {
const mid = (left + right) / 2n;
let found = false;
let currentSum = BigInt(0);
let currentSum = 0n;
for (let i = 0; i < stakes.length; ++i) {
currentSum = currentSum + (stakes[i] / mid);
if (currentSum >= num) {
Expand All @@ -59,7 +59,7 @@ function findSeatPriceForProtocolAfter49(validators: (CurrentEpochValidatorInfo
if (validators.length < maxNumberOfSeats) {
return stakesSum * BigInt(minimumStakeRatio[0]) / BigInt(minimumStakeRatio[1]);
} else {
return stakes[0] + BigInt(1);
return stakes[0] + 1n;
}
}

Expand Down
10 changes: 5 additions & 5 deletions packages/utils/test/validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ test('find seat price', async () => {
expect(findSeatPrice(
// @ts-expect-error test input
[{ stake: '1000000' }, { stake: '1000000' }, { stake: '100' }], 2, [1, 6250], 49
)).toEqual(BigInt('101'));
)).toEqual(101n);
expect(findSeatPrice(
// @ts-expect-error test input
[{ stake: '1000000' }, { stake: '1000000' }, { stake: '100' }], 3, [1, 6250]
)).toEqual(BigInt('101'));
)).toEqual(101n);
expect(findSeatPrice(
// @ts-expect-error test input
[{ stake: '1000000' }, { stake: '1000000' }, { stake: '100' }], 4, [1, 6250], 49
)).toEqual(BigInt('320'));
)).toEqual(320n);
expect(findSeatPrice(
// @ts-expect-error test input
[{ stake: '1000000' }, { stake: '1000000' }, { stake: '100' }], 4, [1, 6250], 48
)).toEqual(BigInt('500000'));
)).toEqual(500000n);
expect(findSeatPrice(
// @ts-expect-error test input
[{ stake: '1000' }, { stake: '1000' }, { stake: '200' }], 100, [1, 25]
)).toEqual(BigInt('88'));
)).toEqual(88n);
});

test('diff validators', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/wallet-account/src/near.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class Near {
if (config.masterAccount) {
// TODO: figure out better way of specifiying initial balance.
// Hardcoded number below must be enough to pay the gas cost to dev-deploy with near-shell for multiple times
const initialBalance = config.initialBalance ? BigInt(config.initialBalance) : BigInt('500000000000000000000000000');
const initialBalance = config.initialBalance ? BigInt(config.initialBalance) : 500000000000000000000000000n;
this.accountCreator = new LocalAccountCreator(new Account(this.connection, config.masterAccount), initialBalance);
} else if (config.helperUrl) {
this.accountCreator = new UrlAccountCreator(this.connection, config.helperUrl);
Expand Down
2 changes: 1 addition & 1 deletion packages/wallet-account/src/wallet_account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ export class ConnectedWalletAccount extends Account {

const publicKey = PublicKey.from(accessKey.public_key);
// TODO: Cache & listen for nonce updates for given access key
const nonce = accessKey.access_key.nonce + BigInt(1);
const nonce = accessKey.access_key.nonce + 1n;
const transaction = createTransaction(this.accountId, publicKey, receiverId, nonce, actions, blockHash);
await this.walletConnection.requestSignTransactions({
transactions: [transaction],
Expand Down

0 comments on commit 232d940

Please sign in to comment.