Skip to main content

7: Obtaining cycles and deploying your dapp to the mainnet

Beginner
Tutorial

Overview

Cycles are used to measure the resources, such as memory, storage, and compute power, that are used by a canister. When a canister is deployed on the mainnet, cycles are 'charged' for every action that a canister performs.

To obtain cycles, the Internet Computer Protocol's utility token, ICP, can be converted into cycles and transferred into a canister to be used to pay for that canister's consumed resources. Cycles have a fixed price in XDR in order to make canister costs predictable and independent of the price of ICP. One trillion cycles always correspond to one XDR.

Cycles are only associated with canisters and user or developer principals. Canisters require cycles to perform operations and pay for the resources they use. Developers manage the distribution and ownership of cycles through the cycles ledger. The cycles ledger is used to convert ICP tokens into cycles, send cycles to canisters, and create new canisters with cycles.

Option A: Redeeming a cycles faucet coupon

At hackathon events, DFINITY may offer cycles faucet coupons to participants, which can be redeemed for 10T free cycles.

If such cycles faucet coupon was handed out to you, navigate to the website https://faucet.dfinity.org and follow the instructions there.

Option B: Converting ICP tokens to cycles

The typical way to obtain cycles is to convert ICP tokens. You first you need to obtain ICP tokens through a crypto exchange, or they can be received through other activities such as participating in the NNS governance and receiving grants from the DFINITY foundation.

To get your account ID so you know where to send your ICP tokens, run the command:

dfx ledger account-id

Once you have sent some ICP to this account ID, you can verify that they were received by checking the balance with the command:

dfx ledger --network ic balance

After you have your ICP tokens ready, you can convert them into cycles using the command:

dfx cycles convert --amount AMOUNT --network ic

Deploying your dapp to the mainnet

Now that you've obtained some cycles, you can deploy your vite-motoko-react sample project to the mainnet.

First, make sure you have an active connection to the mainnet. In your terminal window, run the command:

dfx ping ic

A successful connection to the mainnet will return an output such as:

{
"certified_height": 57374997 "ic_api_version": "0.18.0" "impl_hash": "d5896681ceac74c83c9473654de75214d5079193294ade3775e89a81270fd0cf" "impl_version": "f8f59f896499f2fef394d8321116f83351c59aa8" "replica_health_status": "healthy" "root_key": [48, 129, 130, 48, 29, 6, 13, 43, 6, 1, 4, 1, 130, 220, 124, 5, 3, 1, 2, 1, 6, 12, 43, 6, 1, 4, 1, 130, 220, 124, 5, 3, 2, 1, 3, 97, 0, 129, 76, 14, 110, 199, 31, 171, 88, 59, 8, 189, 129, 55, 60, 37, 92, 60, 55, 27, 46, 132, 134, 60, 152, 164, 241, 224, 139, 116, 35, 93, 20, 251, 93, 156, 12, 213, 70, 217, 104, 95, 145, 58, 12, 11, 44, 197, 52, 21, 131, 191, 75, 67, 146, 228, 103, 219, 150, 214, 91, 155, 180, 203, 113, 113, 18, 248, 71, 46, 13, 90, 77, 20, 80, 95, 253, 116, 132, 176, 18, 145, 9, 28, 95, 135, 185, 136, 131, 70, 63, 152, 9, 26, 11, 170, 174]
}

Now, you can deploy your dapp to the mainnet by running the following command:

dfx deploy --network ic

In this command, the --network flag specifies which network the dapp should be deployed on. Other options for this flag are --network local and --network playground. Using the flag --network ic is required to deploy your dapp on the mainnet. If this flag is not included, your dapp will only be deployed locally by default.

Deploying to the playground is ICP's equivalent of deploying to a testnet network.

Now, let's use your dapp! To access the dapp's frontend, first you need to get the canister's URL. To get this, run the command:

dfx canister id frontend --network ic

This command will return the canister's URL, which should look like this:

https://5h5yf-eiaaa-aaaaa-qaada-cai.icp0.io

If you open this URL in your web browser, you will see the sample dapp frontend that you saw when you deployed it locally. The only difference is that now your dapp is hosted 100% onchain, does not rely on your local replica to keep running, and you can send this URL to anyone in the world to view your dapp!

Next steps