---
# System prepended metadata

title: Polkadot for contract dApp builders

---

---
type: slide
description: ​"There are many Polkadot layer-1 chains you could launch your dApp on including AlephZero, Astar, Moonbeam. Different chains also have different smart contract execution VMs — the popular ones being the famous EVM and WebAssembly-based "pallet contracts". In this workshop you'll get hands on experience learning ink!, the smart contract language that compiles to WebAssembly and learn about the options for deploying your dApps on Polkadot."
---

# Polkadot for contract dApp builders

This workshop aims to give you a lay of the land of the different smart contract platforms on Polkadot and introduce you to ink! &mdash; the native smart contracting language for the Polkadot ecosystem. 

Note:
(to facilitator) spend 2 minutes introducing the workshop plan and quests; about 10 minutes going through the background slides while participants setup their environment; 15 minutes creating a contract, going through it and building it; 5 minutes going over contracts UI; account for time for questions and tangents; time permitting go to quest #2.

---

## Learning objectives ✍️

* Learn what options you have for **building and deploying** smart contracts on Polkadot 
* Learn how to get started with ink!
    * Set up your local environment
    * Deploy your first ink! smart contract
* Complete a mini-quest and earn some KSM!
* Learn about the relation with FRAME runtime development (time permitting)

---

## Quests 🔎

Workshop quests are a way to help you apply what we learn during our workshops.

---

**Today's quests 🤠**

1. Setup your local environment and compile the flipper contract
2. Deploy the contract to testnet using the contracts UI

---

Clone the workshop repo 🧬

