> ## 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.

# Các hàm SVM

> Các hàm dành cho Solana và các blockchain tương thích SVM

Các hàm này khả dụng khi sử dụng addon SVM cho Solana và các
blockchain tương thích SVM.

## system_program_id

`svm::system_program_id` trả về id của system program,
`11111111111111111111111111111111`.

```hcl
output "system_program_id" {
    value = svm::system_program_id()
}
// > 11111111111111111111111111111111
```

---

## default_pubkey

`svm::default_pubkey` trả về một khóa công khai mặc định,
`11111111111111111111111111111111`.

```hcl
output "default_pubkey" {
    value = svm::default_pubkey()
}
// > 11111111111111111111111111111111
```

---

## get_instruction_data_from_idl_path

`svm::get_instruction_data_from_idl_path` tạo instruction data đã được mã hóa cho một
lần gọi chương trình, đồng thời kiểm tra kiểu và tuần tự hóa dựa trên
tệp IDL được cung cấp.

### Đầu vào

| Tên               | Bắt buộc | Kiểu          | Mô tả                                                 |
| ------------------ | -------- | ------------- | ----------------------------------------------------------- |
| `idl_path`         | bắt buộc | string        | Đường dẫn, tính từ txtx.yml, đến tệp IDL `.json` |
| `instruction_name` | bắt buộc | string        | Tên của instruction cần tạo dữ liệu            |
| `arguments`        | tùy chọn | array[string] | Các đối số của instruction cần tạo dữ liệu              |

```hcl
output "data" {
    value = svm::get_instruction_data_from_idl_path("idl.json", "my_instruction", ["arg1", "arg2"])
}
// > data: 0xc3f1b80e7f9b443504000000617267310400000061726732
```

Với IDL của Anchor, dữ liệu gồm discriminator 8 byte của instruction (ở đây là
`c3f1b80e7f9b4435`, 8 byte đầu tiên của `sha256("global:my_instruction")`)
theo sau là các đối số được mã hóa Borsh: mỗi chuỗi gồm độ dài 4 byte little-endian
và các byte UTF-8 của chuỗi đó.

---

## get_instruction_data_from_idl

`svm::get_instruction_data_from_idl` tạo instruction data đã được mã hóa cho một
lần gọi chương trình, đồng thời kiểm tra kiểu và tuần tự hóa dựa trên
dữ liệu IDL được cung cấp.

### Đầu vào

| Tên               | Bắt buộc | Kiểu              | Mô tả                                                             |
| ------------------ | -------- | ----------------- | ----------------------------------------------------------------------- |
| `idl`              | bắt buộc | string \| SVM_IDL | IDL của chương trình                                                         |
| `instruction_name` | bắt buộc | string            | Tên của instruction cần tạo dữ liệu, theo chỉ mục trong IDL |
| `arguments`        | tùy chọn | array[string]     | Các đối số của instruction cần tạo dữ liệu                          |

```hcl
output "data" {
    value = svm::get_instruction_data_from_idl(variable.contract.idl, "my_instruction", ["arg1", "arg2"])
}
// > data: 0xc3f1b80e7f9b443504000000617267310400000061726732
```

---

## get_program_from_anchor_project

`svm::get_program_from_anchor_project` truy xuất các artifact triển khai
của một chương trình trong dự án Anchor.

### Đầu vào

| Tên           | Bắt buộc | Kiểu   | Mô tả                              |
| -------------- | -------- | ------ | ---------------------------------------- |
| `program_name` | bắt buộc | string | Tên của chương trình đang được triển khai   |
| `keypair_path` | tùy chọn | string | Vị trí của tệp keypair của chương trình |
| `idl_path`     | tùy chọn | string | Vị trí của tệp IDL của chương trình     |
| `bin_path`     | tùy chọn | string | Vị trí của tệp nhị phân của chương trình  |

```hcl
variable "contract" {
    value = svm::get_program_from_anchor_project("my_program")
}
output "idl" {
    value = variable.contract.idl
}
```

---

## get_program_from_native_project

`svm::get_program_from_native_project` truy xuất các artifact triển khai
của một chương trình không dùng Anchor.

### Đầu vào

| Tên           | Bắt buộc | Kiểu   | Mô tả                              |
| -------------- | -------- | ------ | ---------------------------------------- |
| `program_name` | bắt buộc | string | Tên của chương trình đang được triển khai   |
| `keypair_path` | tùy chọn | string | Vị trí của tệp keypair của chương trình |
| `idl_path`     | tùy chọn | string | Vị trí của tệp IDL của chương trình     |
| `bin_path`     | tùy chọn | string | Vị trí của tệp nhị phân của chương trình  |

