Build Your Own Criptocurrency with Ethereum

A Gentle Introduction

Ethereum is a programmable blockchain with a decentralized platform to run your smart contracts on. These contracts are executed within the blockchain and run exactly as scheduled without any possibility of speed drop, censorship, fraud or third party interference [ETHEREUM, 2017].

Ethereum has arrived with innovations, introducing smart contract and other ways that we can utilize a blockchain — not only for cryptocurrency — and with it a new world of possibilities opens up. I will show you the basics of creating your own cryptocurrency, so that you can get a sense of all of this.

What is a Smart Contract?

Smart contracts can be described as highly programmable digital money. Imagine sending money automatically from one person to another, but only when a certain set of conditions are met. Imagine an example: in a real-world case, would you enter into a contract with someone you’ve never met? Your answer to that question is probably No, and the reason is probably that a contract requires an involved infrastructure. Sometimes you need a relationship of trust between the parties; sometimes you depend on the legal work system, police force, lawyer costs, among other factors.

But in Ethereum, you don’t need any of this. All requirements can be automated by the blockchain and you will be in a reliable environment at a lower cost.

The development environment

Installing what you need

Solidity is a high-level programming language for smart contracts whose syntax is similar to JavaScript. It is designed for the Ethereum Virtual Machine (EVM).

Solidity is statically typed, supports inheritance, libraries, and complex user-defined types. It is the most popular language for use in smart contracts, but it’s not the only one. There are other options such as Serpent, LLL and Mutan — the latter of which is no longer used [READTHEDOCS, 2017].

To create your own cryptocurrency with Ethereum, you will need to install Solidity and a few other tools:

  • REMIX, a Browser-based IDE;

  • Solidity plugin for IntelliJ IDEA;

  • Visual Studio Extension Solidity plugin for Microsoft Visual Studio; and

For more integrations consult this link: https://solidity.readthedocs.io/en/develop/.

Starting development

The upcoming Figures describe the code of our cryptocurrency example. The code is also available at this GitHub link.

ERC20 Token Standard

The ERC20 Token Standard is an interface of what functions and events an Ethereum token contract should implement. It is described in Figure 3:

pragma solidity ^0.4.8;

interface ERC20 {
    function totalSupply() constant returns (uint256 totalSupply);
    function balanceOf(address _owner) constant returns (uint256 balance);
    function transfer(address _to, uint256 _value) returns (bool success);
    function transferFrom(address _from, address _to, uint256 _value) returns (bool success);
    function approve(address _spender, uint256 _value) returns (bool success);
    function allowance(address _owner, address _spender) constant returns (uint256 remaining);

    event Transfer(address indexed _from, address indexed _to, uint256 _value);
    event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}

Here’s a brief explanation of what each method does:

totalSupply returns how many tokens the cryptocurrency has.
balanceOf returns how many tokens a user has.
transfer receives two parameters: “to” is the address you want to send a value to; “value” is how much you will send.
transferFrom receives three parameters: “from”, “to”, and “value”. It is just another way of transfering.
approve returns whether the transfer has been successfully finished.
**allowance **returns the quantity of remaining tokens which have not been spent yet.

These methods describe the basic operations and events that you will need to implement in your cryptocurrency.

The SafeMath library

To prevent overflow from occurring in transfer operations when working with big numbers, you can use the SafeMath library, which is displayed in Figure 4.

pragma solidity ^0.4.11;
/**
 * @title SafeMath
 * @dev Math operations with safety checks that throw on error
 */
library SafeMath {
  function mul(uint256 a, uint256 b) internal constant returns (uint256) {
    uint256 c = a * b;
    assert(a == 0 || c / a == b);
    return c;
  }

  function div(uint256 a, uint256 b) internal constant 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 constant returns (uint256) {
    assert(b <= a);
    return a - b;
  }

  function add(uint256 a, uint256 b) internal constant returns (uint256) {
    uint256 c = a + b;
    assert(c >= a);
    return c;
  }
}

Complete Implementation of ERC20 token

Figures 5 show the complete implementation of the interface described in Figure 3. Again, this is just a suggestion of what you can do, and by no means a hard rule. Thus, you can change it however you want.

pragma solidity ^0.4.8;