![qrcode_contractsws](https://hackmd.io/_uploads/SkrsIynh6.png)

https://github.com/JoinWebZero/contracts-workshop

Note:
Contains link to this page, instructions for quests etc.

---

## Get your environment setup🛑

Could take 5-10 minutes if you don't have Rust installed. 

Do this while we go through some background stuff.


---

#### Spoiler alert: There's no such thing as deploying smart contracts "to Polkadot" (at least not today).

_You deploy to Polkadot appchains (aka parachains) that support smart contracts_. 💡

---

## Background 🖼️

- **Polkadot** is a network of specialized layer-1 under the same shared security
- Some appchains are EVM compatible, others WASM compatible
- Some appchains support both
- Different ones offer different features

---

## Two type of smart contracts for Polkadot

- EVM compatible (most popular language is Solidity) 🙌
- WASM (`pallet-contracts`) compatible (most popular language is ink!) 🦀

---

## Examples of EVM compatible appchains

- Moonbeam
- Unique network
- Centrifuge
- Astar

---

## Examples of Wasm compatible appchains

![Screenshot 2024-02-27 at 13.56.32](https://hackmd.io/_uploads/S1yq3TonT.png)

---

## Paying for gas ⛽︎

Each appchain has their own native tokens to pay for transaction fees.

---

Q: How can I tell if a chain is smart contract compatible? 

Note:
A: it contains a module called `contracts`, which is the Wasm VM ink! compiles to. You can check out the contracts crate here: https://crates.io/crates/pallet-contracts

---

![Screenshot 2024-02-27 at 15.17.08](https://hackmd.io/_uploads/ByqOky33T.png)

Note:
Link to screenshot: https://astar.subscan.io/runtime

---

![Screenshot 2024-02-27 at 15.11.00](https://hackmd.io/_uploads/BJE-ACiha.png)

Note:
Link to Astar chain state: https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Frpc.astar.network#/chainstate

---

<!-- ---

## ink! vs. Solidity

|                       | ink!                        | Solidity      |
| :-------------------- | :-------------------------- | :------------ |
| Virtual Machine       | Any Wasm VM                 | EVM           |
| Encoding              | Wasm                        | EVM Byte Code |
| Language              | Rust                        | Standalone    |
| Overflow Protection   | Enabled by default          | Yes           |
| Constructor Functions | Multiple                    | Single        |
| Tooling               | Anything that supports Rust | Custom        |
| Versioning            | Semantic                    | Semantic      |
| Has Metadata?         | Yes                         | Yes           |
| Multi-File Project    | Planned                     | Yes           |
| Storage Entries       | Variable                    | 256 bits      |
| Supported Types       | Docs                        | Docs          |
| Has Interfaces?       | Yes (Rust Traits)           | Yes           |


Note:
Learn more: https://use.ink/ink-vs-solidity
 -->

---

# Write an ink! contract

<img src="https://hackmd.io/_uploads/rkyDxy336.svg" alt="drawing" width="100"/>

---

## Get your terminals out! 

Check `cargo contract --help`

```text
Commands:
  new          Setup and create a new smart contract project
  build        Compiles the contract, generates metadata, bundles both together in a `<name>.contract` file
```

Note:
Go over the commands we'll be using and maybe explore others briefly.

---

### Quest #1: Deploy your contract using the contracts UI! 🎒

1. Build contract _with custom event added_
2. Get testnet tokens
3. Deploy to testnet
5. Interact with your contract

---

## Create a basic ink contract 🪶

1. `cargo contract new <contract-name>`
1. `cargo contract check`
1. `cargo contract build`

----

#### Things to discover

- ink macros
- build contract
- artifacts

----

#### Contract parts

- Storage, structs, derive impls
- Contructors, messages, events

Note:
Additional things depending on questions:
- Can have two constructors: default or some custom (decent runthrough here https://youtu.be/DV5n-GD9sOA?t=1988)

---

### Deploy to testnet 🚀

1. Get dev wallet: https://polkadot.js.org/extension/
1. Get ROCs: https://paritytech.github.io/polkadot-testnet-faucet/ or AlephZero Testnet: https://faucet.test.azero.dev/
3. Deploy using Contracts UI: https://contracts-ui.substrate.io/
4. Block explorer: https://test.azero.dev/#/explorer

---

### Your turn! Add events to complete quest 💪

---

 # More time? 👀⏳

----

## Smart contract limitations

- ink! contracts are a great starting point for developing applications in the Polkadot ecosystem.
- but ... they are exposed to the inherent limitations of the smart contract execution environment

----

## Limitations 

- Sharing of blockspace with other smart contracts, volatile "gas" fees.
- No flexibility for who pays the gas fees.
- Relative poor performance of interpreted smart contract (untrusted) code compared to pre-compiled Parachain runtime (trusted) code.

Note:

Read more from source: https://use.ink/migrate-ink-contracts-to-polkadot-frame-parachain#example-migration

----

## Quest #2: turn an ink! contract into a parachain (bonus) 😎

This workshop hasn't taught you about [FRAME](https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/polkadot_sdk/frame_runtime/index.html) yet, but.. a hugely under appreciated aspect of ink! is how easy it is to turn a prototype written in ink! into it's own appchain. 🤯

----

Time permitting... let's follow the steps [here](https://use.ink/migrate-ink-contracts-to-polkadot-frame-parachain#example-migration) and:

- Create a repo with a basic FRAME pallet structure
- Migrate the parts of your contract to a FRAME pallet

Make sure to join us later to dive deeper into building with the Polkadot SDK!

---

# Resources 🌯

---

### Tools and libs

- Open Brush: https://use.ink/getting-started/use-openbrush
- Swanky Suite: https://use.ink/getting-started/swanky 
- ink! analyzer: https://use.ink/getting-started/ink-analyzer
- Polkadot Contract Wizard: https://contractwizard.xyz/
- ink!athon: https://use.ink/getting-started/inkathon

---

## Learn more

* ink! docs: https://use.ink/
* Runtime vs smart contracts: https://github.com/paritytech/polkadot-sdk/tree/master/substrate/frame

---

## Feedback and quest submission 🙏

<img src="https://hackmd.io/_uploads/B1hzvxn26.png" width=180/>

https://forms.gle/wodrk22muSntbquR6 

