May 16, 2024 04:54 AM
(This post was last modified: May 16, 2024 05:48 AM by Hackers.)
![[Image: banner-1.gif]](https://i.ibb.co/J74v9LR/banner-1.gif)
Hi all, today you will learn how to create a smart contract that allows you to have your own custom ERC20 token. This can be used as a currency so you can issue tokens for any reason. The token itself does not have any functionality on-contract other than the default OpenZeppelin ERC20 features.
Let me break down the whole contract for you, but if you want to copy the full source code you can do so - it's in the spoiler at end of my post!
![[Image: divider.png]](https://i.ibb.co/9Zr9WzJ/divider.png)
Code
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;Code
contract FallenExampleToken is ERC20, Ownable {Code
uint256 public constant MAX_SUPPLY = 1699786 * 10**18; // 18 max decimal places, max supply of 1699786 'coins'Code
constructor() ERC20("FallenExampleToken", "FET") {
_mint(msg.sender, MAX_SUPPLY);
}The constructor runs once when the contract is deployed. In this case, we are giving our coin a full name & a nickname. This is what will show our token abbreviation on blockchain scanners and data.
In this case, we are minting the total max supply to the address that deploys the contract. You can then use transfer functions to send a balance from your owner address to another user. In other contracts you could remove this _mint() func use in the constructor, which would leave all tokens unminted. This would allow you to then mint on demand later on instead of having them pre-minted. Of course, the amount of logic & special rules you add is up to you.
Code
function mint(address to, uint256 amount) external onlyOwner {
require(totalSupply() + amount <= MAX_SUPPLY, "Exceeds maximum token supply");
_mint(to, amount);
}This is the individual mint function above that is used. It checks to make sure the new amount being minted will not cause the total supply to exceed the max. If so, it will reject it with the require statement's error. If it can proceed, it will mint the new tokens to the address.
I am not showing the transfer function because it's a core part of OpenZeppelin's ERC20 contracts, so you can refer to their documentation on those types of processes.
Code
function decimals() public view virtual override returns (uint8) {
return 18;
}This is what allows external systems to see that this FallenExampleToken (FET) is tracking up to 18 decimal places of sub-tokens, but this is what lets people know what the main token is & how many subtokens make up 1 token.
![[Image: divider-gif.gif]](https://i.ibb.co/G2HzzXd/divider-gif.gif)
Here is the full source code for you to use. You can deploy this as-is and have your own currency immediately. Enjoy! :)
Spoiler (Click to View)
Looking for more? Check this out:
https://hackforums.net/showthread.php?tid=6271201



![[Image: getgrass-signature.png]](https://jacobriggs.io/assets/images/misc/hf/getgrass-signature.png)