Compiling and deploying an ERC-20 contract on Sepolia testnet using Hardhat in this assignment.
-
Follow the instructions here to get an API key for Sepolia (not Goerli).
-
Take note of the following:
npx
commands are always run in the project root directory. Don't run them inside subdirectories.- Never upload your
.env
files to Github. It has your private key and Alchemy API key. These can be misused.
-
Create a new directory and enter it.
mkdir my-token cd my-token
-
Initialize a new Node.js project.
npm init -y
The directory should contain a single file called
package.json
. -
Install Hardhat by running the following command in the
my-token
directory.npm install --save-dev hardhat
The
package.json
file will now have ahardhat
section underdevDependencies
. -
Create a Hardhat project by running the following command.
npx hardhat
Select
Create a JavaScript project
and chooseY
for installing@nomicfoundation/hardhat-toolbox
.The directory will have a file called
hardhat.config.js
with the following contents.require("@nomicfoundation/hardhat-toolbox"); /** @type import('hardhat/config').HardhatUserConfig */ module.exports = { solidity: "0.8.24", };
-
Create a file called
.env
in the project directory with the following contents.API_URL = "https://eth-sepolia.g.alchemy.com/v2/your-api-key" PRIVATE_KEY = "your-metamask-private-key"
Follow these instructions to export your private key from Metamask. The
API_URL
needs to be copied from your Alchemy account.NOTE: If you are going to push the project code to a public Github/Gitlab repository, remember to add the
.env
file to your.gitignore
. -
Update the
hardhat.config.js
file to have the following content.require("@nomicfoundation/hardhat-toolbox"); require('dotenv').config(); const { API_URL, PRIVATE_KEY } = process.env; /** @type import('hardhat/config').HardhatUserConfig */ module.exports = { solidity: "0.8.24", defaultNetwork: "sepolia", networks: { hardhat: {}, sepolia: { url: API_URL, accounts: [`0x${PRIVATE_KEY}`] } }, };
-
Your ERC-20 token will be based on the implementation by OpenZeppelin. Install the Node.js package containing OpenZeppelin's contracts by running the following command in the project directory.
npm install @openzeppelin/contracts
The installed contracts can be found in the
node_modules
directory in your project directory. The path will benode_modules/@openzeppelin/contracts/
. We will be inheriting the ERC-20 implementation atnode_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol
.
-
The project directory contains a
contracts
directory with aLock.sol
file. Delete it. -
Create a new file in the
contracts
directory calledMyToken.sol
. -
Copy and paste the following code into
MyToken.sol
after replacingXYZ
with your initials. For example, my initials would beKSA
.//SPDX-License-Identifier: Unlicense pragma solidity ^0.8.0; import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; contract XYZToken is ERC20 { uint constant _initial_supply = 100 * (10**18); /* ERC 20 constructor takes in two strings: 1. The name of your token name 2. A symbol for your token */ constructor() ERC20("XYZ Token", "XYZT") { _mint(msg.sender, _initial_supply); } }
-
Compile the contract by running the following command.
npx hardhat compile
You should see a message saying
Compiled 1 Solidity file successfully.
-
Create a directory called
scripts
mkdir scripts
-
Create a file called
deploy.js
in thescripts
directory with the following content. Note: Change "XYZToken" to the name of your contract.async function main() { const MyToken = await ethers.getContractFactory("XYZToken"); const my_token_promise = await MyToken.deploy(); const my_token = await my_token_promise.waitForDeployment(); const my_token_address = await my_token.getAddress(); console.log("ERC-20 contract deployed to address:", my_token_address); } main() .then(() => process.exit(0)) .catch(error => { console.error(error); process.exit(1); });
-
Deploy the contract by running the following command.
npx hardhat run scripts/deploy.js --network sepolia
You should see a message of the following form. The address will be different in your case.
ERC-20 contract deployed to address: 0xbb8Ab9564596Ccbfe0C6eD49D7FdB056eE741CE5
-
Go to https://sepolia.etherscan.io/token/[Your Token Address] to see the token details. Notice that you have to enter the address of the newly created token in the URL.
-
Follow the instructions in sections 4.1, and 4.2 here to deploy an ERC-20 token contract.
-
Follow the instructions in section 4.3 here to transfer some your tokens to a different addresss.
-
My Sepolia ERC-20 contract: HERE
-
Sepolia Transaction transferring my token: HERE