top of page
  • Twitter
  • Telegram
  • Discord
  • GitHub
  • Youtube
  • Writer's pictureAndrey Shevchenko

How to Build The Next Big Thing with a Substrate Blockchain

Updated: May 3, 2023

Substrate is an open-source technology framework that currently offers a powerful way of creating modular and extremely customizable blockchains on Polkadot and beyond.


In this article, we will explore Substrate's core features, its relationship with Polkadot, the role of the Rust programming language, and some real-world use cases within the Polkadot ecosystem.


Introduction to Substrate & Polkadot Blockchains


Substrate is a modular framework designed for building customizable and upgradable blockchains. Developed by Parity Technologies, Substrate can be used to create blockchains that can connect to the Polkadot network, which is a scalable and interoperable blockchain platform. Substrate provides a collection of libraries and tools that developers can use to build their own blockchain tailored to their specific needs.


The framework makes it possible to create blockchains that can interact with other Substrate-based chains and external networks, thanks to its built-in compatibility with Polkadot's ecosystem.


It’s important to note that Substrate is not Polkadot: the framework can be used to build fully independent chains as well, and there are multiple examples of such platforms. But to get the most out of Substrate, connecting to Polkadot or Kusama is probably going to be on your todo list because of the interoperability it enables through XCM, a messaging protocol that allows different blockchains to communicate and exchange information securely and efficiently. Currently, XCM only works for Polkadot/Kusama (also known as Dotsama) parachains. Parachains are individual blockchains connected to and secured by the main Polkadot or Kusama network.


Rust: A Critical Component in Substrate Development


Rust is a powerful programming language that offers clean, efficient code, and crucial security guarantees at compile time, making it an ideal choice for blockchain development. For instance, when performing mathematical calculations in blockchains, fixed-point arithmetic is commonly used to ensure absolute precision. Rust's operator overloading features allow developers to seamlessly integrate custom mathematical libraries, making each operation easily understandable at a glance.


Consider an example where a Substrate pallet (module) implements a voting system. Rust's strong type system and memory safety features make it easy to ensure that votes are accurately counted and stored, preventing potential manipulation or loss of data.


Furthermore, with Rust's advanced enum objects, the pallet is able to easily distinguish between both successful and failed outcomes, such as "vote accepted," "invalid vote," or "voting period expired," which can also return key outputs to the main program. In this scenario, Rust's very explicit error-handling mechanisms can help the developer create specific (and, thus, safe) responses to each possible outcome.


Rust's ability to detect potential memory leaks, type inconsistencies, and overflow issues at compile time significantly reduces time spent debugging in testing environments, leading to a smoother development experience overall. It's important to note, however, that Rust cannot automatically fix all bugs. Some critical issues may occur due to code that functions correctly under normal conditions but allows malformed input to disrupt the internal logic. Therefore, comprehensive unit tests, including negative cases or fuzzed inputs, are essential to ensure the security of your Substrate blockchain, as well as any other blockchain development projects.


As a Web2 developer familiar with Rust, diving into Substrate development is an excellent opportunity to apply your skills in the Web3 domain. However, there is still a learning curve, which includes becoming acquainted with custom libraries and adopting a "thinking with transactions" mindset. This approach differs from classic Web2 patterns, such as async-await and concurrency, as blockchains are typically structured on a purely sequential and atomic basis, with very limited asynchronous operations.


Now, let’s go deep into the inner structure of Substrate and how you can efficiently use its modules to create your blockchain.


A Deep Dive into Substrate Blockchains


How Does Substrate Work?


Substrate consists of several components that work together to provide a complete blockchain node implementation. You can think of the blockchain node as the blockchain: the entirety of the core logic, stored data, networking, and smart contract execution systems of the blockchains are packaged into the node software. Then to make the blockchain decentralized, the node needs to communicate with other peers to make sure their version is synchronized.


At a high level, a Substrate node is split into two components: the Client outer node and the Runtime.

Substrate offers default implementations for all modules you may need (including multiple options for a given function). You can change any of them as you wish or implement your own logic. In practice, unless you really know what you’re doing, it’s rare that you’ll be working on the Client modules. Instead, most of the magic of making a custom Substrate blockchain is in the Runtime.


Implementing Your Own Runtime


The Runtime is the core business logic of the blockchain, and it’s composed of modules called pallets. The Runtime-related modules are called FRAME pallets and libraries. A Runtime is nothing but a collection of pallets, some default (the FRAME pallets) and some custom-made by you.


Check out the full list of FRAME pallets. Some of the most interesting include:


There is also an ecosystem of custom-built pallets that can be used by anyone. A good example of that is the Frontier pallet that allows running a full-on Ethereum emulation on Substrate. This pallet was jointly developed by Parity, Polkadot’s development team and PureStake, the primary contributor to the Moonbeam project.


