<style type="text/css">
.reveal {
font-size: 22px;
}
</style>
### 使用 Rust 在 Vara Network 上构建 Wasm 智能合约应用
###### --- 15 分钟上手 dApp 开发

https://vara.network
<address class="author">
航标 | Gear Foundation 开发者关系工程师
</address>
---
### Vara Network
Substrate-based, Proof-of-Stake, Standalone, Layer-1 network powered by the __Gear Protocol__
The mission of Vara is to bridging the gap between web2 and web3.
### Gear Protocol
Next-gen WASM smart contract engine for Substrate with innovative features and functionalities, including:
- Actor Model
- Parallel Execution
- Gas Reservation
- Delayed Execution & Automation
- Async Messages
- ... (https://wiki.gear-tech.io/)
---
### Whoami
- 😎 Arch Linux User Repository Contributor
- 👨💻 Once an SRE at a Legal Tech Startup
- ⚙️ Validator Manager / DevRel at Gear Foundation
Projects:
- 🐳 [kubernot](https://github.com/kubernot/kubernot)
- 🐚 [subshell](https://github.com/subdirectory/subshell)
- 📗 [cargo-docs](https://github.com/btwiuse/cargo-docs)
---
### same, same, but different
| Git | Blockchain |
| --- | --- |
| commit | block |
| root-commit | genesis block |
| commit hash / - | block hash / number |
| committer | block author (miner) |
| previous commit | previous block |
| branches | forks |
| file | address |
| diff | transaction |
| manual review | automated consensus |
| - | tokenomics |
---
WebAssembly, Rust
Web3 / Distributed Ledger Technology / Blockchain / Smart Contracts
- bitcoin
- ethereum
- evm/solidity
- polkadot/substrate
- gear protocol/rust
- pallet-contract/ink!
- pallet-evm/solidity

---
把关系捋顺...
- 1994: Nick Szabo 首次提出 Smart contract 的概念
- 2008: Bitcoin 白皮书发布
- 2009: Bitcoin Network 启动
- 2013: Vitalik Buterin 成立 Ethereum 项目
- 2014: Gavin Wood 作为 CTO 撰写 Ethereum 黄皮书 并设计了 Solidity 语言
- 2015: WebAssembly 项目首次被提出
- 2015: Rust 1.0 发布
- ↓
---
- ↑
- 2015: Gavin Wood 成立 ParityTech (原EthCore)
- 2016: Gavin Wood 发布 Polkadot 白皮书,异构多链架构
- 2017: Web3 Foundation 成立 / parity-{bitcoin,ethereum,zcash,wallet}
- 2017: WebAssembly MVP 标准发布
- 2018: Gavin Wood 发布 Substrate 框架
- 2019: W3C 正式将 WebAssembly 1.0 纳入 Web 标准
- 2019: ink! 智能合约语言 (eDSL) 面世
- 2020: Polkadot Network 启动
- 2021: Nikolay Volf 成立 Gear Technologies
- 2022: WebAssembly 2.0 草案发布
- 2022: Gear Protocol 白皮书发布
- 2023: Vara Network 启动
---
### So, What is a Smart Contract?
- A smart contract is a computer program or a transaction protocol that is intended to automatically execute, control or document events and actions according to the terms of a contract or an agreement
- The Ethereum white paper describes Bitcoin as a weaker version of smart contracts (distributed ledger), proposing a stronger version of smart contracts (distributed state machines) based on the Solidity language, which is Turing complete.
- The term "smart contract" has been more specifically associated with general-purpose computation on a blockchain or distributed ledger since the launch of the Ethereum blockchain in 2015.
---

> A self-executing contract with the terms of the agreement between buyer and seller being directly written into lines of code.
Vending machines are considered the earliest form of technology similar to smart contracts.
---
## There's no "smart contracts"

## Just "synchronized state machines running on an unstoppable peer-to-peer network of computers"
---
### Examples dApps
- DAO: On-Chain Governance
- Gaming: Fully On-Chain Games, zkPokers
- DeFi: Dexes, Swaps
- Collectibles: CryptoKitties
- Supply Chain Management
---
### Why Wasm
> 基于区块链的智能合约平台是去中心化的云计算网络。你的云应用程序(即智能合约)不是由中心 Operator 运行所有工作负载,而是由网络中的节点执行。由于智能合约不受信任并且必须非常频繁地执行(每台计算机每秒数百次),因此 Wasm 是该应用场景的理想执行引擎。事实上,几乎所有领先的智能合约区块链网络都采用了 Wasm
(Polkadot, NEAR, Dfinity, __GEAR__, Filecoin, Ripple, EOS, CosmWasm, Quai Network)
--- [Introducing the Wasm landscape, CNCF 2023](https://www.cncf.io/blog/2023/09/06/introducing-the-wasm-landscape/)
---
WASM vs EVM

---
### Rust vs Solidity

https://medium.com/@VaraNetwork/solidity-vs-rust-which-programming-language-should-you-choose-for-web3-apps-20513bda1bcc
---
### Rust: strengths and weaknesses

---
### Actor Model in Gear Protocol

---
一切皆 Actor
每个 Actor 封装了自己的状态和行为
通过消息传递进行通信,而不是共享内存
在 Actor 处理收到的消息时,它可以:
- 向另一个 Actor 发送消息
- 创建新的 Actor
- 改变其内部状态
> Note: Gear Protocol 在传统的 Actor 模型上额外保证了合约(Program)间消息的顺序
---
### `gstd` - Gear 智能合约标准库

https://docs.rs/gstd/1.0.3/gstd/
---
### solidity vs gstd API comparison
| solidity | gstd |
| --- | --- |
| `address` | `gstd::ActorId` |
| `event` | `enum`/`struct` |
| `enum` | `enum` |
| `map` | `gstd::prelude::collections::{HashMap,BTreeMap}` |
| `string` | `gstd::prelude::String` |
| `[]` | `gstd::prelude::Vec` |
| `call`/`emit` | `gstd::msg::send` |
| `msg.data` | `gstd::msg::load_bytes` |
| `msg.value` | `gstd::msg::value` |
| `msg.sender` | `gstd::msg::source` |
| `address(this)` | `gstd::exec::program_id` |
| `block.number` | `gstd::exec::block_height` |
| `block.timestamp` | `gstd::exec::block_timestamp` |
---
### notable differences
| solidity | gstd |
| --- | --- |
| `msg.origin` | - |
| `payable` | - |
| - | `gstd::exec::wait_for` |
| - | `gstd::msg::send_delayed` |
| - | `gstd::exec::system_reserve_gas` |
| `contract` | - |
| `constructor` | `init` |
| contract methods | `handle` |
| `console.log` | `gstd::debug!`/`gstd::dbg!` |
---
### flipper contract: Solidity
```
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Flipper {
bool public lastFlipResult;
event Flip(bool result);
function flip() external {
// Perform the flip
bool result = !lastFlipResult;
// Store the result
lastFlipResult = result;
// Emit the event
emit Flip(result);
}
}
```
---
### flipper contract: Gear
./io/src/lib.rs
```
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;
#[derive(Debug, Decode, Encode, TypeInfo)]
pub enum FlipperAction {
Flip,
}
#[derive(Debug, Decode, Encode, TypeInfo)]
pub enum FlipperEvent {
FlippedTo(u8),
}
```
./src/lib.rs
```
#![no_std]
mod io;
use io::{FlipperAction, FlipperEvent};
static mut FlipperState: bool = false;
#[no_mangle]
unsafe extern "C" fn handle() {
let action: FlipperAction = gstd::msg::load().expect("failed to load input message");
match action {
FlipperAction::Flip => {
FlipperState = !FlipperState;
let event = FlipperEvent::FlippedTo(FlipperState as u8);
gstd::msg::reply(event, 0).expect("failed to send response");
},
}
}
```
---
./build.rs
```
fn main() {
gear_wasm_builder::build();
}
```
./rust-toolchain.toml
```
[toolchain]
channel = "nightly"
targets = [ "wasm32-unknown-unknown" ]
profile = "minimal"
```
./Cargo.toml
```
[package]
name = "gear-flipper"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
gstd = { git = "https://github.com/gear-tech/gear.git", branch = "stable" }
scale-info = { version = "2", default-features = false, features = ["derive"] }
parity-scale-codec = { version = "3", default-features = false, features = ["derive"] }
[build-dependencies]
gear-wasm-builder = { git = "https://github.com/gear-tech/gear.git", branch = "stable" }
[dev-dependencies]
gtest = { git = "https://github.com/gear-tech/gear.git", branch = "stable" }
```
---
# Demo: 在 Vara Testnet 上部署应用
https://github.com/gear-foundation/dapps-template
<p align="center">
<a href="https://vscode.k1s.io">
<img src="https://gitpod.io/button/open-in-gitpod.svg" width="240" alt="GEAR">
</a>
</p>
- 准备工作: 配置钱包
- 安装 Polkadot.js extension
- https://polkadot.js.org/extension
- 生成随机钱包地址
- 右上角 + => Create new account => 保存助记词 => 设置账户名称 / 密码
- 网页部署工具 Gear IDEA
- 测试网水龙头
- https://idea.gear-tech.io
- 上传 wasm blobs
---
# Thanks!
<img src="https://hackmd.io/_uploads/SyK8P72d2.jpg" width="240" alt="GEAR">
<img src="https://hackmd.io/_uploads/By_kgE3_2.png" width="240" alt="VARA">
https://vara.network
{"title":"使用 Rust 在 Vara Network 上构建 Wasm 智能合约应用","description":"本 PPT 链接: https://hackmd.io/@btwiuse/insc","contributors":"[{\"id\":\"94262fbf-81ae-4ed7-933c-561a41bd977a\",\"add\":13013,\"del\":7422}]"}