/ethereum-hello-world-tutorial

A simple solidity smart contract

Primary LanguageJavaScript

Hello World Truffle Tutorial

  1. install dependencies
npm i -g ethereumjs-testrpc
npm i -g truffle
  1. init truffle project
truffle init
  1. write solidity Helloworld.sol contract
pragma solidity ^0.4.4;

contract Helloworld {
    string _greeting;

    function setGreeting(string greeting) {
        _greeting = greeting;
    }

    function sayHello() returns(string) {
        return _greeting;
    }
}
  1. write 2_deploy_contracts.js migration script
var Helloworld = artifacts.require("./Helloworld.sol");

module.exports = function(deployer) {
  deployer.deploy(Helloworld);
};
  1. open up new terminal and run testrpc
testrpc
  1. build solidity smart contract
truffle build
  1. migrate Helloworld smart contract
truffle migrate
  1. test smart contract on truffle console
truffle console
/* console opens */
var hw;
Helloworld.deployed().then(function(d){hw = d;});
hw.sayHello.call() // ''
hw.setGreeting("hi github!");
hw.sayHello.call() // 'hi github!'
  1. change sayHello on Helloworld contract
function sayHello() returns(string) {
    return "im changed!";
}
  1. compile and migrate updated contract.. then run console
truffle compile
truffle migrate
/* deployed contract */
truffle console
  1. run sayHello

Notice sayHello output did not change. That is because contracts are cannot be changed once deployed to the blockchain.

var hw;
Helloworld.deployed().then(function(d){hw = d;});
hw.sayHello.call() // 'hi github!'