Beginning Solidity: Creating a Cryptocurrency Token on the Ethereum Network

If it seems like there’s a new cryptocurrency being released every day, it’s because there is. Some of the big crypto-players have set up platforms to make the creation of your own ‘cryptocurrency’ simple. For example, Ethereum-based projects like Golem and Augur. These are run on the same blockchain as Ethereum, so they’re typically referred to as assets or tokens, instead of independent cryptocurrencies.

Ethereum Tokens

Solidity: The Ethereum Programming Language

Ethereum has the capabilities to run code on top of its blockchain network. The Ethereum team developed their own programming language called Solidity. In this tutorial I will walk you through creating a simple token in the Solidity language.

Step 0: The Setup

Ethereum provides an online IDE for editing your code, but it isn’t necessary to use. Any text editor will suffice, but the online IDE will highlight syntax and continuously compile. One can also download Mist to edit the information, which is Ethereum’s official program. Mist is also handy for executing contracts on the test-net, which aids in the ab

Step 1: Code Outline

Before we do anything, we need to tell our complier how to deal with the code it’s about to receive. This can be achieved by writing the following for a headline:

pragma solidity 0.4.11;

We then create the general outline for our cryptocurrency.

contract bxcCoin {
	function bxcCoin() {
	
	}
}

contract bxcCoin{} is a keyword, letting Solidity know that we are creating a contract with the name bxcCoin. function bxcCoin() is a function that is executed upon the initialization of the contract. The function with the same name as the contract is executed initially and only once.

Step 2: Adding Some Variables

Now we need to fill in our functions! Above our function lets create a variable that allows us to associate an address to a numerical value (i.e. users’ balances).

contract bxcCoin {
	
	mapping (address => uint256) public balances;
	
	function bxcCoin() {
	
	}
}

mapping (address => uint256) states that this variable will be an array from address types to unsigned integer types. public means that it can be view by anyone. balances is the name of the variable. So something like

 balances[123456789] = 100

would set address 123456789 to have a balance of 100.

Now lets add some variables we can manipulate upon initialization.

contract bxcCoin {
	
	mapping (address => uint256) public balances;
	
	function bxcCoin (uint256 initialSupply) {
		balances[msg.sender] = initialSupply;
	}
}

We create an unsigned integer called initialSupply, by putting it in the parentheses. This value is required to execute this function. We take this value and assign it to msg.sender‘s balance. msg.sender will always refer to the person creating the action.

Step 3: Transferring Coins

Let’s create another function called transfer to let the contract move tokens.

contract bxcCoin {
	
	mapping (address => uint256) public balances;
	
	function bxcCoin (uint256 initialSupply) {
		balances[msg.sender] = initialSupply;
	}
	
	function transfer(address _to, uint _value){
		if (balances[msg.sender] < _value) throw;
		if (balances[_to] + _value < balances[_to]) throw;
		balances[msg.sender] -= _value;
		balances[_to] += _value;
	}
}

As you can see transfer takes in two variables. _to and _value that determine who gets the tokens and how much, respectively. Remember, we can get the sender with the global msg.sender variable that is built into Ethereum.

The four statements inside of transfer do the following in order:

  1. Throws an error if the sender has insufficient funds.
  2. Throws an error if the sent amount is negative.
  3. Removes the sent amount from the sender.
  4. Credits the sent amount to the recipient.

Step 4: Run it!

If you happen to have Mist set up, you’re all ready to run! Simply click on “Deploy new contract,” input your code, and run it!

Leave a Reply

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