Q: 假如你不需要一个 Canister 了,可以卸载它。但怎么回收剩余的 cycles?请写明详细操作步骤。 A:
- 准备一个能接受 cycles 的 canister, 比如 cycles_wallet,或者新建一个 canister,里面新建一个 public update function,比如
import Nat "mo:base/Nat";
import Nat64 "mo:base/Nat64";
import Cycles "mo:base/ExperimentalCycles";
public func wallet_receive() : async { accepted: Nat64 } {
let available = Cycles.available();
let accepted = Cycles.accept(available);
{ accepted = Nat64.fromNat(accepted) };
};
- 更新需要删除的 canister 的代码,同样新建一个 public update function,比如
import Nat "mo:base/Nat";
import Nat64 "mo:base/Nat64";
import Cycles "mo:base/ExperimentalCycles";
public func transfer(receiver : shared () -> async () : async { refunded : Nat } {
let amount = Cycles.balance();
Cycles.add(amount);
await receiver();
{ refunded = Cycles.refunded() };
};
dfx canister --no-wallet --network ic call <canister to be deleted> transfer '(func "<canister id to recieve>".wallet_receive)'
则所有的cycles从要删除的canister转移到receive上