starknet.js Documentation
website·Indexed 29 days ago
https://starknet-js.comJavaScript and TypeScript library for interacting with the Starknet network. The documentation provides API references for versions 7.6.4, 10.8.0, and 11.x, including guides on connecting to smart contracts, handling Cairo data types, managing L1 to L2 communication, and implementing WalletAccount architecture for transaction signing.
What's inside starknet.js
- Starknet.js is the official JavaScript/TypeScript library for interacting with the Starknet network. It provides a comprehensive suite of tools for building decentralized applications (dApps), offering full TypeScript support, smart contract type generation, and optimized performance for Starknet's high throughput.
Overview of RPC.RPCSPEC09 Namespace
TheRPC.RPCSPEC09namespace provides the primary interface for interacting with Starknet RPC services. It organizes the API into three main sub-namespaces:API,WALLET_API, andPAYMASTER_API. It also re-exports essential types and method definitions used across these interfaces.Overview of CairoBytes31
TheCairoBytes31class represents a Cairocore::bytes_31::bytes31type. It stores up to 31 bytes, right-aligned in a fixed 31-byte buffer. Because it is right-aligned, the original length of the input is not recoverable from the value (e.g.,'0x41'and'0x0041'result in the sameCairoBytes31value). For cases where the exact byte count must be preserved, useCairoByteArrayinstead. An empty string''or'0x'is treated as empty bytes.Overview of Starknet Paymasters
A Paymaster allows Starknet accounts to pay gas fees using alternative tokens (e.g., ETH, USDC) instead of STRK. There are two primary transaction types:
default: The account pays the fees using a supported token.sponsored: A dApp covers the gas fees on behalf of the user.
Requirement: Accounts must be compatible with SNIP-9 (Outside execution) to use Paymaster services.
Overview of CairoArray
TheCairoArrayclass represents a Cairo dynamic array, which consists of any number of values of the same type. It supports bothcore::array::Array::<T>andcore::array::Span::<T>ABI types. Unlike tuples, dynamic arrays carry their length on the wire; the serialized felts begin with the number of elements that follow. Forfelt252orbytes31element types, text input is automatically split into 31-character chunks.Overview of AccountInterface
TheAccountInterfaceis the primary interface for interacting with Starknet account contracts. It abstracts the complexities of nonce management, transaction signing using the account's private key, and interaction with the account contract'sexecuteentrypoint. Key capabilities include transaction execution, fee estimation, contract deployment via the Universal Deployer Contract (UDC), paymaster support for sponsored transactions, and EIP-712 message signing.Overview of Outside Execution (Meta-Transactions)
Outside Execution, based on SNIP-9, allows a protocol or third party to submit transactions on behalf of a user account provided they have the required signatures. This is primarily used for:
- Delayed Orders: Atomic control over execution (e.g., limit orders).
- Fee Subsidy: The executor (sender) pays the gas fees, enabling accounts without gas tokens to execute transactions.
Overview of CairoTypeCustomEnum
ACairoTypeCustomEnumrepresents a Cairo custom enum: one of several named variants, each carrying its own type. On the wire, it is represented as the index of the active variant followed by that variant's value. Because the value itself does not indicate which variant is active, the index must be provided unless aCairoCustomEnumobject (which explicitly names its variant) is used.Overview of Outside Execution (Meta-Transactions)
Outside Execution, based on SNIP-9, allows a protocol to submit transactions on behalf of a user account provided they have the necessary signatures. This is primarily used for:
- Delayed Orders: Providing atomic control over execution (e.g., matching limit orders).
- Fee Subsidy: Allowing the transaction sender (executor) to pay gas fees, enabling accounts without gas tokens to execute transactions.
Overview of WalletAccount for DApps
The
WalletAccountclass allows DApps to ask a browser wallet to sign and send transactions. Unlike the standardAccountclass, the private key is held securely within the wallet, meaning the DApp does not need to manage private key security.Constraint: This class is designed exclusively for DApps and cannot be used in Node.js scripts.
To initialize a
WalletAccount, a provider is required (eitherProviderOptionslike{ nodeUrl }or aProviderInterfaceinstance such asRpcProvider) to handle all blockchain read activities.Overview of types.RPC.RPCSPEC08.API namespace
Thetypes.RPC.RPCSPEC08.APInamespace provides the core type definitions for interacting with Starknet RPC and WebSocket interfaces. It includes a wide array of error interfaces used to handle specific failure cases during blockchain interactions, as well as type aliases for different categories of RPC methods.Overview of ProviderInterface
TheProviderInterfaceclass serves as the primary interface for interacting with the Starknet blockchain. It provides methods to query chain information, retrieve block data, and interact with smart contracts.