Function ic_cdk::call

source ·
pub fn call<T: ArgumentEncoder, R: for<'a> ArgumentDecoder<'a>>(
    id: Principal,
    method: &str,
    args: T,
) -> impl Future<Output = CallResult<R>> + Send + Sync
Expand description

Performs an asynchronous call to another canister.

§Example

Assuming that the callee canister has following interface:

service : {
    add_user: (name: text) -> (nat64);
}

It can be called:

async fn call_add_user() -> u64 {
    let (user_id,) = call(callee_canister(), "add_user", ("Alice".to_string(),)).await.unwrap();
    user_id
}

§Note

  • Both argument and return types are tuples even if it has only one value, e.g (user_id,), ("Alice".to_string(),).
  • The type annotation on return type is required. Or the return type can be inferred from the context.
  • The asynchronous call must be awaited in order for the inter-canister call to be made.
  • If the reply payload is not a valid encoding of the expected type T, the call results in RejectionCode::CanisterError error.