> ## Documentation Index
> Fetch the complete documentation index at: https://solana.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> This page is part of the Solana developer documentation at https://solana.com/developers/docs. Append `.md` to any docs URL, or send `Accept: text/markdown`, to get the page as Markdown.
> Index of all pages: https://solana.com/llms.txt. Per-section indexes such as https://solana.com/developers/docs/payments/llms.txt are smaller; prefer them when working on one task.
> Coding agents: install the official Solana skill for current APIs and conventions with `npx skills add https://github.com/solana-foundation/solana-dev-skill` (also served at https://solana.com/SKILL.md).
> Code in the cookbook and task pages comes from tested example files. Prefer it over writing API calls from memory.

# Quản lý tài khoản

> Các phương thức quản lý tài khoản, số dư và rent trong Go

Các phương thức để nạp tiền, đọc và ghi tài khoản.

## Airdrop

```go
func (s *LiteSVM) Airdrop(pubkey solana.PublicKey, lamports uint64) error
```

Nạp lamport vào một tài khoản. Được triển khai dưới dạng giao dịch chuyển system-program thực sự
từ faucet nội bộ, nên tài khoản nhận phải có khả năng giữ số dư
kết quả.

```go
priv, err := solana.NewRandomPrivateKey()
if err != nil {
    t.Fatal(err)
}
payer := priv.PublicKey()

// Airdrop 5 SOL
if err := svm.Airdrop(payer, 5_000_000_000); err != nil {
    t.Fatal(err)
}
```

## Số dư

```go
func (s *LiteSVM) Balance(pubkey solana.PublicKey) (uint64, bool, error)
```

Trả về số dư SOL của tài khoản. Giá trị thứ hai là `false` khi tài khoản
không tồn tại - khác với "tài khoản tồn tại với 0 lamport".

```go
lamports, ok, err := svm.Balance(payer)
if err != nil {
    t.Fatal(err)
}
if !ok {
    t.Log("account does not exist")
} else {
    t.Logf("balance: %d lamports (%.3f SOL)", lamports, float64(lamports)/1e9)
}
```

## GetAccount

```go
func (s *LiteSVM) GetAccount(pubkey solana.PublicKey) *solana.Account
```

Lấy một bản sao của tài khoản, hoặc `nil` nếu tài khoản không tồn tại.
`*solana.Account` là một struct thuần của
[`gagliardetto/solana-go`](https://github.com/gagliardetto/solana-go), vì vậy không
có gì cần đóng, và việc thay đổi giá trị được trả về sẽ không thay đổi trạng thái
SVM cho đến khi bạn truyền nó vào `SetAccount`.

```go
acct := svm.GetAccount(payer)
if acct == nil {
    t.Fatal("account missing")
}

_ = acct.Lamports   // uint64
_ = acct.Owner      // solana.PublicKey
_ = acct.Executable // bool
_ = acct.RentEpoch  // uint64
_ = acct.Data       // []byte (copy)
```

### Các trường của tài khoản

| Trường        | Kiểu               | Mô tả                       |
| ------------ | ------------------ | --------------------------------- |
| `Lamports`   | `uint64`           | Số dư SOL                       |
| `Owner`      | `solana.PublicKey` | Chương trình sở hữu                    |
| `Executable` | `bool`             | Tài khoản có thể thực thi hay không |
| `RentEpoch`  | `uint64`           | Rent epoch                        |
| `Data`       | `[]byte`           | Bản sao dữ liệu tài khoản          |

## NewAccount

```go
func NewAccount(lamports uint64, data []byte, owner solana.PublicKey, executable bool, rentEpoch uint64) (*solana.Account, error)
```

Tạo một `*solana.Account` để truyền vào `SetAccount`. Kết quả lỗi luôn là
`nil`; một composite literal như
`&solana.Account{Lamports: rent, Data: payload, Owner: ownerProgramID}` cũng
có tác dụng tương tự.

```go
payload := []byte{1, 2, 3, 4}
rent, err := svm.MinimumBalanceForRentExemption(len(payload))
if err != nil {
    t.Fatal(err)
}

acct, err := litesvm.NewAccount(rent, payload, ownerProgramID, false, 0)
if err != nil {
    t.Fatal(err)
}
t.Logf("account: %d lamports, owner %s", acct.Lamports, acct.Owner)
```

## SetAccount

```go
func (s *LiteSVM) SetAccount(pubkey solana.PublicKey, acct *solana.Account) error
```

Lưu một bản sao của `acct` tại `pubkey`, thay thế mọi tài khoản hiện có. SVM giữ
bản sao dữ liệu riêng của nó. Việc ghi vào địa chỉ sysvar sẽ làm mới sysvar đó, và
tài khoản có 0 lamport sẽ bị xóa thay vì được lưu.

```go
if err := svm.SetAccount(targetAddr, acct); err != nil {
    t.Fatal(err)
}
```

<Info>
  `SetAccount` nhanh hơn đáng kể so với việc thực thi giao dịch khi bạn chỉ
  cần khởi tạo sẵn nhiều tài khoản cho một bài kiểm thử. Hãy đảm bảo giá trị lamport
  tối thiểu bằng mức tối thiểu để được miễn rent cho kích thước dữ liệu.
</Info>

## MinimumBalanceForRentExemption

```go
func (s *LiteSVM) MinimumBalanceForRentExemption(dataLen int) (uint64, error)
```

Tính số lamport cần thiết để một tài khoản có độ dài dữ liệu cho trước được
miễn rent.

```go
for _, size := range []int{0, 100, 1024} {
    min, err := svm.MinimumBalanceForRentExemption(size)
    if err != nil {
        t.Fatal(err)
    }
    t.Logf("%4d bytes -> %d lamports", size, min)
}
```

## Quản lý chương trình

### AddProgram

```go
func (s *LiteSVM) AddProgram(programID solana.PublicKey, programBytes []byte) error
```

Tải một chương trình SBF từ các byte trong bộ nhớ.

```go
bytes, err := os.ReadFile("./target/deploy/my_program.so")
if err != nil {
    t.Fatal(err)
}
if err := svm.AddProgram(programID, bytes); err != nil {
    t.Fatal(err)
}
```

### AddProgramFromFile

```go
func (s *LiteSVM) AddProgramFromFile(programID solana.PublicKey, path string) error
```

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

```go
if err := svm.AddProgramFromFile(programID, "./target/deploy/my_program.so"); err != nil {
    t.Fatal(err)
}
```

### AddProgramWithLoader

```go
func (s *LiteSVM) AddProgramWithLoader(programID solana.PublicKey, programBytes []byte, loader solana.PublicKey) error
```

Tải một chương trình SBF bằng một loader cụ thể. Hữu ích khi kiểm thử với
phiên bản BPF loader không phải mặc định.

## Thiết lập hàng loạt tài khoản

Đối với các bài kiểm thử cần khởi tạo sẵn nhiều tài khoản, hãy lặp qua `SetAccount`:

```go
const N = 10
for i := 0; i < N; i++ {
    pk := solana.NewWallet().PublicKey()
    data := []byte{byte(i)}
    rent, err := svm.MinimumBalanceForRentExemption(len(data))
    if err != nil {
        t.Fatal(err)
    }

    acct, err := litesvm.NewAccount(rent, data, owner, false, 0)
    if err != nil {
        t.Fatal(err)
    }
    if err := svm.SetAccount(pk, acct); err != nil {
        t.Fatal(err)
    }
}
```

<Info>
  `SetAccount` sao chép dữ liệu tài khoản, vì vậy bạn có thể tái sử dụng hoặc sửa đổi `acct` sau
  khi gọi mà không ảnh hưởng đến SVM.
</Info>
