Use this repo to test and deploy a crowfunding contract using the hardhat-deploy
plugin.
Check the package.json
file to see a list of scripts for a quicker debugging.
To run this repo you need to install the following packages:
- git
- After installing the package run in the terminal the command
git --version
and if the installation was successful the output should look like this:git version x.xx.x
- After installing the package run in the terminal the command
- Nodejs
- In the terminal run the command
node --version
, if the output looks likevxx.xx.x
that means the package was installed.
- In the terminal run the command
- Yarn
- In the terminal run the command
yarn --version
, if the output looks likex.xx.xx
that means the package was installed.
- In the terminal run the command
Clone this repo, cd into the folder and run yarn
to install all missing dependencies:
git clone https://github.com/PacelliV/hardhat-fund-me.git
cd hardhat-fund-me
yarn
- Compile:
yarn hardhat compile
- Deployment:
yarn hardhat deploy
- Testing:
yarn hardhat test
- Test Coverage:
yarn hardhat coverage
- Setup environment variables:
You'll need to set your RPC_URL_GOERLI
and PRIVATE_KEY
as enviroment variables. You can add them in an .env
file.
PRIVATE_KEY
: The private key of your account (like from Metamask). NOTE: IT IS RECOMMENDED TO CREATE A NEW ACCOUNT FOR TESTING PURPOSES AND NEVER USE AN ACCOUNT WITH REAL FUNDS.- You can learn how to export a private key here.
RPC_URL_GOERLI
: This is the url of the goerli node you are working with. You can set up one for free in Alchemy.
- Get test ETH
Go to https://goerlifaucet.com/ to get free goerliETH.
- Deploy
yarn hardhat deploy --network goerli
After deploying to a tesnet or local net, you can test the functions of the contract by running the following scripts:
yarn hardhat run scripts/fund.js --network goerli
or
yarn hardhat run scripts/withdraw.js --network goerli
To run the scripts in hardhat localhost add a new terminal, run yarn hardhat node
and replace goerli
for localhost
.
To know how much gas will cost running things in your contract, first go to the hardhat.config.js
file, change the status of the gasReporter
from false
to true
and run the following command:
yarn hardhat test
You'll see the cost report in a new file called gasReport.txt
.
The Polygon network is added for comparison, to see how much gas will cost deploying and running your contract in this network just uncomment the lines of code in the hardhat.config.js
file or add any other network of your interest.
To get an USD estimation of gas you will need an API to get price of different cryptocurrencies, you can get one for free from CoinMarketCap and set up this API as COINMARKETCAP_API_KEY
, this variable aslo goes in the .env
file.
Then, uncomment the line coinmarketcap: COINMARKETCAP_API_KEY,
in the hardhat.config.js
file to get the USD estimation. It's worth noting that everytime you run your tests it will use an API call, so it might make sense to have the variable coinmarketcap
disabled until you need it. All you need to do to disable it is commenting the line.
If you deploy to a tesnet or mainnet you can verify your contract if you get an API Key from Etherscan and set it as ETHERSCAN_API_KEY
in your .env
file as seen in .env.example
.
In it's current state, if you have your API Key set, it will auto verify contracts goerli contracts.
However, you can manually verify with:
yarn hardhat verify <CONTRACT_ADDRESS> <CONSTRUCTOR_ARGUMENTS>
I want to thanks PatrickAlphaC for teaching me the necessary tools to complete this project in my journey to become a full stack developer.
I hope you like this repo and it ends up being useful for you ๐จโ๐ป