Building blockchain applications involves a lot of resources, which leads to a longer development process and requires input from a team of experts in different fields of programming, such as cryptography, database administration, etc. Polkadot aims to reduce these headaches and simplify blockchain development.
What is Polkadot?
Polkadot is a network for blockchain interoperability. As a network protocol, it provides custom blockchain applications a platform to transfer messages, including value, in a trust-free fashion, share their unique features, and initiate cross-blockchain transfers of any data or asset.
The custom blockchains built on top of Polkadot, also known as parachains, can carry out interchain transactions using the shared security provided by Polkadot in a trust-minimized way. This protocol gives birth to an interconnected internet of blockchains.
What are parachains?
The structure of the applications on Polkadot means each application can process transactions in a parallel nature, hence the name parachain.
A parachain can be a blockchain or an application-specific data structure that is globally coherent. Parachains can function independently on the Polkadot network and implement their own governance, economics, etc.
Polkadot makes no rules for how the parachains decide to handle their transactions. But it does provide a means through the cross-consensus messaging (XCM) format for these independent applications to communicate.
Parachains are connected by the relay chain, which also provides the shared security, pooled security, and contributes to the network’s scalability.
What is a parathread?
The provision of shared security by Polkadot isn’t free security; parachains make this bond with the network for to secure Polkadot.
Parathreads have access to the same throughput, API, and functionality on Polkadot as the parachains, but these services are provided on a pay-as-you-go basis. This allows more infrastructure chains and improves composability.
What is Substrate?
Building a blockchain application can demand a lot of resources. A blockchain project can have good features and real-world value, but without the right resources to fund its development, it won’t get off the ground.
Substrate is a generic blockchain framework. As such, it provides plug-and-play components for building blockchain applications and networks. Its modular and pluggable libraries enable you to implement signatures, consensus mechanisms, and other parts of the chain as desired. At the core of Substrate is its composable nature to drive customization while building an application on the Polkadot network.
Polkadot as a network provides the Substrate chains with the protocols to send messages across each other. You can host your Substrate chains on the Polkadot network through an auction process, but your Substrate chains doesn’t necessarily have to be hosted on the Polkadot network unless you want to take advantage of its features.
Develop and deploy a Substrate blockchain app
The following vital components serve as the core layers when building a blockchain application:
- Database layer
- Transaction queue
- Networking layer
- Block authoring
- State transition function
Substrate provides the core components needed to ease the development process while creating a blockchain. This enables you to concentrate on building specific features or customizations per the needs of your project.
You can easily set up a blockchain application using an out-of-the-box, working, Substrate-based node template.
To follow along with this tutorial, you’ll need:
- Rust locally installed and configured to set up a development environment
- Basic knowledge of software programming and the use of terminal
Step 1: Set up a blockchain application with Substrate
To set up a development environment, we’ll use existing templates. The Substrate templates provide a working development environment. These templates are starter kits extended to add more features and customizations when building on Substrate.
git clone https://github.com/substrate-developer-hub/substrate-node-template
Run the following commands to add the Nigtly build for Rust:
rustup update nightly rustup target add wasm32-unknown-unknown --toolchain nightly
Next, change directory into the
./substrate-node-template folder and check out to the
latest version within the repository:
cd substrate-node-template git checkout latest
This repository contains Rust files that can be modified accordingly to the features (customizations) that are specific to the blockchain project.
Within the project, let’s compile and run the node template with the following commands:
$ cargo build --release 2021-12-16 00:36:30 Running in --dev mode, RPC CORS has been disabled. 2021-12-16 00:36:30 Substrate Node ... 2021-12-16 00:36:33 📦 Highest known block at #0 2021-12-16 00:36:33 〽️ Prometheus exporter started at 127.0.0.1:9615 2021-12-16 00:36:33 Listening for new connections on 127.0.0.1:9944. 2021-12-16 00:36:36 🙌 Starting consensus session on top of parent 0x4bbcc70ccccc322d314a5df12a814c28d40e6879b7b930df5ac5a50fe4be4c30 2021-12-16 00:36:36 🎁 Prepared block for proposing at 1 (1 ms) [hash: 0x18f1c7bf91a1544c9a0e35ac08c8f036b4cb2f8d8297233fffadb94022b982a7; parent_hash: 0x4bbc…4c30; extrinsics (1): [0x6458…325e]] 2021-12-16 00:36:36 🔖 Pre-sealed block for proposal at 1. Hash now 0xf10d170d82617ff5df6752dc911d3483badf34b005c8c48a46aeb6b708c915b2, previously 0x18f1c7bf91a1544c9a0e35ac08c8f036b4cb2f8d8297233fffadb94022b982a7. 2021-12-16 00:36:36 ✨ Imported #1 (0xf10d…15b2) 2021-12-16 00:36:38 💤 Idle (0 peers), best: #1 (0xf10d…15b2), finalized #0 (0x4bbc…4c30), ⬇ 0 ⬆ 0 ... 2021-12-16 00:36:42 🔖 Pre-sealed block for proposal at 2. Hash now 0x409138fda4f59dc093dce60fefbaca31c354ce18cef1bbea6f69a5009af6e0f4, previously 0x484e81ea10a15f04a640a595cb51d41eecc05919b4a16839852ba4d8a69440e1. ...
In the above terminal output, our blockchain application produces new blocks and reaches a consensus about the state they describe.
Next, let’s set up a frontend application to interact with our blockchain currently running on the terminal. Open a new terminal session and run the following commands:
git clone https://github.com/substrate-developer-hub/substrate-front-end-template
Now, let’s change directory into the newly created folder,
substrate-front-end-template, and install the necessary dependencies for the UI:
Once we have successfully installed all needed dependencies, we can get it started using the
yarn start command:
The above image displays current information about the chain we’re connected to, including the current block and finalized block.
Within the basic UI structure for testing our application, we have sample accounts, some with funds available. Using the
Transfer component within the webpage, we can copy an account’s address, paste it into the To input field, and transfer funds to that account.
We can also decide to add more components, called pallets, to add more functionalities. Or, we can build and publish our own customized pallets beyond this tutorial’s scope. Using the Pallet Interactor, we can add pallets to the node template.
Step 2: Deploy your blockchain application
To test our blockchain application, we need to start a Polkadot-like chain (the relay chain), then connect it to the relay chain using a local testnet environment.
There are relay chains available that serve as a testnet on Polkadot. We’ll use Rococo, a popular Polkadot parachain testnet, to launch a parachain testnet to test our application. The Rococo network uses the proof of authority consensus mechanism. You could alternatively use the Westend testnet to launch and test a parachain on Polkadot, but for this tutorial, I recommend using Rococo.
We’ll use a Cumulus-based Substrate node to set up a parachain as Rococo integrates Polkadot with Cumulus. The Cumulus-based Substrate node is also the same as the above
substrate-node-template project with a few modifications required to register as a Parachain or Parathread on Rococo.
To deploy a parachain on the Polkadot network, you need to acquire a parachain slot through an auction process, which is more like a proposal to launch a new parachain project on Polkadot. The bidding process for a slot on Polkadot can be found in the Polkadot docs.
In this tutorial, we introduced concepts associated with building blockchain applications on the Polkadot network, such as parachains, parathreads, a relay chain, and the Substrate framework.
You should know have the foundational skills to develop and host a blockchain or network of blockchains, as well as applications designed to handle a specific task. These projects can range from an Oracle blockchain to provide real-world data, to other parachains within the Polkadot network, to a network of DeFi application, to bridges to other blockchain platforms from the Polkadot community.
The list of projects you can build on the Polkadot blockchain is endless. The interoperable nature of Polkadot provides a common ground for all these projects to seamlessly interact with each other, creating a new Web3 of borderless interconnectivity.