Manage Cycles used by Canisters
Unlike smart contracts in Ethereum and other blockchains, Canisters in Internet Computer are executed in Cycles (opens in a new tab) which are paid by developers, not the end users. Cycles are a measure of computation and storage resources consumed by a Canister. When a new Canister is created, it is assigned a certain number of cycles usually 4TC (4000000000000) deducted from your identity's Cycles Wallet. The number of cycles assigned to a Canister is called the Canister’s balance. The balance of a canister is decremented as the Canister consumes cycles when processing updates ~ transactions.
Note that query to Canisters is free and won't consume cycles.
You can check the balance of a Canister using the following command:
For example, to check the balance of the beam Canister:
dfx canister --network ic status beam
You can also use Canister ID.
dfx canister --network ic status 2ekax-oqaaa-aaaah-qc6qa-cai
When the number of Canisters deployed increases, it becomes difficult to manage the cycles used by each Canister. That is why we developed a Cycles Top Up script (opens in a new tab) program to top up the cycles of each Canister and automated it in GitHub Actions workflow as a scheduled job.
Cycles Top Up script will top up the cycles of all the Canisters as indicated in
canister_ids.json when their Cycles are below the threshold - currently
4TC (4000000000000). It will transfer
2TC (2000000000000) to the Canister. You can run the script manually or schedule it as a job in GitHub Actions (opens in a new tab) like what we do.
The source of Cycles comes from the Cycles Wallet of the dfx identity running the script. It is important to make sure that the Cycles Wallet has enough cycles to top up the Canisters. You can check the balance of the Cycles Wallet using the following command:
dfx wallet --network ic balance
To run the script at
./scripts/topup-canisters.js (opens in a new tab):
Switch to the dfx identity that has enough cycles to top up the Canisters.
For example, to switch to the dfx identity
dfx identity use icprod
Then run the script:
npm run topup
To schedule the
Cycles Top Up script, you can use GitHub Actions workflow. For example, we have a GitHub Actions workflow - .github/workflows/topupprod.yml (opens in a new tab).
All you need to do is to configure GitHub Actions secret
DFX_WALLETS_PROD which you probably already have if you followed the Setup CI/CD pipeline
Cycles Top Up script, it will significantly reduce your DevOps tasks to manage the Cycles of your Canisters. However, you still need to manage the one single Cycles Wallet to make sure it has enough Cycles to top up the Canisters.