Reading from Network
This section explores how to read data from the Solana network by fetching different accounts to understand the structure of a Solana account.
On Solana, all data exists in "accounts". You can think of data on Solana as a public database with a single "Accounts" table, where each entry is an account with the same base Account type.
Accounts
Accounts on Solana can store "state" or "executable" programs. Each account has an "address" (public key) that serves as its unique ID used to locate its corresponding on-chain data.
Solana accounts contain either:
- State: Data that meant to be read from and persisted. For example, information about tokens, user data, or other data defined within a program.
- Executable Programs: Accounts containing the actual code of Solana programs. These accounts store instructions that users can invoke.
This separation of program code and program state is a key feature of Solana's Account Model. For more details, refer to the Solana Account Model page.
Fetch Wallet Account
This example demonstrates how to:
- Generate a new keypair (public/private key pair).
- Request an airdrop of SOL to fund the new address.
- Retrieve the account data for the funded address.
On Solana, funding a new address with SOL automatically creates an account owned by the System Program. All "wallet" accounts are simply System Program owned accounts that hold SOL and can sign transactions.
import { Keypair, Connection, LAMPORTS_PER_SOL } from "@solana/web3.js";const keypair = Keypair.generate();console.log(`Public Key: ${keypair.publicKey}`);const connection = new Connection("http://localhost:8899", "confirmed");// Funding an address with SOL automatically creates an accountconst signature = await connection.requestAirdrop(keypair.publicKey,LAMPORTS_PER_SOL);await connection.confirmTransaction(signature, "confirmed");const accountInfo = await connection.getAccountInfo(keypair.publicKey);console.log(JSON.stringify(accountInfo, null, 2));
You must enable the "Enable Custom URL Param" setting on Solana Explorer.
If not enabled, links will default to localhost:8899 instead of the Mirror.ad RPC URL.
Fetch Token Program
This example fetches the Token Program to demonstrate the difference between wallet and program accounts.
import { Connection, PublicKey } from "@solana/web3.js";const connection = new Connection("https://api.mainnet-beta.solana.com","confirmed");const address = new PublicKey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");const accountInfo = await connection.getAccountInfo(address);
You must enable the "Enable Custom URL Param" setting on Solana Explorer.
If not enabled, links will default to localhost:8899 instead of the Mirror.ad RPC URL.
Fetch Mint Account
This example fetches the USD Coin (USDC) Mint account to show how programs on Solana store state in separate accounts.
A Mint account is an account owned by the Token Program. It stores global metadata for a specific token, including the total supply, number of decimals, and the accounts authorized to mint or freeze tokens. The Mint account's address uniquely identifies a token on the Solana network.
import { Connection, PublicKey } from "@solana/web3.js";const connection = new Connection("https://api.mainnet-beta.solana.com","confirmed");const address = new PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");const accountInfo = await connection.getAccountInfo(address);
You must enable the "Enable Custom URL Param" setting on Solana Explorer.
If not enabled, links will default to localhost:8899 instead of the Mirror.ad RPC URL.
Deserialize Mint Account
The data
field of a Solana account contains raw bytes. To interpret this data
meaningfully, you must deserialize it into the appropriate data type defined by
the program that owns the account.
Most Solana programs provide client libraries with helper functions that abstract away the deserialization process. These functions convert the raw account bytes into structured data types, making it easier to work with the account data.
For example, @solana/spl-token
includes the
getMint()
function to help deserialize a Mint account's
data
field into the
Mint
data type.
import { PublicKey, Connection } from "@solana/web3.js";import { getMint } from "@solana/spl-token";const connection = new Connection("https://api.mainnet-beta.solana.com","confirmed");const address = new PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");const mintData = await getMint(connection, address, "confirmed");
You must enable the "Enable Custom URL Param" setting on Solana Explorer.
If not enabled, links will default to localhost:8899 instead of the Mirror.ad RPC URL.
Is this page helpful?