Jenis akun

Ringkasan

Akun program menyimpan kode sBPF yang dapat dieksekusi. Akun data menyimpan state, yang dimiliki oleh program. Akun sistem dimiliki oleh System Program. Sysvars menyediakan state cluster-wide yang dapat diakses di alamat yang telah ditentukan.

Field executable menentukan kategori akun:

  • Akun program: executable = true. Berisi kode yang dapat dieksekusi.
  • Akun data: executable = false. Menyimpan state atau data pengguna.

Pemisahan kode dari state yang dapat diubah ini berarti program di-deploy sekali dan dapat mengelola sejumlah akun data.

Akun program

Akun program menyimpan kode yang dapat dieksekusi. Setiap akun program dimiliki oleh program loader. Ketika sebuah program di-deploy, runtime membuat akun program untuk menyimpan bytecode-nya.

Diagram of a program account, its 4 components and its loader program.Diagram of a program account, its 4 components and its loader program.

Akun data program

Program yang di-deploy menggunakan loader-v3 (lihat Program loader) tidak menyimpan bytecode yang dapat dieksekusi di field data mereka sendiri. Sebaliknya, data mereka menunjuk ke akun data program terpisah yang berisi kode program. (Lihat diagram di bawah.)

A program account with data. The data points to a separate program data accountA program account with data. The data points to a separate program data account

Selama deployment atau upgrade program, akun buffer digunakan untuk sementara menyimpan upload.

Contoh berikut mengambil akun Token Program. Field executable adalah true, mengonfirmasi bahwa ini adalah akun program.

import { Address, createSolanaRpc } from "@solana/kit";
const rpc = createSolanaRpc("https://api.mainnet.solana.com");
const programId = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" as Address;
const accountInfo = await rpc
.getAccountInfo(programId, { encoding: "base64" })
.send();
console.log(accountInfo);
Console
Click to execute the code.

Akun data

Akun data tidak berisi kode yang dapat dieksekusi. Mereka menyimpan state yang didefinisikan oleh program.

Akun state program

Program menyimpan state mereka dalam akun data. Membuat akun state program melibatkan dua langkah:

  1. Panggil System Program untuk membuat akun. System Program mentransfer kepemilikan ke program yang ditentukan.
  2. Program pemilik menginisialisasi field data akun sesuai dengan instruksi nya.

Diagram of a data account owned by a program accountDiagram of a data account owned by a program account

Contoh berikut membuat dan mengambil akun Token Mint yang dimiliki oleh program Token 2022.

import {
airdropFactory,
appendTransactionMessageInstructions,
createSolanaRpc,
createSolanaRpcSubscriptions,
createTransactionMessage,
generateKeyPairSigner,
getSignatureFromTransaction,
lamports,
pipe,
sendAndConfirmTransactionFactory,
setTransactionMessageFeePayerSigner,
setTransactionMessageLifetimeUsingBlockhash,
signTransactionMessageWithSigners
} from "@solana/kit";
import { getCreateAccountInstruction } from "@solana-program/system";
import {
getInitializeMintInstruction,
getMintSize,
TOKEN_2022_PROGRAM_ADDRESS,
fetchMint
} from "@solana-program/token-2022";
// Create Connection, local validator in this example
const rpc = createSolanaRpc("http://localhost:8899");
const rpcSubscriptions = createSolanaRpcSubscriptions("ws://localhost:8900");
// Generate keypairs for fee payer
const feePayer = await generateKeyPairSigner();
// Fund fee payer
await airdropFactory({ rpc, rpcSubscriptions })({
recipientAddress: feePayer.address,
lamports: lamports(1_000_000_000n),
commitment: "confirmed"
});
// Generate keypair to use as address of mint
const mint = await generateKeyPairSigner();
// Get default mint account size (in bytes), no extensions enabled
const space = BigInt(getMintSize());
// Get minimum balance for rent exemption
const rent = await rpc.getMinimumBalanceForRentExemption(space).send();
// Instruction to create new account for mint (token 2022 program)
// Invokes the system program
const createAccountInstruction = getCreateAccountInstruction({
payer: feePayer,
newAccount: mint,
lamports: rent,
space,
programAddress: TOKEN_2022_PROGRAM_ADDRESS
});
// Instruction to initialize mint account data
// Invokes the token 2022 program
const initializeMintInstruction = getInitializeMintInstruction({
mint: mint.address,
decimals: 9,
mintAuthority: feePayer.address
});
const instructions = [createAccountInstruction, initializeMintInstruction];
// Get latest blockhash to include in transaction
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
// Create transaction message
const transactionMessage = pipe(
createTransactionMessage({ version: 0 }), // Create transaction message
(tx) => setTransactionMessageFeePayerSigner(feePayer, tx), // Set fee payer
(tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx), // Set transaction blockhash
(tx) => appendTransactionMessageInstructions(instructions, tx) // Append instructions
);
// Sign transaction message with required signers (fee payer and mint keypair)
const signedTransaction =
await signTransactionMessageWithSigners(transactionMessage);
// Send and confirm transaction
await sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions })(
signedTransaction,
{ commitment: "confirmed" }
);
// Get transaction signature
const transactionSignature = getSignatureFromTransaction(signedTransaction);
console.log("Mint Address:", mint.address);
console.log("Transaction Signature:", transactionSignature);
const accountInfo = await rpc.getAccountInfo(mint.address).send();
console.log(accountInfo);
const mintAccount = await fetchMint(rpc, mint.address);
console.log(mintAccount);
Console
Click to execute the code.

