How To Create Your Own Cryptocurrency Token

5,484 reads

Welcome to our brief tutorial on how to create your own cryptocurrency token! We’ll use the popular Ethereum “ERC-20” token as a basis for this tutorial. ERC-20 tokens can be used in decentralized smart contracts to represent everything form discounts, currencies, or anything that you want to be publicly tradable and have a fixed supply. We’ll assume our readers have at least a basic understand understanding of coding by including example functions, variables, and standards. Ethereum smart contracts use the language Solidity, which you can learn from the Solidity Documentation. If this tutorial was helpful, please let us know by following us on Twitter, Facebook, Instagram, or Medium. This article is part of the ELIX blog, a series on crowdfunding, technology and blockchain trends.

Background

Below are the separate functions required for an ERC-20 smart contract. It’s important to note that these functions aren’t required for the program to compile, but rather required to conform to the ERC-20 token standard. For some background on what ERC means, check out recent blog article How To Fund Your Next Business With A Crowdlending Smart Contract.

Here’s a list of the standard functions included in ERC-20 smart contracts:

totalSupply() public view returns (uint256 totalSupply) balanceOf(address _owner) public view returns (uint256 balance) transfer(address _to, uint256 _value) public returns (bool success) transferFrom(address _from, address _to, uint256 _value) public returns (bool success) approve(address _spender, uint256 _value) public returns (bool success) allowance(address _owner, address _spender) public view returns (uint256 remaining)

Events:

Events are ways to record when functions execute by providing easily accessible logs for off-chain analysis. For example, they provide easy ways to check when tokens have been transferred. Here are two events always recorded by ERC-20 contracts:

Transfer(address indexed _from, address indexed _to, uint256 _value) . [Triggered when tokens are transferred.] Approval(address indexed _owner, address indexed _spender, uint256 _value) [Triggered whenever approve(address _spender, uint256 _value) is called.]

A token adheres to a standard like ERC-20 if it implements at least these required functions and events.

Getting Set Up

Navigate to the online Solidity IDE Remix.

For this tutorial, we’ll use a Solidity browser IDE called Remix. It’s a great way to get started without having to download a compiler to your computer. Although it’s not great for complex unit testing or debugging, it does contain some syntax debugging tools and the option to choose from a list of most recent compilers. Here’s what Remix looks like when you first open it:

Go ahead and delete the text from ballot.sol, which is a just an example smart contract for a hypothetical voting scenario.

We’re going to make an Ethereum token and will be using ERC-20 standard code as a foundation. Add the following code to Remix to get started:

pragma solidity ^0.4.10;

contract tokenName { // set contract name to token name



string public name;

string public symbol;

uint8 public decimals;

uint256 public totalSupply;



// Balances for each account

mapping(address => uint256) balances;

address devAddress;

// Events

event Approval(address indexed _owner, address indexed _spender, uint256 _value);

event Transfer(address indexed from, address indexed to, uint256 value);



// Owner of account approves the transfer of an amount to another account

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

// This is the constructor and automatically runs when the smart contract is uploaded

function tokenName() { // Set the constructor to the same name as the contract name

name = "add your token name here"; // set the token name here

symbol = "SYMB"; // set the Symbol here

decimals = 18; // set the number of decimals

devAddress=0x0000000000000000000000000000000000000000; // Add the address that you will distribute tokens from here

uint initialBalance=1000000000000000000*1000000; // 1M tokens

balances[devAddress]=initialBalance;

totalSupply+=initialBalance; // Set the total suppy

}

function balanceOf(address _owner) constant returns (uint256 balance) {

return balances[_owner];

}

// Transfer the balance from owner's account to another account

function transfer(address _to, uint256 _amount) returns (bool success) {

if (balances[msg.sender] >= _amount

&& _amount > 0

&& balances[_to] + _amount > balances[_to]) {

balances[msg.sender] -= _amount;

balances[_to] += _amount;

Transfer(msg.sender, _to, _amount);

return true;

} else {

return false;

}

}

function transferFrom(

address _from,

address _to,

uint256 _amount

) returns (bool success) {

if (balances[_from] >= _amount

&& allowed[_from][msg.sender] >= _amount

&& _amount > 0

&& balances[_to] + _amount > balances[_to]) {

balances[_from] -= _amount;

allowed[_from][msg.sender] -= _amount;

balances[_to] += _amount;

return true;

} else {

return false;

}

}

// Allow _spender to withdraw from your account, multiple times, up to the _value amount.

// If this function is called again it overwrites the current allowance with _value.

function approve(address _spender, uint256 _amount) returns (bool success) {

allowed[msg.sender][_spender] = _amount;

Approval(msg.sender, _spender, _amount);

return true;

}

}

Change the token contract name to your desired token name:

