Skip to content

Instantly share code, notes, and snippets.

@flyq
Last active November 27, 2021 10:05
Show Gist options
  • Save flyq/d1903f2a43692c352bd03e01c0268923 to your computer and use it in GitHub Desktop.
Save flyq/d1903f2a43692c352bd03e01c0268923 to your computer and use it in GitHub Desktop.

Q: 假如你不需要一个 Canister 了,可以卸载它。但怎么回收剩余的 cycles?请写明详细操作步骤。 A:

  1. 准备一个能接受 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) };
};
  1. 更新需要删除的 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() };
};
  1. dfx canister --no-wallet --network ic call <canister to be deleted> transfer '(func "<canister id to recieve>".wallet_receive)'则所有的cycles从要删除的canister转移到receive上
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment