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

# SVM 函数

> 适用于 Solana 和 SVM 兼容区块链的函数

在使用适用于 Solana 和 SVM 兼容区块链的 SVM 插件时,可以使用这些函数。

## system_program_id

`svm::system_program_id` 返回 System Program 的 id,即 `11111111111111111111111111111111`。

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

---

## default_pubkey

`svm::default_pubkey` 返回默认公钥,即 `11111111111111111111111111111111`。

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

---

## get_instruction_data_from_idl_path

`svm::get_instruction_data_from_idl_path` 为程序调用创建编码后的 instruction data,并基于所提供的 IDL 文件进行类型检查和序列化。

### 输入

| 名称               | 是否必需 | 类型          | 描述                                                 |
| ------------------ | -------- | ------------- | ----------------------------------------------------------- |
| `idl_path`         | 必需 | string        | IDL `.json` 文件的路径,相对于 txtx.yml |
| `instruction_name` | 必需 | string        | 要为其生成数据的指令名称            |
| `arguments`        | 可选 | array[string] | 要为其生成数据的指令参数              |

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

对于 Anchor IDL,数据由指令的 8 字节 discriminator(此处为 `c3f1b80e7f9b4435`,即 `sha256("global:my_instruction")` 的前 8 个字节)以及经过 Borsh 编码的参数组成:每个字符串由 4 字节小端序长度和其 UTF-8 字节构成。

---

## get_instruction_data_from_idl

`svm::get_instruction_data_from_idl` 为程序调用创建编码后的 instruction data,并基于所提供的 IDL 数据进行类型检查和序列化。

### 输入

| 名称               | 是否必需 | 类型              | 描述                                                             |
| ------------------ | -------- | ----------------- | ----------------------------------------------------------------------- |
| `idl`              | 必需 | string \| SVM_IDL | 程序 IDL                                                         |
| `instruction_name` | 必需 | string            | 要为其生成数据的指令名称,以 IDL 中的索引为准 |
| `arguments`        | 可选 | array[string]     | 要为其生成数据的指令参数                          |

```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` 从 Anchor 项目中获取程序的部署工件。

### 输入

| 名称           | 是否必需 | 类型   | 描述                              |
| -------------- | -------- | ------ | ---------------------------------------- |
| `program_name` | 必需 | string | 要部署的程序名称   |
| `keypair_path` | 可选 | string | 程序 keypair 文件的位置 |
| `idl_path`     | 可选 | string | 程序 IDL 文件的位置     |
| `bin_path`     | 可选 | string | 程序二进制文件的位置  |

```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` 获取非 Anchor 程序的部署工件。

### 输入

| 名称           | 是否必需 | 类型   | 描述                              |
| -------------- | -------- | ------ | ---------------------------------------- |
| `program_name` | 必需 | string | 要部署的程序名称   |
| `keypair_path` | 可选 | string | 程序 keypair 文件的位置 |
| `idl_path`     | 可选 | string | 程序 IDL 文件的位置     |
| `bin_path`     | 可选 | string | 程序二进制文件的位置  |

```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` 将所提供的 SOL 数量转换为 lamports。

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

---

## lamports_to_sol

`svm::lamports_to_sol` 将所提供的 lamports 数量转换为 SOL。

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

---

## find_pda

`svm::find_pda` 使用所提供的程序 id 和 seeds 查找有效的 PDA。

### 输入

| 名称         | 是否必需 | 类型          | 描述                                        |
| ------------ | -------- | ------------- | -------------------------------------------------- |
| `program_id` | 必需 | string        | 派生该 PDA 的程序地址 |
| `seeds`      | 可选 | array[string] | 可选的 seeds 数组(最多 16 个,每个 32 字节) |

```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` 根据所提供的钱包地址和代币 mint 地址,计算 associated token account 的地址。

### 输入

| 名称                 | 是否必需 | 类型   | 描述                   |
| -------------------- | -------- | ------ | ----------------------------- |
| `wallet_address`     | 必需 | string | 钱包地址     |
| `token_mint_address` | 可选 | string | 代币 mint 地址 |

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

---

## create_token_account_instruction

`svm::create_token_account_instruction` 创建用于创建 associated token account 的原始指令字节。

### 输入

| 名称                 | 是否必需 | 类型   | 描述                        |
| -------------------- | -------- | ------ | ---------------------------------- |
| `funding_address`    | 必需 | string | 出资账户的地址 |
| `wallet_address`     | 必需 | string | 钱包地址          |
| `token_mint_address` | 可选 | string | 代币 mint 地址      |
| `token_program_id`   | 可选 | string | 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` 创建 u64 整数的字节数组表示,适合在 PDA 派生中用作 seed。

### 输入

| 名称    | 是否必需 | 类型    | 描述                                |
| ------- | -------- | ------- | ------------------------------------------ |
| `value` | 必需 | integer | 要转换为字节数组的 u64 整数 |

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

---

## i64

`svm::i64` 创建 i64 整数的字节数组表示,适合在 PDA 派生中用作 seed。

### 输入

| 名称    | 是否必需 | 类型    | 描述                                |
| ------- | -------- | ------- | ------------------------------------------ |
| `value` | 必需 | integer | 要转换为字节数组的 i64 整数 |

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