Reading from Network
This guide 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 in this table represents an individual 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 meant for reading and persistence. This includes 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 the network executes.
This separation of program code and program state forms a key feature of Solana's Account Model. For more details, refer to the Solana Account Model page.
Fetch Playground Wallet
Next, you can look at a familiar account - your own Playground Wallet. This example fetches this account and examines its structure to understand what a basic Solana account looks like.
Open Example
Click this link to open the example in Solana Playground. You'll see the following code:
const address = pg.wallet.publicKey;const accountInfo = await pg.connection.getAccountInfo(address);console.log(JSON.stringify(accountInfo, null, 2));
Run Example
In the Playground terminal, type the run
command and hit enter:
$run
When you run the code, you'll see the account details for your wallet displayed in the terminal.
Fetch Token Program
Next, this guide examines the Token Extensions program, an executable program for interacting with tokens on Solana.
Open Example
Click this link to open the example in Solana Playground. You'll see the following code:
import { PublicKey } from "@solana/web3.js";const address = new PublicKey("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb");const accountInfo = await pg.connection.getAccountInfo(address);console.log(JSON.stringify(accountInfo, null, 2));
Instead of fetching your Playground wallet, this example fetches the Token Extensions Program account.
Run Example
Run the code using the run
command in the terminal.
$run
Examine the output and how this program account differs from your wallet account.
Fetch Mint Account
In this step, the guide examines a Mint account, which represents a unique token on the Solana network.
Open Example
Click this link to open the example in Solana Playground. You'll see the following code:
import { PublicKey } from "@solana/web3.js";const address = new PublicKey("C33qt1dZGZSsqTrHdtLKXPZNoxs6U1ZBfyDkzmj6mXeR");const accountInfo = await pg.connection.getAccountInfo(address);console.log(JSON.stringify(accountInfo, null, 2));
In this example, the code fetches the address of an existing Mint account on devnet.
Deserialize Mint Account Data
An account's data
field contains bytes requiring further deserialization
into the expected data type. The program that owns the account
defines this data structure.
To help with deserialization, most program developers provide helper functions in their client libraries that handle converting the raw bytes into the appropriate data type.
For example, the @solana/spl-token
library provides functions like
getMint()
to help deserialize a token mint account's bytes
into the
Mint
data type.
Open this next example in Solana Playground. You'll see the following code:
import { PublicKey } from "@solana/web3.js";import { getMint, TOKEN_2022_PROGRAM_ID } from "@solana/spl-token";const address = new PublicKey("C33qt1dZGZSsqTrHdtLKXPZNoxs6U1ZBfyDkzmj6mXeR");const mintData = await getMint(pg.connection,address,"confirmed",TOKEN_2022_PROGRAM_ID,);
This example uses the getMint()
helper function to automatically
deserialize the data field of the Mint account.
Run the code using the run
command.
$run
You should see the following deserialized Mint account data.
Is this page helpful?