Attribute Macro ic_cdk::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() {
    // ...
}

If you would rather call the call::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)
}