How to create a new Ethereum token with Magic Code Generator

Most of the actual tokens created on the Ethereum platform are based on the ERC20 interface standard, there are, at the moment of writing these words, 5736 tokens supporting the ERC20 interface. This number is growing considerably every month. However, not all tokens act as a store of value.  By defining a common set of rules for Ethereum-based tokens to adhere to, ERC-20 allows developers of wallets, exchanges and other smart contracts, to know in advance how any new token based on the standard will behave.

The ERC20 interface is defined and implemented as an Ethereum smart contract. However, it can potentially be defined in any other language like for instance in C# for the Neo platform

The ERC20 interface is defined as following

interface ERC20Interface {
    function totalSupply() constant returns (uint totalSupply);
    function balanceOf(address _owner) constant returns (uint balance);
    function transfer(address _to, uint _value) returns (bool success);
    function transferFrom(address _from, address _to, uint _value) returns (bool success);
    function approve(address _spender, uint _value) returns (bool success);
    function allowance(address _owner, address _spender) constant returns (uint remaining);
    event Transfer(address indexed _from, address indexed _to, uint _value);
    event Approval(address indexed _owner, address indexed _spender, uint _value);
}

A contract implementing this interface will inherit its specification as illustrated in the following example.

contract FixedSupplyToken is ERC20Interface

Each function defined in the interface needs to be implemented in the contract inheriting the interface. For example, the balance of function will be defined as follow.

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

}

A complete implementation of this interface is documented in the Ethereum Wiki.

Even if this interface allowed great progress by providing a single interface wallet and exchanges can use to interface with tokens, it, nonetheless, carry some problems as documented in the ERC233 proposition.

ERC20 implementation can interpret the standard interface in different ways.

A contract interface is what is presented to smart contracts clients. The smart contract implementing the procedures associated with the interface inherits from the interface. Instead of an interface which allows only functions as members of that interface, a base contract can be used instead. In contrast to an interface, a base class allows data to be inherited. For example, the ERC20 standard base contract could include some data as illustrated in the example below.

contract ERC20Interface {
    string name;
    uint8 decimals;
    string symbol;
    address owner;
    function totalSupply() constant returns (uint totalSupply);
    function balanceOf(address _owner) constant returns (uint balance);
    function transfer(address _to, uint _value) returns (bool success);
    function transferFrom(address _from, address _to, uint _value) returns (bool success);
    function approve(address _spender, uint _value) returns (bool success);
    function allowance(address _owner, address _spender) constant returns (uint remaining);
    event Transfer(address indexed _from, address indexed _to, uint _value);
    event Approval(address indexed _owner, address indexed _spender, uint _value);
}

Since, by default, the properties are public, a solidity compiler will generate getters and setters which basically are functions to this interface. For example, With Web3, the property name will be accessed from JavaScript with:

Contract.name();

Which returns the name stored in this account.

On the other hand, the implementation of this interface will include the needed map to manage the collection of accounts for that created token. Something like:

Mapping (address => uint256) public balances;

Mapping (address => mapping (address => uint 256)) public allowances;

For more details, examine the ERC20 reference implementation in the Ethereum Wiki.

Using Magic Code Generator to create the ERC20 code.

On Magic Code Generator, because it supports a model driven approach, the first step is to specify a model as illustrated below for ERC20:

model = [
    {
        // service specifications
        service: {
            url: 'http://localhost:8545',
            name: 'ERC20Tokens',  
            type: 'ethereum', 
            contractAddress: '0xa588b75c081159c798feb5b8b64dffcce06f1805'
        }
    },
    {
        // singleton specifications
        singleton: {
            name: 'ERC20Interface',
            model: {
                symbol: { type: String, label: "Symbol", default:'MGX' },
                decimals: { type: Number, label: "Decimals", default: 18 },
                supply: { type: Number, label: "Supply", default: 1000000000},
                owner: { type: Address, label: "Owner" }
            },
            interfaces: {
                solidity: [
                    { Function: 'totalSupply() constant returns (uint totalSupply)' },
                    { Function: 'balanceOf(address _owner) constant returns (uint balance)' },
                    { Function: 'transfer(address _to, uint _value) returns (bool success)' },
                    { Function: 'transferFrom(address _from, address _to, uint _value) returns (bool success)' },
                    { Function: 'approve(address _spender, uint _value) returns (bool success)' },
                    { Function: 'allowance(address _owner, address _spender) constant returns (uint remaining)' },
                    { Event: 'Transfer(address indexed _from, address indexed _to, uint _value)' },
                    { Event: 'Approval(address indexed _owner, address indexed _spender, uint _value)' }
                ]
            }
        }
    }
];

At the moment, from this model, the Magic Code Generator automatically produces 2 files. So, less code to write. However, we are working toward the more ambitious goal to produce also the web service and the client application allowing you to set the default values in the smart contract. The data specified in the model will serve as templates to create an angular form communicating with a generated web service which in turn will produce the base contract with the default values set. This base contract can then be inherited into an implementation contract.

Thus, the code architecture proposed by the Magic code Generator is based on the notion of a base contract (which includes data) or an interface contract (which includes only functions). The latter is inherited by an implementation contract. The base contract or interface contract are automatically produced from the model and may also be used to generate a form to fulfill the default contract property values. The contract inheriting from the base contract implements each published function procedures.

contract ERC20Implementation is ERC20Interface {…}

So, to recap, Magic Code Generator produces the Solidity base contract file from which the implementation contract can inherit and implement the procedure associated with each function defined in the interface. Magic Code also generates the JavaScript class encapsulating the interaction with the contract.

The philosophy behind this structure is to separate the concerns in two different areas:

  • The base contract or interface is what is exposed to the outside world, how other contracts and javascript clients will interact with the implemented contract.
  • The implementation contract imports the base contract specification and implements the functions.

Because Magic Code Generator will generate only the base contract, it is not destroyed when the interface is modified. It will simply create a coding discrepancy and produce a compile error.

Actually, Magic Code Generator is packaged as a Visual Studio Code extension. The project is a work in progress as we add new functionalities. To generate the files from a model is quite easy, just a right mouse click (function click on the mac) over the model file name (with a .mdl extension) and select “Magic Code Generator”. It will automatically generate the files from the model as illustrated below.

The Magic Code Generator Visual Studio Code Extension can be installed from the Visual Studio Code marketplace.

Leave a Reply

Your email address will not be published. Required fields are marked *