What is an Ethereum Token?

This post explains what an Ethereum “token” or “coin” is (and is not). It will convey an intuitive understanding of tokens and describe the implementation of a very simple token in Solidity.

Cryptocurrencies

Tokens are similar to blockchain-based cryptocurrencies, so it will be helpful to review a few salient qualities of cryptocurrencies like Bitcoin and Ethereum:

Cryptocurrencies exist entirely on a blockchain—any relationship to objects (or currencies) in the physical world is based purely on agreements outside of the blockchain.

Cryptocurrency allocations are maintained as per-account balances on the blockchain—an account only “owns” an amount of cryptocurrency to the extent that the blockchain’s ledger reflects that balance for that account.

Cryptocurrency balances can change by virtue of verifiable transfers from one account to another on the blockchain.

The common thread above is that cryptocurrencies are blockchain entities. Cryptocurrencies have physical-world value because people are willing to transfer them on the blockchain in exchange for physical-world money, goods or services.

Cryptocurrency-like Tokens

Our previous
post on banking
demonstrated that smart contracts can trivially maintain per-account balances, and that it’s trivial to update those balances via transactions. Using very similar techniques, a contract could maintain per-account balances of a new cryptocurrency. If the contract both maintained per-account balances and supported account-to-account balance transfers, you would have all the necessary components of a cryptocurrency. Such contracts create “tokens” (or “coins”).

Contracts that create new tokens are very easy to develop and deploy, which is a big reason why so many “Initial Coin Offerings” are built upon the Ethereum network.

A Simple Token Implementation

minimal_token.sol

pragma solidity^0.4.19;contractMinimalToken{// Track how many tokens are owned by each address.mapping(address=>uint256)publicbalanceOf;functionMinimalToken(uint256totalSupply)public{// Initially assign all tokens to the contract's creator.balanceOf[msg.sender]=totalSupply;}functiontransfer(addressto,uint256amount)public{require(balanceOf[msg.sender]>=amount);balanceOf[msg.sender]-=amount;// deduct from sender's balancebalanceOf[to]+=amount;// add to recipient's balance}}

This contract has the necessary components of a cryptocurrency:

It maintains a per-account balance in the balanceOf state variable.

It supports account-to-account transfers via the transfer function.

Note that most tokens built on Ethereum implement a standard called ERC-20. Tokens implementing this standard API can be viewed and transferred using generic wallet software, and they can be bought and sold on generic currency exchanges. The minimal token described here implements only a subset of the ERC-20 standard. In a future post, we’ll implement the rest of the standard.