这些函数在使用 SVM 插件连接 Solana 和 SVM 兼容区块链时可用。
system_program_id
svm::system_program_id 返回 System Program 的 ID,
11111111111111111111111111111111。
output "system_program_id" {value = svm::system_program_id()}// > 11111111111111111111111111111111
default_pubkey
svm::default_pubkey 返回默认的 pubkey, 11111111111111111111111111111111。
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 | 相对于 txtx.yml 的 IDL .json 文件路径 |
instruction_name | 必填 | string | 要生成数据的指令名称 |
arguments | 可选 | array[string] | 要生成数据的指令参数 |
output "data" {value = svm::get_instruction_data_from_idl("/path/to/idl.json", "my_instruction", ["arg1", "arg2"])}// > data: 0x95763bdcc47fa1b305000000776f726c64
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] | 要生成数据的指令参数 |
output "data" {value = svm::get_instruction_data_from_idl(variable.contract.idl, "my_instruction", ["arg1", "arg2"])}// > data: 0x95763bdcc47fa1b305000000776f726c64
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 | 程序二进制文件的位置 |
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 | 程序二进制文件的位置 |
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 数量转换为 lamport。
output "lamports" {value = svm::sol_to_lamports(1.1)}// lamports: 1100000000
lamports_to_sol
svm::lamports_to_sol 将提供的 lamport 数量转换为 SOL。
output "sol" {value = svm::lamports_to_sol(1100000000)}// sol: 1.1
find_pda
svm::find_pda 使用提供的程序 ID 和种子查找有效的 PDA。
输入
| 名称 | 必填 | 类型 | 描述 |
|---|---|---|---|
program_id | 必填 | string | 派生 PDA 所依据的程序地址 |
seeds | 可选 | array[string] | 可选的种子数组(最多 16 个,每个最大 32 字节) |
variable "pda" {value = svm::find_pda("3bv3j4GvMPjvvBX9QdoX27pVoWhDSXpwKZipFF1QiVr6", ["data"])}output "pda" {value = std::encode_base58(variable.pda.pda)}output "bump" {value = variable.pda.bump_seed}// > pda: 4amHoWMBgLkPfM8Nq9ZP33Liq9FCuqrLoU1feejkdsUJ// > bump: 252
get_associated_token_account
svm::get_associated_token_account
计算给定钱包地址和代币铸造地址对应的 associated token account 的地址。
输入参数
| 名称 | 是否必填 | 类型 | 描述 |
|---|---|---|---|
wallet_address | 必填 | string | 钱包地址 |
token_mint_address | 可选 | string | 代币铸造地址 |
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 | 代币铸造地址 |
token_program_id | 可选 | string | token program 地址 |
action "call" "svm::process_instructions" {signers = [signer.caller]instruction {raw_bytes = svm::create_token_account_instruction(signer.caller.address, // funding addresssigner.caller.address, // wallet addressvariable.token_mint, // token mint addressvariable.token_program // token program id)}}
u64
svm::u64 将 u64 整数转换为字节数组表示,适用于在 PDA 派生中作为 seed 使用。
输入参数
| 名称 | 是否必填 | 类型 | 描述 |
|---|---|---|---|
value | 必填 | integer | 要转换为字节数组的 u64 整数 |
variable "pda" {value = svm::find_pda("3bv3j4GvMPjvvBX9QdoX27pVoWhDSXpwKZipFF1QiVr6", [svm::u64(1000000000)])}
i64
svm::i64 将 i64 整数转换为字节数组表示,适用于在 PDA 派生中作为 seed 使用。
输入参数
| 名称 | 是否必填 | 类型 | 描述 |
|---|---|---|---|
value | 必填 | integer | 要转换为字节数组的 i64 整数 |
variable "pda" {value = svm::find_pda("3bv3j4GvMPjvvBX9QdoX27pVoWhDSXpwKZipFF1QiVr6", [svm::i64(-1000000000)])}
Is this page helpful?