L1 The Code

The Code

For this project, you will be building your very own minimal custom Token for use on the Ethereum Blockchain. We will be using Solidity, Ethereum’s standard smart contract language.
In this lesson, we will walk you through some of the basic building blocks for a simple token contract. As most of Solidity is based around the Java programming language, if you have experience with that you should find it relatively straightforward.

Imports

First Step is to call in the Solidity Code Library, so the program knows what to do with the standard Solidity functions we’ll be using

Solidity API Library:

pragma solidity ^0.4.20;

Now you can use the built in smart contract Class

contract MyTokanium {

}

Reserved Words

• require
• public
• function
• constructor
• contract
• balanceOf
• sender
• msg

Arrays

Create an array to store all the account addresses so we know who has what coin(s)

mapping (address => uint256) public balanceOf;

Constructor

Now we can add the Constructor

function myTokanium (unit 256 initialTokenSupply) public {

}

Within the constructor function we’ll set the creator of the contract as the owner and give him all the MyTokanium coins

balanceOf[msg.sender] = initialSupply;

At this point your code will look something like this:

_________________________________________________________________________

pragma solidity ^0.4.20;

contract MyTokanium {

/* This creates an array with all balances */
mapping (address => uint256) public balanceOf;

/* Initializes contract with initial supply tokens to the creator of the contract */
function MyTokanium ( uint256 initialSupply ) public {
balanceOf[msg.sender] = initialSupply; // Give the creator all initial tokens
}

}

_________________________________________________________________________

Functions

So now we have our first smart contract, and it has its own list of accounts, and a token supply. However, the owner has all the coins but wait he can’t do anything with them. Ah we forgot to add a sending function. Let’s do that now.

So now we need a way for the coins to transfer out of that account.
Let’s create a transfer function.

function transfer(address _to, uint256 _value) public {
}

Our function needs to take in two values; the address we want to send coins to “_to” and the amount of coins we want to send “_value”

Within our function there will be 4 things we’ll be adding for Tokanium, first we want to set up a few checks to make sure the function will go through properly.

Our first check is to make sure the sender has enough coins in his account to cover the transaction:

require( balanceOf [ msg.sender ] >= _value );

Now we’re going to double check that after we send the coins the receiver’s account it won’t throw an overflow error and cause the receiver to lose all their coins, if somehow their account can only hold 99 coins and they already have 90 coins, and we send them 12, we want to make sure it doesn’t try to add an addition digit by rolling over the ten and force them to end up with 01 coins at the end of the transaction. In effect burning 101 coins.

So, we can prevent this by throwing an overflow error if when we send them money their account will have more money than it started with!

require( balanceOf [ _to ] + _value >= balanceOf [ _to ] );

Now we have a few ways to check if the transaction can go through or not, we can make it happen in real time! In practice, you should check some of the more recent EIPs to see what kind of protection is currently recommended, especially for financial related scripts.

First, we subtract the coins from the sender’s account

balanceOf [ msg.sender ] -= _value ;

Then we add the same amount of coins to the receivers

balanceOf [ _to ] += _value ;

That’ll conclude the Minimal Viable Token segment.

Your code should now look like this:

_________________________________________________________________________

pragma solidity ^0.4.20;

contract MyTokanium {
/* This creates an array with all balances */
mapping (address => uint256) public balanceOf;

/* Initializes contract with initial supply tokens to the creator of the contract */
function MyTokanium (
uint256 initialSupply
) public {
balanceOf [ msg.sender ] = initialSupply; // Give the creator all initial tokens
}

/* Send coins */
function transfer(address _to, uint256 _value) public {
require ( balanceOf [ msg.sender ] >= _value ); // Check if the sender has enough
require ( balanceOf [ _to ] + _value >= balanceOf [ _to ] ); // Check for overflows
balanceOf [ msg.sender ] -= _value; // Subtract from the sender
balanceOf [ _to ] += _value; // Add the same to the recipient
}
}
_________________________________________________________________________