文書の過去の版を表示しています。
Hardhatを使ったスマートコントラクト開発
ほぼ Hardhat オフィシャルのドキュメント通りに Hardhat を使ってみます。
前提
npm をインストールしておいてください。
インストール
下記「npx hardhat」コマンドを実行すると「What do you want to do?」と聞かれますので、今回は「Create a basic sample project」で作業を進めます。それ以外はデフォルトのままエンターを入力してください。
> mkdir hardhat_project > cd hardhat_project hardhat_project> npx hardhat Need to install the following packages: hardhat Ok to proceed? (y) y 888 888 888 888 888 888 888 888 888 888 888 888 888 888 888 8888888888 8888b. 888d888 .d88888 88888b. 8888b. 888888 888 888 88b 888P d88 888 888 88b 88b 888 888 888 .d888888 888 888 888 888 888 .d888888 888 888 888 888 888 888 Y88b 888 888 888 888 888 Y88b. 888 888 Y888888 888 Y88888 888 888 Y888888 Y888 Welcome to Hardhat v2.9.3 ? What do you want to do? … ❯ Create a basic sample project Create an advanced sample project Create an advanced sample project that uses TypeScript Create an empty hardhat.config.js Quit √ What do you want to do? · Create a basic sample project √ Hardhat project root: · C:\Users\shinobu\NoNameSeminer\ethereum\hardhat_project √ Do you want to add a .gitignore? (Y/n) · y √ Help us improve Hardhat with anonymous crash reports & basic usage data? (Y/n) · true You need to install these dependencies to run the sample project: npm install --save-dev "hardhat@^2.9.3" "@nomiclabs/hardhat-waffle@^2.0.0" "ethereum-waffle@^3.0.0" "chai@^4.2.0" "@nomiclabs/hardhat-ethers@^2.0.0" "ethers@^5.0.0" Project created See the README.md file for some example tasks you can run.
上記の最後で指定された通り、必要な npm パッケージをインストールします。
hardhat_project> npm install --save-dev "hardhat@^2.9.3" "@nomiclabs/hardhat-waffle@^2.0.0" "ethereum-waffle@^3.0.0" "chai@^4.2.0" "@nomiclabs/hardhat-ethers@^2.0.0" "ethers@^5.0.0"
生成されたファイルを確認してみます。
hardhat_project> ls ディレクトリ: C:\Users\shinobu\NoNameSeminer\ethereum\hardhat_project Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 2022/05/11 13:34 contracts d----- 2022/05/11 13:43 node_modules d----- 2022/05/11 13:34 scripts d----- 2022/05/11 13:34 test -a---- 2022/05/11 13:34 83 .gitignore -a---- 2022/05/11 11:16 572 hardhat.config.js -a---- 2022/05/11 13:43 863535 package-lock.json -a---- 2022/05/11 13:43 249 package.json -a---- 2022/05/11 11:16 467 README.md
- contracts: スマートコントラクトのソースコードを格納するディレクトリ
- サンプルの「Greeter.sol」が既に格納されています。
- scripts: スマートコントラクトを ethereum ブロックチェーンにデプロイするためのスクリプトを格納するディレクトリ(他の目的のスクリプトが格納されることもあるかもしれません。)
- サンプルの「Greeter」スマートコントラクトをデプロイする sample-script.js が既に格納されています。
- test: テストプログラムを格納するディレクトリ
- サンプルの「Greeter」スマートコントラクトをテストする sample-test.js が既に格納されています。
- hardhat.config.js: Hardhat の設定ファイル
Hardhat のバージョンとコマンドの仕様を確認してみます。
hardhat_project> npx hardhat Hardhat version 2.9.3 Usage: hardhat [GLOBAL OPTIONS] <TASK> [TASK OPTIONS] GLOBAL OPTIONS: --config A Hardhat config file. --emoji Use emoji in messages. --help Shows this message, or a task's help if its name is provided --max-memory The maximum amount of memory that Hardhat can use. --network The network to connect to. --show-stack-traces Show stack traces. --tsconfig A TypeScript config file. --verbose Enables Hardhat verbose logging --version Shows hardhat's version. AVAILABLE TASKS: check Check whatever you need clean Clears the cache and deletes all artifacts compile Compiles the entire project, building all artifacts console Opens a hardhat console flatten Flattens and prints contracts and their dependencies help Prints this message node Starts a JSON-RPC server on top of Hardhat Network run Runs a user-defined script after compiling the project test Runs mocha tests To get help for a specific task run: npx hardhat help [task]
コンパイル
Hardhat セットアップ時に自動的に作成されている contracts/Greeter.sol をコンパイルします。
hardhat_project> npx hardhat compile
テスト
Hardhat セットアップ時に自動的に作成されている test/sample-test.js を使って Greeter スマートコントラクトのテストを行います。
hardhat_project> npx hardhat test Greeter Deploying a Greeter with greeting: Hello, world! Changing greeting from 'Hello, world!' to 'Hola, mundo!' ✔ Should return the new greeting once it's changed (1328ms) 1 passing (1s)
node の起動
Hardhat を Ganache や ganache-cli のように ethereum ブロックチェーン(node)を起動することができます。 ethereum ブロックチェーンにスマートコントラクトをデプロイし、Hardhat 外部からも連携ができるように、ethereum ブロックチェーンを起動します。
新たなターミナル(PowerShell等)を起動し、以下のコマンドで node を起動してください。
hardhat_project> npx hardhat node
デプロイ
Hardhat セットアップ時に自動的に作成されている scripts/sample-script.js を使って Greeter スマートコントラクトを ethereum ブロックチェーンにデプロイします。
先ほど立ち上げた node にデプロイするために「–network localhost」オプションを指定します。
hardhat_project> npx hardhat run scripts/sample-script.js --network localhost Deploying a Greeter with greeting: Hello, Hardhat! Greeter deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
上記の場合だと「0x5FbDB2315678afecb367f032d93F642f64180aa3」が Greeter スマートコントラクトのアドレスですので、メモしておいてください。
コンソールでスマートコントラクトの実行
「npx hardhat console」でコンソールが起動します。 先ほど立ち上げた node に接続するために「–network localhost」オプションを指定します。
hardhat_project> npx hardhat console --network localhost Welcome to Node.js v16.13.1. > const Greeter = await ethers.getContractAt('Greeter', '0x5fbdb2315678afecb367f032d93f642f64180aa3'); undefined > await Greeter.greet(); 'Hello, Hardhat!' > await Greeter.setGreeting('Hello, World! World is fun!!!'); { hash: '0x3868b3b8f7a87a5fd8453444eba9f17193e1835be6ea6cd90ce1b31e41a03b16', type: 2, accessList: [], blockHash: '0x481f987f494e634543be25dbc0e262504709acdf0f00fa9f7ad80d2cf344a4f4', blockNumber: 2, transactionIndex: 0, confirmations: 1, from: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', gasPrice: BigNumber { _hex: '0x2dd9cf7c', _isBigNumber: true }, maxPriorityFeePerGas: BigNumber { _hex: '0x00', _isBigNumber: true }, maxFeePerGas: BigNumber { _hex: '0x3a07aa98', _isBigNumber: true }, gasLimit: BigNumber { _hex: '0x8c47', _isBigNumber: true }, to: '0x5FbDB2315678afecb367f032d93F642f64180aa3', value: BigNumber { _hex: '0x00', _isBigNumber: true }, nonce: 1, data: '0xa41368620000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001d48656c6c6f2c20576f726c642120576f726c642069732066756e212121000000', r: '0x240d275144cca8e5e06e0e46e181c19d8b24605211e0e03af72f9159a24b1a7c', s: '0x6953c1d27ef130ce4330d95191c7d861b85b338ae2d44a1c27fb3ed94acab803', v: 0, creates: null, chainId: 31337, wait: [Function (anonymous)] } > await Greeter.greet(); 'Hello, World! World is fun!!!'