The woodcutter stood stark, glazing upwards at the grandiosity of the towering tree, then glanced at the miniscule axe on his shoulders. Not getting started meant not getting it done. So he started hacking !
From hardhat to testnet, this is real. Example of a smart contract deployed to the Ethereum testnet at the end of this tutorial:
We use Solidity to write smart contracts that are compiled, and later can be interacted upon via Ether Javascript to run various operations like fetching information, moving balance and so on.
- hardhat
- solidity 0.8.29 3 node 22.14.0 4 npm 10.9.2
It's a simple local dev environment for ethereum blockchain.
After, run:
npx hardhat compile
## run hardhat test
npx hardhat test
//or custom script in package.json
npm run test
- First enable the network with
npx hardhat node
- Then, deploy !
npx hardhat run scripts/deploy.js --network localhost
Next, learning how to interact with it.
contracts/
: Contains the Solidity smart contractToken.sol
.scripts/
: Contains the deployment scriptdeploy.js
.test/
: Contains test files for the smart contract.hardhat.config.js
: Hardhat configuration file.
The Token.sol
contract implements a simple ERC-20 token with features like token transfer, balance checking, and total supply.
The project includes unit tests using Mocha and Chai to verify the contract's functionality. Key test cases include:
- Verifying the initial total supply.
- Checking token balances after transfers.
- Testing for correct event emissions.
To interact with the deployed contract, you can use Ether.js. For example, to get the token balance of an address:
// Example using Ether.js (within a Node.js script)
const { ethers } = require("ethers");
const contractAddress = "0x5FbDB2315678afecb367f032d93F642f64180aa3"; // Replace with your deployed contract address
const provider = new ethers.providers.JsonRpcProvider("[http://127.0.0.1:8545/](https://www.google.com/search?q=http://127.0.0.1:8545/)"); // Local Hardhat node
const token = new ethers.Contract(contractAddress, ["function balanceOf(address) view returns (uint256)"], provider);
async function getBalance(address) {
const balance = await token.balanceOf(address);
console.log(`Balance of ${address}: ${ethers.utils.formatEther(balance)}`);
}
getBalance("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"); // Example address◊
Dependencies
- React
- React Router
- Ether.js
- Webpack for compiling and tooling
Frontend is built with REACT
- In the frontend folder, run
npm run dev
to start the local server - OR,
npm run build
Metamask suppport is added, however need to figure out :
- Passing data properly,
- connecting with local hardhat node (how it is happening, since we removed json rpc settings)
- What to do when user has metamask, but hasn't authenticated, instead of regular error of duplicate
- What to do after metamask is connected sucessfully
(update) : We have figured these out, however, still need to build an application that makes it possible to send and receive tokens. Also in the dApp, we should be able to display token values ! Basically we will be building the fundamentals of cryptic transactions with a beautiful UI.
For privacy to safeguard private keys and RCP urls !
- Rename .env.sample to .env and update values
npm install dotenv
update .env file with RPC URL From https://developer.metamask.io/ (need to signup and create an api key)
SEPOLIA_RPC_URL=https://sepolia.infura.io/v3/your-infura-project-id
PRIVATE_KEY=private-key-of-your-wallet
## for local development - you can not use this part of code if you want
METAMASK_ADDRESS_1=0xaddress-1
METAMASK_ADDRESS_2=0xaddress-2
First import and call the dotenv
import * as dotenv from "dotenv";
dotenv.config();
// dotenv helps to load files from the .env file.
//then add the network
sepolia: {
url: process.env.SEPOLIA_RPC_URL || "",
accounts: process.env.PRIVATE_KEY ? [process.env.PRIVATE_KEY] : []
}
This is test ethereum which has no monetary value !! https://cloud.google.com/application/web3/faucet/ethereum/sepolia After setting up a Sepolia Ethereum wallet with metamask, use above link to send some ethereum (no real value only for testnet) for gas fees !
npx hardhat compile
$ npx hardhat run scripts/deploy-testnet.ts --network sepolia
Token deployed to: 0x4705559cDC3c8deDc0438ED5f3F090C8043F6792
The token address provided will be unique, which you can now use to view your blockchain ledger
https://sepolia.etherscan.io/address/0x4705559cDC3c8deDc0438ED5f3F090C8043F6792
And there we go, at least we now have a small idea of what blockchain is !
- Building a better UI, and showing user related data like total coins !
- Further testing sending/receiving the cryptocurrency between accounts !
That's it, the tree has shaken !