MA
manishdait/ts-hardhat
This project is a demonstration of using Hardhat with TypeScript to develop, compile, test, and deploy smart contracts.
Hardhat Using Typescript
This project is a simple demonstration of using Hardhat with TypeScript to develop, compile, test, and deploy smart contracts.
Creating a Hardhat + TypeScript Project From Scratch
- Initialize NPM
mkdir ts-hardhat
cd ts-hardhat
npm init- Install Hardhat
npm install --save-dev hardhat- Create Hardhat Project
npx hardhat --initCompile Your Contracts
To compile your Solidity smart contracts:
npx hardhat compileThis generates artifacts in the artifacts/ folder.
Run Tests
Your test files live inside the test/ folder.
To run tests:
npx hardhat testDeploying the Contract
The deployment script is inside scripts/deploy.ts.
Example 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))Run Deployment
- 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
Summary of Commands
| 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 |