Easily blockchain project (smart contract) with hardhat
This project implements a simple tax record system on the Ethereum blockchain using Hardhat and TypeScript. It allows for adding, retrieving, and deleting tax records on the blockchain.
- Node.js (v14+ recommended)
- npm or yarn
- An Ethereum wallet (like MetaMask)
- Alchemy API key (for deploying to Sepolia testnet and mainnet)
-
Clone the repository:
git clone https://github.com/code4mk/easy-deploy-blockchain-project-hardhat.git cd easy-deploy-blockchain-project-hardhat
-
Install dependencies:
npm install
-
Create a
.env
file in the root directory and add your configuration:SEPOLIA_RPC_URL=https://eth-sepolia.g.alchemy.com/v2/YOUR_ALCHEMY_KEY PRIVATE_KEY=your_wallet_private_key MAINNET_RPC_URL=https://eth-mainnet.g.alchemy.com/v2/YOUR_ALCHEMY_KEY MAINNET_PRIVATE_KEY=your_mainnet_private_key ETHERSCAN_API_KEY=your_etherscan_api_key
Compile the smart contracts:
npx hardhat compile
Run the test suite:
npx hardhat test
-
Start a local Hardhat node:
npx hardhat node
-
In a new terminal, deploy the contract:
npm run deploy:localhost
-
Interact with the contract:
npx hardhat run scripts/interact.ts --network localhost
-
Ensure you have Sepolia ETH. You can get some from a faucet like Alchemy's Sepolia Faucet.
-
Deploy the contract:
npm run deploy:sepolia
-
Interact with the contract:
npx hardhat run scripts/interact.ts --network sepolia
-
Ensure you have sufficient ETH in your wallet to cover gas fees.
-
Deploy the contract:
npm run deploy:mainnet
-
Interact with the contract:
npx hardhat run scripts/interact.ts --network mainnet
You can also interact with the deployed contract using the Hardhat console:
-
For localhost:
npx hardhat console --network localhost
-
For Sepolia:
npx hardhat console --network sepolia
-
For Mainnet:
npx hardhat console --network mainnet
Then, in the console:
const TaxRecord = await ethers.getContractFactory("TaxRecord");
const taxRecord = await TaxRecord.attach("DEPLOYED_CONTRACT_ADDRESS");
await taxRecord.addRecord("John Doe", ethers.parseEther("1.5"));
const record = await taxRecord.getRecord(1);
console.log(record);
Replace DEPLOYED_CONTRACT_ADDRESS
with the actual address of your deployed contract.
- For Sepolia testnet, get free ETH from a Sepolia faucet.
- For mainnet deployment, ensure you have sufficient ETH to cover gas fees.
After deployment, you can verify your contract on Etherscan:
npx hardhat verify --network sepolia DEPLOYED_CONTRACT_ADDRESS
Replace sepolia
with mainnet
for mainnet deployments.
tax-record-smart-contract/
├── contracts/
│ └── TaxRecord.sol
├── scripts/
│ ├── deploy.ts
│ └── interact.ts
├── test/
│ └── TaxRecord.test.ts
├── .env
├── .gitignore
├── hardhat.config.ts
├── package.json
├── tsconfig.json
└── README.md
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License.