You are on page 1of 13

Ink!

智能合约
王大锤

Engineer@Parity
maggiedong@parity.io
Substrate Smart Contracts

Built using Rust and Wasm.


Why WebAssembly?

Wasm is a platform independent


executable format
● Wasm is Sandboxed
● Wasm is Fast
● Wasm is Compact
● Wasm is Well Supported
Why Rust?

Parity is a Rust company as much


as it is a blockchain company.
● Rust builds directly to Wasm
● Rust has a rapidly developing
ecosystem
● Rust code is more ergonomic
and easier to get right!
● You technically don’t need to use Rust.
合约模块
ink! Solidity

Virtual Machine Any Wasm VM EVM

Encoding Wasm EVM Byte Code

Language Rust Standalone

Overflow Protection Enabled by default None

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


环境初始化设置
https://docs.substrate.io/tutorials/smart-contracts/prepare-your-first-contract/

rustup component add rust-src --toolchain nightly

rustup target add wasm32-unknown-unknown --toolchain stable

brew install binaryen

# 安装substrate contracts node 节点

git clone https://github.com/paritytech/substrate-contracts-node.git

git checkout v0.20.0

# 安装contract插件

cargo install dylint-link

cargo install cargo-contract --version 2.0.0-alpha.1


合约架构

存储 #[ink(storage)]

合约实例化方法#[ink(constructor)]

公共方法(用户可调用)#[ink(message)]

事件 #[ink(event)]
Ink! 支持的存储类型

Common types: Ink! types: Substrate types:


bool, Mapping AccountId

u{8,16,32,64,128}, (ink storage正在经历重构) Balance

i{8,16,32,64,128}, Hash

String,

Tuples,
合约调用runtime中的方法
pallet-contracts & ink!
Ink contract

ink_env wasm
Host functions
EnvBackend

offchain onchain pallet-contracts

seal_call
seal_transfer
Std impl ext::xxx
工具推荐

https://polkadot.js.org/apps

https://contracts-ui.substrate.io/
Feel free to add your
question

https://github.com/paritytech/ink-docs/issues

You might also like