Complete Guide to Ethereum Development
Learn to build, test, and deploy smart contracts
Hardhat is a de-facto local development environment for Ethereum software
https://nodejs.org/en/download/
https://www.alchemy.com/
Create separate folders for JavaScript and TypeScript projects:
# Create directories mkdir Project_JScript mkdir Project_TSCript
# Install Hardhat locally npm install --save-dev hardhat # For specific version npm install --save-dev "hardhat@^2.12.0"
This command brings up the project initialization menu:
npx hardhat
again
contracts/ scripts/ test/ .gitignore hardhat.config.js package.json README.md
contracts/ scripts/ test/ typechain-types/ hardhat.config.ts tsconfig.json package.json
require("@nomicfoundation/hardhat-toolbox"); module.exports = { solidity: "0.8.17", };
import { HardhatUserConfig } from "hardhat/config"; import "@nomicfoundation/hardhat-toolbox"; const config: HardhatUserConfig = { solidity: "0.8.17", }; export default config;
Downloading compiler 0.8.17 Compiled 1 Solidity file successfully
Install these modules for comprehensive smart contract testing:
npm install --save-dev @nomicfoundation/hardhat-toolbox npm install --save-dev @typechain/hardhat --legacy-peer-deps npm install --save-dev hardhat-gas-reporter --legacy-peer-deps npm install --save-dev solidity-coverage --legacy-peer-deps
npx hardhat test npx hardhat test test/my-tests.ts
This starts a standalone local Hardhat network with:
Compiled 1 Solidity file successfully Lock with 1 ETH and unlock timestamp 1706936380 deployed to 0x5FbDB2315678afecb367f032d93F642f64180aa3
Update your hardhat.config.js for Goerli deployment:
require("@nomicfoundation/hardhat-toolbox"); const ALCHEMY_API_KEY = "YOUR_ALCHEMY_KEY"; const GOERLI_PRIVATE_KEY = "YOUR_PRIVATE_KEY"; module.exports = { solidity: "0.8.17", etherscan: { apiKey: "YOUR_ETHERSCAN_API_KEY", }, networks: { goerli: { url: `https://eth-goerli.alchemyapi.io/v2/${ALCHEMY_API_KEY}`, accounts: [GOERLI_PRIVATE_KEY] } } };
https://goerlifaucet.com/
# Deploy to Goerli npx hardhat run scripts/deploy.ts --network goerli # Deploy to Sepolia npx hardhat run scripts/deploy.js --network sepolia # Deploy to Mainnet (be careful!) npx hardhat run scripts/deploy.ts --network mainnet
const ALCHEMY_API_KEY = "YOUR_SEPOLIA_KEY"; const SEPOLIA_PRIVATE_KEY = "YOUR_PRIVATE_KEY"; module.exports = { solidity: "0.8.18", networks: { sepolia: { url: `https://eth-sepolia.g.alchemy.com/v2/${ALCHEMY_API_KEY}`, accounts: [SEPOLIA_PRIVATE_KEY] } } };
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; import "@openzeppelin/contracts/utils/Counters.sol"; import "@openzeppelin/contracts/access/Ownable.sol"; contract MyNFT is ERC721URIStorage, Ownable { using Counters for Counters.Counter; Counters.Counter private _tokenIds; constructor() ERC721("MyNFT", "NFT") {} function mintNFT(address recipient, string memory tokenURI) public onlyOwner returns (uint256) { _tokenIds.increment(); uint256 newItemId = _tokenIds.current(); _mint(recipient, newItemId); _setTokenURI(newItemId, tokenURI); return newItemId; } }
// Reduce locked amount in deploy.ts const lockedAmount = ethers.utils.parseEther("0.05"); // Instead of 1 ETH
npx hardhat --verbose
npx hardhat
againhardhat.org/hardhat-runner/docs/guides/typescript
hardhat.org/hardhat-runner/docs/getting-started
hardhat.org/hardhat-network/docs/overview
Ready to build amazing dApps! 🚀