Web3.js API Examples
Web3 API Reference Guide
The @solana/web3.js
library is a package that has coverage over the
Solana JSON RPC API.
You can find the full documentation for the @solana/web3.js
library
here.
General
Connection
Connection is used to interact with the Solana JSON RPC. You can use Connection to confirm transactions, get account info, and more.
You create a connection by defining the JSON RPC cluster endpoint and the desired commitment. Once this is complete, you can use this connection object to interact with any of the Solana JSON RPC API.
Example Usage
The above example shows only a few of the methods on Connection. Please see the source generated docs for the full list.
Transaction
A transaction is used to interact with programs on the Solana blockchain. These transactions are constructed with TransactionInstructions, containing all the accounts possible to interact with, as well as any needed data or program addresses. Each TransactionInstruction consists of keys, data, and a programId. You can do multiple instructions in a single transaction, interacting with multiple programs at once.
Example Usage
Keypair
The keypair is used to create an account with a public key and secret key within Solana. You can either generate, generate from a seed, or create from a secret key.
Example Usage
Using generate
generates a random Keypair for use as an account on Solana.
Using fromSeed
, you can generate a Keypair using a deterministic constructor.
fromSecret
creates a Keypair from a secret Uint8array. You can see that the
publicKey for the generate
Keypair and fromSecret
Keypair are the same
because the secret from the generate
Keypair is used in fromSecret
.
Warning: Do not use fromSeed
unless you are creating a seed with high
entropy. Do not share your seed. Treat the seed like you would a private key.
PublicKey
PublicKey is used throughout @solana/web3.js
in transactions, keypairs, and
programs. You require publickey when listing each account in a transaction and
as a general identifier on Solana.
A PublicKey can be created with a base58 encoded string, buffer, Uint8Array, number, and an array of numbers.
Example Usage
SystemProgram
The SystemProgram grants the ability to create accounts, allocate account data, assign an account to programs, work with nonce accounts, and transfer lamports. You can use the SystemInstruction class to help with decoding and reading individual instructions
Example Usage
Secp256k1Program
The Secp256k1Program is used to verify Secp256k1 signatures, which are used by both Bitcoin and Ethereum.
Example Usage
Message
Message is used as another way to construct transactions. You can construct a message using the accounts, header, instructions, and recentBlockhash that are a part of a transaction. A Transaction is a Message plus the list of required signatures required to execute the transaction.
Example Usage
Struct
The struct class is used to create Rust compatible structs in javascript. This class is only compatible with Borsh encoded Rust structs.
Example Usage
Struct in Rust:
Using web3:
Enum
The Enum class is used to represent a Rust compatible Enum in javascript. The enum will just be a string representation if logged but can be properly encoded/decoded when used in conjunction with Struct. This class is only compatible with Borsh encoded Rust enumerations.
Example Usage
Rust:
Web3:
NonceAccount
Normally a transaction is rejected if a transaction's recentBlockhash
field is
too old. To provide for certain custodial services, Nonce Accounts are used.
Transactions which use a recentBlockhash
captured on-chain by a Nonce Account
do not expire as long at the Nonce Account is not advanced.
You can create a nonce account by first creating a normal account, then using
SystemProgram
to make the account a Nonce Account.
Example Usage
The above example shows both how to create a NonceAccount
using
SystemProgram.createNonceAccount
, as well as how to retrieve the
NonceAccount
from accountInfo. Using the nonce, you can create transactions
offline with the nonce in place of the recentBlockhash
.
VoteAccount
Vote account is an object that grants the capability of decoding vote accounts from the native vote account program on the network.
Example Usage
Staking
StakeProgram
The StakeProgram facilitates staking SOL and delegating them to any validators on the network. You can use StakeProgram to create a stake account, stake some SOL, authorize accounts for withdrawal of your stake, deactivate your stake, and withdraw your funds. The StakeInstruction class is used to decode and read more instructions from transactions calling the StakeProgram
Example Usage
Authorized
Authorized is an object used when creating an authorized account for staking
within Solana. You can designate a staker
and withdrawer
separately,
allowing for a different account to withdraw other than the staker.
You can find more usage of the Authorized
object under
StakeProgram
Lockup
Lockup is used in conjunction with the StakeProgram to create an account. The Lockup is used to determine how long the stake will be locked, or unable to be retrieved. If the Lockup is set to 0 for both epoch and the Unix timestamp, the lockup will be disabled for the stake account.
Example Usage
The above code creates a createStakeAccountInstruction
to be used when
creating an account with the StakeProgram
. The Lockup is set to 0 for both the
epoch and Unix timestamp, disabling lockup for the account.
See StakeProgram for more.