This project is a simple demonstration of using Hardhat with TypeScript to develop, compile, test, and deploy smart contracts.
- Initialize NPM
mkdir ts-hardhat
cd ts-hardhat
npm init- Install Hardhat
npm install --save-dev hardhat- Create Hardhat Project
npx hardhat --initTo compile your Solidity smart contracts:
npx hardhat compileThis generates artifacts in the artifacts/ folder.
Your test files live inside the test/ folder.
To run tests:
npx hardhat testThe deployment script is inside scripts/deploy.ts.
import { network } from "hardhat";
const { ethers } = await network.connect();
async function main() {
const contractFactory = await ethers.getContractFactory('FundMe');
const fundMe = await contractFactory.deploy();
const address = await fundMe.getAddress();
console.log(`Contract deploy at ${address}`);
}
main()
.then(() => process.exit(0))
.catch((err) => process.exit(1))- Deploy the contract
npx hardhat run scripts/deploy.ts-
Deploy to a Custom Network (Example: Hardhat Local Node)
- Start node:
npx hardhat node
- Deploy:
npx hardhat run scripts/deploy.ts --network hardhatLocal
| Purpose | Command |
|---|---|
| Create project | npx hardhat --init |
| Compile contracts | npx hardhat compile |
| Run tests | npx hardhat test |
| Deploy | npx hardhat run scripts/deploy.ts --network <network> |
| Start local node | npx hardhat node |