Hardhat is one of the popular smart contract development frameworks. It consists of different components for editing, compiling, debugging and deploying your smart contracts and dApps, all of which work together to create a complete development environment.
Hardhat smart contract
mkdir <project-name>;cd <project-name>
Initialize a project with Hardhat: npx hardhat init.
Counter contract deployed to
https://https://explorer-testnet.biteigen.xyz/address/0xCF79A6a817F49cE37e7AE73F49A1A5a90FC28c84
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.ts file:
import { ethers } from "ethers";
import { useEffect, useState } from 'react';
import './App.css';
const counterAddress = "0xCF79A6a817F49cE37e7AE73F49A1A5a90FC28c84"
const abi = `[{"inputs":[],"name":"increment","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"retrieve","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}]`;
function App() {
const [isLoading, setIsLoading] = useState(false);
const [count, setCount] = useState(0);
useEffect(() => {
const fetchCount = async () => {
const data = await readCounterValue();
return data;
};
fetchCount().catch(console.error);
}, []);
async function requestAccount() {
await window.ethereum?.request?.({ method: "eth_requestAccounts" });
}
async function updateCounter() {
if (typeof window.ethereum === "undefined") return;
await requestAccount();
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const contract = new ethers.Contract(counterAddress, abi, signer);
const transaction = await contract.increment();
setIsLoading(true);
await transaction.wait();
setIsLoading(false);
readCounterValue();
}
async function readCounterValue() {
if (typeof window.ethereum === "undefined") return;
const provider = new ethers.providers.Web3Provider(window.ethereum);
const contract = new ethers.Contract(
counterAddress,
abi,
provider
);
try {
const data = await contract.retrieve();
setCount(parseInt(data.toString()));
} catch (err) {
alert(
"Switch your MetaMask network to BitEigen Testnet and refresh this page!"
);
}
}
const incrementCounter = async () => {
await updateCounter();
};
return (
<div className="container">
<button
onClick={incrementCounter}
disabled={isLoading}
className="button"
>
{isLoading ? "loading..." : `counter: ${count}`}
</button>
</div>
);
}
export default App;
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 deploySC ... command and looks like this 0xCF79A6a817F49cE37e7AE73F49A1A5a90FC28c84.
Now, run the Counter dApp by simply using npm run start in CLI at the project root.
You have successfully deployed a dApp on the BitEigen Testnet.