为 Rust canisters生成 Candid 文件

Generating Candid files for Rust canisters
Overview

In versions of the Rusk CDK v0.11.0 and higher, the Candid export workflow has been changed. You can call the ic_cdk::export_candid!() macro to enable the Candid export behavior, then use the candid-extractor to extract the Candid from the canister's Wasm.

在 Rusk CDK v0.11.0 及更高版本中,Candid 导出工作流程已更改。 您可以调用 ic_cdk::export_candid!() 宏来启用 Candid 导出行为,然后使用 candid-extractor 从canister容器的 Wasm 中提取 Candid。

Preparation
  • Step 1: Upgrade ic-cdk dependency to v0.11.0 or higher:
# Cargo.toml
[dependencies]
ic-cdk = "0.11.0"
cargo install candid-extractor

With cargo-binstall you can install the prebuilt binary without waiting for compilation:

cargo binstall candid-extractor
Workflow
  • Step 1. Call the export_candid macro at the end of your lib.rs file:
#[query]
fn hello(name: String) -> String {
format!("Hello, {}!", name)
}

#[update]
fn world(name: String) -> String {
format!("World, {}!", name)
}

// Enable Candid export
ic_cdk::export_candid!();
  • Step 2: Compile the Canister Wasm module.
cargo build --release --target wasm32-unknown-unknown --package <CANISTER>

The Wasm module will be at:

target/wasm32-unknown-unknown/release/<CANISTER>.wasm
  • Step 3: Extract candid from the Wasm module and save to a file:
candid-extractor target/wasm32-unknown-unknown/release/<CANISTER>.wasm > <CANISTER>.did
为 Rust canisters生成 Candid 文件
arkMeta Crypto Network Limited, arkSong 2023年10月10日
标签
登录 留下评论

Candid简介与使用方法