Prerequisites
You should already have a Hardhat project set up, and have deployed your smart contract successfully. See the deploy a smart contract using Hardhat tutorial for how to do so. Optionally, but strongly recommended: You should also have successfully verified your smart contract. See the verify a smart contract using Hardhat tutorial for how to do so.Start the Hardhat console
Use the following command to start an interactive Javascript REPL.Counter smart contract.
To do so, use ethers.getContractFactory(...) and contract.attach('0x...');.
For example, if the smart contract was deployed to 0x98798cc92651B1876e9Cc91EcBcfe64cac720a1b, the commands should look like this:
> as the shell prompt.
The results of each prompt are output without this prefix.
The contents of your terminal will therefore look similar to this:
counter.
Invoke function - query
Queries are read-only operations. So smart contract state is not updated. As no state change is needed, no wallets, signatures, or transaction fees (gas) are required. Use the following command to query thevalue() function.
Note that
0n means 0, the n suffix indicates that it is
a BigInt
and not a Number.This is because Solidity’s uint256 (the return type of the value() function in the smart contract),
is not possible to be represented with Number,
as the largest possible integer value for that is 2^53 - 1.
Thus BigInt needs to be used instead.Invoke function - transaction
Transactions are write operations. So smart contract state is updated. As state change can occur, the transaction must be signed by a wallet, and transaction fees (gas) need to be paid. Use the following command to transact theincrement(num) function.
Note that gas price is stated in wei.
1 wei = 10^-18 INJ.
1n because 0 + 1 = 1.
Stop the Hardhat console
PressCtrl+C twice in a row, or enter the .exit command.