Swanky Suite - WASM smart contract development environment preparation

This article describes preparations for using Swanky Suite as a convenient tool for developing smart contracts using WASM (WebAssembly) .

# Introduction

Swanky Suite is developed by the community of Astar Network , a public blockchain from Japan, and aims to be an "all-in -one " tool for Wasm smart contract developers . It is a so-called IDE .

https://medium.com/astar-network/swanky-the-all-in-one-wasm-tool-50c0ed9f07a6

At the time of writing, the latest version of swanky-cli is v1.0.7 .

https://github.com/AstarNetwork/swanky-cli/releases

# Development environment goals

In this article, we aim to create the following environment.


> Creating an environment for developing WASM smart contracts for blockchains built with the Substrate framework (Shiden/Astar Network, etc.)

| ITEM | INFO |
|:-----------:|:------------|
| OS       | Windows 10 / 11|
| Windows Subsystem for Linux| Ubuntu 22.04.1 LTS| 
| development language	| ink!  (Rust-based smart contract development language for blockchain made with Substrate framework)|
| Integrated Development Environment (IDE)| Swanky Suite |


Make a test project with the flipper template and check until it compiles.


# This environment

Install in the Windows Subsystem for Linux environment. This was the target because there were more considerations than using it in the Linux / macOS environment.

* Ubuntu 22.04.01 LTS on WSL

# Preparation

Prepare before introducing Swanky Suite.
Based on the following information.

* https://docs.astar.network/docs/wasm/sc-dev/env-setup
* https://docs.substrate.io/install/windows/
* https://learn.microsoft.com/en-us/windows/wsl/setup/environment
* https://use.ink/getting-started/setup/
* https://github.com/paritytech/cargo-contract#installation

## WSL Setup

Have a Windows machine ready to use your WSL = Windows Subsystem for Linux. Installation is easy if you are using Windows 10 or Windows 11.
I will omit the details of installing WSL, but please refer to the following.

https://learn.microsoft.com/ja-jp/windows/wsl/install


```Poiwershell: PowerShell
> wsl --install
```
## Install Ubuntu
I have the latest Ubuntu 22.04.01 LTS installed at the time of writing. It is easily deployable from the Microsoft Store.
(This installation is omitted.)

## Setup after installing Ubuntu
After installing Ubuntu, boot Ubuntu from the start menu. After waiting for a while , create a general user and set a password to use with sudo, and it will be available.

![w1.PNG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/75458/d0b97422-376b-3423-5cbe-47ebc5754cc1.png)


## Install Rust

Install Rust.

