Reading from Network
Now, let's explore how to read data from the Solana network. We'll fetch a few different accounts to understand the structure of a Solana account.
On Solana, all data is contained in what we call "accounts". You can think of data on Solana as a public database with a single "Accounts" table, where each entry in this table is an individual account with the same base Account type.
Accounts
Accounts on Solana can store "state" or "executable" programs, all of which can be thought of as entries in the same "Accounts" table. 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: This is data that's meant to be read from and persisted. It could be information about tokens, user data, or any other type of data defined within a program.
- Executable Programs: These are accounts that contain the actual code of Solana programs. They contain the instructions that can be executed on the network.
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 Playground Wallet
Let's start by looking at a familiar account - your own Playground Wallet! We'll fetch this account and examine 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:
Run Example
In the Playground terminal, type the run
command and hit enter:
When you run the code, you'll see the account details for your wallet displayed in the terminal. The output should look similar to the following:
Fetch Token Program
Next, we'll examine 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:
Instead of fetching your Playground wallet, here we fetch the Token Extensions Program account.
Run Example
Run the code using the run
command in the terminal.
Examine the output and how this program account differs from your wallet account.
Fetch Mint Account
In this step, we'll examine 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:
In this example, we'll fetch the address of an existing Mint account on devnet.
Deserialize Mint Account Data
An account's data
field contains bytes that need to be further deserialized
into the expected data type. The structure of this data is defined by the
program that owns the account.
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:
This example uses the getMint
helper function to automatically deserialize the
data field of the Mint account.
Run the code using the run
command.
You should see the following deserialized Mint account data.
Last updated on