The FRAME libraries offer the interface required to communicate with other pallets and create your own implementation. You can either make small changes to piggyback off existing pallets or make something extremely powerful and complex (such as the aforementioned Frontier).


Substrate’s modularity is extremely helpful when designing your chain. If you’re developing an app-chain DEX, you can pretty much just join together default pallets to handle balances, tokens, XCM transfers and then implement your own pallet for the actual DEX logic, avoiding a lot of the work involved in creating your own chain.


And if you want to update your Runtime, Substrate also makes it very easy with upgradability pallets. This allows you to make smooth, forkless updates to the chain just as you’d do with upgradable contracts.


The potential of Substrate is immense, and teams are only now starting to scratch beneath the surface. Let’s review a few really cool cases of things you can only do with Substrate!


The Power of Substrate Blockchains in Case Studies: Moonbeam and OAK Network


Substrate has enabled the development of many innovative blockchain projects within the Polkadot ecosystem. Here, we’ll dive deep into two of them: Moonbeam and OAK Network.


Moonbeam’s Improved EVM


Moonbeam is a cross-chain and Ethereum-compatible smart contract platform built on Substrate that allows near-complete equivalency with regular Ethereum implementations. You can deploy Solidity or Vyper smart contracts just as you would on Ethereum, you can use the same wallet addresses and RPC interface as implemented on Ethereum (meaning you can use wallets like Metamask with it). Importantly, you can still use all of the important Substrate features like XCM, governance, and runtime upgrades.


Most of these features are enabled by the Frontier pallet, which offers a complete emulation of Ethereum within Substrate. Unlike other EVM-compatible platforms, which are usually just ports of Ethereum’s Geth node software, Frontier is designed to be Substrate-native from the start.


This means that beyond merely emulating Ethereum on Substrate, Moonbeam enables multiple add-ons that make it more flexible and useful than any other EVM chain.


A notable example is the XC-20 standard, a way to bring XCM assets from other Polkadot chains as native ERC-20s on Moonbeam. With a few communicating pallets, the Moonbeam team managed to connect the two different token standards so that you can easily hold and trade tokens like DOT or KSM in your Metamask wallet.



More cool features are available as precompiles: custom pieces of code that can be accessed from Solidity smart contracts. Precompiles exist on Ethereum as well, primarily for efficient cryptographic calculations. On Moonbeam, this concept has been extended to create things like:

  • The randomness precompile, allowing provably random lotteries or other uses of random numbers.

  • The batch precompile, which allows batching together multiple transactions from the same sender. One use case is bypassing the annoying approve-transact mechanic of the ERC-20 standard.

  • The call-permit precompile, which allows executing any kind of gasless transaction by just signing a command and letting a “sponsor” execute it.

Substrate’s modularity is what enables these additional use cases, easily improving the UX of the EVM while maintaining full compatibility.


OAK Network’s Event-based Transactions


Blockchains suffer from one important drawback: every transaction must be initiated by a user, known as Externally Owned Account (EOA) in Ethereum. This means that smart contracts that require some kind of periodic action or maintenance, or responding independently to events, need to also set up a complex system of agents known as keepers. These are supposed to call certain “poke” transactions to initiate smart contract callbacks, which by itself is quite simple: the hard part is making these keepers decentralized.


Enter Substrate and OAK Network: this project is introducing event-based transactions in their Runtime, which automatically respond to callbacks set by external developers. A good practical example is trustless auto-trading: envision a decentralized trading platform that enables users to establish customized conditions for executing trades, such as initiating buy or sell orders when a token's price hits a predefined threshold. Leveraging event-based transactions, the OAK Network can seamlessly execute a callback once the specified conditions are fulfilled, facilitating the trade without necessitating any manual intervention from users or developers. This not only streamlines the trading process but also enhances the overall user experience on the platform.




OAK Network is designed as an automation network used by both individuals (for example, to set up automatic orders such as stop loss), and by projects who have some kind of maintenance requirement inside their contracts.


With XCM, OAK Network can execute these callbacks on any chain connected to Polkadot and Kusama, making it a pure service-provider chain for others and a true application-specific chain.


Substrate: Blockchain Development Elevated to New Heights


Substrate has emerged as a powerful and versatile framework for building highly customizable blockchains tailored to specific needs within the Polkadot ecosystem and beyond. The technical achievements in Substrate are truly ahead of their time: the combination of easy modularity, interoperability, and flexibility are unmatched anywhere in the space. Still, this kind of power also carries a heavy development burden, making smart contracts still an easier development path for most dApps.


But Substrate is so flexible that you can use it to make other Substrate blockchains even easier to create, and there are some teams working right now on making this a reality.




369 views0 comments

留言


bottom of page