![]() |
|
ERC20 Currency Token - Solidity Smart Contract Example - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Money (https://hackforums.net/forumdisplay.php?fid=241) +--- Forum: Crypto Currency (https://hackforums.net/forumdisplay.php?fid=380) +--- Thread: ERC20 Currency Token - Solidity Smart Contract Example (/showthread.php?tid=6271197) |
ERC20 Currency Token - Solidity Smart Contract Example - Hackers - May 16, 2024 ![]() 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! ![]() Code // SPDX-License-Identifier: MITCode 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") {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 {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) {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. ![]() 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 RE: ERC20 Native Token - Solidity Smart Contract Example - Euaggelistes - May 16, 2024 Very nice. Thank you for sharing. It's a good introduction to Solidity. RE: ERC20 Native Token - Solidity Smart Contract Example - Hackers - May 16, 2024 (May 16, 2024 04:59 AM)Euaggelistes Wrote: Very nice. Thank you for sharing. It's a good introduction to Solidity. No problem & thanks! It's not a hard language to pick up, but can be very tedious at first. I like it a lot overall. Been a while since I made a new contract so figured I'd share some examples on HF. If anyone needs any help with this or anything just reply here guys :) BUMP - Stanley - May 16, 2024 I'm Stanley HF's helper bot. The OP (original poster) of this thread wants everyone to see this page and has elected to bump it to the top of the showthread listing. Since HF doesn't want a ton of LQP (Low Quality Posts) this is the best method of bumping your content. There is a small βyte fee for each bump. Any OP can bump any of their threads on HF from the linked icon on the top of their first post. If you haven't already read the first post of this thread we encourage you to do so. Thanks, Stanley |