Basically, it is a simple Hardhat project. The project contains a Solidity smart contract, which can be deployed to various blockchains such as Sepolia, Ganache, and, of course, Hardhat’s local chain. This project serves as an example of how to deploy smart contracts easily.
-
Contract folder
You can put your contracts here. -
Scripts folder
This folder is for your scripts. -
Tasks folder
Here you can create custom Hardhat tasks. -
Test folder
Here you can write tests for your scripts.
1. Download the repo:
git clone https://github.com/TheRealMagyar/hardhat-simple-storage
2. Go into the project folder:
cd hardhat-simple-storage
3. Install the packages (I use yarn package manager, but you can use npm as well):
yarn
4. Create a .env file in the project folder and paste the following:
SEPOLIA_RPC_URL=https://1rpc.io/sepolia
METAMASK_PRIVATE_KEY=key
GANACHE_RPC_URL=HTTP://127.0.0.1:7545
GANACHE_PRIVATE_KEY=key
ETHERSCAN_API_KEY=api-key
COINMARKETCAP_API_KEY=api-key
fill in with your api keys and private keys.
5. Create a .gitignore file (it is optional)
node_modules
artifacts
cache
coverage
coverage.json
gas-report.txt
.env
.DS_Store
yarn hardhat run scripts/deploy.js
(You can use another networks also, but at first you have to setup them in hardhat.config.js)
yarn hardhat run scripts/deploy.js --netowork sepolia
yarn hardhat help
yarn hardhat test
npx hardhat node
yarn hardhat compile
(Run Codes Easily In The Console)
yarn hardhat console
yarn hardhat console --network sepolia
(it checks the current block number on the chain)
yarn hardhat block-number --network sepolia
(it reports you the transaction costs) At first you have to go in the hardhat.config.js file and enable the gas-reporter. It should look like this:
gasReporter: {
enabled: true,
outputFile: "gas-report.txt",
noColors: true,
currency: "USD",
coinmarketcap: COINMARKETCAP_API_KEY,
token: "ETH",
offline: true,
},
Run a test: yarn hardhat test
and it'll create a gas-report.txt file with the report for you.
It is an important tool for testing. You can see which rows are covered in the SimpleStorage.sol file.
yarn hardhat coverage
These are the default settings. The gas-reporter is disabled in default mode. At the networks you can add another networks. If you use another solidity version you can change it in the 20th row.
module.exports = {
defaultNetwork: "hardhat",
networks: {
sepolia: {
url: SEPOLIA_RPC_URL,
accounts: [METAMASK_PRIVATE_KEY],
chainId: 11155111,
},
ganache: {
url: GANACHE_RPC_URL,
account: GANACHE_PRIVATE_KEY,
chainId: 1337,
},
localhost: {
url: "http://127.0.0.1:8545/",
//accounts: Thanks hardhat lmfao!
chainId: 31337,
},
},
solidity: "0.8.8",
etherscan: {
apiKey: ETHERSCAN_API_KEY,
},
sourcify: {
enabled: true,
},
gasReporter: {
enabled: false,
outputFile: "gas-report.txt",
noColors: true,
currency: "USD",
coinmarketcap: COINMARKETCAP_API_KEY,
token: "ETH",
offline: true,
},
};
Hardhat
yarn add --dev hardhat
yarn hardhat
- create a javascript project
- root (default)
- .gitignore YES
- Do you want dependencies? YES
yarn add --dev prettier prettier-plugin-solidity