Skip to main content

"Hello World" from Solidity

How to say "Hello World" in the era of blockchain?

Reflections

Once upon a time, I worked in CTO role of a great startup, and in order to find talented code developers, we ran a competition for the most creative "Hello World" application. It was a lot of fun! I was not able to participate in the competition back then, but here is my delayed entry.

Step 1. Environment Configuration

# Installing Truffle framework (http://truffleframework.com/)
$ sudo npm install -g truffle
# Creating project
$ mkdir solidity-experiments
$ cd solidity-experiments/
$ truffle init
# Installing and starting Etherium local test network
$ sudo npm install -g ethereumjs-testrpc
$ testrpc &

Step 2. Point truffle framework to the test network

Update truffle.js file created by the init to point to your test network. File content should look like this:
module.exports = {
 networks: {
  development: {
   host: "localhost",
   port: 8545,
   network_id: "*" // Match any network id
  }
 }
};



Step 3. Create Hello.sol Solidity contract in the "/contracts" directory 

pragma solidity ^0.4.4;

contract Hello {
  function Hello() public {
    // constructor
  }

  function sayHello() public pure returns (string) {
     //console.log("sayHello() function called...");
     return 'Hello World!';
  }
}


Step 4. Create 2_deploy_contracts.js file in /migrations directory

var Hello1 = artifacts.require("./Hello.sol");

module.exports = function(deployer) {
  deployer.deploy(Hello1);
};


Step 5.  Deploy the script from truffle console 

$ truffle console
truffle(development)>  truffle migrate --reset
Using network 'development'.

Running migration: 1_initial_migration.js
  Deploying Migrations...
  ... 0xb6bbeaaf3649ecb38d548cba96f681682dad9e0225726924fbee3ce36eff94e3
  Migrations: 0xc08c46796ba0edc0bebbbd0d90868c010055cb0e
Saving successful migration to network...
  ... 0x16fe364b9f2c3e8f07fa1ebd6b84b8ad9b4e750d8698a7e920d824ebd019dd80
Saving artifacts...
Running migration: 2_deploy_contracts.js
  Deploying Hello...
  ... 0xfe120836b2d7395bd988104feff018fe352f93555f71003bbf1a64671cca9ba1
  Hello: 0x2b649a87d20ce1ac3b6a0218e911165fa0f095f0
Saving successful migration to network...
  ... 0x1a09073a3b3f7996f3d63a81a99d8cd09198ad7b467f35f7ddc500a4291332b9
Saving artifacts...
truffle(development)>


Step 6.  Run "Hello World"

truffle(development)> var he = Hello.at(Hello.address)
undefined
truffle(development)> he.sayHello()
'Hello World!'

Comments

Popular posts from this blog

Respect Coin

Respect I think it's time to talk about currency. Let's create a Respect Coin. Step 1. Install OpenZeppelin library  npm install zeppelin-solidity When it comes to coins, I like to use some functions that smart people already implemented and other smart people verified. I think that Zeppelin is a nice collection of Solidity contracts that can be trusted. Let's use the StandardToken contract and use it as a parent class for our own RespectCoin contract. Step 2. Create RespectCoin contract and store it in "contracts/RespectCoin.sol" file  pragma solidity ^0.4.4; import "../node_modules/zeppelin-solidity/contracts/token/StandardToken.sol"; /** * @title RespectCoin * @dev ERC20 Token example, where all tokens are pre-assigned to th e creator. * Note they can later distribute these tokens as they wish using `transfer` and other * `StandardToken` functions. */ contract RespectCoin is StandardToken { string public constant name = &

"Hello World" with counter

Friend of mine pointed out that the "Hello World" contract described in my previous post does not keep state. Reading and writing data to the blockchain is important and can be challenging. In this post I will add a counter to count number of times the sayHello() function was called.    Step 1. Create  a new contract Let's create a file HelloWithCounter.sol and put it into the "contracts" directory. pragma solidity ^0.4.4; contract HelloWithCounter { uint private helloCounter; function Hello() public { // constructor helloCounter = 0; } function sayHello() public returns (string) { helloCounter = helloCounter+1; return strConcat('I say Hello #', uintToString(helloCounter)); } function getHelloCounter() constant public returns (string) { return uintToString(helloCounter); } function uintToString(uint v) internal pure returns (string str) { uint maxlength = 100; bytes memory reversed = new byt