Project Eth 2: Making an Ethereum Coin

What You Need

An Ethereum host that is mining, as you prepared in a previous project.

Purpose

To learn more about Solidity and Contracts.

Starting Mining

If the geth console is not open, execute this command to start it:
geth --mine --datadir eth-data --networkid 123 --nodiscover --maxpeers 0 console 2>>geth.log
To verify that it's mining, execute this command to see the current block. Then wait 30 seconds and execute it again.
eth.blockNumber
The block number should increase, as shown below.

Unlocking your Account

To spend Ether, you must unlock your account. In the geth console, execute these commands:
primary = eth.accounts[0]
personal.unlockAccount(primary)
Enter your password. After a pause of several seconds, the response is "true", as shown below.

Understanding the Coin Contract

Here's the code for a coin that could be used for various purposes. Don't enter this code into the terminal--it's just presented here so you can read through it more easily.
contract token { 
    mapping (address => uint) public coinBalanceOf;
    event CoinTransfer(address sender, address receiver, uint amount);

  /* Initializes contract with initial supply tokens to the creator of the contract */
  function token(uint supply) {
        coinBalanceOf[msg.sender] = supply;
    }

  /* Very simple trade function */
    function sendCoin(address receiver, uint amount) returns(bool sufficient) {
        if (coinBalanceOf[msg.sender] < amount) return false;
        coinBalanceOf[msg.sender] -= amount;
        coinBalanceOf[receiver] += amount;
        CoinTransfer(msg.sender, receiver, amount);
        return true;
    }
}

Compiling your Contract

In the geth console, copy and paste in this command. It includes the whole source code of your contract, without comments, on one line.
var tokenSource = ' contract token { mapping (address => uint) public coinBalanceOf; event CoinTransfer(address sender, address receiver, uint amount); /* Initializes contract with initial supply tokens to the creator of the contract */ function token(uint supply) { coinBalanceOf[msg.sender] = supply; } /* Very simple trade function */ function sendCoin(address receiver, uint amount) returns(bool sufficient) { if (coinBalanceOf[msg.sender] < amount) return false; coinBalanceOf[msg.sender] -= amount; coinBalanceOf[receiver] += amount; CoinTransfer(msg.sender, receiver, amount); return true; } }'
Only the end of the code is visible, and the reply is "undefined", as shown below.

In the geth console, execute this command.

var tokenCompiled = eth.compile.solidity(tokenSource)
The reply is "undefined" again, as shown below.

Preparing your Contract for Deployment

In the geth console, paste in these commands.
var supply = 10000;
var tokenContract = web3.eth.contract(tokenCompiled.token.info.abiDefinition);
var token = tokenContract.new(
  supply,
  {
    from:web3.eth.accounts[0], 
    data:tokenCompiled.token.code, 
    gas: 1000000
  }, function(e, contract){
    if(!e) {

      if(!contract.address) {
        console.log("Contract transaction send: TransactionHash: " + contract.transactionHash + " waiting to be mined...");

      } else {
        console.log("Contract mined! Address: " + contract.address);
        console.log(contract);
      }

    }
})
A "Contract transaction send" message appears, and after a few seconds, you see "Contract mined!", as shown below. Make a note of your contract's "Address" here--you will need it later.

Testing the Contract

In the geth console, execute this command.
token.coinBalanceOf(eth.accounts[0]) + " tokens"
You have 10000 tokens, as shown below.

Setting Up a Watcher

The watcher will print a message on the console every time anyone sends a coin.

In the geth console, execute this command.

var event = token.CoinTransfer({}, '', function(error, result){
  if (!error)
    console.log("Coin transfer: " + result.args.amount + " tokens were sent. Balances now are as following: \n Sender:\t" + result.args.sender + " \t" + token.coinBalanceOf.call(result.args.sender) + " tokens \n Receiver:\t" + result.args.receiver + " \t" + token.coinBalanceOf.call(result.args.receiver) + " tokens" )
});
The reply is "undefined", as shown below.

Creating a New Account

In the geth console, execute these commands to create a new account.
personal.newAccount()
Enter a password twice. Your new account's address appears, as shown below.

Viewing All Accounts

In the geth console, execute this command to see all account numbers.
personal.listAccounts
You now have two accounts, as shown below.

Sending Coins

We'll send 1000 coins from the original account (eth.accounts[0]) to the new account (eth.accounts[1]).

In the geth console, execute this command.

token.coinBalanceOf(eth.accounts[0]) + " tokens"
The transaction succeeds and returns a transaction ID. Then the Watcher prints out the new balances in both accounts, as shown below.

Sources

Contract Tutorial
Ethereum Frontier Guide


Posted 7-11-16 by Sam Bowne