I have a trouble deploying to `ic` network, something about not enough cycles despite I have much

I successfully deployed my app to localhost, but when I try to deploy to the ic network, I have weird errors:

$  dfx deploy --network ic main
WARN: The default identity is not stored securely. Do not use it to control a lot of cycles/ICP. Create a new identity with `dfx identity new` and use it in mainnet-facing commands with the `--identity` flag
Deploying: CanDBIndex NacDBIndex main order payments pst
Creating canisters...
Creating canister NacDBIndex...
Error: Failed while trying to deploy canisters.
Caused by: Failed while trying to deploy canisters.
  Failed while trying to register all canisters.
    Failed to create canister 'NacDBIndex'.
      The replica returned a replica error: reject code CanisterError, reject message Canister nx4xj-pyaaa-aaaap-qbpyq-cai is out of cycles: requested 3_100_000_000_000 cycles but the available balance is 1_489_868_472_464 cycles and the freezing threshold 2_252_090_640 cycles, error code None

My code nowhere asks for 3_100_000_000_000 cycles. In NacDBIndex initialization code I don’t ask for cycles. I also don’t create any new canisters in NacDBIndex initialization code.

My balance is enough:

$ dfx ledger --network ic balance
1.19786348 ICP

(2.81186473T cycles).

What’s wrong?

When you use dfx deploy your ICP balance is irrelevant. The balance of your cycles wallet matters. You can check it with dfx wallet --network ic balance and I would expect that it has ~1.5T cycles in it.

The 3.1T comes from dfx using this many cycles by default to create a canister. If you want to use a different amount, use --with-cycles <amount>, and don’t forget that 0.1T will be deducted from that amount to pay for canister creation

Thank you, that problem was solved. Now a new problem:

$ dfx deploy --ic --with-cycles 110000000000 frontend
...
Installing canisters...
Module hash 2ee9c1699a02f78de8f3b76d24c2bba488cb857f14d01a3840043124aaf22358 is already installed.
Module hash 8538716649ea18a3b5ca2a2a04b5e5905ea28e00348ecedb23fd1ffc034e39a3 is already installed.
Installing code for canister frontend, with canister ID i6wid-dyaaa-aaaap-qbqba-cai
Error: Failed while trying to deploy canisters.
Caused by: Failed while trying to deploy canisters.
  Failed while trying to install all canisters.
    Failed to install wasm module to canister 'frontend'.
      Failed during wasm installation call: The replica returned a replica error: reject code CanisterError, reject message Canister installation failed with `Canister i6wid-dyaaa-aaaap-qbqba-cai is out of cycles: requested 80_000_590_000 cycles but the available balance is 9_217_014_000 cycles and the freezing threshold 50_250 cycles`, error code None

I added enough cycles to the frontend using --with-cycles, but it does not work anyway. I even tried to delete frontend canister, in order for it to be recreated with the right amount of cycles, but that didn’t help.

I further have:

. .env && dfx canister call --network ic CanDBIndex init "(vec { principal \"ruuoz-anyad-jumcs-huq7s-3eh7h-ja6j2-cmp2n-elv23-tghui-mve6f-xqe\"; principal \"$CANISTER_ID_MAIN\"; principal \"$CANISTER_ID_ORDER\" })"
WARN: The default identity is not stored securely. Do not use it to control a lot of cycles/ICP. Create a new identity with `dfx identity new` and use it in mainnet-facing commands with the `--identity` flag
Error: Failed update call.
Caused by: Failed update call.
  The replica returned a replica error: reject code CanisterReject, reject message Canister installation failed with `Canister izxox-oaaaa-aaaap-qbqbq-cai is out of cycles: requested 80_000_590_000 cycles but the available balance is 10_000_000_000 cycles and the freezing threshold 41_070 cycles`, error code None

How do I override 10_000_000_000 (apparently, the amount passed to an actor constructor of another canister)? I never specified to top up for a so small sum, and have no idea where it goes from.

Looks like 110B wasn’t enough cycles. It costs 100B to create the canister, leaving the newly created canister with 10B. As you can see, it wants to have 80B available to install your wasm. This is because the execution environment reserves the maximum amount of cycles your call could consume and will then free the unused cycles again. I recommend you try to create the canister with at least 200B cycles so that the installation then succeeds

dfx deploy --ic --with-cycles 210000000000 frontend
...

Failed during wasm installation call: The replica returned a replica error: reject code CanisterError, reject message Canister installation failed with [Canister i6wid-dyaaa-aaaap-qbqba-cai is out of cycles: requested 80_000_590_000 cycles but the available balance is 8_434_028_000 cycles and the freezing threshold 50_250 cycles], error code None
also does not work.

Why? Now it seems to pass enough cycles.

If the canister is already created --with-cycles has no effect. You can also use dfx canister deposit-cycles to send the canister some more cycles