Ethereum: Deploying smart contracts with the Ethereum wallet

This article continues on from the previous one where we installed the Ethereum wallet and connected to a private blockchain running locally on geth. In this article we’ll deploy a simple contract to the same blockchain using the Ethereum wallet.

Previously…

Check out these previous articles which describe setting up the dev environment and other tools I’ll be using:

Get ready with geth

First make sure geth is running locally. I won’t describe this step in too much detail so refer to earlier articles if needed.

  1. Open a new terminal window and cd into the directory that contains your private chain folder.

    For me the directory is named /geth/privchain so cd into directory /geth

  2. Now we’ll start geth

    geth --port 3020 --networkid 58342 --nodiscover --datadir="privchain" --maxpeers=0 --ipcpath $HOME/.ethereum/geth.ipc
    

    After starting the geth node you should see something like IPC endpoint opened: /home/someone/.ethereum/geth.ipc

    Leave geth running in this terminal window.

Open the Ethereum wallet

  1. Open another terminal window and cd to the directory containing the Ethereum wallet (for example linux-unpacked directory) then run the Ethereum wallet by typing the below:

    ./ethereumwallet
    
  2. The Ethereum Wallet splash screen should display and say Looking for peers (it shouldn’t find any since we’re running a private blockchain with a single geth node).

    ethereum wallet splash

  3. On the wallet splash screen click LAUNCH APPLICATION

Deploy a simple contract using the wallet

  1. In the Ethereum Wallet click the CONTRACTS tab at the top, then click DEPLOY NEW CONTRACT.

    ethereum wallet splash

  2. We have the option to send some Ether with the contract (remember it’s not real Ether, just ones previously mined on this private chain).

    DO NOT send any Ether with your contract for now.

    ethereum wallet splash

  3. Scroll down to the SOLIDITY CONTRACT SOURCE CODE editor pane.

    We’ll deploy a simple token contract based on the code published at https://www.ethereum.org/token

    Paste the following:

    pragma solidity ^0.4.11;
    contract MyToken {
        string public name;
        string public symbol;
        uint8 public decimals;
        /* This creates an array with all balances */
        mapping(address => uint256) public balanceOf;
    
        function MyToken() {
            name = "Ooze Token";
            symbol = "OOZ";
            decimals = 2;
            balanceOf[msg.sender] = 21000000;
        }
    
        /* Send coins */
        function transfer(address _to, uint256 _value) {
            /* Add and subtract new balances */
            balanceOf[msg.sender] -= _value;
            balanceOf[_to] += _value;
        }
    }
    
  4. If there are any errors in your contract code the editor will highlight them for you like the screenshot below.

    ethereum wallet splash

  5. When all looks good you’ll be able to select your contract…

    ethereum wallet splash

    …and click DEPLOY.

    ethereum wallet splash

  6. The Create contract prompt will display. Make sure that the value of Provide maximum fee is higher than Estimated fee consumption.

    Hint: click the blue text in the Provide maximum fee value to edit the amount of gas to send.

  7. Enter your acount password and click SEND TRANSACTION.

    ethereum wallet splash

  8. The wallet should now display the transaction as pending under Latest Transactions:

    ethereum wallet splash

Mine the contract with geth

Now we need to mine the contract into a new block. We’ll use the geth javascript console to interact with the running geth node. Without this step the transaction will stay pending at 0 confirmations.

  1. If you don’t have the geth javascript console open, launch a new terminal and type:

    geth attach ipc://home/someone/.ethereum/geth.ipc
    
  2. Now instruct geth to start mining:

    miner.start()
    
  3. In the other terminal window (where geth is running) you should start to see some activity.

    I’ll admit this part still confuses me because geth generated a new DAG. But it has been a while since I mined this particular blockchain, so even though I’ve only got 22 blocks the most recent one is quite old. I let the DAG generation complete and eventually the transaction was mined into a block.

    INFO [08-19|00:52:50] Starting mining operation 
    INFO [08-19|00:52:50] Commit new mining work                   number=23 txs=1 uncles=0 elapsed=1.145ms
    INFO [08-19|00:52:53] Generating ethash verification cache     epoch=1 percentage=70 elapsed=3.034s
    INFO [08-19|00:52:54] Generated ethash verification cache      epoch=1 elapsed=4.288s
    INFO [08-19|00:53:22] Generating DAG in progress               epoch=1 percentage=0  elapsed=27.533s
    INFO [08-19|00:53:47] Generating DAG in progress               epoch=1 percentage=1  elapsed=52.478s
    INFO [08-19|00:54:13] Generating DAG in progress               epoch=1 percentage=2  elapsed=1m19.031s
    
  4. Once the transaction has been mined, stop geth from mining:

    miner.stop()
    
  5. In the Etherum Wallet the transaction will show as confirmed:

    ethereum wallet splash

    Also notice that the block number and Ether total have increased (mining rewards came to an address in this wallet):

    ethereum wallet splash

  6. If you now click the CONTRACTS tab again, the new contract should already be on the watch list under CUSTOM CONTRACTS.

    ethereum wallet splash

  7. Click on the contract to view details. Here we can also copy the contract address, and interact with the contract by calling it’s functions.

    ethereum wallet splash

Add contract to Wallet custom tokens

Since our contract is a token we can also add it under CUSTOM TOKENS.

  1. Go back to the CONTRACTS tab, then under CUSTOM TOKENS select WATCH TOKEN.

  2. Paste in the contract address

    The rest of the fields (Token name, Symbol, Decimals) should auto-populate with the values we set in the solidity code of the contract deployed at this address.

    ethereum wallet splash

  3. Click OK.

  4. Our token will now appear under CUSTOM TOKENS and display the balance we set in the solidity code (as the contract creator, we were assigned 210,000 OOZ).

    ethereum wallet splash

Troubleshooting

  • You may get this error while deploying the contract:

    It seems this transaction will fail. If you submit it, it may consume all the gas you provide.

    Make sure you are not sending any Ether while deploying the example contract code. The constructor of this contract does not accept Ether and the deployment will fail. Refer this StackExchange answer for more detail.

    ethereum wallet splash

  • After deploying the contract, you may see an error in your wallet transaction history:

    No data is deployed on the contract address!

    In this case you may have made the mistake above and sent Ether with the contract. Try deploy the same contract again with no Ether.

  • Another possibility is not enough gas was sent. In the transaction confirmation window click the blue gas number and type to edit it. Then click SEND TRANSACTION. This StackExchange answer helped me.

    ethereum wallet splash