Tài liệu SolanaLiteSVMTypeScriptTài liệu tham khảo API

Quản Lý Tài Khoản

Các phương thức quản lý tài khoản, số dư và thiết lập trạng thái tài khoản.

airdrop

airdrop(address: Address, lamports: Lamports): TransactionMetadata | FailedTransactionMetadata | null

Nạp lamport vào một tài khoản.

import { lamports } from "@solana/kit";
// Airdrop 5 SOL
const result = client.svm.airdrop(account.address, lamports(5_000_000_000n));
if (result) {
// Note: Result properties are getter functions
console.log("Compute units:", result.computeUnitsConsumed());
}

setAccount

setAccount(account: EncodedAccount): LiteSVM

Thiết lập trạng thái tài khoản trực tiếp mà không cần giao dịch.

import { address, lamports } from "@solana/kit";
client.svm.setAccount({
address: myAddress,
data: new Uint8Array([1, 2, 3, 4]),
executable: false,
lamports: lamports(1_000_000n),
programAddress: address("11111111111111111111111111111111"),
space: 4n
});

Thuộc Tính EncodedAccount

Thuộc tínhKiểuMô tả
addressAddresspubkey của tài khoản
dataUint8ArrayDữ liệu byte của tài khoản
executablebooleanTài khoản có thể thực thi hay không
lamportsLamportsSố dư SOL
programAddressAddressChương trình sở hữu
spacebigintKích thước dữ liệu tính bằng byte

getAccount

getAccount(address: Address): MaybeEncodedAccount

Truy xuất dữ liệu tài khoản.

const account = client.svm.getAccount(myAddress);
if (account.exists) {
console.log("Address:", account.address);
console.log("Lamports:", account.lamports);
console.log("Owner:", account.programAddress);
console.log("Data:", account.data);
console.log("Executable:", account.executable);
}

getBalance

getBalance(address: Address): Lamports | null

Lấy số dư SOL của tài khoản. Trả về null nếu tài khoản không tồn tại.

const balance = client.svm.getBalance(account.address);
if (balance !== null) {
console.log("Balance:", balance, "lamports");
console.log("Balance:", Number(balance) / 1e9, "SOL");
} else {
console.log("Account does not exist");
}

minimumBalanceForRentExemption

minimumBalanceForRentExemption(dataLen: bigint): bigint

Tính toán số lamport tối thiểu cần thiết để được miễn rent.

const dataSize = 100n;
const minBalance = client.svm.minimumBalanceForRentExemption(dataSize);
console.log(
`Rent-exempt minimum for ${dataSize} bytes:`,
minBalance,
"lamports"
);
// Use when setting up accounts
client.svm.setAccount({
address: myAddress,
data: new Uint8Array(Number(dataSize)),
executable: false,
lamports: lamports(minBalance),
programAddress: programId,
space: dataSize
});

Quản Lý Chương Trình

addProgram

addProgram(programId: Address, programBytes: Uint8Array): LiteSVM

Tải một chương trình từ dữ liệu byte.

const programBytes = fs.readFileSync("path/to/program.so");
client.svm.addProgram(programId, programBytes);

addProgramFromFile

addProgramFromFile(programId: Address, path: string): LiteSVM

Tải một chương trình từ đường dẫn tệp .so.

import { generateKeyPairSigner } from "@solana/kit";
const programId = await generateKeyPairSigner().then((s) => s.address);
client.svm.addProgramFromFile(
programId,
"/path/to/target/deploy/my_program.so"
);

Thiết Lập Tài Khoản Hàng Loạt

Để kiểm thử, bạn có thể thiết lập nhiều tài khoản một cách hiệu quả:

// Set up multiple test accounts
const accounts = await Promise.all(
Array.from({ length: 10 }, () => generateKeyPairSigner())
);
for (const [index, account] of accounts.entries()) {
client.svm.setAccount({
address: account.address,
data: new Uint8Array([index]),
executable: false,
lamports: lamports(1_000_000n),
programAddress: address("11111111111111111111111111111111"),
space: 1n
});
}

Sử dụng setAccount nhanh hơn airdrop khi thiết lập tài khoản hàng loạt vì nó không thực thi giao dịch.

Is this page helpful?

Mục lục

Chỉnh sửa trang