
“Polkadot Knowledge Graph"It is our entry-level article on Polkadot from zero to one. We try to start from the most basic part of Polkadot and provide you with a comprehensive understanding of Polkadot. Challenges, but we hope that through such efforts, everyone can correctly understand Polkadot, and people who do not know Polkadot can easily and quickly grasp Polkadot-related knowledge.picture
Polkadot Knowledge Graph
"It is our entry-level article on Polkadot from zero to one. We try to start from the most basic part of Polkadot and provide you with a comprehensive understanding of Polkadot. Challenges, but we hope that through such efforts, everyone can correctly understand Polkadot, and people who do not know Polkadot can easily and quickly grasp Polkadot-related knowledge.Today is the 65th issue of this column. Polkadot itself does not support smart contracts, but Polkadot’s parachain will support smart contracts, and Substrate smart contracts are inherently safer and have a lower development threshold. So how to build in Polkadot? What about smart contracts?
Smart contracts are an essential function of the blockchain. It is precisely because of smart contracts that the blockchain realizes machine trust. Currently, it has a wide range of application scenarios, including digital identity, supply chain, trade finance, mortgage and other fields.
Substrate smart contracts allow users to add business logic based on the core logic of the blockchain, while establishing a protection mechanism against saboteurs.
Compared with parachains, the development speed of smart contracts is faster and the deployment is less difficult. Therefore, smart contract platforms such as Moonbeam, Astar, and Phala provide more options for applications that want to go online quickly.
The Polkadot relay chain itself does not support smart contracts, but the parachains on Polkadot will support smart contracts.
The difference between developing smart contracts and parachains
abstraction layerWhen you write a smart contract, you are creating instructions that are associated with and deployed on a specific chain address. In contrast, the runtime module is the entire logic of a chain of state transitions (called state transition functions).
Smart contracts must be intentionally upgradable, while parachains will have the ability to exchange their code entirely through root commands or through the governance pallet.
When you build a smart contract, it will eventually be deployed to a target chain with its own environment
. Parachains allow developers to declare the environment of their own chain, and even allow others to write smart contracts for it.
Gas fee
Smart contracts must find ways to limit their own execution, otherwise full nodes are vulnerable to DOS attacks.
For example, an infinite loop in a smart contract could consume the computing resources of the entire chain, preventing others from using it. The discontinuity problem states that, for sufficiently powerful languages, it is impossible to know in advance whether or not a program will halt execution.。
Some platforms, like Bitcoin, get around this constraint by offering a very limited scripting language. Other projects, such as Ethereum, charge "gas" to smart contracts to provide the right to execute their code.
If the smart contract does get into a state that never stops executing, it will eventually run out of gas, stop executing, and any state transitions the smart contract would have made are rolled back.
Polkadot uses a weight-fee model instead of a gas metering model
Parachains can implement arbitrarily powerful programming languages and do not include the concept of gas for their own native logic.
This means that certain features are easier for developers to implement, but certain constructs, such as loops without a terminating condition, should not be implemented.
It is often wiser to leave certain logic (such as complex loops that may run indefinitely) to the non-smart contract layer, or even try to eliminate it. Parachains try to be proactive, while smart contract platforms are event-driven.
Build smart contracts
Substrate currently supports smart contracts out of the box in two ways:
EVM Pallet provided by Frontier.
The Contracts Pallet in the FRAME library based on Wasm contracts.
secondary title
Frontier is a suite of tools that enables Substrate chains to natively run Ethereum contracts (EVM) with the same API/RPC interface that Ethereum exposes on Substrate. Ethereum addresses can also be mapped directly to Substrate's SS58 scheme from existing accounts.
Substrate contract
Substrate provides a built-in contract pallet; over time, more parachains will support WebAssembly smart contracts.https://www.youtube.com/watch?v=fKHkFBXaUxQ
picture
Additionally, there is the EVM Pallet, which allows parachains to implement the Ethereum EVM, thus enabling direct ports of Ethereum contracts.https://stackoverflow.com/questions/56040779/when-should-i-build-a-substrate-runtime-module-versus-a-substrate-smart-contract/56041305#56041305)
A video version of the smart contract overview can be found on the Polkadot YouTube channel.https://github.com/paritytech/ink)
Contracts Pallet
resource
1.Wasm(WebAssembly)When should I build Substrate runtime and Substrate smart contracts? This post technically answers when a developer might choose to develop a runtime instead of a smart contract. (
2.Below is a list of resources currently available to developers who want to start writing smart contracts for deployment on Substrate-based parachains. ink! - Parity's ink! for writing smart contracts. (Developers who have written smart contracts before may be more familiar with the experience of deploying to an EVM-based chain. However, the Contracts Pallet makes some notable improvements to the design of the EVM:
3.: The Contracts Pallet uses WebAssembly as its compilation target. Any language that compiles to Wasm can potentially be used to write smart contracts. Still, it's better to have a dedicated domain-specific language, so Parity offers the ink! language.Deposit
: Contracts must hold a large enough deposit (named ContractDeposit) to prove their existence on-chain. Deployers need to deposit it into a new contract based on ExistentialDeposit.
4.Caching: Contracts are cached by default, so this means they only need to be deployed once and can then be instantiated as many times as needed.
This helps keep the storage load on-chain to a minimum. On top of that, when the contract is no longer used and existing deposits are depleted, the code is removed from storage (known as reaping).https://github.com/paritytech/substrate/pull/9669
Storage Rent
: Deprecated Pallet_contracts was originally designed to deal with unbounded state growth by charging for state consumed by contracts, but was later deprecated.
See the related pull request for more details:
Polkadot Standard Proposal (PSP)https://github.com/w3f/PSPs
Ink!
The Web3 Foundation supports Polkadot's proposals that define a set of standards that meet the needs of the ecosystem. These standards go through several acceptance stages where the participation of the entire community is required to establish a worthy and future-proof standard.
All groups that benefit from a standard need to agree on its content.
Some of these PSPs are Contracts Pallet for Substrate: Please visit the Polkadot Standard Proposal (PSP) Github for more information:
ink! is a domain-specific language for writing smart contracts in Rust that compiles to Wasm code. As it says in its readme, it's still experimental, so intrepid developers should be aware that they may have a bumpy but workable development experience.
There are a few projects that have built projects with ink!, with a decent level of complexity, so it's mature enough to start building interesting stuff.
For interested developers, they can start writing smart contracts using ink! by studying already written examples. These can serve as guidelines for writing more complex logic that will be deployable on smart contract parachains.https://docs.openbrush.io/
- ink! lays many of the foundations for a new smart contract stack based on the Wasm virtual machine and compatible with Substrate chains.https://github.com/patractlabs/metis
ink!'s smart contract library collects some examples of smart contracts below. Are you developing a smart contract example? Please let us add it to this page!
OpenBrush: An ink! library that provides standard PSP-based contracts, as well as useful contracts and macros for building.
Parity Tech
ink!:
https://github.com/paritytech/inkSubstrate contracts Pallet:https://github.com/paritytech/substrate/tree/master/frame/contracts
Metis: A standard library of Wasm contracts developed by Patract Labs.
Moonbeam:
Astar:
Acala:
Phala:
The smart contract environment is still maturing
https://www.polkaproject.com/#/projects?cateID=1&tagID=6
Moonbeam
It is still early days for smart contracts on Polkadot, and its development is only now stabilizing. We are actively producing content to help developers keep up and will maintain the wiki with the latest resources. You should also follow the links below for the latest information:
Parachain
Many smart contract platforms are being built as parachains in the ecosystem. A list of communities creating and maintaining different Polkadot-based smart contract platforms can be found at PolkaProjects.
Moonbeam is another project that plans to deploy to Polkadot as a parachain and will support Ethereum-compatible smart contracts.https://docs.moonbeam.network/Astar
Since Moonbeam uses Frontier, an interoperability layer with existing Ethereum tools, it will support all applications written for the EVM environment with little friction.
Moonbeam’s leading network, Moonriver, was launched on Kusama as a parachain. The parachain function has been launched, and the function is being released gradually. The final phase of the release will include EVM functionality and balance transfers.
Try following Moonbeam's documentation to deploy a smart contract.
Astar (formerly Plasm) is a smart contract platform built on Substrate, and intends to integrate with Polkadot as a parallel chain.https://docs.astar.network/docs/wasm/sc-dev/ink-dev/
Acala
Shiden is Astar's leading network, currently running on Kusama, and supports EVM, WASM and Layer2 solutions.
Astar is designed to accommodate all Layer 2 scaling solutions and supports all Layer 2 protocols through OVM (Optimistic Virtual Machine).
Try deploying ethereum or ink! Follow their docs for smart contracts.https://wiki.acala.network/build/development-guide/smart-contracts
Phala
Acala is a decentralized financial alliance and DeFi infrastructure chain, providing a set of protocols as the DeF center on Polkadot. Karura, Acala's leading network running on Kusama as a parachain.
Interested teams can now deploy DApps and smart contracts on Karura's platform. Acala is also implementing the Acala EVM.
Try to deploy Acala smart contract following Acala EVM's documentation.https://wiki.phala.network/en-us/general/phala-network/intro/
Phala is a privacy-preserving cloud computing platform designed to provide strong confidentiality guarantees as a cross-chain platform.
As a smart contract platform, Phala will support encrypted smart contracts on Polkadot. Khala is the lead network of Phala, running on Kusama as a parachain.
Try to deploy an encrypted smart contract following the Phala documentation.https://matrix.to/#/#polkadot-watercooler:web3.foundation
keep in touchhttps://area51.stackexchange.com/proposals/126136/substrateWhile the tooling is still in a mature stage, the benefit of doing it early is getting familiar with the project and staying ahead of it, allowing you to innovate and create something truly new.
If you have any interesting ideas about smart contracts on Polkadot, please feel free to enter Polkadot Watercooler for discussion.
Developers may be interested in joining the Polkadot Noobs Lounge https://matrix.to/#/#polkadotnoobs:matrix.org or Substrate Technical
to ask their questions. As always, stay connected with Polkadot and Kusama by following social channels.
May all be well!
https://mirror.xyz/0x9A259b3a2316281Cc948cE2Cf1Ac610a79844f05