見出し画像

Soneium Examples ①「Contracts」

はじめに

こ記事は以下のSoneiumリポジトリの「Hardhat Example」を元したチュートリアルです。


本チュートリアルの環境

OS: Ubuntu 24.10 x86_64
Node.js: 22.12.0
NPM: 10.9.0
pnmp: 9.15.0

実行環境の準備

まず Soneium Minato にコントラクトをデプロイするための準備をします。

1,EVM ウォレット作成

Metamask 等で準備します。詳細は割愛します。

2,Soneium Minato のネットワーク登録

ウォレットに Soneium Minato ネットワークの登録します。エンドポイントの情報は下記のページに記載されています。

3,テストトークン(ETH)の準備

Sepolia から Soneium Minato への ETH(テストトークン)を以下のSuperbridge ブリッジを利用してして用意します。

4,Node.js インストール

hoge@sample:~$ curl -fsSL https://deb.nodesource.com/setup_22.x -o nodesource_setup.sh
hoge@sample:~$ sudo -E bash nodesource_setup.sh
[sudo] password for hoge:
2024-12-08 14:18:07 - Installing pre-requisites
Hit:1 https://repos-droplet.digitalocean.com/apt/droplet-agent main InRelease
Hit:2 https://repos.insights.digitalocean.com/apt/do-agent main InRelease
Hit:3 http://security.ubuntu.com/ubuntu oracular-security InRelease
Hit:4 http://mirrors.digitalocean.com/ubuntu oracular InRelease
Hit:5 http://mirrors.digitalocean.com/ubuntu oracular-updates InRelease
Hit:6 http://mirrors.digitalocean.com/ubuntu oracular-backports InRelease
Reading package lists... Done
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
apt-transport-https is already the newest version (2.9.8).
ca-certificates is already the newest version (20240203).
curl is already the newest version (8.9.1-2ubuntu2.1).
curl set to manually installed.
gnupg is already the newest version (2.4.4-2ubuntu18).
gnupg set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 38 not upgraded.
Hit:1 https://repos.insights.digitalocean.com/apt/do-agent main InRelease
Get:2 https://deb.nodesource.com/node_22.x nodistro InRelease [12.1 kB]
Hit:3 https://repos-droplet.digitalocean.com/apt/droplet-agent main InRelease
Get:4 https://deb.nodesource.com/node_22.x nodistro/main amd64 Packages [4521 B]
Hit:5 http://security.ubuntu.com/ubuntu oracular-security InRelease
Hit:6 http://mirrors.digitalocean.com/ubuntu oracular InRelease
Hit:7 http://mirrors.digitalocean.com/ubuntu oracular-updates InRelease
Hit:8 http://mirrors.digitalocean.com/ubuntu oracular-backports InRelease
Fetched 16.7 kB in 1s (12.0 kB/s)
Reading package lists... Done
2024-12-08 14:18:13 - Repository configured successfully.
2024-12-08 14:18:13 - To install Node.js, run: apt-get install nodejs -y
2024-12-08 14:18:13 - You can use N|solid Runtime as a node.js alternative
2024-12-08 14:18:13 - To install N|solid Runtime, run: apt-get install nsolid -y
sudo apt-get install -y nodejs

hoge@sample:~$ sudo apt-get install -y nodejs
(省略)

hoge@sample:~$ node -v
v22.12.0

hoge@sample:~$ npm -v
10.9.0

5,pnmp インストール

hoge@sample:~$ curl -fsSL https://get.pnpm.io/install.sh | sh -
==> Downloading pnpm binaries 9.15.0
 WARN  using --force I sure hope you know what you are doing
Copying pnpm CLI from /tmp/tmp.lf8WnQ80LI/pnpm to /home/hoge/.local/share/pnpm/pnpm
Appended new lines to /home/hoge/.bashrc

Next configuration changes were made:
export PNPM_HOME="/home/hoge/.local/share/pnpm"
case ":$PATH:" in
":$PNPM_HOME:") ;;
*) export PATH="$PNPM_HOME:$PATH" ;;
esac

To start using pnpm, run:
source /home/hoge/.bashrc
hoge@sample:~$ source ~/.bashrc

Sample NFT Smart-Contract Project の実行

リポジトリのクローン

このプロジェクトは、基本的な Hardhat の使用例を示しており、サンプルNFTコントラクト、そのコントラクトのテスト、および Soneium Minato にコントラクトをデプロイするように構成された Hardhat Ignition モジュールが含まれています。

hoge@sample:~$ git clone https://github.com/Soneium/soneium-examples.git
Cloning into 'soneium-examples'...
remote: Enumerating objects: 165, done.
remote: Counting objects: 100% (165/165), done.
remote: Compressing objects: 100% (105/105), done.
remote: Total 165 (delta 56), reused 141 (delta 37), pack-reused 0 (from 0)
Receiving objects: 100% (165/165), 978.74 KiB | 19.97 MiB/s, done.
Resolving deltas: 100% (56/56), done.

パッケージのインストール

$ pnpm i
(省略)

デプロイヤーの秘密鍵を設定

Hardhat の構成変数マネージャーを使用して、プライベートキーを設定します。(実行環境の準備で作成したウォレットの秘密鍵を利用)

$ npx hardhat vars set PRIVATE_KEY
✔ Enter value: · ****************************************************************
The configuration variable has been stored in /home/hoge/.config/hardhat-nodejs/vars.json

コントラクトをコンパイル

$ npx hardhat compile
Downloading compiler 0.8.27
Generating typings for: 17 artifacts in dir: typechain-types for target: ethers-v6
Successfully generated 60 typings!
Compiled 15 Solidity files successfully (evm target: paris).

コントラクトをテスト

テストする前に、ローカルのHardhatネットワークにコントラクトをデプロイします。

$ npx hardhat ignition deploy ./ignition/modules/NFT.ts --network hardhat
You are running Hardhat Ignition against an in-process instance of Hardhat Network.
This will execute the deployment, but the results will be lost.
You can use --network <network-name> to deploy to a different network.

Hardhat Ignition 🚀

Deploying [ NftModule ]

Batch #1
 Executed NftModule#DemoNFT

[ NftModule ] successfully deployed 🚀

Deployed Addresses

NftModule#DemoNFT - 0x5FbDB2315678afecb367f032d93F642f64180aa3
$ npx hardhat test ./test/NFT.ts --network hardhat

 NFT

  Deployment
   ✔ Should set the right owner (1009ms)

 1 passing (1s)

コントラクトをデプロイ

デプロイされた環境における不整合(例えば、デプロイヤーアカウントの不整合)により、コントラクトをデプロイする前に手動でignition/deploymentsフォルダーを削除する必要があるかもしれません。

$ npx hardhat ignition deploy ./ignition/modules/NFT.ts
✔ Confirm deploy to network minato (1946)? … yes

Hardhat Ignition 🚀

Deploying [ NftModule ]

Batch #1
 Executed NftModule#DemoNFT

[ NftModule ] successfully deployed 🚀

Deployed Addresses

NftModule#DemoNFT - 0x7f4bc4689918D413CB04C177f784846B85Fbd657

コントラクトの確認

$ npx hardhat verify 0x7f4bc4689918D413CB04C177f784846B85Fbd657 [DEPLOYER_ADDRESS]

Soneium Minato のエクスプローラーでも確認できます。

補足

このサンプルコントラクト自体はシンプルなため、Blockscout のバイトコードデータベースを使う事で、verify をしなくとも検証が完了されます。

以上、

いいなと思ったら応援しよう!