near-api-js 7.0.0
#1691 f8666a0 Thanks @r-near! - Update parseNearAmount to only accept either a numeric string, or actual number, and not undefined, as well as throw error rather than silently return null; #1691 f8666a0 Thanks @r-near!…
- The newest stable release in the 7.0 line: near-api-js 7.0.4
- Newest near-api-js: near-api-js 7.4.0
Major Changes
#1691
f8666a0Thanks @r-near! - UpdateparseNearAmountto only accept either a numeric string, or actual number, and not undefined, as well as throw error rather than silently returnnull#1691
f8666a0Thanks @r-near! - Rename stableviewValidatorsV2function back toviewValidators#1809
eee87c2Thanks @gagdiez! - Consolidate functionality of the@near-js/*packages into a single, tree-shakeablenear-api-jspackage#1805
9356419Thanks @denbite! - Remove no longer needed error utils likegetErrorTypeFromErrorMessage,parseRpcError,parseResultError, etc from the package as errors are now handled internally thanks to strongly typed RPC errors and end users can simply catch them in their application using specific error classes imported fromrpc-errorssub-path#1781
1caddf4Thanks @denbite! - ReplaceTypedErrorclass with a few specific errors created from fully type-safe RPC interfaces#1808
bf7c148Thanks @denbite! - RenameTypedContractback toContract#1669
683e1dbThanks @denbite! - Remove no longer maintained packages@near-js/iframe-rpc,@near-js/biometric-ed25519and@near-js/clientwith@near-js/cookbookexamples#1670
54e4d48Thanks @denbite! - Remove no longer maintained packages@near-js/keystores,@near-js/keystores-nodeand@near-js/keystores-browser#1673
5d86344Thanks @denbite! - Consolidate@near-js/*packages into a singlenear-api-jscodebase#1795
b96c604Thanks @denbite! - RenovateProvider.queryby removing the deprecated inline-argument overload and fully aligning the method with the nearcore RPC API spec.Previously supported (no longer works):
provider.query("view_account", JSON.stringify({ ... }));New required usage (fully typed):
provider.query({ request_type: "view_account", ... });Once
request_typeis specified, remaining parameters are inferred automatically by the IDE.#1668
20672fbThanks @denbite! - Remove deprecated functionality#1813
858783cThanks @gagdiez! - Renamed actionCreator helper object to simply "actions", and separated the helper addKey into addFullAccessKey and addFunctionCallKey#1589
5cfdab8Thanks @AlexKushnir1! - Refactor the abstract classSignerto implement every method, except forgetPublicKeyandsignBytes, which are intended for users
Minor Changes
#1790
2f7a30dThanks @denbite! - Introduce many specific errors based on generated response types from OpenAPI spec and which are thrown when callingProvider.sendJsonRpcmethod#1798
a52592bThanks @gagdiez! - Added a new multi-key signer which handles multiples keys and transparently rotates them as users ask to sign transactions#1751
85f279eThanks @denbite! - Addnep413module that implementssignMessageandverifyMessagefunctions#1748
917cceaThanks @denbite! - MakeAccountto takeKeyPairStringas third parameter#1761
9d8799bThanks @denbite! - IntroduceteraToGasandgigaToGashelper functions to convertGasamounts conviniently#1761
fcbb1feThanks @denbite! - IntroduceyoctoToNearandnearToYoctohelper functions#1796
c2601e2Thanks @denbite! - Add a newProvider.viewGlobalContractCodemethod for retrieving global contract code using either acodeHash, or anaccountIdidentifier.Example usage:
provider.viewGlobalContractCode({ identifier: { accountId: "global_contract.near" }, }); provider.viewGlobalContractCode({ identifier: { codeHash: "J1arLz48fgXcGyCPVckFwLnewNH6j1uw79thsvwqGYTY" }, });#1799
a7c3cd9Thanks @denbite! - AddparseSeedPhrasemethod, exported via theseed-phrasesub-path, which converts a string seed phrase into aKeyPair.For example:
import { parseSeedPhrase } from "near-api-js/seed-phrase"; const keyPair = parseSeedPhrase("tag interest match ..."); keyPair.getPublicKey().toString(); // ed25519:9uMmkWHW... keyPair.toString(); // ed25519:3N6TYZVRrkQxh...#1747
493d9e5Thanks @denbite! - Add sub-path exports for "tokens", "tokens/mainnet" and "tokens/testnet" to improve tree-shaking#1794
7b6ff4fThanks @denbite! - Extend theAccountconstructor to accept an RPC URL string in addition to aProvider, making account instantiation simpler and more ergonomic.New supported usage:
new Account("user.near", "https://rpc.testnet.near.org");Previously, users had to always manually create a provider:
const provider = new JsonRpcProvider({ url: "https://rpc.testnet.near.org" }); new Account("user.near", provider);#1803
9f3e14dThanks @denbite! - Export strongly typed RPC error classes behind therpc-errorssub-path, for end users to reliably handle specific RPC failures.For example, end users can now catch action-level errors (e.g. when a transaction fails because the recipient account does not exist):
import { AccountDoesNotExistActionError } from "near-api-js/rpc-errors"; try { await account.transfer({ amount: nearToYocto(0.01), receiverId: "unexisted_account_111.testnet", }); } catch (error) { if (error instanceof AccountDoesNotExistActionError) { console.error( `Transaction ${error.txHash} failed because recipient ${error.accountId} does not exist!` ); } }Or, RPC request validation and parsing errors can also be handled explicitly (e.g. when an invalid account ID format is included in a transaction):
import { RpcRequestParseError } from "near-api-js/rpc-errors"; try { await account.transfer({ amount: nearToYocto(0.001), receiverId: "account_name_that_fail_validation%.testnet", }); } catch (error) { if (error instanceof RpcRequestParseError) { // Failed parsing args: the value could not be decoded from borsh due to: // invalid value: "account_name_that_fail_validation%.testnet", // the Account ID contains an invalid character '%' at index 33 console.error(error.message); } }Or, some requests may reference data that is no longer available or cannot be resolved by the node (e.g. querying a block that cannot be found):
import { UnknownBlockError } from "near-api-js/rpc-errors"; const unexistedBlockHeight = 1_000_000_000_000_000; try { await provider.viewBlock({ blockId: unexistedBlockHeight }); } catch (error) { if (error instanceof UnknownBlockError) { console.error( `Block at height ${unexistedBlockHeight} could not be found on the node!` ); } }All exported errors ultimately extend the
RpcErrorbase class, so applications can also catch and handle any RPC-related error in a single place when fine-grained handling is not required:import { RpcError } from "near-api-js/rpc-errors"; try { await provider.viewAccessKey({ accountId: "user.testnet", publicKey: "ed25519:EaQnZxCMwh9yhkqW2XE2umd21iNmXep1BkM6Wtw2Qr1b", }); } catch (error) { if (error instanceof RpcError) { // Access key ed25519:EaQnZx... does not exist at block height ... console.error(error.message); } } try { await provider.viewAccessKey({ accountId: "user.testnet", publicKey: "ed25519:EaQnZxCMwh9yhkqW2XE2umd21iNmXep1BkM6Wtw2Qr1bxxxx", // invalid key here }); } catch (error) { if (error instanceof RpcError) { // Failed parsing args: invalid key length: expected the input of 32 bytes, but 33 was given console.error(error.message); } }#1754
b3b2947Thanks @gagdiez! - Added optional signer argument to signing functions on account class#1793
ddceeabThanks @denbite! - Add concurrent transactions support by introducing nonce caching and automatic retries onInvalidNonceError.The following example that previously failed due to nonce collisions, now works reliably:
await Promise.all([ account.transfer({ amount: nearToYocto(1), receiverId: "user1.testnet", }), account.transfer({ amount: nearToYocto(2), receiverId: "user2.testnet", }), account.transfer({ amount: nearToYocto(3), receiverId: "user3.testnet", }), ]);
Patch Changes
#1752
e6c61aeThanks @denbite! - Remove redundantnullargument fromProvider.viewValidatorsmethod#1691
f8666a0Thanks @r-near! - Add defaultEXECUTED_OPTIMISTICvalue towaitUntilargument ofviewTransactionStatusandviewTransactionStatusWithReceipts#1692
4ecebdeThanks @r-near! - feat: replace ESLint with Biome for linting#1762
c711103Thanks @denbite! - Speed up someAccountmethods by merging sequentialPromisesintoPromise.all()for slightly better performance#1693
26ed0cdThanks @denbite! - Enable TypeScriptstrictmode for better reliability#1774
741671bThanks @denbite! - ExtendProviderinterfaces using types generated fromnearcoreOpenAPI spec#1806
7d73ef1Thanks @denbite! - Handle HTTP 502 and 429 errors#1743
a1ea9aaThanks @denbite! - Switch to object args for public APIs across the package#1801
3e045feThanks @gagdiez! - Replaced some dependencies by internal functions#1720
1a41e6cThanks @denbite! - Use the most strictest checks intsconfig