Create and deploy a Smart Contract (6/6)

This tutorial is part of a series of articles introduced here.

In part 5, we finalized the configuration of the private blockchain. We sent ethers between our nodes and verified that the database is properly synchronised.

In this part, we will describe how to create and deploy a Smart Contract on the private blockchain.

NB: It is important that what we mean by “private Ethereum blockchain” in this context has absolutely nothing to do with the “private blockchain” championed by Hyperledger, Eris/Monax, or the recently announced Enterprise Ethereum Alliance. These are different codebases with different client applications, so they correspond to different implementations of a different set of concepts. They are private in the sense that they limit who has access to their chain. They are also called permissioned blockchains, and to be perfectly transparent, we wish they were not even called blockchains at all. In this series of articles, what we call “private blockchain” is a private instance of the Ethereum implementation. Same code base, same client application, different network identifier and genesis block. In other words, what we will come to create in this series is a chain with the same rules as the main chain, the same consensus algorithm too, but a different root block. This will become clearer in part 3. For more information about the 3-layer model and differences between concepts, implementations and instances, you can also watch our Devoxx talk and read this article.


We are going to create a Smart Contract called SmartToken.

This contract will hold an associative array of addresses and integers. Each address will be associated with a number of tokens that expresses if the user identified by this address is able to use a specific service (tokens > 0) or not (tokens == 0).

As you can see, this example is simplistic. But it will be useful to understand the basic principles of Smart Contracts.

Step 1 – Install Truffle

Truffle is a development framework for Ethereum. There are many others, but we will use Truffle in this tutorial, to develop and deploy our smart contract.

To install Truffle, run the following command:

$ npm install -g truffle

Step 2 – Create the project

From your computer, create the folder that will host your project:

computer$ mkdir -p ~/ChainSkills/Projects/SmartToken

Then, use Truffle to initiate your project:

computer$ cd ~/ChainSkills/Projects/SmartToken
computer$ truffle init

You should obtain the following tree structure:

| |____ConvertLib.sol
| |____MetaCoin.sol
| |____Migrations.sol
| |____1_initial_migration.js
| |____2_deploy_contracts.js
| |____metacoin.js
| |____TestMetacoin.sol

This initial project comes with a sample project (MetaCoin).

Open the project with your favorite text editor (Atom, Sublime, etc.).

Step 3 – Create the contract

In the “contracts” directory, create a file named “SmartToken.sol” and paste the following code:

pragma solidity ^0.4.0;

contract SmartToken {
    mapping(address => uint) tokens;

    event OnValueChanged(address indexed _from, uint _value);

    function depositToken(address recipient, uint value) returns (bool success) {
        tokens[recipient] += value;

        OnValueChanged(recipient, tokens[recipient]);
        return true;

    function withdrawToken(address recipient, uint value) returns (bool success) {
        if ((tokens[recipient] - value) < 0) {
            tokens[recipient] = 0;
        } else {
            tokens[recipient] -= value;

        OnValueChanged(recipient, tokens[recipient]);
        return true;

    function getTokens(address recipient) constant returns (uint value) {
        return tokens[recipient];

This contract has three functions:

  • depositToken: add some tokens to a specific address
  • withdrawToken: withdraw some token from a specific address
  • getTokens: retrieve the number of tokens available for a specific address

Step 4 – Prepare for deployment

Before deploying the Smart Contract, you have to adjust several files in your project.

Step 4.1 – Adapt deployment file

Replace the content of “migrations/2_deploy_contracts.js” with the following content in order to deploy our “SmartToken” Smart Contract:

var SmartToken = artifacts.require("./SmartToken.sol");

module.exports = function(deployer) {

Step 4.2 – Adapt network settings

The file named “truffle.js” contains network settings used to identify your deployment platform.

We will deploy the Smart Contract onto one of our miners.

Let’s change the file to adjust the port number to fit our environment:

module.exports = {
  networks: {
    development: {
      host: "localhost",
      port: 8042,
      network_id: "*" // Match any network id

Step 5 – Deploy the contract

Before proceeding, start your miners to ensure that your Smart Contract will be properly mined and deployed on your private blockchain.

Step 5.1 – Start your miners in 2 different tabs

computer$ ~/ChainSkills/miner1/
computer$ ~/ChainSkills/miner2/

 Step 5.2 – Compile and deploy your contract

Run the command “truffle migrate” to compile and deploy your contract to your blockchain:

computer$ cd ~/ChainSkills/Projects/SmartToken

computer$ truffle compile
computer$ truffle migrate --reset
Running migration: 2_deploy_contracts.js
 Deploying SmartToken...
 SmartToken: 0xce96374a544729a7dedf133659709d6cb0e1aa0e
Saving successful migration to network...
Saving artifacts...
The “–reset” command line argument is used here to force truffle to deploy the contract, even if has already been deployed before, for repeatability’s sake.
And that’s it. Now our SmartToken contract is deployed onto our private Ethereum chain and it is ready to receive calls.

Step 6 – Interact with the contract

You can interact with your contract through the Geth console, the Mist browser or a client application.

In this tutorial, we will show how to use Mist to “watch” the contract and interact with it.

Step 6.1 – Identify the contract

Before using Mist, you have to retrieve two elements about your deployed contract:

  • its address
  • its ABI (Application Binary Interface)

This information can be retrieved through the Truffle console in this way:

computer$ truffle console

truffle(development)> SmartToken.address

truffle(development)> JSON.stringify(SmartToken.abi)

Step 6.2 – Watch the contract on Mist

You can download the latest version of Mist for your system there. Mist is a special kind of browser, a distributed application browser, with a built in Ethereum wallet system that can connect to any network.

Start Mist. As it detects that an IPC file in the default location is being used, it connects to your private network. Once the user interface appears, click the Ethereum Wallet tab on the left, and click the CONTRACTS button in the upper right corner:

Select “WATCH CONTRACT” and fill the form:

  • Give an arbitrary name for the contract, like “SmartToken”
  • Enter the contract address retrieved from the Truffle console (SmartToken.address)
  • Enter the ABI (without enclosing quotes) retrieved from the Truffle console (JSON.stringify(SmartToken.abi))

The contract is now visible on Mist:

And you can interact with it:

Step 6.3 – Test the contract

To test the contract, pick the default address created on the RPi (you will understand why in the next part).

In our tutorial, this address (eth.coinbase) is: “0xb312d49ee143e803e07814ce0213e6327ab19250”

To check your contract, get the number of tokens of this contract:

The function “getTokens” is a constant. You can use it without paying any fees.

Now, deposit some tokens to this address. Choose the account that will execute the function and pay the fees:

When the block is mined, you should see that the value has changed:

Proceed with the withdraw function to ensure that your contract works as expected.

You can also watch the events triggered by the contract:


You have created, compiled and deployed a Smart Contract. We have also used this Smart Contract through Mist.

And that concludes this 6-part tutorial. To sum it up:

  • We installed the Go Ethereum client on both our laptop and our Raspberry Pi
  • We created our very own private Ethereum instance
  • We installed 2 mining nodes on the laptop, and a user node on the Raspberry Pi
  • We made sure all nodes were synchronized
  • And finally we deployed a smart contract to our private instance.

Now, we didn’t want to leave you like that, so we have one more thing… So stay tuned for the epilogue of this series.

Shameless plug

While you are waiting for the next part of this series, we just wanted to let you know that we are currently preparing a full-blown online training about the development of distributed applications on Ethereum and we are looking for your feedback to figure out what you would like to see in this training. If you want to help us, you can take a few minutes to answer a survey here.

And if you just want us to keep you informed when the full online training program will be available, you can register to our mailing list on