
前回の記事では
開発環境へのインストール、アンインストールを
主に紹介しました。
今回は開発環境は揃いましたので
その開発環境を使い
ローカルのブロックチェーンネットワークに接続する内容です。
[getPhpFile() file=’Appeal_01′ html_file=’Appeal_05.html’]
ローカル環境のセットアップ

ローカル環境のセットアップを進めるにあたり
少し私自身が手間取った内容をまとめておきます。
もともとは、
「SolidityとEthereumによる実践スマートコントラクト開発」
を参考にしつつ、勉強を進めておりました。
内容が2021年の内容のため
現在の2023年の各アプリケーションのバージョンが
更新されているため
内容も変更がかかっています。
現在の私の環境のバージョンです
node.jsはカレントのバージョンを切り替えています
それでもこの後の問題は発生しています。
$ Truffle version
Truffle v5.9.0 (core: 5.9.0)
Ganache v7.8.0
Solidity v0.5.16 (solc-js)
Node v16.15.0
Web3.js v1.10.0・Truffle initの挙動が違う
現在のバージョンでは
Truffle initを実行しても
contractsの配下に Migrations.sol
migrationsの配下に 1_initial_migration.js
が作成されない
$ truffle init
Starting init...
================
> Copying project files to /Users/kazuyukiotani/newpro
Init successful, sweet!
Try our scaffold commands to get started:
$ truffle create contract YourContractName # scaffold a contract
$ truffle create test YourTestName # scaffold a test
http://trufflesuite.com/docs
よって、unboxコマンドを利用します。
$ truffle unbox
Starting unbox...
=================
✓ Preparing to download box
✓ Downloading
✓ Cleaning up temporary files
✓ Setting up box
Unbox successful, sweet!
Commands:
Compile: truffle compile
Migrate: truffle migrate
Test: truffle test
こちらだと、今回の検証などで使うファイルが準備されています。
init = プロジェクトの作成
unbox = すでに用意されたプロジェクトを利用
のようなイメージかと・・・
・Truffle testでエラーが発生する
truffle unboxを利用して
プロジェクトを準備
truffle test実施でエラーが発生します。
$ truffle test
This version of µWS is not compatible with your Node.js build:
Error: Cannot find module '../binaries/uws_darwin_arm64_93.node'
Require stack:
- /opt/homebrew/Cellar/truffle/5.9.0/libexec/lib/node_modules/truffle/node_modules/ganache/node_modules/@trufflesuite/uws-js-unofficial/src/uws.js
- /opt/homebrew/Cellar/truffle/5.9.0/libexec/lib/node_modules/truffle/node_modules/ganache/dist/node/core.js
- /opt/homebrew/Cellar/truffle/5.9.0/libexec/lib/node_modules/truffle/build/test.bundled.js
- /opt/homebrew/Cellar/truffle/5.9.0/libexec/lib/node_modules/truffle/node_modules/original-require/index.js
- /opt/homebrew/Cellar/truffle/5.9.0/libexec/lib/node_modules/truffle/build/cli.bundled.js
Falling back to a NodeJS implementation; performance may be degraded.
> Something went wrong while attempting to connect to the network at http://localhost:8545. Check your network configuration.
CONNECTION ERROR: Couldn't connect to node http://localhost:8545.
Truffle v5.9.0 (core: 5.9.0)
Node v16.15.0
エラーの原因を調べており
nodeのバージョンを変更すれば動くなどの情報はありましたが
nodeのバージョンを色々試したが改善せずです。
その後の動きに今は問題なさそうなので
一旦すすめます
今後も対策を調査します。
ちなみに、node.jsのバージョンを切り替える時にも少し
ハマったので共有です。
・node.jsのバージョンの切り替え
現在インストール可能なバージョンを調べます
$ nodebrew ls-remote
v0.0.1 v0.0.2 v0.0.3 v0.0.4 v0.0.5 v0.0.6
:
: 大量なので割愛
:
v19.0.0 v19.0.1 v19.1.0 v19.2.0 v19.3.0 v19.4.0 v19.5.0 v19.6.0
v19.6.1 v19.7.0 v19.8.0 v19.8.1 v19.9.0
v20.0.0 v20.1.0 v20.2.0
io@v1.0.0 io@v1.0.1 io@v1.0.2 io@v1.0.3 io@v1.0.4 io@v1.1.0 io@v1.2.0 io@v1.3.0
io@v1.4.1 io@v1.4.2 io@v1.4.3 io@v1.5.0 io@v1.5.1 io@v1.6.0 io@v1.6.1 io@v1.6.2
io@v1.6.3 io@v1.6.4 io@v1.7.1 io@v1.8.1 io@v1.8.2 io@v1.8.3 io@v1.8.4
io@v2.0.0 io@v2.0.1 io@v2.0.2 io@v2.1.0 io@v2.2.0 io@v2.2.1 io@v2.3.0 io@v2.3.1
io@v2.3.2 io@v2.3.3 io@v2.3.4 io@v2.4.0 io@v2.5.0
io@v3.0.0 io@v3.1.0 io@v3.2.0 io@v3.3.0 io@v3.3.1 必要なバージョンをインストール
$ nodebrew install-binary <バージョン指定>現在インストールされている一覧を確認
$ nodebrew ls
v16.0.0
v16.15.0
v19.9.0
v20.1.0
current: v16.15.0切り替えたいバージョンを指定する
$ use v20.1.0
use v20.1.0
//currentが切り替わったことを確認
$ nodebrew ls
v16.0.0
v16.15.0
v19.9.0
v20.1.0
current: v20.1.0
truffleのバージョン確認でnode.jsのバージョンが変わっていることを確認
$ truffle version
Truffle v5.9.0 (core: 5.9.0)
Ganache v7.8.0
Solidity v0.5.16 (solc-js)
//変更されていることを確認
Node v20.1.0
Web3.js v1.10.0
ただ、私の場合はこれでも切り替えができなかったので
homebrew側のnode指定を確認
$ brew search node@
==> Formulae
libbitcoin-node node-build node@16 nodeenv
linode-cli node-sass node@18 nodenv
llnode node@10 node_exporter ode
node ✔ node@14 nodebrew ✔
nodebrewを利用しているため、nodeをunlink
必要に応じて、再度linkするなど
//必要ないバージョンをアンリンク
$ brew unlink node
//必要に応じて再度リンク
$ brew link <リンクさせたいnode>念の為、ターミナルを閉じ、
ターミナルを立ち上げ
truffle version でnodeのバージョンが
切り替わっていることを確認しました。
では本題に戻りましょう
プロジェクトフォルダの作成