```hcl
variable "contract" {
    value = svm::get_program_from_native_project("my_program")
}
output "idl" {
    value = variable.contract.idl
}
```

---

## sol_to_lamports

`svm::sol_to_lamports` chuyển đổi số lượng SOL được cung cấp sang lamports.

```hcl
output "lamports" {
    value = svm::sol_to_lamports(1.1)
}
// lamports: 1100000000
```

---

## lamports_to_sol

`svm::lamports_to_sol` chuyển đổi số lượng lamports được cung cấp sang SOL.

```hcl
output "sol" {
    value = svm::lamports_to_sol(1100000000)
}
// sol: 1.1
```

---

## find_pda

`svm::find_pda` tìm một PDA hợp lệ bằng program id và các seed được cung cấp.

### Đầu vào

| Tên         | Bắt buộc | Kiểu          | Mô tả                                        |
| ------------ | -------- | ------------- | -------------------------------------------------- |
| `program_id` | bắt buộc | string        | Địa chỉ của chương trình mà PDA được dẫn xuất từ đó |
| `seeds`      | tùy chọn | array[string] | Mảng seed tùy chọn (tối đa 16 seed, mỗi seed 32 byte) |

```hcl
variable "pda" {
    value = svm::find_pda("3bv3j4GvMPjvvBX9QdoX27pVoWhDSXpwKZipFF1QiVr6", ["data"])
}
output "pda" {
    value = variable.pda.pda
}
output "bump" {
    value = variable.pda.bump_seed
}
// > pda: AAU8CSawL7uBQLt4QHdTtwsNuRQthNtYzhMVNx2bwuGC
// > bump: 254
```

---

## get_associated_token_account

`svm::get_associated_token_account` tính địa chỉ của associated token
account cho địa chỉ ví và địa chỉ token mint được cung cấp.

### Đầu vào

| Tên                 | Bắt buộc | Kiểu   | Mô tả                   |
| -------------------- | -------- | ------ | ----------------------------- |
| `wallet_address`     | bắt buộc | string | Địa chỉ của ví     |
| `token_mint_address` | tùy chọn | string | Địa chỉ của token mint |

```hcl
variable "token_account" {
    value = svm::get_associated_token_account(signer.caller.address, "So11111111111111111111111111111111111111112")
}
```

---

## create_token_account_instruction

`svm::create_token_account_instruction` tạo các byte instruction thô để tạo
một associated token account.

### Đầu vào

| Tên                 | Bắt buộc | Kiểu   | Mô tả                        |
| -------------------- | -------- | ------ | ---------------------------------- |
| `funding_address`    | bắt buộc | string | Địa chỉ của tài khoản cấp vốn |
| `wallet_address`     | bắt buộc | string | Địa chỉ của ví          |
| `token_mint_address` | tùy chọn | string | Địa chỉ của token mint      |
| `token_program_id`   | tùy chọn | string | Địa chỉ của token program   |

```hcl
action "call" "svm::process_instructions" {
    signers = [signer.caller]

    instruction {
        raw_bytes = svm::create_token_account_instruction(
            signer.caller.address, // funding address
            signer.caller.address, // wallet address
            variable.token_mint, // token mint address
            variable.token_program // token program id
        )
    }
}
```

---

## u64

`svm::u64` tạo biểu diễn mảng byte của một số nguyên u64, phù hợp để
dùng làm seed khi dẫn xuất PDA.

### Đầu vào

| Tên    | Bắt buộc | Kiểu    | Mô tả                                |
| ------- | -------- | ------- | ------------------------------------------ |
| `value` | bắt buộc | integer | Số nguyên u64 cần chuyển đổi thành mảng byte |

```hcl
variable "pda" {
    value = svm::find_pda("3bv3j4GvMPjvvBX9QdoX27pVoWhDSXpwKZipFF1QiVr6", [svm::u64(1000000000)])
}
```

---

## i64

`svm::i64` tạo biểu diễn mảng byte của một số nguyên i64, phù hợp để
dùng làm seed khi dẫn xuất PDA.

### Đầu vào

| Tên    | Bắt buộc | Kiểu    | Mô tả                                |
| ------- | -------- | ------- | ------------------------------------------ |
| `value` | bắt buộc | integer | Số nguyên i64 cần chuyển đổi thành mảng byte |

```hcl
variable "pda" {
    value = svm::find_pda("3bv3j4GvMPjvvBX9QdoX27pVoWhDSXpwKZipFF1QiVr6", [svm::i64(-1000000000)])
}
```
