Image default
Ethereum

Create your own ERC 20

Add

We are all familiar with ICO’s (Initial coin offering) by now. Many projects rake in millions of $ from investors with the promise of a revolutionary technology through smart contracts that will change the world.
Quite often there is only that, a promise, along with a white paper and a website and the MVP will be developed after the crowdsale. At this stage (and many projects may never get past this stage) the ‘revolutionary’ smart contracts all look somewhat similar and offer no functionality but a crowdsale.

In this article we’ll demonstrate how easy it is to actually make your own ERC20 token for the Ethereum blockchain

Step 1: The ERC20 Interface
In Solidity, the programming language for Ethereum, an interface is a piece of reusable code that defines constraints for functions and events, without implementing the function itself. A contract with the ERC20 interface must have implemented all functions defined in the interface.
Luckily we do not have to write all of this code ourselves and we can just copy it from the following repository: https://github.com/OpenZeppelin/zeppelin solidity/tree/master/contracts/token –
To get started open https://remix.ethereum.org and make two files: SafeMath.sol and ERC20.sol paste in the code below into each of the respective files.

pragma solidity ^0.4.10;

library SafeMath {
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
uint256 c = a * b;
assert(c / a == b);
return c;
}

function div(uint256 a, uint256 b) internal pure returns (uint256) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn’t hold
return c;
}

function sub(uint256 a, uint256 b) internal pure returns (uint256) {
assert(b <= a); return a - b; } function add(uint256 a, uint256 b) internal pure returns (uint256) { uint256 c = a + b; assert(c >= a);
return c;
}
}
………………………………………………………………………………………………………………………………………….
pragma solidity ^0.4.10;

interface ERC20 {
function balanceOf(address who) public view returns (uint256);
function transfer(address to, uint256 value) public returns (bool);
function allowance(address owner, address spender) public view returns (uint256);
function transferFrom(address from, address to, uint256 value) public returns (bool);
function approve(address spender, uint256 value) public returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}

Step 2: Implementing our functions
Great work, or building blocks are almost set. Now we have to implement the functions we have defined in our interface. For this we will combine BasicToken.sol and StandardToken.sol from OpenZeppelin into one file called StandardToken.sol .
Here’s a high level overview of what these functions do:
BalanceOf : returns the token balance of an Ethereum address.
Transfer : transfers an amount to an address of our choosing.
Approve : sets an amount of tokens a specified address is allowed to spend on our behalf
Allowance : check how much an address is allowed to spend on our behalf
TransferFrom : specify an address to transfer from if we are allowed by that address to spend some tokens.
Event Transfer and event Approval will log an output event onto the blockchain.

pragma solidity ^0.4.10;

import ‘browser/SafeMath.sol’;
import ‘browser/ERC20.sol’;

contract StandardToken is ERC20 {
using SafeMath for uint;

string internal _name;
string internal _symbol;
uint8 internal _decimals;
uint256 internal _totalSupply;

mapping (address => uint256) internal balances;
mapping (address => mapping (address => uint256)) internal allowed;

function StandardToken(string name, string symbol, uint8 decimals, uint256 totalSupply) public {
_symbol = symbol;
_name = name;
_decimals = decimals;
_totalSupply = totalSupply;
balances[msg.sender] = totalSupply;
}

function name()
public
view
returns (string) {
return _name;
}

function symbol()
public
view
returns (string) {
return _symbol;
}

function decimals()
public
view
returns (uint8) {
return _decimals;
}

function totalSupply()
public
view
returns (uint256) {
return _totalSupply;
}

function transfer(address _to, uint256 _value) public returns (bool) {
require(_to != address(0));
require(_value <= balances[msg.sender]); balances[msg.sender] = SafeMath.sub(balances[msg.sender], _value); balances[_to] = SafeMath.add(balances[_to], _value); Transfer(msg.sender, _to, _value); return true; } function balanceOf(address _owner) public view returns (uint256 balance) { return balances[_owner]; } function transferFrom(address _from, address _to, uint256 _value) public returns (bool) { require(_to != address(0)); require(_value <= balances[_from]); require(_value <= allowed[_from][msg.sender]); balances[_from] = SafeMath.sub(balances[_from], _value); balances[_to] = SafeMath.add(balances[_to], _value); allowed[_from][msg.sender] = SafeMath.sub(allowed[_from][msg.sender], _value); Transfer(_from, _to, _value); return true; } function approve(address _spender, uint256 _value) public returns (bool) { allowed[msg.sender][_spender] = _value; Approval(msg.sender, _spender, _value); return true; } function allowance(address _owner, address _spender) public view returns (uint256) { return allowed[_owner][_spender]; } function increaseApproval(address _spender, uint _addedValue) public returns (bool) { allowed[msg.sender][_spender] = SafeMath.add(allowed[msg.sender][_spender], _addedValue); Approval(msg.sender, _spender, allowed[msg.sender][_spender]); return true; } function decreaseApproval(address _spender, uint _subtractedValue) public returns (bool) { uint oldValue = allowed[msg.sender][_spender]; if (_subtractedValue > oldValue) {
allowed[msg.sender][_spender] = 0;
} else {
allowed[msg.sender][_spender] = SafeMath.sub(oldValue, _subtractedValue);
}
Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
return true;
}

}

Step 3: Add your own touch
That was the hardest part. Now all there’s left to do is specify a name, symbol and supply for our token.

Because we tweaked things a little bit we do not have to actually code this! We can just enter the parameters we want when creating the contract in remix. Next to the create button enter a name, symbol, # decimals and total supply in that order (see example below) and press create.

Example
Well done ! You have successfully created your first token. Now you can test the functions by sending tokens to the addresses you’ll find in the dropdown menu next to ‘account’ in remix.
You can also deploy this to one of the test networks if you install Metamask.

Add

Related posts

Institutional demand for Cryptocurrency increases.

CP Team

Koinex introduces 23 crypto-to-crypto trading pairs

CP Team

Ethereum Fund Recovery Proposal Continues To Spark Debate

CP Team

Leave a Comment