まずはプロジェクトフォルダの作成からです。
大体はログインした
(macではターミナルを立ち上げた場所)かと思います
今回のプロジェクトのフォルダを作成しましょう。
$ mkdir testProject次に、フォルダに移動して
下記コマンドを実行します
$ ce testProject
$ Truffle unbox <target project>何も指定しない場合には
metacoin というプロジェクトが自動に適用されます
Starting unbox...
=================
✓ Preparing to download box
✓ Downloading
✓ Cleaning up temporary files
✓ Setting up box
Unbox successful, sweet!
Commands:
Compile: truffle compile
Migrate: truffle migrate
Test: truffle test
また、
https://trufflesuite.com/boxes/
に各種boxが準備されています。
テスト実行

対象のプロジェクトのフォルダ内で
truffle test を入力すればテスト実行は可能
この場合は全てのテストを実施です
ただし、現段階では、下記エラーがでます。
$ truffle test
This version of µWS is not compatible with your Node.js build:
Error: Cannot find module '../binaries/uws_darwin_arm64_115.node'
Require stack:
- /opt/homebrew/Cellar/truffle/5.9.0/libexec/lib/node_modules/truffle/node_modules/ganache/node_modules/@trufflesuite/uws-js-unofficial/src/uws.js
- /opt/homebrew/Cellar/truffle/5.9.0/libexec/lib/node_modules/truffle/node_modules/ganache/dist/node/core.js
- /opt/homebrew/Cellar/truffle/5.9.0/libexec/lib/node_modules/truffle/build/test.bundled.js
- /opt/homebrew/Cellar/truffle/5.9.0/libexec/lib/node_modules/truffle/node_modules/original-require/index.js
- /opt/homebrew/Cellar/truffle/5.9.0/libexec/lib/node_modules/truffle/build/cli.bundled.js
Falling back to a NodeJS implementation; performance may be degraded.
> Something went wrong while attempting to connect to the network at http://localhost:8545. Check your network configuration.
CONNECTION ERROR: Couldn't connect to node http://localhost:8545.
Truffle v5.9.0 (core: 5.9.0)
Node v20.1.0
「Falling back to a NodeJS implementation; performance may be degraded.」
については、性能低下といっており、一旦無視
「Something went wrong while attempting to connect to the network at http://localhost:8545. Check your network configuration.
CONNECTION ERROR: Couldn’t connect to node http://localhost:8545.」
はネットワークに繋がらないっていってます。
そもそもローカルネットワーク立ち上げていないし
設定も確認が必要ですね。
testの内容については、いったんここでは深く確認せず
次へ行きます。
コンパイル実行

truffle compile でコンパイルは可能です、
$ truffle compile
Compiling your contracts...
===========================
> Compiling ./contracts/ConvertLib.sol
> Compiling ./contracts/MetaCoin.sol
> Compiling ./contracts/Migrations.sol
> Artifacts written to /Users/xxxxxxxxxxx/testProject/build/contracts
> Compiled successfully using:
- solc: 0.5.16+commit.9c3226ce.Emscripten.clang「Compiled successfully using:」
どうもコンパイルは成功しているみたいですね。
マイグレーション