Akun sistem

Akun yang tetap dimiliki oleh System Program setelah pembuatan disebut akun sistem. Mengirim SOL ke alamat baru untuk pertama kalinya membuat akun baru di alamat tersebut yang dimiliki oleh System Program.

Semua akun wallet adalah akun sistem. Pembayar biaya pada transaksi harus berupa akun sistem, karena hanya akun yang dimiliki System Program yang dapat membayar biaya transaksi.

A wallet owned by the System Program containing 1,000,000 lamportsA wallet owned by the System Program containing 1,000,000 lamports

Contoh berikut menghasilkan keypair baru, mendanainya dengan SOL, dan mengambil akun tersebut. Field owner adalah 11111111111111111111111111111111 (System Program).

import {
airdropFactory,
createSolanaRpc,
createSolanaRpcSubscriptions,
generateKeyPairSigner,
lamports
} from "@solana/kit";
// Create a connection to Solana cluster
const rpc = createSolanaRpc("http://localhost:8899");
const rpcSubscriptions = createSolanaRpcSubscriptions("ws://localhost:8900");
// Generate a new keypair
const keypair = await generateKeyPairSigner();
console.log(`Public Key: ${keypair.address}`);
// Funding an address with SOL automatically creates an account
const signature = await airdropFactory({ rpc, rpcSubscriptions })({
recipientAddress: keypair.address,
lamports: lamports(1_000_000_000n),
commitment: "confirmed"
});
const accountInfo = await rpc.getAccountInfo(keypair.address).send();
console.log(accountInfo);
Console
Click to execute the code.

Akun sysvar

Akun sysvar adalah akun khusus pada alamat yang telah ditentukan sebelumnya yang menyediakan akses read-only ke data state cluster. Akun ini diperbarui secara dinamis setiap slot.

SysvarAlamatTujuan
ClockSysvarC1ock11111111111111111111111111111111Slot, epoch, dan timestamp Unix saat ini
EpochScheduleSysvarEpochSchedu1e111111111111111111111111Konstanta penjadwalan epoch yang ditetapkan di genesis
EpochRewardsSysvarEpochRewards1111111111111111111111111Status dan progres distribusi reward epoch
RentSysvarRent111111111111111111111111111111111Tarif rent dan ambang batas pembebasan
SlotHashesSysvarS1otHashes111111111111111111111111111Hash terbaru dari bank parent slot
StakeHistorySysvarStakeHistory1111111111111111111111111Aktivasi dan deaktivasi stake per epoch
LastRestartSlotSysvarLastRestartS1ot1111111111111111111111Slot restart cluster terakhir
InstructionsSysvar1nstructions1111111111111111111111111Instruksi terserialisasi dari transaksi saat ini
SlotHistorySysvarS1otHistory11111111111111111111111111Catatan slot mana yang diproduksi selama epoch terakhir

Contoh berikut mengambil dan mendeserialisasi akun Sysvar Clock.

import { createSolanaRpc } from "@solana/kit";
import { fetchSysvarClock, SYSVAR_CLOCK_ADDRESS } from "@solana/sysvars";
const rpc = createSolanaRpc("https://api.mainnet.solana.com");
const accountInfo = await rpc
.getAccountInfo(SYSVAR_CLOCK_ADDRESS, { encoding: "base64" })
.send();
console.log(accountInfo);
// Automatically fetch and deserialize the account data
const clock = await fetchSysvarClock(rpc);
console.log(clock);
Console
Click to execute the code.

Is this page helpful?

Daftar Isi

Edit Halaman

Dikelola oleh

© 2026 Yayasan Solana.
Semua hak dilindungi.
Terhubung