2021 Sub0 online: Wasm Smart Contract with Patract Labs

Introduction

Jupiter

Ask!

Redspot

Metis

Europa

Parascan

Himalia

Ask! — Bring AssemblyScript to 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

  • 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

  • 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

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

  • 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
  • Custom Commands
  • Detailed logging for contract execution:

Demo

npx redspot-new erc20
ink_primitives = { version = "3.0.0-rc5", default-features = false }ink_metadata = { version = "3.0.0-rc5", default-features = false, features = ["derive"], optional = true }ink_env = { version = "3.0.0-rc5", default-features = false }ink_storage = { version = "3.0.0-rc5", default-features = false }ink_lang = { version = "3.0.0-rc5", default-features = false }
  • 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.
  • Simply double click the icon to start it. Making sure the default port is not occupied.
  • Click start to start the node.
px redspot run scripts/deploy.ts
  • 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
  • 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
cd contractscargo +nightly contract build --keep-debug-symbols --optimization-passes=0
mkdir ../artifactscp ./target/ink/erc20_pausable.contract ../artifacts/cp ./target/ink/metadata.json ../artifacts/erc20_pausable.json
  • 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
  • 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 is providing solutions for the development of Parachains and DApps in the Wasm smart contract ecosystem of Polkadot.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

SPACE-iZ Revolutionising Property Amidst COVID-19

SingularDTV Featured in the LA Times: “A Craze is Sweeping the Planet”

BLOCK.CO and the University of Central Asia are Now Partners

6 NFT Project Ideas & The Communities Behind Them

The Biggest Trends in Blockchain, According to Penn Blockchain

Here is how Blockchain can work for consumer and employee reward

Global Telegram AMA with EY Tan, Co-founder of Aurigami at 2PM UTC on May 2

A Fair Transition

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Patract| Wasm Contract Open Platform

Patract| Wasm Contract Open Platform

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

More from Medium

Why Cryptoadz were chosen as the third NFT project to have its own marketplace

A token that helps the environment

The token is based on trx blockchain.