Ethereum: Intro to Smart Contracts with Solidity and Truffle

‘Hello World’ Smart Contract with Truffle & testrpc

Previously we setup an ethereum dev environment with truffle and ethereumjs-testrpc. This article will step through using truffle to create a basic hello world smart contract in Solidity, and deploying it locally with ethereumjs-testrpc.

  • First, create a new directory to work from. In Ubuntu type:

    mkdir helloworld
    cd helloworld
    
  • Then run truffle init to create a new app

    truffle init
    

    This creates some new folders and files:

    • contracts
    • migrations
    • test
    • truffle.js
  • Open the project in Atom using remote-sync. I’ve outlined this process in a previous article. You can also edit these files directly in Ubuntu if you prefer.

  • Create a new file in the /contracts folder called HelloWorld.sol

    Atom new file

  • Enter the following Solidity code.

    pragma solidity ^0.4.0;
    contract HelloWorld {
        address public creator; // data-type 'address'
        uint public myNumber; // data-type 'unit'
        string public message; // data-type 'string'
    
        // constructor - runs once on contract creation transaction
        function HelloWorld() {
            creator = msg.sender; // set 'creator' variable to address of transaction sender
            message = 'Hello, world'; // set our message
            myNumber = 5; // also set a number
        }
    }
    

    There are many great Solidity references already out there, so I won’t attempt to explain this code and confuse the matter.

  • Now we need to edit migrations/2_deploy_contracts.js

    //var ConvertLib = artifacts.require("./ConvertLib.sol");
    //var MetaCoin = artifacts.require("./MetaCoin.sol");
    var HelloWorld = artifacts.require("./HelloWorld.sol");
    
    module.exports = function(deployer) {
      //deployer.deploy(ConvertLib);
      //deployer.link(ConvertLib, MetaCoin);
      //deployer.deploy(MetaCoin);
      deployer.deploy(HelloWorld);
    };
    
  • In Ubuntu, open a new terminal window and start testrpc

    testrpc
    
  • In the first terminal window, make sure the truffle project is still your working directory and type

    truffle compile
    

    If there are no compile errors, you should get output like Writing artifacts to ./build/contracts.

  • Now run truffle migrate to deploy the contract to testrpc:

    truffle migrate
    
  • Start truffle console to interact with the contract on the blockchain

    truffle console
    
  • View the accounts created by testrpc

    > web3.eth.accounts
    

    Individual accounts can be stored in variables:

    > var ac1 = web3.eth.accounts[0]
    > var ac2 = web3.eth.accounts[1]
    
  • Get our contract ABI (Application Binary Interface) and store it in a variable

    > var app
    > HelloWorld.deployed().then(function(instance) { app = instance; })
    
  • Now we can use app to interact with the contract (note that contract storage must be declared public for this to work, i.e. address public creator).

    > app.creator.call()
    

    This should match the value returned by web3.eth.accounts[0].

  • Display the message:

    > app.message.call()
    

    Output:

    'Hello, world'
    

Wrapping up

To call functions we can use

> app.functionName(arg1, argn)

Or, if the function is a getter we can use

> app.functionName.call()

To read back a value we can also use the promise:

> app.functionName.call().then(function(result){ console.log(result);})

When calling functions, we can specify the account to send from. By default it will use web3.eth.accounts[0].

> app.functionName(argn, {from: ac2})

References