|
| 1 | +--- |
| 2 | +title: 'Developing a frontend with DevNet' |
| 3 | +description: 'Integrate a frontend using a locally running blockchain with Clarinet DevNet' |
| 4 | +--- |
| 5 | + |
| 6 | +## Introduction |
| 7 | + |
| 8 | +Once you have reached a point where your Clarity smart contract is functional, you may want to develop a web frontend |
| 9 | +against your contract. This can be challenging, as the contract must be deployed to a live blockchain to fully |
| 10 | +interact with it from a web application. Clarinet provides an easy method to deploy your contract to a blockchain that |
| 11 | +runs locally on your machine that is configurable and controllable. This integration feature is called DevNet. |
| 12 | + |
| 13 | +DevNet allows you to perform frontend development and integration testing without the need to deploy your contract to |
| 14 | +public testnet. This is valuable if you are in the early stages of developing a product, or if you are developing a |
| 15 | +contract and application in stealth. DevNet uses Docker to launch local instances of Bitcoin, Stacks, Stacks API, Stacks |
| 16 | +Explorer, and Bitcoin Explorer, and provides total configuration control over all those instances. Once running, DevNet |
| 17 | +automatically deploys your contracts and creates Stacks accounts with pre-defined balances. |
| 18 | + |
| 19 | +The services launched by DevNet represent a full instance of the Stacks blockchain with the Proof of Transfer consensus |
| 20 | +mechanism running against a locally running Bitcoin testnet. DevNet allows you to control block times, PoX transactions, |
| 21 | +and contract deployments. Because DevNet is running locally, it can be reset or re-configured at any time. This allows |
| 22 | +for rapid frontend development without the need to interact with the public blockchain. |
| 23 | + |
| 24 | +## Prerequisites |
| 25 | + |
| 26 | +In order to run DevNet, you must have [Clarinet installed][], and you also should have Docker installed locally. Refer |
| 27 | +to the [Docker documentation][] for instructions on installing Docker on your development machine. |
| 28 | + |
| 29 | +## Launching DevNet |
| 30 | + |
| 31 | +Clarinet provides sensible a sensible default configuration for DevNet. If you wish to use the default configuration, |
| 32 | +you can launch DevNet from the root of your Clarinet project with the command: |
| 33 | + |
| 34 | +```sh |
| 35 | +clarinet integrate |
| 36 | +``` |
| 37 | + |
| 38 | +Clarinet fetches the appropriate Docker images for the Bitcoin node, Stacks node, Stacks API node, and the Bitcoin |
| 39 | +and Stacks Explorers. This can take several minutes on first launch. Once the images are launched, the DevNet interface |
| 40 | +is displayed in your terminal window. The contracts in your project are deployed to the DevNet blockchain in the second |
| 41 | +block of the chain, so you may need to wait for the third block before launching your frontend development environment. |
| 42 | + |
| 43 | +Review the following sections for information about the DevNet interface and configuration options for DevNet. |
| 44 | + |
| 45 | +## DevNet interface |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +The DevNet interface is displayed as a terminal GUI and consists of four primary panels: the system log, service status, |
| 50 | +mempool summary, and a minimal block explorer. |
| 51 | + |
| 52 | +The system log provides a log of events happening throughout the DevNet stack. You can use this log to monitor the |
| 53 | +health of the local blockchain and review any events that occur. For services that provide a web interface, the URL |
| 54 | +for the local service is displayed next to the container name. You can connect to these URLs using a web browser to |
| 55 | +access the service. |
| 56 | + |
| 57 | +The service status provides a status summary for the Docker containers that make up the DevNet stack. A green icon next |
| 58 | +to the container indicates that it is in a healthy state, a yellow icon indicates that the container is booting, and a |
| 59 | +red icon indicates that there is a problem with the service. |
| 60 | + |
| 61 | +The mempool summary displays a list of transactions in the mempool. These include historical transactions from the |
| 62 | +beginning of the blockchain. |
| 63 | + |
| 64 | +The block explorer has two sub-panels: the block summary and the block transactions. You can use the `Arrow` keys to |
| 65 | +select a block within the chain (shown at the top of the block explorer), and the block summary and block transactions |
| 66 | +panels display information about that block. The block summary displays the Stacks block height, the Stacks block hash, |
| 67 | +the Bitcoin block height of the anchor block, and the PoX cycle number of the block. The block transactions panel |
| 68 | +displays all Stacks transactions that were included in the block. |
| 69 | + |
| 70 | +You can access the locally running Stacks Explorer and Bitcoin Explorer from the URLs in the service status window for |
| 71 | +more detailed information about the blocks. |
| 72 | + |
| 73 | +You can press `0` in the interface to reset the DevNet. Press `Ctrl` + `C` to stop the DevNet and shut down the |
| 74 | +containers. |
| 75 | + |
| 76 | +## Configuring DevNet |
| 77 | + |
| 78 | +By default, DevNet launches a local Stacks 2.0 testnet with a fixed block time of 30 seconds. It runs Docker images |
| 79 | +that host a Bitcoin node, a Stacks Node, the Stacks API, the Stacks Explorer, and the Bitcoin Explorer. The default |
| 80 | +settings should be adequate for most developers, but you can change many of the settings to customize your |
| 81 | +development environment. |
| 82 | + |
| 83 | +DevNet settings are located in the `settings/Devnet.toml` file. The file defines the wallets that are created in the |
| 84 | +DevNet blockchain, the Stacks miner configuration, Proof of Transfer activity, and many other options. |
| 85 | + |
| 86 | +### Accounts configuration |
| 87 | + |
| 88 | +By default, Clarinet generates 10 wallets in the DevNet configuration file, a deployer wallet and 9 other accounts. |
| 89 | +The accounts are seeded with a configurable balance of STX. Each wallet is defined under the heading |
| 90 | +`[accounts.wallet_name]` in the TOML configuration file. Each heading has the following options: |
| 91 | + |
| 92 | +- `mnemonic`: the 24-word keyphrase used to generate the wallet address |
| 93 | +- `balance`: the balance in micro-STX of the account when the blockchain starts |
| 94 | + |
| 95 | +The private key (`secret_key`), Stacks address, and BTC address are provided as comments under each wallet. These are |
| 96 | +useful for configuring [stacking orders][] on DevNet. |
| 97 | + |
| 98 | +### Blockchain configuration |
| 99 | + |
| 100 | +DevNet provides a sensible default configuration for the local blockchain, with a fixed block time of 30 seconds and |
| 101 | +the latest development images for each of the Stacks and Bitcoin nodes. These parameters are defined under the |
| 102 | +`[devnet]` heading. You can customize these defaults by setting any of the following parameters. |
| 103 | + |
| 104 | +-> Note: if any of the parameters are not supplied in the configuration file, the default value is used. |
| 105 | + |
| 106 | +- `pox_stacking_orders`: defined by [stacking orders][] headings later in the file |
| 107 | +- `orchestrator_port`: the port number for the Bitcoin orchestrator service |
| 108 | +- `bitcoin_node_p2p_port`: the port number for Bitcoin P2P network traffic |
| 109 | +- `bitcoin_node_rpc_port`: the port number for Bitcoin RPC network traffic |
| 110 | +- `bitcoin_node_username`: the username for the Bitcoin node container |
| 111 | +- `bitcoin_node_password`: the password for the Bitcoin node container |
| 112 | +- `bitcoin_controller_port`: the port number for the Bitcoin controller network traffic |
| 113 | +- `bitcoin_controller_block_time`: the fixed block time for the testnet in milliseconds |
| 114 | +- `stacks_node_rpc_port`: the port number for Stacks RPC network traffic |
| 115 | +- `stacks_node_p2p_port`: the port number for Stacks P2P network traffic |
| 116 | +- `stacks_node_events_observers`: a whitelist of addresses for observing Stacks node events |
| 117 | +- `stacks_api_port`: the port number for Stacks API network traffic |
| 118 | +- `stacks_api_events_port`: the port number for Stacks API events network traffic |
| 119 | +- `bitcoin_explorer_port`: the port number for Bitcoin Explorer HTTP traffic |
| 120 | +- `stacks_explorer_port`: the port number for Stacks Explorer HTTP traffic |
| 121 | +- `miner_mnemonic`: the 24-word keyphrase for the STX miner wallet |
| 122 | +- `miner_derivation_path`: the derivation path for the STX miner |
| 123 | +- `working_dir`: the local working directory for filesystem storage for the testnet |
| 124 | +- `postgres_port`: the port number for the Postgres DB (for running the Stacks API) |
| 125 | +- `postgres_username`: the username for the Postgres DB |
| 126 | +- `postgres_password`: the password for the Postgres DB |
| 127 | +- `postgres_database`: the database name of the Postgres DB |
| 128 | +- `bitcoin_node_image_url`: a Docker image path for the Bitcoin node container |
| 129 | +- `stacks_node_image_url`: a Docker image path for the Stacks node container |
| 130 | +- `stacks_api_image_url`: a Docker image path for the Stacks API node container |
| 131 | +- `stacks_explorer_image_url`: a Docker image path for the Stacks Explorer node container |
| 132 | +- `bitcoin_explorer_image_url`: a Docker image path for the Bitcoin Explorer node container |
| 133 | +- `postgres_image_url`: a Docker image path for the Postgres DB container |
| 134 | +- `disable_bitcoin_explorer`: Boolean to set if the Bitcoin Explorer container runs in the DevNet stack |
| 135 | +- `disable_stacks_explorer`: Boolean to set if the Stacks Explorer container runs in the DevNet stack |
| 136 | +- `disable_stacks_api`: Boolean to set if the Stacks API container runs in the DevNet stack |
| 137 | + |
| 138 | +### Stacking orders |
| 139 | + |
| 140 | +You can configure any of the wallets in the DevNet to participate in stacking to exercise the PoX contract |
| 141 | +within DevNet. This can be useful if you are developing a contract that interacts with the PoX contract and you need |
| 142 | +to set specific test conditions. |
| 143 | + |
| 144 | +Each stacking order is defined under the heading `[[devnet.pox_stacking_orders]]`. This heading is repeated for as many |
| 145 | +stacking orders that are necessary for your configuration. |
| 146 | + |
| 147 | +- `start_at_cycle`: the stacking cycle that the wallet should start particiating in. The wallet's stacking order |
| 148 | + occurs at the block preceding the beginning of that cycle. |
| 149 | +- `duration`: the stacking duration for the stacking cycle |
| 150 | +- `wallet`: the alias of the wallet participating |
| 151 | +- `slots`: the number of stacking slots that the wallet will participate in |
| 152 | +- `btc_address`: the BTC address that stacking rewards should be sent to |
| 153 | + |
| 154 | +[clarinet installed]: /write-smart-contracts/clarinet#installing-clarinet |
| 155 | +[docker documentation]: https://docs.docker.com/get-docker/ |
| 156 | +[stacking orders]: #stacking-orders |
0 commit comments