Basic Contract

Start here

Before running this tutorial you will need

  • Rinkeby eth

  • Infura API key

  • ettherscan API key

  • Private key from your wallet address

installation

npm install hardhat
npm install ethers
npm install dotenv
npm install @nomiclabs/hardhat-ethers
npm install @nomiclabs/hardhat-etherscan

npx hardhat // deploy hardhat

contracts/Practice.sol

scripts/deploy.js

Please change YOUR_CONTRACTNAME to your contract name you want to deploy. (in this case "Practice")

const { ethers, upgrades } = require("hardhat");
async function main () {
	// We get the contract to deploy
	const Box = await ethers.getContractFactory('YOUR_CONTRACT_NAME');
	console.log('Deploying Box...');
	const box = await Box.deploy();
	await box.deployed();
	console.log('Box deployed to:', box.address);
  }
  
  main()
	.then(() => process.exit(0))
	.catch(error => {
	  console.error(error);
	  process.exit(1);
	});

hardhat.config.js

Overwrite your hardhat config with codes below...

require("@nomiclabs/hardhat-ethers");
require("@nomiclabs/hardhat-etherscan");
require('dotenv').config()

console.log(process.env.INFURA_API_KEY, process.env.PRI_KEY);
module.exports = {
  solidity: "0.8.10",
  networks: {
    rinkeby: {
      url: `https://rinkeby.infura.io/v3/${process.env.INFURA_API_KEY}`,
      accounts: [process.env.PRI_KEY],
    },
  },
  etherscan: {
    apiKey: process.env.ETHERSCAN_API_KEY,
  },
};

.env

INFURA_API_KEY=xxxxxxxxx
PRI_KEY=yyyyyyy
ETHERSCAN_API_KEY=zzzzzzzzzzz

INFURA_API_KEY

MAKE SURE YOU DONT SHARE YOUR PRIVATE KEY TO ANYONE ELSE !!!

Folder structure

your folder should look like this now

Deploy via terminal


//run script
env $(cat .env) npx hardhat run --network rinkeby scripts/deploy.js

//verify contract
env $(cat .env) npx hardhat verify --network rinkeby CONTRACT_ADDRESS

// if any error
npx hardhat clean

// IF ERROR ON WINDOWS USER, RUN VS CODE AS ADMINISTRATOR
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

Last updated

Was this helpful?