How to create your own Ethereum Token from scratch

What is an ERC-20 token?

  • TotalSupply: provides information about the total token supply
  • BalanceOf: provides account balance of the owner’s account
  • Transfer: executes transfers of a specified number of tokens to a specified address
  • TransferFrom: executes transfers of a specified number of tokens from a specified address
  • Approve: allow a spender to withdraw a set number of tokens from a specified account
  • Allowance: returns a set number of tokens from a spender to the owner

Creating a token: Prerequisites

  • Metamask wallet chrome extension
  • Select the ropsten test network from metamask
  • Get test ETH from a ropsten Ethereum faucet
  • Set up an empty file in the Remix IDE

Safe math Library

contract SafeMath {

function add(uint a, uint b) public pure returns (uint c) {
c = a + b;
require(c >= a);
}

function sub(uint a, uint b) public pure returns (uint c) {
require(b <= a);
c = a — b;
}

function mul(uint a, uint b) public pure returns (uint c) {
c = a * b;
require(a == 0 || c / a == b);
}

function div(uint a, uint b) public pure returns (uint c) {
require(b > 0);
c = a / b;
}
}

ERC-20 Interface

contract ERC20Interface {function totalSupply() public constant returns (uint);
function balanceOf(address owner) public constant returns (uint balance);
function transfer(address to, uint tokens) public returns (bool success);
function transferFrom(address from, address to, uint tokens) public returns (bool success);
function approve(address spender, uint tokens) public returns (bool success);
function allowance(address _owner, address _spender) public view returns (uint256 remaining)
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed owner, address indexed spender, uint tokens);
}
contract ApproveAndCallFallBack {function getApproval(address from, uint256 tokens, address token, bytes data) public;}

Token Contract

contract TokenName is ERC20Interface, SafeMath {
uint public _totalSupply;
string public name;
string public symbol;
uint8 public decimals;
mapping(address => uint) private balances;
mapping(address => mapping(address => uint)) allowed;
constructor() public {
name = “TurtleToken”;
symbol = “TUR”;
decimals = 18;
_totalSupply = 1000000000000000000000;
balances[Wallet Address] = 09850eBDc96EaadcEE3286616697529670f061D;
emit Transfer(address(0), Wallet Address, _totalSupply);
}
  • TurtleToken is the name of the token.
  • TURis the token symbol, which is a combination of 3 capital letters.
  • 18is an integer value that represents token decimals. This determines how much you can divide your token. The standard is 18. 0 decimals means your token can’t be divisible.
  • 1000000000000000000000is the token supply, and is a integer value. This will determine the total amount of tokens in existence. *Note that the total supply value must have additional zeros depending on your decimals. E.g if your Token Decimals value is 4 and you want 100 tokens in existence then you would enter 100000.*
  • 09850eBDc96EaadcEE3286616697529670f061D is the Wallet Address.Replace this with your MetaMask Wallet Address (you can copy and paste this directly from the MetaMask extension in your browser.) This is where thee initial supply of tokens will go.
function totalSupply() public constant returns (uint) {
return _totalSupply — balances[address(0)];
}
function balanceOf(address owner) public constant returns (uint balance) {
return balances[owner];
}
function transfer(address to, uint tokens) public returns (bool success) {
balances[msg.sender] = sub(balances[msg.sender], tokens);
balances[to] = add(balances[to], tokens);
emit Transfer(msg.sender, to, tokens);
return true;
}
function transferFrom(address from, address to, uint tokens) public returns (bool success) {
balances[from] = sub(balances[from], tokens);
allowed[from][msg.sender] = sub(allowed[from][msg.sender], tokens);
balances[to] = add(balances[to], tokens);
emit Transfer(from, to, tokens);
return true;
}
function approve(address spender, uint tokens) public returns (bool success) {
allowed[msg.sender][spender] = tokens;
emit Approval(msg.sender, spender, tokens);
return true;
}
function allowance(address owner, address spender) public constant returns (uint remaining) {
return allowed[owner][spender];
}
function approveAndCall(address spender, uint tokens, bytes data) public returns (bool success) {
allowed[msg.sender][spender] = tokens;
emit Approval(msg.sender, spender, tokens);
ApproveAndCallFallBack(spender).getApproval(msg.sender, tokens, this, data);
return true;
}
function () public payable {
revert();
}
}

Deploying the Token

Let’s Connect!

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

47. Payment Digest-II

What is ICO? Initial Offering Simply Explained

apM Members Spotlight Series Coming Soon!

Old School Banking… $35.62 in 2 Years. DeFi… $57.77 in 1 DAY!

SWYFT Official Project Update August 18th

Markets Weekly Overview for August 30 — September 5

📝 Let’s check out ApeSale WhitePaper to see our upcoming development path

Coin Master Reward Link 2020

Coin Master Reward Link 2020

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aleeza Jahan

Aleeza Jahan

More from Medium

Why should you use Infura for your web3 projects?

Infura logo

The Complete Guide for Starters to Develop on Solana with Solidity

Current status of cross-chain remittances with overseas exchanges-How to get rid of BINANCE…

Hardhat vs Truffle