The Chain logo

Create your own ERC-20 tokens

In this new article we are going to create our own Learn Blockchain Coins (ABC) tokens with the ERC-20 standard to create fungible tokens. We will use one of the possible implementations that meet the ERC-20 standard with which we can later create tokens with specific parameters.

By BlockchainXPublished 4 years ago 6 min read
Like

In this new article we are going to create our own Learn Blockchain Coins (ABC) tokens with the ERC-20 standard to create fungible tokens. We will use one of the possible implementations that meet the ERC-20 standard with which we can later create tokens with specific parameters.

Abstract contract ERC-20

What we are going to do is create an abstract contract that defines the necessary functions for our ERC-20 contracts. For those who don't know what an abstract contract is, the concept is similar to an abstract class. An abstract contract is a contract that cannot be instantiated but can be used as the basis for other contracts. A contract becomes abstract when at least one of its functions does not include implementation, leaving the classes that inherit from said contract to be responsible for the implementation.

contract ERC20Interface {

function totalSupply () public view returns (uint);

function balanceOf (address tokenOwner) public view returns (uint balance);

function allowance (address tokenOwner, address spender) public view returns (uint remaining);

function transfer (address to, uint tokens) public returns (bool success);

function approve (address spender, uint tokens) public returns (bool success);

function transferFrom (address from, address to, uint tokens) public returns (bool success);

event Transfer (address indexed from, address indexed to, uint tokens);

event Approval (address indexed tokenOwner, address indexed spender, uint tokens);

}

The purpose of each of the functions is explained later in the code.

Safe maths library

It is important to use safe math operations that throw exceptions in case the result of a calculation is not correct. With this library we would avoid, for example, the overflow problem generated by adding two numbers whose result is greater than the maximum accepted by the type used. For example, using the uint8 type the largest number we can represent is 255 (2 8 -1). If we add 255 to 1, the result would be 0 because overflow occurs, and that is not the expected result. These kinds of errors have caused problems in ERC20 token contracts recently and should be avoided. You can read more about it at https://media.consensys.net/detecting-batchoverflow-and-similar-flaws-in-ethereum-smart-contracts-93cf5a5aaac8

The code for our safe math operations library would be:

ERC-20 token contract

The following contract implements the functions of our abstract ERC-20 contract and also adds the state variables for the symbol, name, total number of tokens and number of decimals of our token. When the contract is created, the tokens are initially assigned to the creator of the contract.

contract ERC20Token is ERC20Interface {

using SafeMath for uint; // use our safe math library

string public symbol;

string public name;

uint8 public decimals;

uint _totalSupply;

mapping (address => uint) balances;

mapping (address => mapping (address => uint)) allowed;

// ------------------------------------------------ ------------------------

// Constructor

// --------------------- -------------------------------------------------- -

constructor (

uint256 _initialAmount,

string _tokenName,

uint8 _decimalUnits,

string _tokenSymbol

) public {

balances [msg.sender] = _initialAmount;

_totalSupply = _initialAmount;

name = _tokenName;

decimals = _decimalUnits;

symbol = _tokenSymbol;

}

// ----------------------------------------------- -------------------------

// Fixed Total supply

// ------------------ -------------------------------------------------- ----

function totalSupply () public view returns (uint) {

return _totalSupply;

}

// ----------------------------------------------- -------------------------

// Get the token balance for account `tokenOwner`

// ------------------------------------------------ ------------------------

function balanceOf (address tokenOwner) public view returns (uint balance) {

return balances [tokenOwner];

}

// ----------------------------------------------- -------------------------

// Transfer the balance from token owner's account to `to` account

// - Owner's account must have sufficient balance to transfer

// - 0 value transfers are allowed

// ---------------------------------------- --------------------------------

function transfer (address to, uint tokens) public returns (bool success) {

balances [ msg.sender] = balances [msg.sender] .sub (tokens);

balances [to] = balances [to] .add (tokens);

emit Transfer (msg.sender, to, tokens);

return true;

}

// ----------------------------------------------- -------------------------

// Token owner can approve for `spender` to transferFrom (...)` tokens`

// from the token owner's account

//

// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md

// recommends that there are no checks for the approval double-spend attack

// as this should be implemented in user interfaces

// ---------------------------------------- --------------------------------

function approve (address spender, uint tokens) public returns (bool success) {

allowed [msg.sender] [spender] = tokens;

emit Approval (msg.sender, spender, tokens);

return true;

}

// ----------------------------------------------- -------------------------

// Transfer `tokens` from the` from` account to the `to` account

//

// The calling account must already have sufficient tokens approve (...) - d

// for spending from the `from` account and

// - From account must have sufficient balance to transfer

// - Spender must have sufficient allowance to transfer

// - 0 value transfers are allowed

// ------------------------------------------------ ------------------------

function transferFrom (address from, address to, uint tokens) public returns (bool success) {

balances [from] = balances [ from] .sub (tokens);

allowed [from] [msg.sender] = allowed [from] [msg.sender] .sub (tokens);

balances [to] = balances [to] .add (tokens);

emit Transfer (from, to, tokens);

return true;

}

// ----------------------------------------------- -------------------------

// Returns the amount of tokens approved by the owner that can be

// transferred to the spender's account

// - -------------------------------------------------- --------------------

function allowance (address tokenOwner, address spender) public view returns (uint remaining) {

return allowed [tokenOwner] [spender];

}

// ----------------------------------------------- -------------------------

// Fallback function. Don't accept ETH

// ------------------------------------------- -----------------------------

function () public payable {

revert ();

}

}

Deploy our contract in the Ethereum test network

Once our contract is defined it is time to deploy it on the Ethereum testnet. For this we will only have to copy the previous code in Remix indicating the parameters to initialize our token contract (supply, name, decimals and symbol).

Keep in mind that the supply must include the decimals, that is, if you want there to be a total of 1,000 “complete” tokens and they are divisible by 2 decimals, in supply you should enter 100,000 units (or 1,000.00 tokens).

Etherscan

We can see that the contract was created successfully at address 0x5131DbF853ab6341298938fAc2Ca18b1dd172feA in Etherscan:

We are already the proud holders of 1,000 ABC tokens 🙂

From now on we can start playing by sending and receiving tokens.

Add our ABC token to MetaMask

Having followed the ERC-20 standard, we can use any wallet that supports that standard.

MetaMask does not currently support the sending of ERC-20 tokens, but it does help us to visualize them. To view our tokens it is necessary to indicate the address where the contract was deployed and the symbol and number of decimals will automatically be filled in:

Once the token has been added, we can see our balance of ABC tokens:

Send ABC tokens to another address

As we said, it is not possible to send ERC-20 tokens only with MetaMask but we can connect MetaMask and MyEtherWallet to send tokens. MyEtherWallet has the option of accessing an account using MetaMask and thus not having to enter our private key directly:

We will need to add the ABC token to MyEtherWallet:

Once I do this, we will see our balance of ABC tokens in MyEtherWallet and we can send tokens to another address:

We confirm the sending of the transaction in MetaMask:

Once the transaction has been mined we can see the transaction in Etherscan and that the balance of Account 1 is now 900 ABC tokens and Account 2 now shows 100 ABC tokens:

And so far this article. I hope you liked it and if you want ABC tokens do not hesitate to contact me and I will share a few with you, who knows, maybe in a few years they are worth a fortune!

tokens
Like

About the Creator

BlockchainX

BlockchainX is a leading Blockchain development company rendering services across all streams. From NFT market to metaverse development, we help you simplify your tasks and identify better solutions for business in web3.

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.