This article gives a brief overview of the different ways to implement smart contracts for Substrate-based blockchains. It also aims to provide insight on reasons for choosing smart contract development over runtime development for your on-chain logic.
Smart Contract Toolkits
Substrate provides two smart contract virtual machines which can be added to your runtime. Each come with additional tools to ease development depending on your use cases.
合约 (Contracts) 模块
The FRAME Contracts pallet provides functionality for a Substrate runtime to deploy and execute WebAssembly Smart Contracts. It has its own smart contract language, specially designed to write contracts that optimize for correctness, conciseness and efficiency. Learn more in this section.
The FRAME EVM pallet provides an EVM execution environment for Substrate's Ethereum compatibility layer, known as Frontier. It allows unmodified EVM code to be executed in a Substrate-based blockchain, designed to closely emulate the functionality of executing contracts on the Ethereum mainnet within the Substrate runtime. Learn more in this section.
Note: Substrate is built to enable developers to extend what's provided out of the box. We encourage further development of alternative smart contract platforms on top of the Substrate runtime. Use these pre-built pallets to inform how you might design your own system or how you could port over an existing system to work on a Substrate-based chain.
Smart Contracts vs. Runtime Development
Developing Substrate runtimes and smart contracts are two different approaches to building "decentralized applications" using Substrate.
沙箱: 一个合约无法直接修改核心区块链存储或其他合约的存储。 它的作用是限制合约只能修改自己所拥有的状态，且只能向其它合约或 Runtime 函数发起外部调用。
状态租赁：合约会因为占用了区块链的空间而需要为其付费。 这确保人们不会从 “免费、无限的存储” 中获利。
这些不同的开销使得智能合约运行得更慢、更昂贵，但是合约开发的 “目标受众” 与 Runtime 开发者的不同。
Contracts allow your community to extend and develop on top of your runtime logic without needing to go through all the craziness of proposals, runtime upgrades, etc... 它甚至可以作为未来的 Runtime 变更的测试基础，将你的网络隔离于日后可能发生因成长产生的痛楚或错误。
In summary, Substrate Smart Contracts::
On the other hand, runtime development affords none of these protections or safe guards that Smart Contracts give you. As a runtime developer, the barrier to entry on the code you produce jumps way up.
您可以完全控制网络上每个节点运行的基本逻辑。 You have full access to each and every storage item across all of your pallets, which you can modify and control. 甚至可以用错误的逻辑或糟糕的错误处理代码来砸你自己的区块链。 In essence, runtime engineers have a lot more responsibility for the correctness and robustness of the code they write.
Substrate runtime development has the intention of producing lean, performant, and fast nodes. 它不提供任何保护，不提供交易回退的开销， 也不隐式地引入区块链上节点运行的收费系统。 This means while you are developing runtime functions, it is up to you to correctly assess and apply fees to different parts of your runtime logic such that it will not be abused by malicious actors.
In summary, Substrate Runtime Development:
- Provides low level access to your entire blockchain.
- Removes the overhead of built-in safety for performance, giving developers increased flexibility at the cost of increased responsibility.
- Raises the entry bar for developers, where developers are not only responsible for writing working code but must constantly check to avoid writing broken code.
Choosing the Right Approach
Substrate runtime development and Smart Contracts each provide tools designed for different problem spaces. There is likely some amount of overlap in the kinds of problems each one can solve, but there is also a clear set of problems suited for only one of the two. To give just one example in each category:
- Runtime Development: Building a privacy layer on top of transactions in your blockchain.
- Smart Contract: Introducing multi-signature wallets over the currency of your blockchain.
- Use Case Specific: Building a gaming dApp which may need to build up a community of users (leaning towards a Smart Contract), or may need to scale to millions of transactions a day (leaning more towards Runtime Development).
In addition to everything written above, you also need to take into account the associated costs of setting up your dApp using one approach over the other. 部署合约是一个相对简单和容易的过程，因为你可以利用现有的网络。 The only costs to you are the fees which you pay to deploy and maintain your contract.
Setting up your own blockchain, on the other hand has the cost of building a community who find value in the service you provide. Or, the additional costs associated with establishing a private network with the overhead of a cloud computing based architecture and general network maintenance.
It is hard to provide guidance on every possible scenario as each one depends on specific use cases and design decisions. In general, runtime development is most favorable for applications that require higher degrees of flexibility and adaptability — for example, applications that require accomodating different types of users or layers of governance. The table below is meant to help inform your decisions on which approach to use based on different situations.
|Runtime 开发||智能合约||Use Case Specific|
If you are building on Polkadot, you can also deploy smart contracts on its parachain. Check here for comparison between developing on parachain, parathread, and smart contract.
- 查看 Substrate 合约模块 如何在现行智能合约基础上进行迭代。
- 检查 EVM 模块 以查看你的下一个项目是否需要它。
- Learn more about why Rust is an ideal smart contract language.
- 按照 教程，将合约模块添加到你的 FRAME Runtime 中。
- Learn how to start developing with the Contracts pallet and ink!.