Thanks for the solution and explanation @AdamS, it did the job.
canister_balance128
is also a good tips, I’ll use it. I was using canister_status
because I am still hoping that somewhere there is an information about the minimal amount of cycles to retain before deleting a canister.
If it can be useful in the future for anyone landing on this, here the implementation of above elements:
use ic_cdk::api::management_canister::main::{ CanisterIdRecord, deposit_cycles };
use ic_cdk::api::{ canister_balance128 };
#[update]
async fn transfer_cycles() {
// TODO: determine effective threshold - how few cycles should be retained before deleting the canister?
// use freezing_threshold_in_cycles? - https://github.com/dfinity/interface-spec/pull/18/files
// actually above PR was ultimately deleted? - https://forum.dfinity.org/t/minimal-cycles-to-delete-canister/15926
// Source: https://forum.dfinity.org/t/candid-nat-to-u128/16016
let balance: u128 = canister_balance128();
let cycles: u128 = balance - 100_000_000_000u128;
if cycles > 0 {
let arg_deposit = CanisterIdRecord { canister_id: caller };
deposit_cycles(arg_deposit, cycles).await.unwrap();
}
}