truffle-config.js に開発環境の接続情報の記載部分があるので
development 部分の
portを「7545」に修正
localhost とはループバックアドレスといい
「127.0.0.1」と同じ意味なので修正の必要はありません
自分自身のアドレスを参照します。
$ vi truffle-config.js
module.exports = {
networks: {
development: {
host: "localhost",
port: 7545, //ここを修正
network_id: "*", // Match any network id
gas: 5000000
}
},
compilers: {
solc: {
settings: {
optimizer: {
enabled: true, // Default: false
runs: 200 // Default: 200
},
}
}
}
};
修正後はcat等で内容が正しく更新されているか
確認しておきましょう
$ cat truffle-config.js
module.exports = {
networks: {
development: {
host: "localhost",
port: 7545,
network_id: "*", // Match any network id
gas: 5000000
}
},
compilers: {
solc: {
settings: {
optimizer: {
enabled: true, // Default: false
runs: 200 // Default: 200
},
}
}
}
};次に「Ganache」を起動させます。
Ganacheは画面が存在するため
アプリケーションを起動させてください。

「NEW WORKSPACE」を選択し「ADD PRPJECT」から
先ほど修正した「truffle-config.js」を指定します

次にGanacheの右上の「START」をクリックします。

すると下記画面に切り替わり、ローカル上で
イーサリアムブロックチェーンが動いている状況が確認できます

ではtruffleからマイグレーションさせます。
マイグレーションは
truffle migrate のコマンドで実施します。
$ truffle migrate
This version of µWS is not compatible with your Node.js build:
Error: Cannot find module '../binaries/uws_darwin_arm64_115.node'
Require stack:
- /opt/homebrew/Cellar/truffle/5.9.0/libexec/lib/node_modules/truffle/node_modules/ganache/node_modules/@trufflesuite/uws-js-unofficial/src/uws.js
- /opt/homebrew/Cellar/truffle/5.9.0/libexec/lib/node_modules/truffle/node_modules/ganache/dist/node/core.js
- /opt/homebrew/Cellar/truffle/5.9.0/libexec/lib/node_modules/truffle/build/migrate.bundled.js
- /opt/homebrew/Cellar/truffle/5.9.0/libexec/lib/node_modules/truffle/node_modules/original-require/index.js
- /opt/homebrew/Cellar/truffle/5.9.0/libexec/lib/node_modules/truffle/build/cli.bundled.js
Falling back to a NodeJS implementation; performance may be degraded.
Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.
Starting migrations...
======================
> Network name: 'development'
> Network id: 5777
> Block gas limit: 6721975 (0x6691b7)
1_initial_migration.js
======================
Deploying 'Migrations'
----------------------
> transaction hash: 0xa765fbad2ee6fc01f349365a4e86d07e1241ce9a6f562aab1e61c919c3397b15
> Blocks: 0 Seconds: 0
> contract address: 0x45d444aBa1f55b2d534CF3124e88595c7dE29D4F
> block number: 1
> block timestamp: 1684689350
> account: 0x1D3F3388d3693980Bdb6C24052bfbc021Fec4f5D
> balance: 99.99942764725
> gas used: 169586 (0x29672)
> gas price: 3.375 gwei
> value sent: 0 ETH
> total cost: 0.00057235275 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.00057235275 ETH
2_deploy_contracts.js
=====================
Deploying 'ConvertLib'
----------------------
> transaction hash: 0x54ee4b97c1db08920805053627699f48db459e826e996925ead2bb634c1cfe69
> Blocks: 0 Seconds: 0
> contract address: 0x390182658fE873bCbC96f854D6932F68bf00C848
> block number: 3
> block timestamp: 1684689350
> account: 0x1D3F3388d3693980Bdb6C24052bfbc021Fec4f5D
> balance: 99.998996952724562397
> gas used: 88560 (0x159f0)
> gas price: 3.17606086 gwei
> value sent: 0 ETH
> total cost: 0.0002812719497616 ETH
Linking
-------
* Contract: MetaCoin <--> Library: ConvertLib (at address: 0x390182658fE873bCbC96f854D6932F68bf00C848)
Deploying 'MetaCoin'
--------------------
> transaction hash: 0xe1183030bbb3c8985cc5aaba1b4a0668fad7a983285328a8ede20e0ad7f133c5
> Blocks: 0 Seconds: 0
> contract address: 0xDc1bFfb13029884A35897792bA7D19caE90b31B8
> block number: 4
> block timestamp: 1684689350
> account: 0x1D3F3388d3693980Bdb6C24052bfbc021Fec4f5D
> balance: 99.998364333500880979
> gas used: 204481 (0x31ec1)
> gas price: 3.093779978 gwei
> value sent: 0 ETH
> total cost: 0.000632619223681418 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.000913891173443018 ETH
Summary
=======
> Total deployments: 3
> Final cost: 0.001486243923443018 ETH
「Falling back to a NodeJS implementation; performance may be degraded.」
こちらは、一旦無視で
「Everything is up to date, there is nothing to compile.」
すでに先にコンパイルを済ましているので
スキップされたようです。
エラーなど出ていませんね
これでマイグレーションはされました。
まとめ
一部問題はあるものの
無事マイグレーションまで終わりました。
これで、ネットワークの内容を見たり
実際にトランザクションを発生させて
動きを確認できます。
次回は、MetaMaskも接続して、実際に動きを確認したいと思います。
[getPhpFile() file=’Appeal_01′ html_file=’Appeal_01.html’]





