Hardhat 環境設定

Yan Ru Su

Introduction Hardhat

Hardhat 是 Ethereum 智能合約開發的完整環境工具,用於編譯、測試、部署和除錯 Solidity 程式碼。

Build node envorinment

use terminal in Mac

1
2
3
4
5
6
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash\. "$HOME/.nvm/nvm.sh”
nvm install 22
nvm use 22
nvm alias default 22
npm install npm --global
npm init

Install hardhat package

1
npm install --save-dev hardhat@^2.22.6

Initial a hardhat project

1
2
npx hardhat init
npm install --save-dev @nomicfoundation/hardhat-toolbox

Write a contract

We use hardhat tutorial contract example

4. Writing and compiling smart contracts | Ethereum development environment for professionals by Nomic Foundation

Create a “contracts” dictionary

1
mkdir contracts

Create a .sol file

1
touch xxx.sol

Paste the contract example from hardhat tutorial

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//SPDX-License-Identifier: UNLICENSED

// Solidity files have to start with this pragma.
// It will be used by the Solidity compiler to validate its version.
pragma solidity ^0.8.0;

// This is the main building block for smart contracts.
contract Token {
// Some string type variables to identify the token.
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's balance.
mapping(address => uint256) balances;

// The Transfer event helps off-chain applications understand
// what happens within your contract.
event Transfer(address indexed _from, address indexed _to, uint256 _value);

/**
* Contract initialization.
*/
constructor() {
// The totalSupply is assigned to the 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`, the
// transaction will revert.
require(balances[msg.sender] >= amount, "Not enough tokens");

// Transfer the amount.
balances[msg.sender] -= amount;
balances[to] += amount;

// Notify off-chain applications of the transfer.
emit Transfer(msg.sender, 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];
}
}

Compile

Use the compile command
1
npx hardhat compile

if success show

1
Compiled 14 Solidity files successfully (evm target: paris).