Enrollment No.
: 236470316013
PRACTICAL-7
AIM: Understand the Concept of Creating ERC20 token & Implementation Using
Python Programming. (CO4)
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/[Link]";
contract MyToken is ERC20 {
constructor() ERC20("MyToken", "MTK") {
// Mint initial supply to the contract deployer
_mint([Link], 1000000 * 10**decimals());
}
}
Step 0 – Installing Global Dependencies (optional)
In order to use Hardhat, you need to have NodeJS (and NPM) installed on your machine.
Step 1 – Setting Up the Development Environment
First of all, create a new folder where the project will be located. Once you have created
the folder, run the following command inside it:
npx hardhat
Step 2 – Knowing the Project Structure
After the setup process is done, you can open the project folder with an IDE (or modern
text editors), and you will see this folder structure:
[TDEC/IT/6TH SEM/FOB]
Enrollment No.: 236470316013
• contracts: The folder containing all relevant source code (Solidity smart contracts);
• scripts: Where useful scripts for interacting with the blockchain are located;
• test: An important folder that will keep all automated tests for you to test your contracts
and improve their security;
• [Link]: All Hardhat settings are placed inside this file;
• [Link]: The file that stores general info about your project such as name, version,
and dependencies;
• [Link]: Used for storing info about your installed dependencies versioning.
This file is generated automatically, and you should never edit this file manually.
Step 3 – Installing External Dependencies
As we talked before, standards like ERC-20 are discussed and improved by the
community. One of the biggest organizations in this world is OpenZeppelin, a huge
player that is responsible for developing some of these well-known standards. We will
use their contracts to help us to develop our ERC-20 token.
For installing OpenZeppelin’s contracts, we will use this command:
npm install @openzeppelin/contracts --save-dev
Step 4 – Creating the Smart Contract
After installing OpenZeppelin’s contracts, we will clean our folders, removing contents
from contracts, scripts and tests.
Now that we have our smart contract file created.
// SPDX-License-Identifier: MIT
[TDEC/IT/6TH SEM/FOB]
Enrollment No.: 236470316013
pragma solidity 0.8.17;
contract Token {
constructor() {
}
}
After writing that piece of code, let’s import the dependencies from OpenZeppelin and
use it on our smart contract. For that, we should add this line to the top of the file (ideally,
between the pragma version and contract declaration):
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;
import "@openzeppelin/contracts/token/ERC20/[Link]";
contract Token is ERC20 {
constructor() ERC20("Rocket Token", "ROCKET") {
}
}
The code is almost done, let’s just do one thing: mint some tokens at the time of
deployment. We will call the _mint function from ERC20 to send one million tokens to
the deployer:
// SPDX-License-Identifier: MIT
[TDEC/IT/6TH SEM/FOB]
Enrollment No.: 236470316013
pragma solidity 0.8.17;
import "@openzeppelin/contracts/token/ERC20/[Link]";
contract Token is ERC20 {
constructor() ERC20("Rocket Token", "ROCKET") {
_mint([Link], 1_000_000 * 10 ** decimals());
}
}
Step 5 – Modifying and Extending Default ERC-20 Behaviors
Modification #1 – allowing users to burn their tokens:
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;
import "@openzeppelin/contracts/token/ERC20/[Link]";
contract Token is ERC20 {
constructor() ERC20("Rocket Token", "ROCKET") {
_mint([Link], 1_000_000 * 10 ** decimals());
}
function burn(uint256 amount) external {
_burn([Link], amount);
}
}
[TDEC/IT/6TH SEM/FOB]
Enrollment No.: 236470316013
Modification #2 – allow users to mint new tokens by depositing some ether:
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;
import "@openzeppelin/contracts/token/ERC20/[Link]";
contract Token is ERC20 {
uint256 price = 0.01 ether; // price of 1 token in ether
constructor() ERC20("Rocket Token", "ROCKET") {
_mint([Link], 1_000_000 * 10 ** decimals());
}
function burn(uint256 amount) external {
_burn([Link], amount);
}
function buy() external payable {
require([Link] > 0, "You must send some ether");
_mint([Link], [Link] * 10 ** decimals() / price);
}
}
Step 6 – Compiling and Deploying
Before building and running our deploy script, install dotenv to manage secrets inside
your project:
npm install dotenv --save-dev
[TDEC/IT/6TH SEM/FOB]
Enrollment No.: 236470316013
To deploy our contract to the blockchain, we will create a [Link] file inside
the scripts folder:
const { ethers } = require('hardhat');
async function main() {
const [deployer] = await [Link]();
[Link]('Deploying contracts with the account:', [Link]);
[Link]('Account balance:', (await [Link]()).toString());
const Token = await [Link]('Token');
const token = await [Link]();
[Link]('Token address:', [Link]);
}
main();
After creating our script, we will add our wallet private keys to make a real transaction on
the blockchain. For that, edit the [Link] file as the following:
require('dotenv/config');
require('@nomicfoundation/hardhat-toolbox');
/** @type import('hardhat/config').HardhatUserConfig */
[Link] = {
solidity: '0.8.17',
networks: {
goerli: {
accounts: {
[TDEC/IT/6TH SEM/FOB]
Enrollment No.: 236470316013
mnemonic: [Link],
},
url: '<[Link]
},
},
};
In the case, we are using ETH Goerli Testnet. If you want to use a mainnet network,
replace the goerli object with a valid network config.
Now you will need to input your wallet’s mnemonic to use it to authenticate and deploy
the smart contract to the network. Create a .env file in the root of the project with the
following content:
MNEMONIC=rifle enter coyote much acid smooth dolphin stairs south cattle immense
paper
The mnemonic here is random and is connected to a wallet without funds. Replace the
content with a wallet’s mnemonic with enough funds to cover the deployment expenses.
After all that, we should be able to run the deployment script with this command:
npx hardhat run scripts/[Link] --network goerli
In this context, the name “goerli” refers to the name of the network inside
the networks object defined in [Link].
npx hardhat flatten > [Link]
[TDEC/IT/6TH SEM/FOB]
Enrollment No.: 236470316013
o/p
Deploying contracts with the account:
0x70997970C51812dc3A010C7d01b50e0d17dc79C8
Account balance: 10000000000000000000
Token address: 0x5FbDB2315678afecb367f032d93F642f64180aa3
[TDEC/IT/6TH SEM/FOB]