contract tokenName { // set contract name to token name

Next, change the constructor name to match the contract name:

function tokenName() { // Set the constructor to the same name as the contract name

Next, set the token symbol, name, decimals and total supply in the constructor:

name = "add your token name here"; // set the token name here

symbol = "SYMB"; // set the Symbol here

decimals = 18; // set the number of decimals

...

uint initialBalance=1000000000000000000*1000000; // 1M tokens

Think of the Symbol as a ticker representing your currency. Try to think of a Symbol that is easy to say and remember.

Decimals describes the smallest unit of your currency in 10^-{decimals}.

Total supply is denoted in the smallest units of the token. With that in mind, the total supply in the above scenario is equivalent to one million tokens, since the smallest unit of this token is 10^-18 units.

Note: there are more functions that can be added to fixed token supply contracts. See the ERC-20 page of the Ethereum wiki for more information.

Compiling Your Code

After you’ve set up the smart contract in Remix, it’s time to compile it. When you compile Ethereum smart code, the compiler produces several pieces of useful information:

The bytecode are the specific program instructions for execution. In the next step, we’ll add the bytecode to the Ethereum blockchain. The Application Binary Interface (ABI) defines the functions and variables in the smart contract, as well as the types of inputs and outputs. You can think of it as defining the functions of the contract as black boxes, and additionally defining all variables. If in future tutorials we describe how to interact with smart contracts, we’ll use the ABI.

Navigate to the upper right of the screen and choose the most recent compiler:

A brief note: Enable optimization if your compiler asks to make your bytecode more efficient. Remember, you pay Ether for every operation (“opcode”)… so the less efficient the compiler in compiling the program logic, the more you’ll spend when interacting with the contract. Enabling optimization in Remix is simple: just check this box under the compiler selection:

Once compiled, the right pane will then show you useful information about the compiled contract. Click on bytecode to view and copy it. You can also inspect the ABI to the left of Bytecode.

Now, we’re ready to add the code to the Ethereum blockchain.

Deploying Your Code

To deploy your bytecode, you need to add it to a transaction in the Ethereum blockchain. While you can to this with libraries like Web3.js, in this tutorial we’ll make it easy by showing how you can use MyEtherWallet (“MEW”). MEW is using all the same tools, but provides an easy web client for executing the transaction.

First, create a new Ethereum wallet using MyEtherWallet.com and send some Ether to your address. If you need to buy Ether, you can buy some from exchanges like Coinbase. Whenever you upload a smart contract or interact with a smart contract on the Ethereum blockchain, you pay a transaction fee for including your operations in the blockchain. There are lots of people trying to use the Ethereum blockchain, so it can be a bit pricey. As software engineers work on scaling Ethereum, expect those fees to drop.

Once you’ve set up your wallet and sent some Ether to your address, it’s time to navigate to the Contracts and click “Deploy Contract.”

Add the bytecode and after unlocking your wallet, generate the transaction and confirm that you wish to send it. You’ll be able to see the transaction as it goes from Pending and then Successful on Etherscan by searching your address in the upper right. MEW will estimate the amount of gas or you can also manually change the amount.

Once the contract is mined Successfully, copy the contract address. For example, for the Elixir smart contract, here’s how to see the smart contract address by viewing the successful deployment transaction:

**An important note on browsers**

It’s not recommended to give a private Ethereum wallet key to any field in a browser page. Metamask is one alternative as a browser extension for signing transactions. In general, it is better to execute transactions with libraries like Web3.js since you don’t place your trust in the creators of a web site (MEW uses the warning “Not Recommended”). For the sake of this tutorial, we’re using MyEtherWallet since it’s easiest for getting started.

After uploading your smart contract, view your wallet without unlocking it by clicking “View w/ Address only” under the “View Wallet Info” tab:

Using the right menu panel, add the custom token contract address that you copied from Etherscan, name, symbol and decimals to the “Add custom token” option:

You’ll then see the tokens in your wallet. Finally, you can use the transaction tool to send your new tokens to any other address:

That’s it!

What Are Verified Smart Contracts?

On smart contract inspection sites and tools like Etherscan, verified smart contracts like the Elixir smart contract are contracts where the developers have provided the code to make the smart contracts more transparent. Any other developer can then compile the smart contract with the specific compiler used to prove the code is authentic.

If your smart contract is likely to be used or viewed by many people, it’s a great idea to verify it. This will let people know they don’t need to trust what you say the contract does. This is especially relevant for smart contracts where even the slightest bug or change can result in the movement or loss of millions of dollars of value, like in the case of the DAO.

Conclusion

You should now be comfortable creating your own Ethereum token and adding it to the Ethereum blockchain. You’ll be able to send tokens to anyone in the world using your favorite token wallet. We hope this tutorial has been helpful and feel free to let us know if you have any questions by letting us know on the ELIX Twitter or ELIX Facebook pages.

Tags