Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
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 namestring public name; string public symbol; uint8 public decimals;uint256 public totalSupply;// Balances for each accountmapping(address => uint256) balances;
address devAddress;
// Eventsevent 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 accountmapping(address => mapping (address => uint256)) allowed;
// This is the constructor and automatically runs when the smart contract is uploadedfunction 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 accountfunction 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 heresymbol = "SYMB"; // set the Symbol heredecimals = 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.
At ELIX, weâre building a web app for easily creating and supporting products and campaigns. Create your first project in just a few minutes here:
How To Create Your Own Cryptocurrency Token was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.