Note that a different path to artifacts is added so that the React app will be able to read the contract ABI within the src folder.
Add scripts
Create a new file, in the contracts folder, named Counter.sol: touch contracts/Counter.sol.
Copy the below code and paste it in the Counter contract code:
Copy
Create a new file in the scripts folder deploy-counter.js: touch scripts/deploy-counter.js.
Add the code below to the deploy-counter.js file:
Copy
Before compiling the contract, you need to install the toolbox. You may need to change directory to install outside the project. Use this command:
Copy
Compile your contract code (i.e., go back to the project root in the CLI):
Copy
Now run the scripts:
Copy
Here’s an output example:
Counter contract deployed to https://explorer.conla.com/address/0x5FbDB2315678afecb367f032d93F642f64180aa3
Update frontend
The next step is to turn Counter.sol into a dApp by importing the ethers and the Counter file, as well as logging the contract’s ABI.
Include the below code in the App.js file:
Copy
Update the counterAddress to your deployed address.
It is the hexadecimal number found at the tail-end of the output of the last npx hardhat run ... command and looks like this 0x5FbDB2315678afecb367f032d93F642f64180aa3.
It must be pasted in the App.js to replace your-contract-address. Be sure to use the deployed address from your own implementation!
Update frontend counter to read from blockchain. Include the below code in the App.js file:
Copy
Also, to import useEffect, insert it like this:
Copy
To be able to track a loader, add this to your state:
Copy
This is within the App() function.
Let frontend counter write to the blockchain by adding the below requestAccount and updateCounter functions:
Copy
Place these two functions above the readCounterValue() function in the App.js file.
Replace the incrementCounter function with this one:
Copy
Update the increment button code to:
Copy
Now, run the Counter dApp by simply using npm start in CLI at the project root.
You have successfully deployed a dApp on the Conla testnet.