starknet.js

repository·develop·Indexed 22 days ago

https://github.com/starknet-io/starknet.js

A JavaScript library for interacting with the Starknet blockchain. It provides tools for building applications, interacting with smart contracts, and managing Starknet-related logic in JavaScript/TypeScript environments. The library includes utilities for calldata compilation, handling Cairo types (such as felt252, u256, and ByteArray), and managing Cairo enums like Option and Result.

Tokens
77.9K
Snippets
181
Records
341
Agent score
78%

What's inside starknet.js

  1. What is Outside Execution (SNIP-9)?

    develop

    Outside Execution, also known as meta-transactions, allows a protocol to submit transactions on behalf of a user account using their signatures. This follows the SNIP-9 standard.

    Key Benefits:

    1. Delayed Orders: Enables atomic control over execution timing (e.g., for limit orders).
    2. Fee Subsidy: The executor (sender) pays the gas fees, allowing users without gas tokens to interact with the protocol.
  2. Use WalletAccount as an Account, Provider, or in a Contract

    develop

    A WalletAccount instance is highly versatile and can be used in three primary ways:

    1. As an Account: Use it to execute transactions or sign messages, just like a standard Account instance.
    2. As a Provider: It implements the RpcProvider interface. You can call provider methods (e.g., getBlockNumber()) directly on it. It uses the RPC node URL provided during instantiation.
    3. In a Contract instance: When passed to a Contract constructor, the WalletAccount acts as both the provider (for read actions) and the signer (for write actions).

    Recommendation: When the network or account address changes, it is strongly recommended to create a new WalletAccount instance to avoid unexpected behavior between reads and writes.

  3. Use RpcProvider to connect to the Starknet network

    develop

    The RpcProvider class is the primary object used to connect your DApp to the Starknet blockchain. To use it, you must define the network (Mainnet, Sepolia Testnet, Devnet, etc.) and select a node (an RPC provider like Alchemy, Zan, or Infura, or your own local node like Pathfinder or Juno).

    Starknet.js communicates with nodes using the RPC specification. It is critical to ensure your Starknet.js version aligns with the RPC version supported by your chosen node.

    import { RpcProvider } from 'starknet';
  4. Understand the documentation structure

    develop

    The documentation is organized into several locations:

    • Current/Future Release: Located in /www/docs. API documentation is in /www/docs/API and guides are in /www/docs/guides.
    • Previous Versions: Located in /www/versioned_docs.
    • Official Hosted Docs: Available at starknet-io.github.io/starknet.js.

    Note on API Docs: API documentation is automatically generated from JSDoc comments in the source code via CI. Do not include changes to /www/docs/API in Pull Requests. Guides in /www/docs/guides must be updated manually in Pull Requests.

  5. Handle contract call results based on Cairo version

    develop

    The structure of the data returned by a contract call depends on whether the contract was written in Cairo 0 or Cairo 1:

    • Cairo 0: The result is an object where the keys correspond to the Cairo variable names.
    • Cairo 1: The result is the variable itself (the direct return value).

    You can detect the contract version using myContract.isCairo1() or check an ABI using cairo.isCairo1Abi(myAbi).

  6. Understanding Starknet Account Creation

    develop

    In Starknet, there are no Externally Owned Accounts (EOA). All accounts are smart contracts. Because account contracts require a fee to be deployed, the creation process follows a specific lifecycle:

    1. Decide on an account type (e.g., OpenZeppelin, Argent, Braavos).
    2. Compute the address of the future account using its class hash and constructor parameters.
    3. Fund the pre-computed address with enough STRK to cover the deployment fee and provide initial balance.
    4. Deploy the account contract to the network.
  7. Understand the STRK20 privacy protocol actions

    develop

    STRK20 is a note-based privacy pool for ERC-20 assets. A DAPP describes desired operations using an array of STRK20_ACTION objects.

    ActiontypeFieldsEffect
    Deposit"deposit"token, amountPublic funds $\rightarrow$ pool (always to self).
    Withdraw"withdraw"token, amount, recipientPool $\rightarrow$ public recipient address.
    Transfer"transfer"token, amount (FELT or "OPEN"), recipientPrivate transfer inside the pool to another registered user.
    Invoke"invoke"contract, calldataCalls an invoke helper contract, executed by the pool.
    Sub-account invoke"subaccount_invoke"dapp_name, nonce, calls, collect_policyCalls contracts through the user's sub-account for this DAPP.

    Note on amount: "OPEN": This is used in multi-action transactions to create an empty note whose value is unknown at build time (e.g., an AMM swap output) and is filled later in the same transaction by a paired invoke or subaccount_invoke action.

  8. Interact with smart contracts using Contract

    develop

    The Contract class manages interactions with a specific smart contract using its ABI. It handles:

    • Issuing call and invoke requests to Starknet.
    • Transforming Cairo types into JavaScript representations (e.g., converting Uint256 to BigNumber).
    • Supporting custom data transformers similar to JSON.parse.
  9. Understand the starknet.js layer model

    develop

    The starknet.js SDK follows a layered architecture for interacting with the network:

    1. Account: The top layer used for signing and submitting transactions.
    2. RpcProvider: Wraps the account layer to parse responses and handle retries.
    3. RpcChannel: The bottom layer that handles raw JSON-RPC communication.

    When building applications, you typically interact with Account and Contract objects which rely on an underlying RpcProvider.

  10. Best practices for TypeScript ABI integration

    develop

    When working with typed contracts in Starknet.js, follow these best practices:

    • Use typedv2(): Always use the .typedv2() method for the latest and most robust type-checking features.
    • Organize ABI files: Keep your generated .ts ABI files in a dedicated directory.
    • Meaningful Naming: Use variable names that reflect the typed nature of your contracts.
    • Type-safe Wrappers: Consider creating wrapper functions that leverage the typed contract to simplify common interactions.