Struct ic_agent::agent::Agent[][src]

pub struct Agent { /* fields omitted */ }
Expand description

A low level Agent to make calls to a Replica endpoint.

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

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

#[derive(CandidType, Deserialize)]
struct CreateCanisterResult {
  canister_id: candid::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")?;

  let waiter = delay::Delay::builder()
    .throttle(std::time::Duration::from_millis(500))
    .timeout(std::time::Duration::from_secs(60 * 5))
    .build();

  // Create a call to the management canister to create a new canister ID,
  // and wait for a result.
  let response = agent.update(&management_canister_id, "provisional_create_canister_with_cycles")
    .with_arg(&Encode!(&Argument { amount: None })?)
    .call_and_wait(waiter)
    .await?;

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

let canister_id = create_a_canister().await.unwrap();
eprintln!("{}", canister_id);

This agent does not understand Candid, and only acts on byte buffers.

Implementations

impl Agent[src]

pub fn builder() -> AgentBuilder[src]

Create an instance of an AgentBuilder for building an Agent. This is simpler than using the AgentConfig and Agent::new().

pub fn new(config: AgentConfig) -> Result<Agent, AgentError>[src]

Create an instance of an Agent.

pub fn set_transport<F: 'static + ReplicaV2Transport + Send + Sync>(
    &mut self,
    transport: F
)
[src]

Set the transport of the Agent.

pub async fn fetch_root_key(&self) -> Result<(), AgentError>[src]

By default, the agent is configured to talk to the main Internet Computer, and verifies responses using a hard-coded public key.

This function will instruct the agent to ask the endpoint for its public key, and use that instead. This is required when talking to a local test instance, for example.

Only use this when you are not talking to the main Internet Computer, otherwise you are prone to man-in-the-middle attacks! Do not call this function by default.

pub fn set_root_key(&self, root_key: Vec<u8>) -> Result<(), AgentError>[src]

By default, the agent is configured to talk to the main Internet Computer, and verifies responses using a hard-coded public key.

Using this function you can set the root key to a known one if you know if beforehand.

pub async fn read_state_canister_info(
    &self,
    canister_id: Principal,
    path: &str
) -> Result<Vec<u8>, AgentError>
[src]

pub async fn request_status_raw(
    &self,
    request_id: &RequestId,
    effective_canister_id: Principal
) -> Result<RequestStatusResponse, AgentError>
[src]

pub fn update<S: Into<String>>(
    &self,
    canister_id: &Principal,
    method_name: S
) -> UpdateBuilder<'_>
[src]

Returns an UpdateBuilder enabling the construction of an update call without passing all arguments.

pub async fn status(&self) -> Result<Status, AgentError>[src]

Calls and returns the information returned by the status endpoint of a replica.

pub fn query<S: Into<String>>(
    &self,
    canister_id: &Principal,
    method_name: S
) -> QueryBuilder<'_>
[src]

Returns a QueryBuilder enabling the construction of a query call without passing all arguments.

Trait Implementations

impl Clone for Agent[src]

fn clone(&self) -> Agent[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

Auto Trait Implementations

impl !RefUnwindSafe for Agent

impl Send for Agent

impl Sync for Agent

impl Unpin for Agent

impl !UnwindSafe for Agent

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T> Instrument for T[src]

fn instrument(self, span: Span) -> Instrumented<Self>[src]

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

fn in_current_span(self) -> Instrumented<Self>[src]

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

Creates owned data from borrowed data, usually by cloning. Read more

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 

pub fn vzip(self) -> V