MaxOS Monterey 12.3.1 nodejs v16.14.0
% mkdir hardhat-tutorial
% cd hardhat-tutorial
% npm init --yes
% npm install --save-dev hardhat
% npx hardhat
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.0.0
? What do you want to do? …
Create a sample project
❯ Create an empty hardhat.config.js
Quit
% npm install --save-dev @nomiclabs/hardhat-ethers ethers @nomiclabs/hardhat-waffle ethereum-waffle chai
require("@nomiclabs/hardhat-waffle");
/**
* @type import('hardhat/config').HardhatUserConfig
*/
module.exports = {
// solidity: "0.7.3",
// ↓ 最新が0.8.13にしておく
solidity: "0.8.13",
};
hardhat-ethersに依存しているため、hardhat-waffleのみでよい。両方追加する必要はない。
contractsという名前の新しいディレクトリを作成し、Token.solという名前のファイルをディレクトリ内に作成します。
コードを以下のようにする
// Solidity files have to start with this pragma.
// It will be used by the Solidity compiler to validate its version.
// pragma solidity ^0.7.0;
// ↓ エラーが出た
pragma solidity ^0.8.13;
// This is the main building block for smart contracts.
contract Token {
// Some string type variables to identify the token.
// The `public` modifier makes a variable readable from outside the contract.
string public name = "My Hardhat Token";
string public symbol = "MHT";
// The fixed amount of tokens stored in an unsigned integer type variable.
uint256 public totalSupply = 1000000;
// An address type variable is used to store ethereum accounts.
address public owner;
// A mapping is a key/value map. Here we store each account balance.
mapping(address => uint256) balances;
/**
* Contract initialization.
*
* The `constructor` is executed only once when the contract is created.
*/
constructor() {
// The totalSupply is assigned to transaction sender, which is the account
// that is deploying the contract.
balances[msg.sender] = totalSupply;
owner = msg.sender;
}
/**
* A function to transfer tokens.
*
* The `external` modifier makes a function *only* callable from outside
* the contract.
*/
function transfer(address to, uint256 amount) external {
// Check if the transaction sender has enough tokens.
// If `require`'s first argument evaluates to `false` then the
// transaction will revert.
require(balances[msg.sender] >= amount, "Not enough tokens");
// Transfer the amount.
balances[msg.sender] -= amount;
balances[to] += amount;
}
/**
* Read only function to retrieve the token balance of a given account.
*
* The `view` modifier indicates that it doesn't modify the contract's
* state, which allows us to call it without executing a transaction.
*/
function balanceOf(address account) external view returns (uint256) {
return balances[account];
}
}
コンパイルする
% npx hardhat compile
Downloading compiler 0.8.13
Compiled 1 Solidity file successfully
プロジェクトのルートディレクトリ内にtestという名前の新しいディレクトリを作成し、Token.jsという名前の新しいファイルを作成します。
const { expect } = require("chai");
describe("Token contract", function () {
it("Deployment should assign the total supply of tokens to the owner", async function () {
const [owner] = await ethers.getSigners();
const Token = await ethers.getContractFactory("Token");
const hardhatToken = await Token.deploy();
const ownerBalance = await hardhatToken.balanceOf(owner.address);
expect(await hardhatToken.totalSupply()).to.equal(ownerBalance);
});
});
npx hardhat test
でテストフォルダのファイルが実行される
% npx hardhat test
Token contract
✔ Deployment should assign the total supply of tokens to the owner (443ms)
1 passing (444ms)
--