@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.
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.
The above example shows only a few of the methods on Connection. Please see the source generated docs for the full list.
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.
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.
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
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 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.
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
The Secp256k1Program is used to verify Secp256k1 signatures, which are used by both Bitcoin and Ethereum.
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.
Struct in Rust:
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.
The above example shows both how to create a
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
Vote account is an object that grants the capability of decoding vote accounts from the native vote account program on the network.
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
Authorized is an object used when creating an authorized account for staking within Solana. You can designate a
withdrawer separately, allowing for a different account to withdraw other than the staker.
You can find more usage of the
Authorized object under
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.
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.