import './ERC20.sol';
import './Safemath.sol';
contract Token is ERC20 {
    using SafeMath for uint256;

    uint public _totalSupply = 0;
    string public constant symbol = "Code";
    string public constant name = "CodeToken";
    uint8 public constant decimals = 18;

    uint256 public constant RATE = 500;
    address public owner;

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

    function token (){
        owner = msg.sender;
    }

    function createTokens() payable{
        require(msg.value > 0);
        uint256 tokens = msg.value.mul(RATE);
        balances[msg.sender] = balances[msg.sender].add(tokens);
        owner.transfer(msg.value);
        _totalSupply = _totalSupply.add(tokens);
    }

    function () payable {
        createTokens();
    }

    function totalSupply() constant returns (uint256 totalSupply){
        return _totalSupply;

    }

    function balanceOf(address _owner) constant returns (uint256 balance){
        return balances[_owner];

    }

    function transfer(address _to, uint256 _value) returns (bool success){
        require(
            balances[msg.sender] >= _value
            && _value > 0
        );
        balances[msg.sender] = balances[msg.sender].sub(_value);
        balances[_to] =balances[_to].add(_value);
        Transfer(msg.sender, _to, _value);
        return true;
    }

    function transferFrom(address _from, address _to, uint256 _value) returns (bool success){
        require(
            allowed[_from][msg.sender] >= _value
            && balances[_from] >= _value
            && _value > 0
        );
        balances[_from] =balances[_from].sub(_value);
        balances[_to] =balances[_to].add(_value);
        allowed[_from][msg.sender] =allowed[_from][msg.sender].sub(_value);
        Transfer(_from,_to,_value);
        return true;
    }

    function approve(address _spender, uint256 _value) returns (bool success){
        allowed[msg.sender][_spender] = _value;
        Approval(msg.sender, _spender, _value);
        return true;
    }

    function allowance(address _owner, address _spender) constant returns (uint256 remaining){
        return allowed[_owner][_spender];

    }
    event Transfer(address indexed _from, address indexed _to, uint256 _value);
    event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}

Additional code to turn your cryptocurrency into a crowdsale

To transform your ERC20 token into a crowdsale, you’ll need to add in the code available in figure 6. What it does is perform a different function when you send Ether toward a contract without calling any specific function. It will transform the value of Ether into a token amount for that contract.

    function createTokens() payable{
        require(msg.value > 0);
        uint256 tokens = msg.value.mul(RATE);
        balances[msg.sender] = balances[msg.sender].add(tokens);
        owner.transfer(msg.value);
        _totalSupply = _totalSupply.add(tokens);
    }

    function () payable {
        createTokens();
    }

Deploying your cryptocurrency

The topics below go over the requirements to deploy your smart contract.

Metamask

To deploy your cryptocurrency, you will need to install Wallet software, which is available on various environments. You can either install it locally on your machine or use an online wallet.

We’ve chosen to present the Metamask Chrome plugin (Figure 7), which counts with a friendly interface and does not require downloading all of the blockchain from Ethereum (nowadays, the blockchain is a huge 20GB).

Figure 7, MetamaskFigure 7, Metamask

Official Wallet

The user interface looks like the one pictured in Figure 8, where you have a wallet to install. You can also deploy your code or create your own cryptocurrency.

figure 8, Ethereum Wallet, avalaible in: ethereum.orgfigure 8, Ethereum Wallet, avalaible in: ethereum.org

Get Ether for test in a testnet environment

You will need Ether to perform the operations that will allow you to create your cryptocurrency. One way to get Ether is via mining, or you can simply buy it with real money [ETHEREUM DOCS, 2017].

Final considerations

The blockchain technology is often seen as cryptocurrency-only and nothing else. However, the possibilities and the future of the technology are quite promising: financial systems (such as the Stellar payment protocol), secure data storage, and distribution of media are just a few examples. Distribution of media, particularly, suffers from content piracy — but when distributed via blockchain it is possible to avoid fraud because only the owner has access to his media (similarly to Bitcoin). Others possibilities enabled by the technology include electronic voting and personal identifiers.

When compared to Bitcoin, your blockchain has a long confirmation interval, a need for high computational power, and a high energy expenditure. In contrast, the Ethereum blockchain has brought solutions to Bitcoin problems such as long confirmation intervals and centralization of mining around a few users. However, its greater complexity in relation to Bitcoin’s architecture implies in inferior security.

In the end, we can’t predict what cryptocurrencies will stand out. Perhaps a new coin that joins the simplicity of Bitcoin with all the potential of Ethereum[de Lucena et al].

The code is available in this link: https://github.com/BCecatto/EthereumExample
If you want more information about the world of Ethereum go to: https://www.ethereum.org/token

References

ETHEREUM, available at: ethereum.org, accessed in March 5, 2018.

ETHEREUM DOCS, available at: http://ethdocs.org/en/latest,
accessed in March 5, 2018.

READTHEDOCS, available at: https://solidity.readthedocs.io/en/develop/,
accessed in March 5, 2018.

Thanks to Paulo Chaves, Guilherme Vinicius Moreira, and Tiárli Oliveira.