Skip to content

Commit 47197a6

Browse files
committed
proofread reading-from-network
1 parent 6fe3c9c commit 47197a6

1 file changed

Lines changed: 95 additions & 62 deletions

File tree

apps/docs/content/docs/en/intro/quick-start/reading-from-network.mdx

Lines changed: 95 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ to locate the account's onchain data. Solana accounts contain either
1717
<WithMentions>
1818

1919
A wallet account is an account owned by the
20-
[System Program](/docs/core/programs#the-system-program). Wallet accounts are
21-
primarily used to hold SOL and sign transactions. When SOL is sent to a new
22-
address for the first time, a system account is automatically created.
20+
[System Program](/docs/core/programs/builtin-programs#the-system-program).
21+
Wallet accounts are primarily used to hold SOL and sign transactions. When SOL
22+
is sent to a new address for the first time, a system account is automatically
23+
created.
2324

2425
The example below [creates a Kit client](mention:client), generates a
2526
[new signer](mention:keypair), [requests SOL](mention:airdrop) to fund the new
@@ -49,7 +50,7 @@ const signer = await generateKeyPairSigner();
4950
console.log(`Address: ${signer.address}`);
5051

5152
// Funding an address with SOL automatically creates an account
52-
// !mention(1:4) airdrop
53+
// !mention airdrop
5354
await client.airdrop(signer.address, lamports(1_000_000_000n));
5455

5556
// !mention info
@@ -144,8 +145,8 @@ appear.
144145

145146
## !!steps
146147

147-
The `lamports` field contains the account's SOL balance, in
148-
[lamports](/docs/references/terminology#lamport).
148+
The `lamports` field contains the account's SOL balance, measured in
149+
[lamports](/docs/references/terminology#lamport), the smallest unit of SOL.
149150

150151
<CodePlaceholder title="Example output" />
151152

@@ -222,8 +223,10 @@ owner is always the System Program, with the address
222223

223224
## !!steps
224225

225-
The `executable` field indicates whether the account can be invoked as a
226-
program. For wallet accounts, the `executable` field is `false`.
226+
The `executable` field tells you whether the account's address is callable.
227+
`true` means the address represents a program that can process instructions.
228+
`false` means the account stores state, such as a wallet balance or account
229+
data, and is not called as a program. Wallet accounts use `false`.
227230

228231
<CodePlaceholder title="Example output" />
229232

@@ -272,9 +275,9 @@ field is still returned for backward compatibility.
272275

273276
## !!steps
274277

275-
The `space` field shows the number of bytes contained in the `data` field.
276-
Account-fetching APIs include the `space` value to save you from counting the
277-
bytes yourself.
278+
The `space` field shows the number of bytes contained in the `data` field. The
279+
`space` field is returned with the account-fetching response, but it is not part
280+
of the account's data type.
278281

279282
For the wallet account example, the `space` field is 0 because the `data` field
280283
contains 0 bytes of data.
@@ -304,18 +307,24 @@ contains 0 bytes of data.
304307
## Fetch the Token Program
305308

306309
The example below fetches the Token Program to demonstrate the difference
307-
between wallet and program accounts. The Token Program's address is the onchain
308-
Program account. For upgradeable programs, the Program account stores loader
309-
metadata and points to a separate ProgramData account that stores the deployed
310-
bytecode for the Token Program's
311-
[source code](https://github.com/solana-program/token/tree/main/program). You
312-
can view the Program account on the
310+
between wallet and program accounts. The Token Program defines instructions for
311+
working with tokens, such as creating and transferring tokens. Programs are
312+
invoked to process instructions. Program state, such as token data and balances,
313+
are stored in separate accounts owned by the program.
314+
315+
The Token Program's address is the onchain Program account. For simplicity, you
316+
can think of the program address as the program, because that is the address
317+
used to invoke it. For upgradeable programs, the Program account stores metadata
318+
and points to a separate ProgramData account that stores the deployed executable
319+
code. You can view the Token Program
320+
[source code](https://github.com/solana-program/token/tree/main/program) and the
321+
Program account on the
313322
[Solana Explorer](https://explorer.solana.com/address/TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA).
314323

315324
<CodeTabs flags="r">
316325

317326
```ts !! title="Fetch program account"
318-
import { address, createClient } from "@solana/kit";
327+
import { address, createClient, fetchJsonParsedAccount } from "@solana/kit";
319328
import { solanaRpc } from "@solana/kit-plugin-rpc";
320329
import { generatedPayer } from "@solana/kit-plugin-signer";
321330

@@ -340,6 +349,14 @@ const accountInfo = await client.rpc
340349
.send();
341350

342351
console.log(accountInfo);
352+
353+
// !mark(1:4)
354+
const parsedAccount = await fetchJsonParsedAccount(
355+
client.rpc,
356+
tokenProgramAddress
357+
);
358+
359+
console.log(parsedAccount);
343360
```
344361

345362
</CodeTabs>
@@ -350,13 +367,10 @@ console.log(accountInfo);
350367

351368
The Token Program is an executable program account. Programs have the same
352369
underlying fields as all [accounts](/docs/core/accounts#account-structure), but
353-
with key differences. Your `slot` and `apiVersion` may differ from the Token
354-
Program output shown here.
370+
with key differences.
355371

356372
The Token Program example uses `base64` encoding to return the Program account's
357-
raw bytes without asking RPC to interpret the bytes. The Program account data is
358-
short because the fetched account is the upgradeable Program account, not the
359-
separate ProgramData account that stores the compiled bytecode.
373+
raw data.
360374

361375
The following steps walk through the fields inside `value` in the order the
362376
fields appear.
@@ -384,8 +398,8 @@ fields appear.
384398

385399
The `data` field stores the BPF Upgradeable Loader's Program account state. The
386400
first tuple item contains the full account data encoded as base64. The second
387-
tuple item identifies the encoding. Here, the 36 bytes are the loader-state
388-
discriminator plus the ProgramData account address.
401+
tuple item identifies the encoding. Here, the 36 bytes include metadata and the
402+
ProgramData account address.
389403

390404
For upgradeable programs, the Program account state points to the separate
391405
ProgramData account that stores the program's executable code.
@@ -463,8 +477,8 @@ Program accounts need enough lamports to remain rent-exempt.
463477
## !!steps
464478

465479
Every program account is owned by a
466-
[loader program](/docs/core/programs#loader-programs). For the Token Program
467-
account, the `owner` is the BPF Upgradeable Loader.
480+
[loader program](/docs/core/programs/program-deployment#loader-programs). For
481+
the Token Program account, the `owner` is the BPF Upgradeable Loader.
468482

469483
<CodePlaceholder title="Token program account" />
470484

@@ -515,7 +529,8 @@ field is still returned for backward compatibility.
515529

516530
The `space` field shows the full size of the Program account's data in bytes.
517531
The `space` value is only `36` bytes because the Program account stores loader
518-
metadata and a 32-byte pointer to ProgramData, not the full compiled program.
532+
metadata and the address of the ProgramData account, not the full compiled
533+
program.
519534

520535
<CodePlaceholder title="Token program account" />
521536

@@ -537,16 +552,40 @@ metadata and a 32-byte pointer to ProgramData, not the full compiled program.
537552
}
538553
```
539554

555+
## !!steps
556+
557+
The previous response returned the Program account's `data` field as a base64
558+
tuple. The parsed response deserializes those bytes into named fields on the
559+
`data` object. The `programData` field contains the address of the ProgramData
560+
account that stores the executable program code.
561+
562+
<CodePlaceholder title="Parsed Token program account" />
563+
564+
```ts !! title="Parsed Token program account"
565+
{
566+
executable: true,
567+
lamports: 43712780n,
568+
programAddress: "BPFLoaderUpgradeab1e11111111111111111111111",
569+
space: 36n,
570+
address: "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
571+
// !focus(1:4)
572+
data: {
573+
programData: "3gvYRKWyXRR9xKWe1ZjPhLY5ZJRN7KDB4rFZFGoJfFk2",
574+
parsedAccountMeta: { program: "bpf-upgradeable-loader", type: "program" }
575+
},
576+
exists: true
577+
}
578+
```
579+
540580
</ScrollyCoding>
541581

542582
## Fetch a mint account
543583

544-
A
545-
[mint account](https://github.com/solana-program/token/blob/program%40v8.0.0/program/src/state.rs#L16-L30)
546-
is an account owned by the Token Program that stores global metadata for a
547-
specific token. The mint account stores the token's total supply, number of
548-
decimals, mint authority, and freeze authority. The mint account's address
549-
uniquely identifies a token on the Solana network.
584+
A [mint account](/docs/tokens/basics/create-mint) is an account owned by the
585+
Token Program that stores global metadata for a specific token. The mint account
586+
stores the token's total supply, number of decimals, mint authority, and freeze
587+
authority. The mint account's address uniquely identifies a token on the Solana
588+
network.
550589

551590
The example below fetches the USD Coin Mint account to demonstrate how a
552591
program's state is stored in a separate account. Exact balances and supply
@@ -590,9 +629,6 @@ Mint accounts store state, not executable code. Mint accounts are owned by the
590629
Token Program, which includes instructions defining how to create and update
591630
mint accounts.
592631

593-
The mint account example uses `base64` encoding so the raw account data is
594-
returned as an encoded string. The mint account is small enough to log directly.
595-
596632
The following steps walk through the fields inside `value` in the order the
597633
fields appear.
598634

@@ -795,21 +831,19 @@ The `space` field shows that the mint account contains 82 bytes of data.
795831

796832
## Deserialize mint account
797833

798-
Before the raw bytes in an account's `data` field can be interpreted
799-
meaningfully, the raw bytes must be deserialized. The program that owns the
800-
account defines the appropriate data type. Most Solana programs provide client
801-
libraries with helper functions that abstract away the deserialization process.
802-
Helper functions convert raw account bytes into structured data types, making
803-
account data easier to work with.
834+
Account data is stored in the `data` field in a serialized format. To read that
835+
data as fields like `supply` or `decimals`, deserialize the `data` field into
836+
the account type defined by the owning program. Most Solana programs provide
837+
client libraries with helper functions for this step. These helpers return
838+
structured account data, making the result easier to work with.
804839

805840
<WithMentions>
806841

807842
For example, the _shell`@solana-program/token`_ library includes the
808843
[_ts`fetchMint()`_](mention:one) function to fetch a mint account and
809844
deserialize the mint account's `data` field into the
810845
[Mint](https://github.com/solana-program/token/blob/program%40v8.0.0/program/src/state.rs#L16-L30)
811-
data type defined by the Token Program. Optional authorities are returned as Kit
812-
`Option` values.
846+
data type defined by the Token Program.
813847

814848
<CodeTabs flags="r">
815849

@@ -977,8 +1011,7 @@ fields in `data` come from the Token Program's `Mint` account type.
9771011
## !!steps
9781012

9791013
The `data.mintAuthority` field shows the only account that can create new units
980-
of the token. Since the mint authority is optional, Kit returns the mint
981-
authority as an `Option`.
1014+
of the token.
9821015

9831016
<CodePlaceholder title="Decoded mint account" />
9841017

@@ -1104,8 +1137,9 @@ Program.
11041137
## !!steps
11051138

11061139
The `data.freezeAuthority` field shows the account with authority to freeze
1107-
token accounts. A frozen token account cannot transfer or burn the token balance
1108-
inside the frozen token account.
1140+
token accounts. A [token account](/docs/tokens/basics/create-token-account) is a
1141+
separate account that stores units of a token for an owner. When frozen, a token
1142+
account cannot transfer or burn its token balance.
11091143

11101144
<CodePlaceholder title="Decoded mint account" />
11111145

@@ -1135,9 +1169,8 @@ inside the frozen token account.
11351169

11361170
## !!steps
11371171

1138-
The `executable` field keeps the same meaning after deserialization. The USDC
1139-
mint account stores token state, so the USDC mint account cannot be invoked as a
1140-
program.
1172+
The `executable` field is false. The USDC mint account stores token state, so
1173+
the USDC mint account cannot be invoked as a program.
11411174

11421175
<CodePlaceholder title="Decoded mint account" />
11431176

@@ -1198,8 +1231,9 @@ lamports value is the mint account's rent-exempt balance, not the token supply.
11981231

11991232
## !!steps
12001233

1201-
The `programAddress` field shows the program that owns the decoded mint account.
1202-
For USDC, the owning program is the Token Program.
1234+
The `programAddress` field shows the program that owns the mint account. For
1235+
USDC, the owning program is the Token Program. Only the program that owns an
1236+
account can modify its `data` field through the program's deployed instructions.
12031237

12041238
<CodePlaceholder title="Decoded mint account" />
12051239

@@ -1262,18 +1296,17 @@ Token Program's base `Mint` account is 82 bytes.
12621296

12631297
### Serialized vs deserialized output
12641298

1265-
Both examples read the same USDC mint account. The raw RPC fetch returns
1266-
serialized bytes in the `data` field. To read the mint account's contents,
1267-
decode the serialized bytes as the account type defined by the owning program.
1268-
For a mint account, the Token Program defines the `Mint` type shown on the
1269-
right.
1299+
Both examples read the same USDC mint account. The first response leaves the
1300+
`data` field serialized. To read the mint account's contents, decode that data
1301+
as the account type defined by the owning program. For a mint account, the Token
1302+
Program defines the `Mint` type shown on the right.
12701303

12711304
<SideBySide>
12721305

12731306
### !left
12741307

12751308
Before decoding, the [`data`](mention:serialized-data) field contains serialized
1276-
account bytes.
1309+
account data.
12771310

12781311
```ts title="Serialized mint account" -w
12791312
{
@@ -1299,8 +1332,8 @@ account bytes.
12991332

13001333
### !right
13011334

1302-
After decoding the serialized bytes as a `Mint`, the
1303-
[`data`](mention:decoded-data) field contains named account fields.
1335+
After decoding the data as a `Mint`, the [`data`](mention:decoded-data) field
1336+
contains named account fields.
13041337

13051338
```ts title="Deserialized mint account" -w
13061339
{

0 commit comments

Comments
 (0)