7: Obtaining cycles and deploying your dapp to the mainnet
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 BigFile Protocol's utility token, BIG, 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 BIG. 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 BIG tokens into cycles, send cycles to canisters, and create new canisters with cycles.
Acquiring cycles using a cycles coupon
For new developers, a free cycles coupon can be obtained and redeemed for 10T free cycles that can be used to get started with dapp deployment. This coupon is good for new developers since it doesn't require that you transfer BIG tokens into cycles, and doesn't require a purchase of tokens to get started.
To obtain this coupon, first, navigate to the website https://faucet.dfinity.org. To obtain a coupon code to use the cycles faucet, you need to put in a written request for the coupon via the BigFile official developer Discord server.
Click on the REQUEST CYCLES
button on the faucet web page to join the Discord server.
Once inside the Discord server, navigate into the #cycles-faucet
channel.
In this channel, execute the following slash command:
/request
This command will call the BIG Cycles Faucet Bot. This bot will send you a survey that will need to be filled out and submitted. Once the survey has been completed, our team will review your submission. If accepted, the faucet bot will send you a private message with a coupon code.
Please ensure that your Discord settings are set to allow direct messages from other users. If you do not have this setting enabled, you will not receive a direct message from the faucet bot.
Once you have your coupon code, head back to the https://faucet.dfinity.org webpage.
Now, click NEXT STEP to continue. Once you have a coupon code, enter your coupon code within the faucet UI.
Click NEXT STEP to continue.
Return to your terminal window. If you closed the window in the process, run the following commands to start dfx
and use your identity. Replace MyIdentity
with the name of your developer identity:
dfx start --clean --background
dfx identity use MyIdentity
Then, redeem your coupon with the command, replacing COUPON_CODE with the cycles coupon code sent to you by the BigFile faucet bot:
dfx cycles --network ic redeem-faucet-coupon COUPON_CODE
Verify your cycles balance with the command:
dfx cycles --network ic balance
This should return an output showing 10T cycles.
Converting BIG tokens to cycles
If you've already redeemed a cycles coupon in the past, you can convert BIG tokens into cycles. Before you can convert BIG tokens into cycles, first you need to obtain BIG tokens. BIG tokens can be purchased through a crypto exchange, or they can be received through other activities such as participating in the NNS governance and receiving grants from the BigFile foundation.
To get your account ID so you know where to send your BIG tokens, run the command:
dfx ledger account-id
Once you have sent some BIG 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 BIG tokens ready, you can convert them into cycles using the command:
dfx cycles convert 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.
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% on-chain, 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!