1/5
_Follow along the course with this video._ --- ### ERC20 Manual Creation Welcome back! Having covered the basics, let's look at how we can manually create our own ERC20 token. This is going to be one of our fastest lessons yet! ### Setting Up Your Environment Open a terminal in Visual Studio Code and run the following: ```sh mkdir foundry-erc20 cd foundry-erc20 code . ``` The above commands will create a new directory for our project, navigate into it, and open the directory in a new Visual Studio Code window. Once we have Visual Studio Code running, we need to initialize a blank Foundry project. Open up the terminal and execute the following command: ```sh forge init ``` Completing these steps sets up a development environment with some convenient boilerplate layouts to work with. Go ahead and delete our 3 `Counter` examples so we can start with a clean slate. ::image{src='/foundry-erc20s/2-erc20-manual-creation/erc20-manual-creation1.PNG' style='width: 100%; height: auto;'} I'm going to show you 2 different ways to create our own token, first the hard way and then a much easier way! You can begin by creating a new token token our `src` directory named `ManualToken.sol`. We can start this contract the same way we've been practicing this whole time (you'll get really familiar with this bit). ```solidity // SPDX-License-Identifier: MIT pragma solidity ^0.8.18; contract ManualToken {} ``` Now, as we covered in the last lesson, all we need to do to make our token compatible is follow the [**ERC20 Token Standard**](https://eips.ethereum.org/EIPS/eip-20). Essentially this means we need to include the required functions/methods for our deployment to follow this standard. Let's add thing functionality then! Let's start with name, decimals and totalSupply ```solidity // SPDX-License-Identifier: MIT pragma solidity ^0.8.18; contract ManualToken { function name() public pure returns(string memory) { return "Manual Token"; } function totalSupply() public pure returns (uint256) { return 100 ether; // 100000000000000000000 } function decimals() public pure returns (uint8) { return 18; } } ``` > ❗ **NOTE** > Despite being an optional method, we're including `decimals` here as a point of clarification since we're declaring our total supply as 100 ether. 100 ether = 100 + 18 decimals places. The next functions required by the ERC20 standard are `balanceOf` and `transfer`, so let's apply those now. ```solidity // SPDX-License-Identifier: MIT pragma solidity ^0.8.18; contract ManualToken { function name() public pure returns(string memory) { return "Manual Token"; } function totalSupply() public pure returns (uint256){ return 100 ether; // 100000000000000000000 } function decimals() public pure returns (uint8) { return 18; } function balanceOf(address _owner) public pure returns (uint256){ return // ??? } } ``` Hmm, what is this function meant to return exactly? We're probably going to need a mapping to track the balances of each address... ```solidity mapping(address => uint256) private s_balances; ``` So now our `balanceOf` function can return this mapped value based on the address parameter being passed. ```solidity function balanceOf(address _owner) public pure returns (uint256) { return balances[_owner]; } ``` An interesting thing that comes to light from this function is - someone's balance of a token is really just some mapping on a smart contract that says `this number is associated with this address` That's it. All swaps, transfers and trades are represented as an updating to the balance of this mapping. > ❗ **PROTIP** > Our name function could also be represented by a public declaration such as `string public name = "ManualToken";`. This is because Solidity creates public getter functions when compiled for any publicly accessible storage variables! Our next required function is transfer: ```solidity function transfer(address _to, uint256 _amount) public { uint256 previousBalance = balanceOf(msg.sender) + balanceOf(_to); s_balance[msg.sender] -= _amount; s_balance[_to] += _amount; require(s_balance(msg.sender) + s_balance(_to) == previousBalance); } ``` So, a basic transfer function could look something like the above, a simple adjustment of the balances mapped to both the sender and receiver addresses in our contract. ### Wrap Up We could absolutely continue going through each of the required functions outlined in the [**ERC20 Token Standard**](https://eips.ethereum.org/EIPS/eip-20) and eventually come out the other side with a compatible contract, but there's an easier way. In the next lesson, we'll investigate an even easier method to spin up a standard ERC20 protocol, with the help of OpenZeppelin. See you there!
This lesson guides you through the manual creation of your own ERC20 token using Solidity. It covers the setup of your development environment, initialization of your project repository, and step-by-step instructions to build and define your ERC20 token's properties and functionalities.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Advanced smart contract development
How to develop a stablecoin
How to develop a DeFi protocol
How to develop a DAO
Advanced smart contracts testing
Fuzz testing
Manual verification
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Security researcher
$49,999 - $120,000 (avg. salary)
Guest lecturers:
Juliette Chevalier
Lead Developer relations at Aragon
Nader Dabit
Director of developer relations at Avara
Ally Haire
Developer relations at Protocol Labs
Harrison
Founder at GasliteGG
Last updated on November 29, 2024
Solidity Developer
Advanced FoundryDuration: 36min
Duration: 3h 06min
Duration: 5h 02min
Duration: 2h 47min
Duration: 1h 23min
Duration: 4h 28min
Duration: 1h 19min
Duration: 58min
Course Overview
About the course
Advanced smart contract development
How to develop a stablecoin
How to develop a DeFi protocol
How to develop a DAO
Advanced smart contracts testing
Fuzz testing
Manual verification
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Security researcher
$49,999 - $120,000 (avg. salary)
Guest lecturers:
Juliette Chevalier
Lead Developer relations at Aragon
Nader Dabit
Director of developer relations at Avara
Ally Haire
Developer relations at Protocol Labs
Harrison
Founder at GasliteGG
Last updated on November 29, 2024
Testimonials
Read what our students have to say about this course.
Chainlink
Chainlink
Gustavo Gonzalez
Solutions Engineer at OpenZeppelin
Francesco Andreoli
Lead Devrel at Metamask
Albert Hu
DeForm Founding Engineer
Radek
Senior Developer Advocate at Ceramic
Boidushya
WalletConnect
Idris
Developer Relations Engineer at Axelar