跳至主要内容

協助翻譯本頁面

🌏

由於尚未翻譯,本頁面內容以英文顯示。協助我們翻譯內容。

翻譯本頁面

這裡沒有漏洞!🐛

此頁面未翻譯,目前特意維持英文原文。

Waffle say hello world tutorial with hardhat and ethers

waffle
smart contracts
solidity
testing
hardhat
ethers.js
page-tutorial-undefined
✍MiZiet
📆2020年10月16日
⏱️4 minute read

In this Waffle tutorial, we will learn how to set up a simple "Hello world" smart contract project, using hardhat and ethers.js. Then we will learn how to add a new functionality to our smart contract and how to test it with Waffle.

Let's start with creating new project:

yarn init

or

npm init

and installing required packages:

yarn add -D hardhat @nomiclabs/hardhat-ethers ethers @nomiclabs/hardhat-waffle ethereum-waffle chai

or

npm install -D hardhat @nomiclabs/hardhat-ethers ethers @nomiclabs/hardhat-waffle ethereum-waffle chai

Next step is creating a sample hardhat project by running 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.3 👷‍
? What do you want to do? …
❯ Create a sample project
Create an empty hardhat.config.js
Quit
顯示全部

Select Create a sample project

Our project's structure should look like this:

1MyWaffleProject
2├── contracts
3│ └── Greeter.sol
4├── node_modules
5├── scripts
6│ └── sample-script.js
7├── test
8│ └── sample-test.js
9├── .gitattributs
10├── .gitignore
11├── hardhat.config.js
12└── package.json
13
顯示全部

Now let's talk about some of these files:

  • Greeter.sol - our smart contract writen in solidity;
1contract Greeter {
2string greeting;
3
4constructor(string memory _greeting) public {
5console.log("Deploying a Greeter with greeting:", _greeting);
6greeting = _greeting;
7}
8
9function greet() public view returns (string memory) {
10return greeting;
11}
12
13function setGreeting(string memory _greeting) public {
14console.log("Changing greeting from '%s' to '%s'", greeting, _greeting);
15greeting = _greeting;
16}
17}
18
顯示全部
📋 複製

Our smart contract can be divided into three parts:

  1. constructor - where we declare a string type variable called greeting,
  2. function greet - a function that will return the greeting when called,
  3. function setGreeting - a function that allows us to change the greeting value.
  • sample-test.js - our tests file
1describe("Greeter", function () {
2 it("Should return the new greeting once it's changed", async function () {
3 const Greeter = await ethers.getContractFactory("Greeter")
4 const greeter = await Greeter.deploy("Hello, world!")
5
6 await greeter.deployed()
7 expect(await greeter.greet()).to.equal("Hello, world!")
8
9 await greeter.setGreeting("Hola, mundo!")
10 expect(await greeter.greet()).to.equal("Hola, mundo!")
11 })
12})
13
顯示全部
📋 複製

Next step consists of compiling our contract and running tests:

Waffle tests use Mocha (a test framework) with Chai (an assertion library). All you have to do is run npx hardhat test and wait for the following message to appear.

✓ Should return the new greeting once it's changed

Everything looks great so far, let's add some more complexity to our project 🙂

Imagine a situation where someone adds an empty string as a greeting. It wouldn't be a warm greeting, right?
Let's make sure that doesn't happen:

We want to use solidity's revert when someone passes an empty string. A good thing is that we can easily test this functionality with Waffle's chai matcher to.be.revertedWith().

1it("Should revert when passing an empty string", async () => {
2 const Greeter = await ethers.getContractFactory("Greeter")
3 const greeter = await Greeter.deploy("Hello, world!")
4
5 await greeter.deployed()
6 await expect(greeter.setGreeting("")).to.be.revertedWith(
7 "Greeting should not be empty"
8 )
9})
10
顯示全部
📋 複製

Looks like our new test didn't pass:

Deploying a Greeter with greeting: Hello, world!
Changing greeting from 'Hello, world!' to 'Hola, mundo!'
✓ Should return the new greeting once it's changed (1514ms)
Deploying a Greeter with greeting: Hello, world!
Changing greeting from 'Hello, world!' to ''
1) Should revert when passing an empty string
1 passing (2s)
1 failing
顯示全部

Let's implement this functionality into our smart contract:

1require(bytes(_greeting).length > 0, "Greeting should not be empty");
2
📋 複製

Now, our setGreeting function looks like this:

1function setGreeting(string memory _greeting) public {
2require(bytes(_greeting).length > 0, "Greeting should not be empty");
3console.log("Changing greeting from '%s' to '%s'", greeting, _greeting);
4greeting = _greeting;
5}
6
📋 複製

Let's run tests again:

✓ Should return the new greeting once it's changed (1467ms)
✓ Should revert when passing an empty string (276ms)
2 passing (2s)

Congrats! You made it :)

Conclusion

We made a simple project with Waffle, Hardhat and ethers.js. We learned how to set up a project, add a test and implement new functionality.

For more great chai matchers to test your smart contracts, check official Waffle's docs.

最後編輯: , Invalid DateTime
編輯頁面

這個頁面對您有幫助嗎?

網站上次更新: 2022年9月26日

使用以太坊

  • 尋找錢包
  • 取得以太幣
  • 去中心化應用程式 (dapp)
  • 第二層
  • 執行節點
  • 穩定幣
  • Stake ETH

生態系統

  • 社群中心
  • 以太坊基金會
  • 以太坊基金會部落格
  • 生態系統支援計畫
  • 以太坊漏洞懸賞計畫
  • 生態系統獎助金計畫
  • 以太坊品牌資產
  • Devcon