13

I am learning to program in Solidity and have followed a basic tutorial on creating a smart contract from a number counter. The tutorial was followed by using Remix and I was very clear about the structure of the code, the deployment and most importantly the interaction with the functions of my contract through this part of REMIX (next image)

enter image description here

In this section I can interact with the reading and writing functions of my contract.

Now I am trying to perform the same exercise using Hardhat, as I want to know better the whole process of developing a smart contract. I have been able to create my project as well as write the code of my contract again and compile the contract using the command npx hardhat compile but I do not understand how to perform the deployment (in the blockchain that comes with hardat) and more importantly see the part where I can test the functions that my contract has.

From what I have read so far, the process in Hardhat would be to create a test within which I define the calls to my functions and display the results using console.log () - this would be correct, some example link would be appreciated.

Diego Aaron
  • 255
  • 1
  • 2
  • 6
  • 2
    This might sound like a silly question but did you go through the Hardhat tutorial? – Paul Razvan Berg Feb 18 '21 at 20:43
  • As a side note, I am big fan of Hardhat. I created my own template, which contains best practices for linting, testing and coverage. You may find it helpful! – Paul Razvan Berg Feb 18 '21 at 20:44
  • 1
    YES I could review that part of the documentation but the closest thing I understood is the testing part, so towards the question since I do not know if there was another way to make the interactions directly with the contract. @PaulRazvanBerg – Diego Aaron Feb 18 '21 at 20:45
  • I'm afraid you will have to rephrase your question in such a way that it is more specific. – Paul Razvan Berg Feb 19 '21 at 10:01
  • Ok @PaulRazvanBerg - I going to update my question. – Diego Aaron Feb 20 '21 at 01:50
  • @PaulRazvanBerg, I just improved the description of the problem, I hope it is better understood, my question. – Diego Aaron Feb 20 '21 at 22:50
  • Idk what hardhat ist, but probably truffle is the best place to start learning after remix – Majd TL Feb 20 '21 at 23:11
  • 1
    @MajdTL Hardhat is arguably a better development environment than Truffle. It offers features such as multi-solc, mainnet forking and support for TypeScript. It's also extensible - there's a host of community-built plugins that enrich what you can do with Hardhat. – Paul Razvan Berg Feb 22 '21 at 14:19

1 Answers1

24

Basically, you need to deploy the contract to your local hardhat net just as you would to any other. That means currently you have to set up a deploy.js like in the official hardhat tutorial. I'm assuming you followed the official tutorial at least until Chapter 7: Deploying to a live network and e.g. @nomiclabs/hardhat-ethers is already properly installed.

Preparations

❶ Run:

npx hardhat node

❷ Open a new console window at your project directory. Deploy your smart contract by using the previously mentioned deploy.js. You need the --network parameter to specify that you want to deploy to localhost.

npx hardhat run --network localhost scripts/deploy.js

The script will throw back some useful information. Keep an eye on the second address (designated by the contract's name) – you will need it in Step 4.

❸ Connect to the hardhat console at localhost with:

npx hardhat console --network localhost

❹ Get the ethers.js contract factory and attach to it.

Use the proper name in getContractFactory() and paste the address from step 2 in attach()

const Token = await ethers.getContractFactory("Token")
const token = await Token.attach("0x5FbDB2315678afecb367f032d93F642f64180aa3")

Or in one command:

const token = await (await ethers.getContractFactory("Token")).attach("0x5FbDB2315678afecb367f032d93F642f64180aa3")

Interact with contract

You're in a JavaScript environment. E.g. call the transfer method of the hardhat tutorial. For the complete API, see ethers.js API Docs

Two more tips:

  • Use await to avoid Promise objects.
  • .toString() helps you to display uint256 numbers (which are too big for JS).

For a more detailed read, refer to the excellent OpenZeppelin tutorial for hardhat.

> await token.transfer("0xdd2fd4581271e230360230f9337d5c0430bf44c0", 42069)
{
  hash: '0xdc0493ce8ed950b4d4558deb65605ba773067804eacfefd7fc64274a8a805dea',
  ...
> (await token.balanceOf("0xdd2fd4581271e230360230f9337d5c0430bf44c0")).toString()
'42069'
ctholho
  • 356
  • 2
  • 4