Writing and Testing a Smart Contract With Hardhat and Ethers.js | by Grace Lungu | May, 2022

A primary overview of how you can write and check a wise contract utilizing the Hardhat surroundings and the Ethers.js library

Picture by Choong Deng Xiang on Unsplash

Hardhat is an Ethereum growth surroundings similar to Truffle, which we’ll use to develop our contract and deploy it on our native community and EthersJs is a library for interacting with the Ethereum blockchain that we’ll use in our check fits to work together with the deployed contract.

A whole set up information of the Hardhat surroundings may be discovered on the official website however let’s go over the required step for our small mission.

So let’s run the next command

npm set up --save-dev hardhat && npx hardhat

It will set up the bundle and launch the interactive interface to create the mission. We’ll choose > Create a sophisticated pattern mission that makes use of TypeScript and proceed.

Hardhat will generate a few information and folders, however we’re solely serious about a number of of them.

The contracts folder will host our good contract, the check folder our check and hardhat.config.ts the generated configuration from hardhat — for which we have to ensure that the solidity model outlined right here matches the required model of our contract.

Think about that you’ve been failing to save lots of your cash as a result of you’ll be able to’t assist however spend and you find yourself breaking your financial savings objectives. Effectively, if solely you possibly can lock your financial savings in a vault that solely offers you again your cash after a specified date has come to cross. That is the concept behind the good contract we’ll be writing.

Let’s go forward and create a file named LockSave.sol contained in the contracts folder.

We want three primary features for this easy contract:

  1. A perform to save lots of ETH for a minimal withdrawal date
  2. A perform to withdraw after the required date has handed
  3. A perform to retrieve all financial savings from the proprietor’s handle

We’ll not focus a lot on the specifics of the contract however right here’s what it appears like.

First, we have to outline the form of our financial savings

Subsequent, we’ll must declare a few variables that may assist retailer and map the financial savings to their creation timestamps and house owners to saving timestamps. This type of mapping helps to govern and entry our information.

We want a few customized errors to make use of for undesirable behaviors, reminiscent of when saving is made with an quantity equal to zero, or when an handle desires to withdraw earlier than the date has handed.

The saving perform

The saving perform takes within the withdrawal timestamp provides a brand new saving to the handle financial savings and returns the info. We use the isValidSaving modifier to verify for the quantity and withdrawal time.

The withdraw perform

We use the saving timestamp to search out the saving, take away it from financial savings then lastly switch again the saved quantity to the proprietor.

However earlier than withdrawing we first verify if the handle has financial savings in addition to the withdrawal time and cross these modifiers to the withdraw perform above.

Lastly, we’ll add yet another perform to retrieve all financial savings belonging to the requesting handle.

As talked about above we’ll create a file contained in the check folder as index.ts then import from chai, ethers and hardhat.

Subsequent, we’ll declare our check swimsuit and deploy our contract in beforeEach. It will deploy a brand new contract each time we run a brand new check case in order that we at all times have a clean contract to work with.

Saving and retrieving the saving

To check the saving perform, we’ll make a saving after defining the withdrawal timestamp after which use the getSavings perform to get the saving. Afterward, we’ll use the returned saving to say the worth and timestamps.

We use ethers.getSigners to get the signer addresses and extract the primary handle [sender] which is the default handle utilized by Hardhat.

ethers.utils.parseEther is a utility perform to transform a price from ETH to WEI which is the worth we wish to ship to the contract when calling the payable methodology save .

The response from the contract is returned as an iterator by EthersJs and by doing financial savings.worth().subsequent().worth we’re capable of entry the values as an array.

And eventually, use anticipate from chai to make the assertions.

Withdrawing the financial savings

For the next check, we first make a saving then make a withdrawal, and at last name the getSavings methodology to say that the quantity was efficiently withdrawn.

That will likely be all for our check circumstances, however extra assertions may very well be accomplished for sad paths reminiscent of an try to withdraw earlier than the date has handed or saving for a date that already handed.

For reference, I’ve hosted the 2 information, each the smart contract and the test on GithubGist.

More Posts