near-api-js 7.0.0-rc.0 → 7.0.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -40,15 +40,17 @@ export class Account {
40
40
  * balance, storage usage, and code hash
41
41
  */
42
42
  async getState() {
43
- const protocolConfig = await this.provider.experimental_protocolConfig({
44
- finality: DEFAULT_FINALITY,
45
- });
46
- const state = await this.provider.viewAccount({
47
- accountId: this.accountId,
48
- blockQuery: {
43
+ const [protocolConfig, state] = await Promise.all([
44
+ this.provider.experimental_protocolConfig({
49
45
  finality: DEFAULT_FINALITY,
50
- },
51
- });
46
+ }),
47
+ this.provider.viewAccount({
48
+ accountId: this.accountId,
49
+ blockQuery: {
50
+ finality: DEFAULT_FINALITY,
51
+ },
52
+ }),
53
+ ]);
52
54
  const costPerByte = BigInt(protocolConfig.runtime_config.storage_amount_per_byte);
53
55
  const usedOnStorage = BigInt(state.storage_usage) * costPerByte;
54
56
  const locked = BigInt(state.locked);
@@ -125,10 +127,12 @@ export class Account {
125
127
  if (!publicKey)
126
128
  throw new Error('Please provide a public key');
127
129
  const pk = PublicKey.from(publicKey);
128
- const accessKey = await this.getAccessKey(pk);
129
- const block = await this.provider.viewBlock({
130
- finality: DEFAULT_FINALITY,
131
- });
130
+ const [accessKey, block] = await Promise.all([
131
+ this.getAccessKey(pk),
132
+ this.provider.viewBlock({
133
+ finality: DEFAULT_FINALITY,
134
+ }),
135
+ ]);
132
136
  const recentBlockHash = block.header.hash;
133
137
  const nonce = BigInt(accessKey.nonce) + 1n;
134
138
  return createTransaction(this.accountId, pk, receiverId, nonce + 1n, actions, baseDecode(recentBlockHash));
@@ -158,12 +162,14 @@ export class Account {
158
162
  if (!publicKey)
159
163
  throw new Error('Please provide a public key');
160
164
  const pk = PublicKey.from(publicKey);
161
- const accessKey = await this.getAccessKey(pk);
165
+ const [accessKey, block] = await Promise.all([
166
+ this.getAccessKey(pk),
167
+ this.provider.viewBlock({
168
+ finality: DEFAULT_FINALITY,
169
+ }),
170
+ ]);
162
171
  const nonce = BigInt(accessKey.nonce) + 1n;
163
- const { header } = await this.provider.viewBlock({
164
- finality: DEFAULT_FINALITY,
165
- });
166
- const maxBlockHeight = BigInt(header.height) + BigInt(blockHeightTtl);
172
+ const maxBlockHeight = BigInt(block.header.height) + BigInt(blockHeightTtl);
167
173
  return buildDelegateAction({
168
174
  receiverId,
169
175
  senderId: this.accountId,
@@ -1,5 +1,5 @@
1
- import { ed25519 } from '@noble/curves/ed25519';
2
- import { randomBytes } from '@noble/hashes/utils';
1
+ import { ed25519 } from '@noble/curves/ed25519.js';
2
+ import { randomBytes } from '@noble/hashes/utils.js';
3
3
  import { baseDecode, baseEncode } from '../utils/index.js';
4
4
  import { KeySize, KeyType } from './constants.js';
5
5
  import { KeyPairBase } from './key_pair_base.js';
@@ -1,4 +1,4 @@
1
- import { randomBytes } from '@noble/hashes/utils';
1
+ import { randomBytes } from '@noble/hashes/utils.js';
2
2
  import secp256k1 from 'secp256k1';
3
3
  import { baseDecode, baseEncode } from '../utils/index.js';
4
4
  import { KeySize, KeyType } from './constants.js';
@@ -1,4 +1,4 @@
1
- import { ed25519 } from '@noble/curves/ed25519';
1
+ import { ed25519 } from '@noble/curves/ed25519.js';
2
2
  import secp256k1 from 'secp256k1';
3
3
  import { baseDecode, baseEncode } from '../utils/index.js';
4
4
  import { KeySize, KeyType } from './constants.js';
package/lib/index.d.ts CHANGED
@@ -5,4 +5,5 @@ export * from './providers/index.js';
5
5
  export * from './signers/index.js';
6
6
  export * from './transactions/index.js';
7
7
  export * from './types/index.js';
8
+ export { gigaToGas, nearToYocto, teraToGas, yoctoToNear } from './units/index.js';
8
9
  export * from './utils/index.js';
package/lib/index.js CHANGED
@@ -22,6 +22,7 @@ export * from './signers/index.js';
22
22
  // ============================================================================
23
23
  export * from './transactions/index.js';
24
24
  export * from './types/index.js';
25
+ export { gigaToGas, nearToYocto, teraToGas, yoctoToNear } from './units/index.js';
25
26
  // ============================================================================
26
27
  // Utilities
27
28
  // ============================================================================
@@ -1,4 +1,4 @@
1
- import { sha256 } from '@noble/hashes/sha256';
1
+ import { sha256 } from '@noble/hashes/sha2.js';
2
2
  import { serialize } from 'borsh';
3
3
  export const Nep413MessageSchema = {
4
4
  struct: {
@@ -1,4 +1,4 @@
1
- import { sha256 } from '@noble/hashes/sha256';
1
+ import { sha256 } from '@noble/hashes/sha2.js';
2
2
  import { KeyType } from '../crypto/index.js';
3
3
  import { serializeMessage } from '../nep413/schema.js';
4
4
  import { encodeDelegateAction, encodeTransaction, Signature, SignedDelegate, SignedTransaction, } from '../transactions/index.js';
@@ -1,4 +1,3 @@
1
- /// <reference types="node" resolution-mode="require"/>
2
1
  import type { PublicKey } from '../crypto/index.js';
3
2
  import { AccessKey, Action, type GlobalContractDeployMode, type GlobalContractIdentifier } from './actions.js';
4
3
  import type { DelegateAction } from './delegate.js';
@@ -9,18 +9,18 @@ import type { Signature } from './signature.js';
9
9
  * signed delegate actions may never be identical to signed transactions with the same fields
10
10
  * @param delegateAction Delegate action to be signed by the meta transaction sender
11
11
  */
12
- export declare function encodeDelegateAction(delegateAction: DelegateAction): Uint8Array;
12
+ export declare function encodeDelegateAction(delegateAction: DelegateAction): Uint8Array<ArrayBuffer>;
13
13
  /**
14
14
  * Borsh-encode a signed delegate for validation and execution by a relayer
15
15
  * @param signedDelegate Signed delegate to be executed in a meta transaction
16
16
  */
17
- export declare function encodeSignedDelegate(signedDelegate: SignedDelegate): Uint8Array;
17
+ export declare function encodeSignedDelegate(signedDelegate: SignedDelegate): Uint8Array<ArrayBufferLike>;
18
18
  /**
19
19
  * Borsh-encode a transaction or signed transaction into a serialized form.
20
20
  * @param transaction The transaction or signed transaction object to be encoded.
21
21
  * @returns A serialized representation of the input transaction.
22
22
  */
23
- export declare function encodeTransaction(transaction: Transaction | SignedTransaction): Uint8Array;
23
+ export declare function encodeTransaction(transaction: Transaction | SignedTransaction): Uint8Array<ArrayBufferLike>;
24
24
  /**
25
25
  * Borsh-decode a Transaction instance from a buffer
26
26
  * @param bytes Uint8Array data to be decoded
@@ -0,0 +1,3 @@
1
+ import type { NumericString } from './types.js';
2
+ export declare function gigaToGas(giga: NumericString | number): bigint;
3
+ export declare function teraToGas(tera: NumericString | number): bigint;
@@ -0,0 +1,9 @@
1
+ import { toUnits } from './utils.js';
2
+ const GIGA_NOMINATION_EXP = 9;
3
+ const TERA_NOMINATION_EXP = 12;
4
+ export function gigaToGas(giga) {
5
+ return toUnits(giga, GIGA_NOMINATION_EXP);
6
+ }
7
+ export function teraToGas(tera) {
8
+ return toUnits(tera, TERA_NOMINATION_EXP);
9
+ }
@@ -0,0 +1,2 @@
1
+ export { gigaToGas, teraToGas } from './gas.js';
2
+ export { nearToYocto, yoctoToNear } from './near.js';
@@ -0,0 +1,2 @@
1
+ export { gigaToGas, teraToGas } from './gas.js';
2
+ export { nearToYocto, yoctoToNear } from './near.js';
@@ -0,0 +1,3 @@
1
+ import type { BigintString, NumericString } from './types.js';
2
+ export declare function nearToYocto(near: number | NumericString): bigint;
3
+ export declare function yoctoToNear(yocto: bigint | BigintString, decimals?: number): NumericString;
@@ -0,0 +1,22 @@
1
+ import { toUnits } from './utils.js';
2
+ const NEAR_NOMINATION_EXP = 24;
3
+ export function nearToYocto(near) {
4
+ return toUnits(near, NEAR_NOMINATION_EXP);
5
+ }
6
+ export function yoctoToNear(yocto, decimals = NEAR_NOMINATION_EXP) {
7
+ decimals = Math.floor(decimals);
8
+ if (decimals < 0 || decimals > NEAR_NOMINATION_EXP) {
9
+ throw new Error(`Decimal places argument must be between 0 and ${NEAR_NOMINATION_EXP}`);
10
+ }
11
+ // check if bigint is constructible
12
+ yocto = `${BigInt(yocto)}`;
13
+ const wholeStr = yocto.substring(0, yocto.length - NEAR_NOMINATION_EXP) || '0';
14
+ const fractionStr = yocto
15
+ .substring(yocto.length - NEAR_NOMINATION_EXP)
16
+ .padStart(NEAR_NOMINATION_EXP, '0')
17
+ .substring(0, decimals);
18
+ return trimTrailingZeroes(`${wholeStr}.${fractionStr}`);
19
+ }
20
+ function trimTrailingZeroes(value) {
21
+ return value.replace(/\.?0*$/, '');
22
+ }
@@ -0,0 +1,2 @@
1
+ export type NumericString = `${number}`;
2
+ export type BigintString = `${bigint}`;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ import type { NumericString } from './types.js';
2
+ export declare function toUnits(amount: NumericString | number, exponent: number): bigint;
@@ -0,0 +1,23 @@
1
+ export function toUnits(amount, exponent) {
2
+ const cleanedAmount = cleanupAmount(`${amount}`);
3
+ if (!cleanedAmount) {
4
+ throw new Error('Amount cannot be empty');
5
+ }
6
+ const split = cleanedAmount.split('.');
7
+ const wholePart = split[0];
8
+ const fracPart = split[1] || '';
9
+ if (split.length > 2 || fracPart.length > exponent) {
10
+ throw new Error(`Cannot parse '${amount}'`);
11
+ }
12
+ return BigInt(trimLeadingZeroes(wholePart + fracPart.padEnd(exponent, '0')));
13
+ }
14
+ function cleanupAmount(amount) {
15
+ return amount.replace(/,/g, '').trim();
16
+ }
17
+ function trimLeadingZeroes(value) {
18
+ value = value.replace(/^0+/, '');
19
+ if (value === '') {
20
+ return '0';
21
+ }
22
+ return value;
23
+ }
@@ -8,6 +8,8 @@ export declare const NEAR_NOMINATION_EXP = 24;
8
8
  */
9
9
  export declare const NEAR_NOMINATION: bigint;
10
10
  /**
11
+ * @deprecated use {@link yoctoToNear} instead.
12
+ *
11
13
  * Convert account balance value from internal indivisible units to NEAR. 1 NEAR is defined by {@link NEAR_NOMINATION}.
12
14
  * Effectively this divides given amount by {@link NEAR_NOMINATION}.
13
15
  *
@@ -17,6 +19,8 @@ export declare const NEAR_NOMINATION: bigint;
17
19
  */
18
20
  export declare function formatNearAmount(balance: string | number | bigint, fracDigits?: number): string;
19
21
  /**
22
+ * @deprecated use {@link nearToYocto} instead.
23
+ *
20
24
  * Convert human readable NEAR amount to internal indivisible units.
21
25
  * Effectively this multiplies given amount by {@link NEAR_NOMINATION}.
22
26
  *
@@ -14,6 +14,8 @@ for (let i = 0, offset = 5n; i < NEAR_NOMINATION_EXP; i++, offset = offset * BN1
14
14
  ROUNDING_OFFSETS[i] = offset;
15
15
  }
16
16
  /**
17
+ * @deprecated use {@link yoctoToNear} instead.
18
+ *
17
19
  * Convert account balance value from internal indivisible units to NEAR. 1 NEAR is defined by {@link NEAR_NOMINATION}.
18
20
  * Effectively this divides given amount by {@link NEAR_NOMINATION}.
19
21
  *
@@ -39,6 +41,8 @@ export function formatNearAmount(balance, fracDigits = NEAR_NOMINATION_EXP) {
39
41
  return trimTrailingZeroes(`${formatWithCommas(wholeStr)}.${fractionStr}`);
40
42
  }
41
43
  /**
44
+ * @deprecated use {@link nearToYocto} instead.
45
+ *
42
46
  * Convert human readable NEAR amount to internal indivisible units.
43
47
  * Effectively this multiplies given amount by {@link NEAR_NOMINATION}.
44
48
  *
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "near-api-js",
3
3
  "description": "JavaScript library to interact with NEAR Protocol via RPC API",
4
- "version": "7.0.0-rc.0",
4
+ "version": "7.0.0-rc.1",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/near/near-api-js.git"
@@ -35,36 +35,36 @@
35
35
  }
36
36
  },
37
37
  "dependencies": {
38
- "@noble/curves": "1.8.1",
39
- "@noble/hashes": "1.7.1",
40
- "@scure/base": "1.2.4",
41
- "borsh": "1.0.0",
38
+ "@noble/curves": "2.0.1",
39
+ "@noble/hashes": "2.0.1",
40
+ "@scure/base": "2.0.0",
41
+ "borsh": "2.0.0",
42
42
  "depd": "2.0.0",
43
- "exponential-backoff": "3.1.2",
43
+ "exponential-backoff": "3.1.3",
44
44
  "is-my-json-valid": "2.20.6",
45
- "mustache": "4.0.0",
45
+ "mustache": "4.2.0",
46
46
  "secp256k1": "5.0.1"
47
47
  },
48
48
  "devDependencies": {
49
49
  "@arethetypeswrong/cli": "0.18.2",
50
- "@biomejs/biome": "^2.3.6",
51
- "@changesets/changelog-github": "0.5.1",
52
- "@changesets/cli": "2.29.7",
53
- "@commitlint/cli": "19.3.0",
54
- "@commitlint/config-conventional": "19.7.1",
55
- "@types/node": "20.0.0",
56
- "@vitest/coverage-v8": "4.0.10",
57
- "@vitest/ui": "4.0.10",
58
- "commitlint": "19.7.1",
50
+ "@biomejs/biome": "^2.3.8",
51
+ "@changesets/changelog-github": "0.5.2",
52
+ "@changesets/cli": "2.29.8",
53
+ "@commitlint/cli": "20.2.0",
54
+ "@commitlint/config-conventional": "20.2.0",
55
+ "@types/node": "24.10.2",
56
+ "@vitest/coverage-v8": "4.0.15",
57
+ "@vitest/ui": "4.0.15",
58
+ "commitlint": "20.2.0",
59
59
  "husky": "9.1.7",
60
60
  "near-hello": "0.5.1",
61
61
  "near-workspaces": "5.0.0",
62
- "rimraf": "6.0.1",
63
- "semver": "7.7.1",
62
+ "rimraf": "6.1.2",
63
+ "semver": "7.7.3",
64
64
  "ts-node": "10.9.2",
65
- "typedoc": "0.27.9",
66
- "typescript": "5.4.5",
67
- "vitest": "4.0.10"
65
+ "typedoc": "0.28.15",
66
+ "typescript": "5.9.3",
67
+ "vitest": "4.0.15"
68
68
  },
69
69
  "keywords": [],
70
70
  "license": "(MIT AND Apache-2.0)",