Smart Contract Stack
Smart Contract Runtime Environment
Astar & Shiden runtimes are based on Substrate, and both networks incorporate
pallet-contracts, a sandboxed environment used to deploy and execute WebAssembly smart contracts. Any language that compiles to Wasm may be deployed and run on this Wasm Virtual Machine, however, the code should be compatible with the
To avoid unnecessary complexity, and writing boilerplate code, the most appropriate method of building will involve the use of an eDSL specifically targeting
pallet-contracts, such as ink! (based on Rust), or ask! (based on AssemblyScript), or possibly others as the ecosystem grows.
After compilation, a Wasm blob can then be deployed and stored on-chain.
As is also the case with Substrate,
pallet-contracts uses weightV2 to charge execution fees. It is composed of
- refTime: The amount of computational time that can be used for execution, in picoseconds.
- proofSize: The amount of storage space that can be used (also called storage bandwidth), in bytes. So access storage assume that it will grow the gas fees.
Gas = Weight = (refTime, proofSize)
Transaction Weight in Substrate Documentation
Automatic Deposit Collection
Additionally, due to the weight, there is also a fee paid for on-chain storage called automatic deposit collection. This fee is paid additionally by the caller, and is calculated along with the price set for each storage item
DepositPerItem, and for each byte of storage
The automatic deposit collection can be simplified as follows:
A caller of a contract will pay a deposit to each contract in which new storage is created, as a result of an executed call. Conversely, a caller will receive a refund from each of the contracts that a call removes storage from.
Ink! 3.0 Blog Post by Parity Ink! 4.0 Blog Post by Parity
Pallet-contracts uses wasmi as a Wasm interpreter to execute Wasm smart contract blobs. Although there is a faster JIT interpreter such as wasmtime available in the native runtime, smart contracts are an untrusted environment which require a higher degree of correctness of interpretation, which makes wasmi a more suitable option.
Two-step Deployment of Contracts
The contract code (Wasm blob), and contract address and storage are decoupled from one another other, so require two steps to deploy a new contract on-chain:
- First, upload the Wasm contract on-chain (every contract Wasm code has a
code_hashas an identifier).
- Second, instantiate the contract - it will create an address and storage for that contract.
- Anyone can instantiate a contract based on its
There are several benefits of decoupling the contract code from the address/storage:
- To save space on-chain. Since a contract can have several constructors and instantiations, a redeployment will create a new instance based on the same underlying code. Think about standardized tokens, like PSP22 & PSP34, that will have one
blobliving on-chain, and as many instantiations as are needed, rather than having to upload code with each new instantiation (for example, on Ethereum).
- To instantiate a new contract using code within an existing smart contract (see the delegator example),
code_hashis all that is needed.
- Some standard contracts such as (PSP22 and PSP34) will only be uploaded on-chain once, preventing users from having to pay gas costs for uploading new code.
- Update contract code for an address: replace the contract code at the specified address with new code (see set_code_hash). Storage and balances will be preserved.
For More Information About
The only library available to communicate with smart contracts is Polkadot.js API.
Parity also developed a web application to interact with contracts called contracts-ui.
The Wasm Stack vs. Ethereum
|L1 Architecture||Ethereum clients||Substrate|
|Smart Contract Runtime Environment||EVM||Wasm pallet-contract + EVM frontier|
|Gas Model||fixed price per instruction||weight + storage fees + loading fees|
|Smart Contract DSLs||Solidity and Vyper||ink! (Rust) and ask! (AssemblyScript)|