从 Rust 代理调用 IC

Calling IC from a Rust agent
Overview

The Rust agent by DFINTY is a simple-to-use library that enables you to build applications and interact with the Internet Computer. It serves as a Rust-based low-level backend for the IC SDK.

DFINTY 的 Rust 代理是一个易于使用的库,使您能够构建应用程序并与互联网计算机交互。 它充当 IC SDK 的基于 Rust 的低级后端。


The ic-agent is a Rust crate that can connect directly to the Internet Computer through the Internet Computer..

ic-agent是一个Rust crate,可以通过Internet计算机直接连接到Internet计算机。

The agent is designed to be compatible with multiple versions of the replica API, and to expose both low-level APIs for communicating with Internet Computer components like the replica and to provide higher-level APIs for communicating with software applications deployed as canisters.

该代理被设计为与多个版本的副本 API 兼容,并公开用于与互联网计算机组件(例如副本)进行通信的低级 API,并提供用于与部署为容器的软件应用程序进行通信的高级 API。

One example of a project that uses the ic-agent is dfx, which you can find here.  https://github.com/dfinity/sdk

  • Rust agent documentation here.  https://docs.rs/ic-agent/latest/ic_agent/
  • Rust agent source code here.       https://github.com/dfinity/agent-rs

The ic-agent is a simple-to-use library that enables you to build applications and interact with the Internet Computer in Rust. It serves as a Rust-based low-level backend for the DFINITY Canister Software Development Kit (SDK) and the command-line execution environment dfx


ic-agent 是一个简单易用的库,使您能够使用 Rust 构建应用程序并与互联网计算机交互。 它充当 DFINITY Canister 软件开发套件 (SDK) 和命令行执行环境 dfx 的基于 Rust 的低级后端


Example

The following example illustrates how to use the Agent interface to send a call to an Internet Computer canister that performs network management operations. In this example, the call to the Internet Computer management canister (aaaaa-aa) creates a placeholder for a new canister by registering a network-specific identifier. The management canister then returns the result in the form of the textual representation of the canister identifier to the caller.


以下示例说明如何使用代理界面向执行网络管理操作的 Internet 计算机容器发送调用。 在此示例中,对 Internet 计算机管理容器 (aaaaa-aa) 的调用通过注册特定于网络的标识符来创建新容器的占位符。 然后,管理容器以容器标识符的文本表示形式将结果返回给调用者。


use ic_agent::{Agent, export::Principal};
use candid::{Encode, Decode, CandidType, Nat};
use serde::Deserialize;

#[derive(CandidType)]
struct Argument {
  amount: Option<Nat>,
}

#[derive(CandidType, Deserialize)]
struct CreateCanisterResult {
  canister_id: Principal,
}

async fn create_a_canister() -> Result<Principal, Box<dyn std::error::Error>> {
  let agent = Agent::builder()
    .with_url(URL)
    .with_identity(create_identity())
    .build()?;
  // Only do the following call when not contacting the IC main net (e.g. a local emulator).
  // This is important as the main net public key is static and a rogue network could return
  // a different key.
  // If you know the root key ahead of time, you can use `agent.set_root_key(root_key);`.
  agent.fetch_root_key().await?;
  let management_canister_id = Principal::from_text("aaaaa-aa")?;

  // Create a call to the management canister to create a new canister ID,
  // and wait for a result.
  // The effective canister id must belong to the canister ranges of the subnet at which the canister is created.
  let effective_canister_id = Principal::from_text("rwlgt-iiaaa-aaaaa-aaaaa-cai").unwrap();
  let response = agent.update(&management_canister_id, "provisional_create_canister_with_cycles")
    .with_effective_canister_id(effective_canister_id)
    .with_arg(Encode!(&Argument { amount: None})?)
    .call_and_wait()
    .await?;

  let result = Decode!(response.as_slice(), CreateCanisterResult)?;
  let canister_id: Principal = result.canister_id;
  Ok(canister_id)
}

let canister_id = create_a_canister().await.unwrap();
eprintln!("{}", canister_id);
从 Rust 代理调用 IC
arkMeta Crypto Network Limited, arkSong 2023年10月9日
标签
登录 留下评论

互联网计算机IC环境下:NFT 开发
NFT development