Attribute Macro update

Source
#[update]
Expand description

Register an update call entry point.

This attribute macro will export a function with name canister_update <name> in the canister module.

ยงExample

#[update]
fn update_function() {
    // ...
}

You can also specify the name of the exported function.

#[update(name = "some_name")]
fn update_function() {
    // ...
}

If you want to hide this method in the Candid generated by export_candid!, you will need to set hidden to true. The entry point still exists in the canister.

#[update(hidden = true)]
fn update_function() {
    // ...
}

You can specify a guard function to be executed before the update function. When the guard function returns an error, the update function will not proceed.

fn guard_function() -> Result<(), String> {
    // ...
}
#[update(guard = "guard_function")]
fn update_function() {
    // ...
}

You can specify a custom function to decode the arguments. The function must take a Vec<u8> as an argument and return the same type as the update arguments.

fn decode_args(arg_bytes: Vec<u8>) -> (u32, u32) {
   // ...
}
#[update(decode_with = "decode_args")]
fn update_function(a: u32, b: u32) {
   // ...
}

You can specify a custom function to encode the return value. The function must take the update return value as an argument and return a Vec<u8>.

fn encode_result(result: (u32, u32)) -> Vec<u8> {
  // ...
}
#[update(encode_with = "encode_result")]
fn update_function() -> (u32, u32) {
 // ...
}

If you would rather call the reply() function than return a value, you will need to set manual_reply to true so that the canister does not trap.

use ic_cdk::api::call::{self, ManualReply};
#[update(manual_reply = true)]
fn update_function() -> ManualReply<MyResult> {
    let result = calculate_result();
    ManualReply::one(result)
}