เตรียมเครื่องมือสำหรับ Rust + Substrate Development

Substrate Mar 10, 2023

สวัสดีครับ บทความนี้เป็นการเตรียมเครื่องมือ การติดตั้งต่างๆ สำหรับ Rust + Substrate นะครับ บน Mac OS โดย บทความนี้เป็นบทความอัพเดทเพิ่มเติมจากก่อนหน้านี้ที่ผมเขียนไว้ นั่นเอง

Devahoy - เตรียมเครื่องมือสำหรับ Substrate Development
สำหรับบทความนี้จะเป็นบทความเกี่ยวกับการเตรียมเครื่องมือ การตั้งค่า สำหรับ Substrate Development กันนะครับ เนื่องจาก Substrate นั้น เขียนด้วยภาษา Rust สิ่งที่เราต้องทำก็คือติดตั้ง Rust และเตรียมค

สิ่งที่ต้องการหลักๆ คือ

  • Rust
  • Rust Toolchain
  • Substrate Contract Node
  • Cargo Contract
  • Polkadot.js หรือ Client UI

1. Install Rust

การติดตั้ง Rust จะใช้ Rustup ในการติดตั้ง (Windows จะได้ไฟล์ .exe มา click install ได้เลย)

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

เมื่อติดตั้งเรียบร้อยแล้ว สามารถเช็ค version ของ rustup ได้ แบบนี้

rustup --version

หรือถ้าติดตั้งแล้ว สามารถอัพเดทได้ด้วยคำสั่ง

rustup update

จะได้ผลลัพธ์ประมาณนี้ (ขึ้นอยู่กับ เวอร์ชั่นของแต่ละคน)

rustup 1.25.2 (17db695f1 2023-02-01)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.68.0 (2c8cc3432 2023-03-06)`

สิ่งที่ rustup ติดตั้งมา ก็จะมี

  • rustc - เป็น Compiler สำหรับ Rust
  • cargo - เป็นตัว Package Manager (เหมือน npm ของฝั่ง Node.js)

2. Rust toolchain

ในการพัฒนา Substrate เราต้องปรับ Rust toolchain เป็นเวอร์ชั่น latest stable และเป็นเวอร์ชั่น nightly ด้วย

rustup default stable
rustup update
rustup update nightly
rustup target add wasm32-unknown-unknown --toolchain nightly

ทดสอบดูว่า Rust toolchain ตั้งค่าถูกมั้ย

rustup show

จะได้ผลลัพธ์เป็นแบบนี้ (ตัวอย่างของผมคือ Mac OS)

Default host: aarch64-apple-darwin
rustup home:  /Users/chai/.rustup

installed toolchains
--------------------

stable-aarch64-apple-darwin (default)
stable-x86_64-apple-darwin
nightly-2022-03-14-aarch64-apple-darwin
nightly-aarch64-apple-darwin
bpf
1.56.0-aarch64-apple-darwin

installed targets for active toolchain
--------------------------------------

aarch64-apple-darwin
wasm32-unknown-unknown
wasm32-wasi
x86_64-apple-darwin

active toolchain
----------------

stable-aarch64-apple-darwin (default)
rustc 1.68.0 (2c8cc3432 2023-03-06)

ดูให้แน่ใจว่า installed targets for active toolchain มี wasm32-unknown-unknown

3. Substrate Contracts Node

ตัว Substrate Contracts Node เป็นเหมือนตัวที่ทำให้เราสามารถรัน Node Subtrate บนเครื่อง local ของเราได้ โดยใช้ node-template และ pallet-contracts (ตัว Smart Contract Module)

วิธีการติดตั้ง ติดตั้งผ่าน Cargo (ตัวที่มาพร้อมกับ Rustup)

cargo install contracts-node --git https://github.com/paritytech/substrate-contracts-node.git --force --locked

จากนั้น เราสามารถ start server ได้ด้วยคำสั่ง

substrate-contracts-node --dev

4. Cargo Contract

ตัว Cargo Contract เป็น Command Line Interface สำหรับทำงานร่วมกับ Smart Contract และ ink!

เพิ่ม Component

rustup component add rust-src

ก่อนติดตั้ง ต้องติดตั้ง Dependencies ก่อน

brew install binaryen
สำหรับ Windows ดาวน์โหลดตัว version windows ที่นี่ binaryen release

ติดตั้ง cargo-dylint

cargo install cargo-dylint dylint-link

สุดท้าย ติดตั้ง cargo-contract

cargo install --force --locked cargo-contract

5. Polkadot.js & Client UI

Polkadot.js ส่วนนี้จะเป็นส่วน UI และ Frontend รวมถึง Wallet ครับ เป็น Optional แต่ถ้ามีก็ดีครับ

  • https://polkadot.js.org/apps/# - เป็นเว็บ ที่ทำให้เราดู Block explorer เชื่อมต่อ Wallet ได้ทุกๆ chain/substrate ใน Polkadot ecosystem
  • Polkadot Extension - ตัว Wallet Extension รองรับ Chrome / Firefox เป็นกระเป๋า Wallet คล้ายๆ Metamask แต่สำหรับ Polkadot
  • Contract UI - เป็นเว็บให้เราสามารถ Deploy contract ได้ (ต้องเชื่อมกับ Substrate Contract Node ที่รัน local)
  • Substrate Frontend Template - เป็นตัว Frontend UI ต่อกับ Polkadot Node
  • Subscan - เป็นตัว Block Explorer แบบเดียวกับ Etherscan

References

Tutorials | Substrate_ Docs
If you are someone who likes to learn by doing, the Substrate tutorials are a…
GitHub - substrate-developer-hub/awesome-substrate: A curated list of awesome projects and resources related to the Substrate blockchain development framework.
A curated list of awesome projects and resources related to the Substrate blockchain development framework. - GitHub - substrate-developer-hub/awesome-substrate: A curated list of awesome projects…

Tags

Chai Phonbopit

เป็น Web Dev ทำงานมา 10 ปีหน่อยๆ ด้วยภาษา JavaScript, Node.js, React, Vue และปัจจุบันกำลังสนใจ Web3, Crypto และ Blockchain เขียนบล็อกที่ https://devahoy.com