{"id":3705,"date":"2022-12-19T07:53:28","date_gmt":"2022-12-19T07:53:28","guid":{"rendered":"https:\/\/metaschool.so\/articles\/?p=3705"},"modified":"2023-08-31T10:24:35","modified_gmt":"2023-08-31T10:24:35","slug":"how-to-create-erc-20-token","status":"publish","type":"post","link":"https:\/\/metaschool.so\/articles\/how-to-create-erc-20-token\/","title":{"rendered":"How to create your own ERC-20 token"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_56_1 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/metaschool.so\/articles\/how-to-create-erc-20-token\/#%E2%AD%90%EF%B8%8F_Some_of_the_key_features_of_ERC-20_tokens\" title=\"\u2b50\ufe0f Some of the key features of ERC-20 tokens\">\u2b50\ufe0f Some of the key features of ERC-20 tokens<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/metaschool.so\/articles\/how-to-create-erc-20-token\/#%F0%9F%8E%AB_How_to_create_an_ERC-20_token_in_8_easy_steps\" title=\"\ud83c\udfab How to create an ERC-20 token in 8 easy steps\">\ud83c\udfab How to create an ERC-20 token in 8 easy steps<\/a><ul class='ez-toc-list-level-3'><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/metaschool.so\/articles\/how-to-create-erc-20-token\/#1_Set_up_a_development_environment\" title=\"1. Set up a development environment\">1. Set up a development environment<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/metaschool.so\/articles\/how-to-create-erc-20-token\/#2_Create_a_new_Truffle_project\" title=\"2. Create a new Truffle project\">2. Create a new Truffle project<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/metaschool.so\/articles\/how-to-create-erc-20-token\/#3_Define_your_ERC-20_token\" title=\"3. Define your ERC-20 token\">3. Define your ERC-20 token<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/metaschool.so\/articles\/how-to-create-erc-20-token\/#4_Compile_the_contract\" title=\"4. Compile the contract\">4. Compile the contract<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/metaschool.so\/articles\/how-to-create-erc-20-token\/#5_Set_up_your_Rinkeby_test_network\" title=\"5. Set up your Rinkeby test network\">5. Set up your Rinkeby test network<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/metaschool.so\/articles\/how-to-create-erc-20-token\/#6_Write_the_smart_contract_code\" title=\"6. Write the smart contract code\">6. Write the smart contract code<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/metaschool.so\/articles\/how-to-create-erc-20-token\/#7_Compile_and_migrate_the_smart_contract\" title=\"7. Compile and migrate the smart contract\">7. Compile and migrate the smart contract<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/metaschool.so\/articles\/how-to-create-erc-20-token\/#8_Test_the_contract\" title=\"8. Test the contract\">8. Test the contract<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n<p>In this easy, step-by-step guide we&#8217;ll be creating an <a href=\"https:\/\/metaschool.so\/articles\/erc20-token\/\">ERC-20 token<\/a> on the Rinkeby test network involves 8 of the following steps! But before that, what&#8217;s an ERC-20 token? <\/p>\n\n\n\n<p>ERC-20 is a standard for tokens on the <a href=\"https:\/\/metaschool.so\/courses\/how-does-ethereum-work-a-deepdive?ref=Articles&amp;utm_source=Blog_Organic\">Ethereum blockchain<\/a>. It defines a set of rules that all ERC-20 tokens must follow, which makes it easier for developers to create and interact with these tokens in a predictable and consistent way.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"some-of-the-key-features-of-erc-20-tokens\"><span class=\"ez-toc-section\" id=\"%E2%AD%90%EF%B8%8F_Some_of_the_key_features_of_ERC-20_tokens\"><\/span>\u2b50\ufe0f Some of the key features of ERC-20 tokens<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Transferability: can be transferred from one Ethereum address to another.<\/li><li><a href=\"https:\/\/metaschool.so\/articles\/fungible-token-meaning\/\">Fungibility<\/a>: are interchangeable, meaning that any two tokens of the same type are equivalent and can be exchanged for one another.<\/li><li>Divisibility: can be divided into smaller units, up to a certain number of decimal places.<\/li><li>Token balance: have a balance associated with each <a href=\"https:\/\/metaschool.so\/articles\/wallet-address-blockchain\/\">Ethereum address<\/a>, which represents the number of tokens that the address holds.<\/li><li>Token approval: can be &#8220;approved&#8221; for transfer by one Ethereum address to another, allowing for the use of third-party smart contracts to facilitate token transfers.<\/li><\/ul>\n\n\n\n<p>Overall, ERC-20 tokens are a popular choice for creating digital assets on the Ethereum blockchain, and have been used for a wide range of applications, including fundraising, loyalty programs, and gaming.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-create-an-erc-20-token-in-8-easy-steps\"><span class=\"ez-toc-section\" id=\"%F0%9F%8E%AB_How_to_create_an_ERC-20_token_in_8_easy_steps\"><\/span>\ud83c\udfab How to create an ERC-20 token in 8 easy steps<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<div class=\"nv-iframe-embed\"><iframe loading=\"lazy\" title=\"Create your Ethereum ERC20 token | Metaschool\" width=\"1200\" height=\"675\" src=\"https:\/\/www.youtube.com\/embed\/HN3e9fJNv0w?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/div>\n<\/div><figcaption><a href=\"https:\/\/metaschool.so\/courses\/create-your-own-ethereum-token-in-just-30-mins?ref=Articles&amp;utm_source=Blog_Organic\">Course: Create your own Ethereum ERC-20 token<\/a><br><\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-set-up-a-development-environment\"><span class=\"ez-toc-section\" id=\"1_Set_up_a_development_environment\"><\/span>1. Set up a development environment<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Install <a href=\"https:\/\/nodejs.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">Node.js<\/a> and the npm package manager.<\/li><li>Install <a href=\"https:\/\/trufflesuite.com\/docs\/truffle\/how-to\/install\/\" target=\"_blank\" rel=\"noreferrer noopener\">Truffle<\/a>, a popular development framework for the <a href=\"https:\/\/metaschool.so\/blockchains\/ethereum\" target=\"_blank\" rel=\"noreferrer noopener\">Ethereum Blockchain<\/a>. You can do this by running the following command:<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\"><code>npm install -g truffle\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-create-a-new-truffle-project\"><span class=\"ez-toc-section\" id=\"2_Create_a_new_Truffle_project\"><\/span>2. Create a new Truffle project<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Create a new directory for your project and navigate to it.<\/li><li>Run the following command to create a new Truffle project:<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\"><code>truffle init\n<\/code><\/pre>\n\n\n\n<p>This will create a basic Truffle project structure with the following directories:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>contracts\/<\/code>: This directory will contain your <a href=\"https:\/\/metaschool.so\/solidity?utm_campaign=smf&amp;utm_source=blog&amp;utm_medium=organic\">Solidity<\/a> contract files.<\/li><li><code>migrations\/<\/code>: This directory will contain scripts for deploying your contracts to the Ethereum network.<\/li><li><code>test\/<\/code>: This directory will contain your test files for testing your contracts.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-define-your-erc-20-token\"><span class=\"ez-toc-section\" id=\"3_Define_your_ERC-20_token\"><\/span>3. Define your ERC-20 token<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Create a new file in the <code>contracts\/<\/code> directory and name it <code>MyToken.sol<\/code>.<\/li><li>Paste the following code into the file. This is the basic structure of an ERC-20 token:<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\"><code>pragma solidity ^0.6.0;\n\nimport \"https:\/\/github.com\/OpenZeppelin\/openzeppelin-solidity\/contracts\/token\/ERC20\/SafeERC20.sol\";\n\ncontract MyToken is SafeERC20 {\n  \/\/ Define your token's name, symbol, and decimals\n  string public name;\n  string public symbol;\n  uint8 public decimals;\n\n  \/\/ Define the total supply of your token\n  uint256 public totalSupply;\n\n  \/\/ Define a mapping to store the balances of each account\n  mapping(address =&gt; uint256) public balances;\n\n  \/\/ Define the constructor function to initialize the variables\n  constructor() public {\n    name = \"My Token\";\n    symbol = \"MTK\";\n    decimals = 18;\n    totalSupply = 1000000000 * (10 ** uint256(decimals));\n    balances&#91;msg.sender] = totalSupply;\n  }\n\n  \/\/ Define a function to allow users to transfer tokens to other accounts\n  function transfer(address _to, uint256 _value) public {\n    require(balances&#91;msg.sender] &gt;= _value &amp;&amp; _value &gt; 0, \"Insufficient balance\");\n    balances&#91;msg.sender] -= _value;\n    balances&#91;_to] += _value;\n  }\n}\n<\/code><\/pre>\n\n\n\n<p>This code defines a contract named <code>MyToken<\/code> that inherits from the <code>SafeERC20<\/code> contract from the <a href=\"https:\/\/metaschool.so\/articles\/what-is-openzeppelin\/\">OpenZeppelin<\/a> library. It has the following features:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A name, symbol, and number of decimals for the token.<\/li><li>A total supply of tokens that is defined in the constructor function.<\/li><li>A mapping to store the balances of each account.<\/li><li>A function to allow users to transfer tokens to other accounts.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-compile-the-contract\"><span class=\"ez-toc-section\" id=\"4_Compile_the_contract\"><\/span>4. Compile the contract<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Open a terminal and navigate to your project directory.<\/li><li>Run the following command to compile your contract:<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\"><code>truffle compile\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-set-up-your-rinkeby-test-network\"><span class=\"ez-toc-section\" id=\"5_Set_up_your_Rinkeby_test_network\"><\/span>5. Set up your Rinkeby test network<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>In the terminal, navigate to the root of your Truffle project directory.<\/li><li>Run the command <code>truffle network --clean<\/code> to reset any previous network configurations.<\/li><li>Run the command <code>truffle network --network rinkeby<\/code> to set up the Rinkeby test network.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-write-the-smart-contract-code\"><span class=\"ez-toc-section\" id=\"6_Write_the_smart_contract_code\"><\/span>6. Write the smart contract code<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Create a file called <code>MyToken.sol<\/code> in the <code>contracts<\/code> directory of your Truffle project.<\/li><li>Copy and paste the following code into the file:<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\"><code>pragma solidity ^0.6.0;\n\nimport \"https:\/\/github.com\/OpenZeppelin\/openzeppelin-solidity\/contracts\/token\/ERC20\/SafeERC20.sol\";\n\ncontract MyToken is SafeERC20 {\n    constructor() public {\n        _mint(msg.sender, 1000000);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>This is the code for a simple ERC-20 token with a constructor that mints 1,000,000 tokens and assigns them to the contract owner.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-compile-and-migrate-the-smart-contract\"><span class=\"ez-toc-section\" id=\"7_Compile_and_migrate_the_smart_contract\"><\/span>7. Compile and migrate the smart contract<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>In the terminal, navigate to the root of your Truffle project directory.<\/li><li>Run the command <code>truffle compile<\/code> to compile the <a href=\"https:\/\/metaschool.so\/articles\/smart-contract-definition\/\">smart contract<\/a>.<\/li><li>Run the command <code>truffle migrate --reset --network rinkeby<\/code> to deploy the contract to the <a href=\"https:\/\/metaschool.so\/articles\/rinkeby-testnet-guide\/\">Rinkeby test network.<\/a><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-test-the-contract\"><span class=\"ez-toc-section\" id=\"8_Test_the_contract\"><\/span>8. Test the contract<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>In the terminal, navigate to the root of your Truffle project directory.<\/li><li>Run the command <code>truffle console --network rinkeby<\/code> to open the Truffle console.<\/li><li>In the console, run the following commands to test the contract:<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\"><code>truffle(rinkeby)&gt; MyToken.deployed().then(inst =&gt; { token = inst })\ntruffle(rinkeby)&gt; token.balanceOf(web3.eth.accounts&#91;0])\n<\/code><\/pre>\n\n\n\n<p>The first command will retrieve the deployed contract instance and assign it to a variable called <code>token<\/code>. The second command will display the balance of the contract owner (i.e., the first account in Ganache). You should see the balance output as 1000000.<\/p>\n\n\n\n<p>That&#8217;s it! You have successfully created an ERC-20 token on the Rinkeby test network. \ud83c\udf89\ud83e\udd20<\/p>\n\n\n\n<p>Note: These instructions are just a basic outline and there are many more steps involved in creating and deploying a production-ready ERC-20 token. It is highly recommended to thoroughly test and debug your contract before deploying it to the main network.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":3858,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[292],"tags":[31],"class_list":["post-3705","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-answers","tag-ethereum-blockchain"],"_links":{"self":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/3705","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/comments?post=3705"}],"version-history":[{"count":10,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/3705\/revisions"}],"predecessor-version":[{"id":6816,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/3705\/revisions\/6816"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media\/3858"}],"wp:attachment":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media?parent=3705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/categories?post=3705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/tags?post=3705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}