> By the way, Rust is the most used language for WASM development. [The State of WebAssembly 2022](https://blog.scottlogic.com/2022/06/20/state-of-wasm-2022.html)

```shell-session: Bash
hoge@DESKTOP-QVCDBPV:~$ sudo apt update
hoge@DESKTOP-QVCDBPV:~$ sudo apt install --assume-yes git clang curl libssl-dev llvm libudev-dev make protobuf-compiler
hoge@DESKTOP-QVCDBPV:~$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
info: downloading installer

Welcome to Rust!

This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.

Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:

  /home/hoge/.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory is located at:

  /home/hoge/.cargo

This can be modified with the CARGO_HOME environment variable.

The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:

  /home/hoge/.cargo/bin

This path will then be added to your PATH environment variable by
modifying the profile files located at:

  /home/hoge/.profile
  /home/hoge/.bashrc

You can uninstall at any time with rustup self uninstall and
these changes will be reverted.

Current installation options:


   default host triple: x86_64-unknown-linux-gnu
     default toolchain: stable (default)
               profile: default
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
> 1



  stable-x86_64-unknown-linux-gnu installed - rustc 1.66.0 (69f9c33d7 2022-12-12)


Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).

To configure your current shell, run:
source "$HOME/.cargo/env"
hoge@DESKTOP-QVCDBPV:~$
```

Update your shell environment to include Cargo.

> Cargo is a build system and package manager for Rust.

```shell-session: Bash
hoge@DESKTOP-QVCDBPV:~$ source ~/.cargo/env
```

Check Rust version.

```shell-session: Bash
hoge@DESKTOP-QVCDBPV:~$ rustc --version
rustc 1.66.0 (69f9c33d7 2022-12-12)
```

Use rustup to configure the Rust toolchain to use the latest stable version as the default toolchain.

```shell-session: Bash
hoge@DESKTOP-QVCDBPV:~$ rustup default stable
info: using existing install for 'stable-x86_64-unknown-linux-gnu'
info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'

  stable-x86_64-unknown-linux-gnu unchanged - rustc 1.66.0 (69f9c33d7 2022-12-12)

hoge@DESKTOP-QVCDBPV:~$ rustup update
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: checking for self-updates

  stable-x86_64-unknown-linux-gnu unchanged - rustc 1.66.0 (69f9c33d7 2022-12-12)

info: cleaning up downloads & tmp directories
```

Add nightly to your Rust toolchain version and WASM target nightly as your development environment.


```shell-session: Bash
hoge@DESKTOP-QVCDBPV:~$ rustup update nightly


  nightly-x86_64-unknown-linux-gnu installed - rustc 1.68.0-nightly (9c07efe84 2022-12-16)

info: checking for self-updates

hoge@DESKTOP-QVCDBPV:~$ rustup target add wasm32-unknown-unknown --toolchain nightly


info: downloading component 'rust-std' for 'wasm32-unknown-unknown'
info: installing component 'rust-std' for 'wasm32-unknown-unknown'
 18.7 MiB /  18.7 MiB (100 %)   9.0 MiB/s in  2s ETA:  0s
```

Check the configured Rust toolchain.

```shell-session: Bash
hoge@DESKTOP-QVCDBPV:~$ rustup show
Default host: x86_64-unknown-linux-gnu
rustup home:  /home/hoge/.rustup

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

stable-x86_64-unknown-linux-gnu (default)
nightly-x86_64-unknown-linux-gnu

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

stable-x86_64-unknown-linux-gnu (default)
rustc 1.66.0 (69f9c33d7 2022-12-12)
```

```shell-session: Bash
hoge@DESKTOP-QVCDBPV:~$ rustup +nightly show
Default host: x86_64-unknown-linux-gnu
rustup home:  /home/hoge/.rustup

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

stable-x86_64-unknown-linux-gnu (default)
nightly-x86_64-unknown-linux-gnu

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

wasm32-unknown-unknown
x86_64-unknown-linux-gnu

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

nightly-x86_64-unknown-linux-gnu (overridden by +toolchain on the command line)
rustc 1.68.0-nightly (9c07efe84 2022-12-16)
```

## Install ink! CLI 

In this environment, an error occurs on the way, so install pkg-config first.

```shell-session: Bash
hoge@DESKTOP-QVCDBPV:~$ sudo apt-get install pkg-config
```
### Install cargo-contract 

```shell-session: Bash
hoge@DESKTOP-QVCDBPV:~$ cargo install cargo-dylint dylint-link



  Installing /home/takeshi/.cargo/bin/dylint-link
   Installed package `dylint-link v2.1.1` (executable `dylint-link`)
     Summary Successfully installed cargo-dylint, dylint-link!

hoge@DESKTOP-QVCDBPV:~$ cargo install cargo-contract --force --locked

  Installing cargo-contract v1.5.1
warning: package `blake2 v0.10.4` in Cargo.lock is yanked in registry `crates-io`, consider running without --locked
warning: package `cpufeatures v0.2.2` in Cargo.lock is yanked in registry `crates-io`, consider running without --locked



   Compiling parity-wasm v0.45.0
   Compiling clap v3.2.16
   Compiling subxt v0.23.0
    Finished release [optimized] target(s) in 2m 46s
  Installing /home/hoge/.cargo/bin/cargo-contract
   Installed package `cargo-contract v1.5.1` (executable `cargo-contract`)

```

## Update Node.js

At the time of writing this article, it was an update required when installing swanky-cli , which will be described later, so I am running it.

* This Ubuntu 22.04.1 has a lower nodejs version (12.22.9)
* Update repository, put updated nodejs, then update npm (8.19.2 -> 9.2.0)


```shell-session: Bash
hoge@DESKTOP-QVCDBPV:~$ curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - 
hoge@DESKTOP-QVCDBPV:~$ sudo apt show nodejs
hoge@DESKTOP-QVCDBPV:~$ sudo apt-get install -y nodejs
hoge@DESKTOP-QVCDBPV:~$ node -v
v18.12.1

hoge@DESKTOP-QVCDBPV:~$ sudo npm install -g npm@9.2.0
hoge@DESKTOP-QVCDBPV:~$ npm -v
9.2.0
```

# Swanky Suite

## Install swanky-cli 

I'm using v1.0.7 at the time of posting this article.

```shell-session: Bash
hoge@DESKTOP-QVCDBPV:~$ sudo npm install -g @astar-network/swanky-cli@1.0.7

added 411 packages in 30s

49 packages are looking for funding
  run `npm fund` for details

hoge@DESKTOP-QVCDBPV:~$ swanky --version
@astar-network/swanky-cli/1.0.7 wsl-x64 node-v18.12.1
```

## Install yarn

``swanky init`` Some errors occur when executing, so as a workaround, install yarn.


```shell-session: Bash
hoge@DESKTOP-VG4GHSG:~$ curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
hoge@DESKTOP-VG4GHSG:~$ echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
hoge@DESKTOP-VG4GHSG:~$ sudo apt-get update && sudo apt-get install yarn

hoge@DESKTOP-VG4GHSG:~$ yarn -v
1.22.19
```

## Create a test project and compile

※ ``swanky init`` Before you do, you need to register git information.

```shell-session: Bash
hoge@DESKTOP-VG4GHSG:~$ git config --global user.name "hoge"
```

``swanky init`` After execution, it will be interactive, so select and enter as follows to proceed.

```shell-session: Bash
hoge@DESKTOP-VG4GHSG:~$ swanky init test_pj
? Which contract language should we use? ink
? Which contract template should we use initially? flipper
? What should we name your initial contract? sample
? What is your name? hoge
? What is your email?
? Do you want to download Swanky node? Yes
✔ Checking dependencies OK
✔ Copying template files OK
✔ Processing templates OK
✔ Initializing git OK
✔ Downloading Swanky node OK
✔ Installing dependencies OK
✔ Writing config OK
🎉 😎 Swanky project successfully initialised! 😎 🎉
hoge@DESKTOP-VG4GHSG:~$
```

When compiling, an error occurs, so execute the following.

* Can't build with standard library: add rust-src to development environment with rustup
* Missing wasm-opt: Install binaryen (https://github.com/WebAssembly/binaryen#tools)


```shell-session: Bash
hoge@DESKTOP-VG4GHSG:~$ rustup component add rust-src --toolchain nightly-x86_64-unknown-linux-gnu
info: downloading component 'rust-src'
info: installing component 'rust-src'

hoge@DESKTOP-VG4GHSG:~$ sudo apt-get install binaryen
```

Go to the created directory and compile.


```shell-session: Bash
hoge@DESKTOP-VG4GHSG:~$ cd test_pj

hoge@DESKTOP-VG4GHSG:~/test_pj$ swanky contract compile sample -v

======== Found contracts ========
        sample
======== Compiling sample ========
Checking with toolchain `nightly-2022-06-30-x86_64-unknown-linux-gnu`
   Compiling proc-macro2 v1.0.47
   Compiling unicode-ident v1.0.5



   Compiling metadata-gen v0.1.0 (/tmp/cargo-contract_dxfxum/.ink/metadata_gen)
    Finished release [optimized] target(s) in 17.88s
     Running `target/ink/release/metadata-gen ''`
======== Compiled sample ========
======== Compiled all contracts ========
======== Compiling Typechain' code ========
@polkadot/util has multiple versions, ensure that there is only one installed.
Either remove and explicitly install matching versions or dedupe using your package manager.
The following conflicting packages were found:
        cjs 10.2.1      node_modules/@polkadot/util/cjs
        cjs 9.7.2       node_modules/@supercolony/typechain-polkadot/node_modules/@polkadot/util/cjs
======== Compiled Typechain' code ========
✔ Contract compiled successfully
✔ Copying artifacts OK

hoge@DESKTOP-VG4GHSG:~/test_pj$ 
```


# Conclusion

In this environment (Windows - WSL), in order to create an environment while removing errors, I took a roundabout way, but I was able to complete the compilation properly.

That's all,