2021 Sub0 online: Wasm Smart Contract with Patract Labs

At the recently concluded 2021 Sub0 online conference, CryptoJedi, Head of Education at Patract Labs, focused on the topic of ‘Wasm Smart Contracts on Patract Labs’, demonstrating how developers can use Patract Labs’ development tools Redspot (contract development scaffolding), Metis (Wasm (contract standard library) and Europa (local test development node) to drive the development of smart contracts. This includes tool installation and writing simple contracts in Ink! and Ask! with Metis providing automated deployment and testing. Finally, he showed how to deploy and interact with contracts using the Jupiter test network. (Demo video to be released at a later date)

Youtube Link: https://www.youtube.com/watch?v=SntZ5P5HE_I

Introduction

Patract Labs was founded with the vision to provide full stack support for smart contract developments based on Substrate. We understand that substrate is a fast growing ecosystem for a variety of different blockchains and they are all carrying different purposes. We are seeing all kinds of substrate based projects emerging and booming.

However, we think smart contracts will play an important role in the substrate eco-system by overseeing exponentially booming projects in other blockchains such as Ethereum, Solana and EOS. Although many projects are just simple smart contracts, they have brought tremendous values to their ecosystem. For example, the Defi projects like Uniswap on Ethereum, Serum on Solana. They are now playing vital roles in the blockchain industry and create liquidity and practical use cases for the blockchain they were built on. And all those successful projects are back-boned by the developer tools and services such as Traffle, etherscan and Ganache. So we believe that providing mature and secured developer tools and service will further benefit the entire substrate Community which allows more developers to get involved.

Therefore, Patract Labs are dedicated to the Substrate ecosystem by providing all kinds of tools and services needed by the smart contract developers in Substrate. We are extremely excited to be involved in the Sub0 Seminar by providing the following products:

Jupiter

The first Product I am covering today is called Jupiter. Jupiter aims to be an open blockchain network, which provides wide support to all smart contracts as long as they are running on a Frame pallet-contract. Jupiter has been alive in the Substrate network and anyone can use it to test their smart contracts for free.

Ask!

The second product is called Ask!. Ask! is an embedded domain-specific language or eDSL that allows developers to write Wasm smart contracts in AssemblyScript. We will cover the details later.

Redspot

Redspot is the contract dev scaffold. Its goal is to liberate developers from the tedious and manual work like contract compiling, deployment, testing. We hope contract developers can finish all the task they need just in one place.

Metis

Metis, similar to Open Zeppelin in the Ethereum ecosystem, is the standard contract library. It provides secure and audited implementations of commonly used components in ink!. With Metis, developers won’t have to repeatedly write or copy/paste the same code over and over again.

Europa

Europa is the contract sandbox environment specially designed for contract developments. It removes some unnecessary components from Substrate and reveals more details during the contract execution.

Parascan

Parascan is a blockchain explorer designed specifically for inspecting smart contract logs and executions of substrate-based chains. Theoretically, Parascan can connect and cache any substrate blockchain and provide timely explorer services.

Himalia

Rightnow, most of the SDKs for Substrate are written in JS. However, many projects and services that need to communicate with Substrate are written in other programming languages such as Go, Java and Python. Himalia is the SDK that provides multi-language support.

Let’s dive deep into the products that we will be demoing today.

Ask! — Bring AssemblyScript to Substrate

Many of us have played around with ink!. However, a great advantage of Wasm is that Wasm supported programming languages compile to it. ink! is great but it requires developers to be familiar with Rust as a prerequisite. Therefore, we created Ask! which is an eDSL similar to ink!. With Ask!, anyone who has experience with typescript or AssemblyScript can easily develop Wasm smart contracts. Overall, we are trying to bring Solidity style development into Substrate.

eDSL

  • the overall layout is similar to ink!
  • Grammar in Assembly Script

Decorators

  • similar to macros in ink!, Ask! provides decorators. That gives very similar code layout
  • High level abstraction, developers can focus on the contract logics without learning how to interact with host functions and pallet-contract. All the complex stuff is done by Ask! under the hood.

Storage

More storage types:

  • SpreadStorableMap
  • SpreadStorableArray
  • PackedStorableMap
  • PackedStorableArray When large amounts of data need to be stored, Spread should be used. When you store a small amount of data and need to access most of the content, such as search or statistical logic, it may be better to choose Packed.

Interface and Inheritance

Because Rust does not support inheritance, a similar computer programming model used in Solidity won’t work.

With Ask!

  • Solidity developers can quickly get used to Ask!
  • Standard Contract Library similar to OpenZeppelin.

Under development

  • We are at version v0.2 and will publish v0.3 soon.

Redspot

Redspot is a development environment to compile, deploy, and debug Wasm smart contracts for substrate-based chains. Redspot helps developers manage and automate the repeating steps during the process of writing smart contracts and dApps. Similar to the architecture of Hard Hat, Redspot is built upon Tasks and Plugins.

Template

  • Based on the contract standard, it automatically generates sample contracts, deploy scripts, test scripts and Redspot Config. eg. npx redspot-new erc20 will create a directory with sample erc20 code, deployment and tests. You don’t have to write up the deployment, configs manually. We are integrating more templates into Redspot. So in the future, you can initiate any contract stands with one command
  • npx support, no additional setup required

Compile

  • Wraps around cargo-contract
  • npx redspot compile
  • npx redspot compiler will compile the code and put into corresponding dir for deployment

Deploy

  • Upload the contract and instantiate it using a script.
  • npx redspot run scripts/deploy.ts — no-compile

Test

  • Test contract methods using script
  • npx redspot test — no-compile

Console

  • Powerful JavaScript Interactive Console for integration testing.
  • control the blockchain operations
  • Before writing the detailed test codes, you can play in the console, understand how Redspot works. Getting the sense of how to test your contract, etc
  • npx redspot explorer

Docker

  • Many times, a contract will be deployed across multiple blockchains, Redspot has integration with docker that allow one contract to be tested on multiple platforms
  • To start Docker: npx redspot testnet
  • Compile on Docker: npx redspot compile — docker true

Plugin

  • not enough for your development
  • Extend Redspot with custom plugins, adding more features, integrate with other development tools
  • Stay compatible with Custom Substrate Chain by extending types by simply writing your custom plugin

Explorer GUI

  • @polkadot/apps but focus on contract
  • fully integrated with Redspot configs, no more operations required once you have the test blockchain setup

Metis — Rust Solution For Standard Contract Library

Current problems when developing in ink!

  • Lack of a standard library makes the smart contract unsafe. The DAO attack in 2016 stole 3.6m Ether due to a bug in the smart contract. This attack enforced hardfork for Ethereum Mainnet Non-standardized contracts will cause tremendous economic loss and further impact the blockchain’s consensus. Imagine having this in 2021, people will really question the security of your blockchain even if it is caused by contract developers.

Developers have to manually copy/paste existing implementations.

  • The source code could be unaudited and unsafe to use.
  • Imagine copying the code from another smart contract project. They may have bugs in the contract that hasn’t been noticed yet. Recklessly copying their code may cause enormous problems in future
  • Time consuming and error-proning during the process.
  • you may miss some logics during copying and pasting
  • or you misunderstood their logics because they may not name their functions correctly
  • you wasted tons of time to stay updated with their contracts to repeat copying and pasting for every release

Introduce Metis

  • To permanently solve the issues listed above, we created Metis
  • Standard Contract library based on reusable components.
  • Unlike Openzeppelin’s Inheritance model, Metis is based on Components, which means users do not directly inherit the standard implementation. Instead, Metis provides a set of reusable components for users to assemble

Metis vs OpenZepplin

  • Inheritance — Used by Openzeppelin
  • Pros: Simplicity : Minimize the code to write for developers
  • Cons: Ambiguity : Conceal method definitions; Uncertain Inheritance tree with multiple Inheritance.
  • eg.In OpenZepplin, when you have multiple inheritance, you may have a hard time figuring which parent contract the method is inherited from
  • Composition — Used by Metis
  • Pros: Clarity : Improve code readability and audibility all function user can interact is coded in place, that makes code auditing much easier
  • Cons: Repetition : Repeat writing the same code for existing implementations.

MCCI architecture

  • Data Model — Storage
  • Components — implementations
  • Controller — combines different components, ERC20 Pausable -> ERC20, pausable, ownable
  • Interface — immutable and mutable function, users interactions

Metis — vs Native ink! (Storage)

  • Users no longer need to declare variables by themselves
  • standardize the data structure
  • easier for Dapp development

Metis — vs Native ink! (Constructor)

  • Skip the process of variable initialization
  • No need to manually emit event
  • developers may forget to emit events that will cause blockchain explorer not be able to catch the changes and give users a hard time validate their transactions.

Metis — vs Native ink! (Event)

  • Stays pretty much the same due to the current design of ink!. Will mitigate this in the near future
  • We will use Macros to automatically generate events based on Metis components. For example, if you have a contract called ERC20 Pausable, we will generate all events included in storage declaration

ink! Metis — vs Native ink! (Message)

  • Contains default method implementation that ensures the security of balance transfer
  • taking care of all the caveats may exist in implementations
  • eg. Check remaining balance
  • allows developers to focus more on their core logics

Design Principle

  • rewriting functions seem stupid in software design
  • writing code on the right side
  • more nature
  • skip writing all redundant code
  • Instantiation means creating an instance of smart contract on the blockchain.
  • Calling a method from another instance means calling a method from another contract.
  • Cross contract calling ends up with wrong results.

Composition of multiple components

  • no need to list all required variables in storage
  • combine on demand, create more possibilities

Europa — out-of-box local test net

  • Europa is another implementation of Substrate blockchain with special focus on smart contract development
  • For better development experience, we removed consensus. The reason was when we were developing contracts using Canvas, we found the auto-generated blocks really bothered us. Therefore, Europa only produces blocks when it receives new extrinsics.
  • No Wasm Runtime. Although Wasm Runtime is great since it allows no hard fork blockchain upgrades, it brings more difficulty to debug contract execution
  • State KV Database. This additional database tracks all state changes when a new block is mined. This is really helpful when we are debugging smart contracts as we can figure what the contract really changes underneath the states of the blockchain.
  • Contract Pallet Modification. Right now, running Wasm smart contracts feels like a blackbox to contract developers as it does not print out all execution details. Therefore, we modified the contract-pallet to allow the node to print out all execution details.

Europa UI

  • We believe running a testing node should be as easy as possible and no setup should be involved. That will make Wasm smart contract development more attractive to developers from all industries.
  • Europa UI’s graphic interface is designed specifically for contract developments to facilitate browsing/debugging smart contracts. we will illustrate it shortly
  • Unlike other Substrate chain while you have to compile it locally. YOu can download the binary release and start your own node with one click. So you don’t have to manually install all the dependencies manually. That will save contract developers a great amount of time to run a node locally.
  • Europa UI is built with Electron and available through all mainstream platforms including Windows, Mac and Ubuntu. But it has some restraints on Operating System Versions. So check your os version before downloading.

Europa UI — demo

  • We believe running a testing node should be as easy as possible and no setup should be involved. That will make Wasm smart contract development more attractive to developers from all industries.
  • Europa UI’s graphic interface is designed specifically for contract developments to facilitate browsing/debugging smart contracts. we will illustrate it shortly
  • Unlike other Substrate chain while you have to compile it locally. YOu can download the binary release and start your own node with one click. So you don’t have to manually install all the dependencies manually. That will save contract developers a great amount of time to run a node locally.
  • Europa UI is built with Electron and available through all mainstream platforms including Windows, Mac and Ubuntu. But it has some restraints on Operating System Versions. So check your os version before downloading.

Europa UI — demo

contract pages

  • check all state changes
  • making rpc calls
  • making rpc calls with trace to get more insights of contract execution Nested Views
  • Nested contract calls may be very confusing
  • gives clear hierarchy of how cross contract calls works

Europa CLI

  • Custom RPCs

europa_forwardToHeight: forward the blockchain to a designated height

europa_backwardToHeight: revert the blockchain to a designated height and remove all blockchains state after that height

europa_modified State Kvs: get all the blockchain state changes

  • Custom Commands

State — kv: same as RPC: europa_modifiedStateKvs -> get all the blockchain state changes.

workspace — allows the developers to switch to different workspaces for different testing scenarios.

  • Detailed logging for contract execution:

It will trace and print all parameters and blockchain changes during the contract execution provides Wasm panic backtrace to exactly locate where the bug happens in smart contract.

Demo

1.Start your Redspot Project using Redspot templates:

remember to change the dependencies in cargo.toml to pull from crate.io, otherwise it will have compatible issues with polkadot.js. ink! is updating very fast and if we pull directly from ink! git, it will have compatible issues. We are seeing problems with Polkadot/app as well. But usually, it get fixed pretty quick.

  • redspot.config.ts: contains all configurations for redspot such as network configs
  • scripts/deploy.ts: is the deployment script to deploy the contract to blockchain
  • tests: contains all tests written for integration tests.

2.Download Europa-UI

Detailed release information can be found here Release v0.3.32. Currently, 3 operating systems are supportd:

For ubuntu, remember to give it permission chmod +x europa-ui-v0.3.32-x86_64-linux.AppImage

3.Start Europa-UI

  • Simply double click the icon to start it. Making sure the default port is not occupied.
  • Click start to start the node.

4.Test Redspot + Europa using templated contract and config

It will compile the contracts and update contract + metadata to Europa-UI. If the metadata does not get uploaded correctly, you can manual add that.

  • open Europa-UI, you should be able to see the contract has been successfully deployed.
  • you can use back to Block to revert the blockchain and remove the deployed test contract

5.Copy dependency in Cargo.toml and contract code in lib.rs into erc20/contracts/Cargo.toml and erc20/contracts/lib.rs

  • cp -R contracts ink_contracts
  • let’s checkout the changes by introducing metis
  • Cargo.toml: we import the metis components that we will be using. note that metis_lang is a must have for projects using Metis. Also, notice we are using branch, as ink! is updating very frequently, to stay compatible with ink! we have corresponding Metis version.
  • lib.rs:
  • instead of just use ink_lang as ink;, we import all dependencies from metis
  • storage: we no longer declare our own variables, we composite multiple metis components
  • event: Unfortunately, we still have to write all the code for events. In the next version of Metis, we will make Metis’s generate those events as long as we have declared them in the storage.
  • Messages: we no longer need to implement functions ourself. We can use Metis’s default implmentation
  • Internal functions: those are functions that are not exposed to users on blockchain, Metis’s macro will automatically generate mose of them. So we no longer need to write them ourself.
  • Metis’s feature of composition:
  • As we mentioned in the slides, Metis allows user to easily put together different components as needed.
  • Extensions:
  • hook: you might be ask why we did not add specific logics to pause transfer in our method implementations. That is because we use hooks in Metis and for every component we have a list of extension that implements the hooks.
  • erc20 extensions: https://github.com/patractlabs/metis/blob/main/crates/components/token/erc20/src/extensions/pausable.rs This extension overrides the before_token_transfer hook and adds the logic in it. So we no longer needs to implement pausable logic in every transfer function we have

6.Build your contract with

then copy it to artifacts dir under erc20 directory

this will build the contract in debug mode and keeps all debug symbols.

7.write the deployment script.

  • copy the code from deploy.ts
  • npx redspot run scripts/deploy.ts --no-compile
  • open Europa-UI to check if the contract has been successfully deployed

8.Writing tests:

  • copy all the code from tests
  • npx redspot test ./tests/erc20_pausable_init.test.ts --no-compile
  • test contract instantiation
  • npx redspot test ./tests/erc20_pausable_ownable.test.ts --no-compile
  • owner is initialized to sender’s address
  • non owner will be able to renounce ownership
  • owner should be able to renounce ownership
  • non-owner shuoldn’t be able to transfer ownership
  • owner can transfer their ownership
  • npx redspot test ./tests/erc20_pausable_pause.test.ts --no-compile
  • default paused state is be false
  • non-owner should be able to pause
  • owner should be able to pause
  • owner can unpause
  • Transfer without paused should be successful
  • Transfer with paused should be failed

About Patract Labs

Patract Labs is providing solutions for the development of Parachains and DApps in the Wasm smart contract ecosystem of Polkadot. We help the community to design and develop on-chain contract modules and Runtime support, and provide DApp developers with full-stack tools and services support, covering development, testing, debugging, deployment, monitoring, data provider and front-end development stages.

Website |Discord| Element|Twitter|YouTube|Github|Telegram

Patract is providing solutions for the development of Parachains and DApps in the Wasm smart contract ecosystem of Polkadot.