Skip to main content

"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 bytes(maxlength);
      uint i = 0;
      while (v != 0) {
        uint remainder = v % 10;
          v = v / 10;
          reversed[i++] = byte(48 + remainder);
      }
      bytes memory s = new bytes(i);
      for (uint j = 0; j < i; j++) {
          s[j] = reversed[i - 1 - j];
      }
      str = string(s);
  }

  function strConcat(string _a, string _b) internal pure returns (string) {
        bytes memory _ba = bytes(_a);
        bytes memory _bb = bytes(_b);
 
        string memory ab = new string(_ba.length + _bb.length);
        bytes memory bab = bytes(ab);
        uint k = 0;
        for (uint i = 0; i < _ba.length; i++) {
          bab[k++] = _ba[i];
        }
        for (i = 0; i < _bb.length; i++) {
          bab[k++] = _bb[i];
        }
        return string(bab);
    }
}


Step 2. Update 2_deploy_contracts.js (in migrations directory) to include new contract. 

Here is how the file should look after the update:
var Hello1 = artifacts.require("./Hello.sol");
var Hello2 = artifacts.require("./HelloWithCounter.sol");


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



Step 3. Start the console

$ truffle console↵


Step 4. Deploy new contract 

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

Running migration: 1_initial_migration.js
  Replacing Migrations...
  ... 0x2dd16d6427594963fce6c0b876bb97eccb686665d28e8a0fb474e8aaef63fd8c
  Migrations: 0xac37a9866815625c4a4c6aa0fc60116e495b1df5
Saving successful migration to network...
  ... 0x5cb753bc95a68ae74a6153b5253686e2286cbe28fa823656f0efcdfa86b010fb
Saving artifacts...
Running migration: 2_deploy_contracts.js
  Replacing Hello...
  ... 0x32884e4eafe5754c0144daa90a74812b53baea3e132f8a160ee99672e91503cc
  Hello: 0x320b839e89a460384ec01919614c7399f80c5213
  Replacing HelloWithCounter...
  ... 0xda8e7f43f62fcd94ffe478b1f135b31640ef8872c97278f10eab7c47b71405f8
  HelloWithCounter: 0xec2a49fb14e9e5b52d4236f9e27e3c45dfeb44b1
Saving successful migration to network...
  ... 0xda21ca9b4bae886293e7f7f3b06a3beaa6b33e67d9f97d88b5a9e5d35ba6ad86
Saving artifacts...

Step 5. Check the contract 

First let's see the counter before any calls to the sayHello()
truffle(development)> var hc = HelloWithCounter.at(HelloWithCounter.address)↵
undefined
truffle(development)> hc.getHelloCounter()↵
''

No surprises here. Counter is empty.


Now let's  say "Hello" for couple times:
truffle(development)> hc.sayHello()
{ tx: '0xd2579761083d00e3d2130cc59bba08ea1e6befa3323f1836c60e99f95da03cfe',
  receipt:
   { transactionHash: '0xd2579761083d00e3d2130cc59bba08ea1e6befa3323f1836c60e99f95da03cfe',
     transactionIndex: 0,
     blockHash: '0x8bb3e28112468c8ed46ea875e157112cc1c79a258062ffeb6756da2706f9c42a',
     blockNumber: 70,
     gasUsed: 47195,
     cumulativeGasUsed: 47195,
     contractAddress: null,
     logs: [],
     status: 1 },
  logs: [] }
truffle(development)> hc.sayHello()
{ tx: '0x57f25d5faf5efaf14d6c77698edb3ed7f6ee2a2cb7f7fa29d87970571ba9e19f',
  receipt:
   { transactionHash: '0x57f25d5faf5efaf14d6c77698edb3ed7f6ee2a2cb7f7fa29d87970571ba9e19f',
     transactionIndex: 0,
     blockHash: '0x16e9d46ca85c902663812cb60e7a0885c0647b2e9196e32b9e698707c24b1f3c',
     blockNumber: 71,
     gasUsed: 32195,
     cumulativeGasUsed: 32195,
     contractAddress: null,
     logs: [],
     status: 1 },
  logs: [] }

Note that some gas is needed to call this function. That's the price for writing data to the Etherium blockhain and (a little bit) for the time of running this function within virtual machines on the network,

Let's check the counter again:
truffle(development)> hc.getHelloCounter()↵
'2'


Comments

Popular posts from this blog

"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 netwo…

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 = "RespectCoin&quo…

Developing Google Glass application showing the Heart Beat Rate

Wearable Sensors + Google GlassWearable sensors  are a lot of fun! I played with them for some time by now, both professionally (at work), and for the coding pleasure. This post is about my latest weekend project - native google glasses app showing heart rate. The idea is to continuously measure hear rate using the sensor and to show pulse rate in real time, for example, while running or exercising. Honestly, I did not find running with Google Glasses to be much fun (as they tend to fell off when I am running), but to see what makes my heart beat faster is an interesting and educational experience.

Google Glass (GG) Turns out to be a nice piece of hardware that is a fun to work with: reasonably fast processor, Android 4.0.1, wide range of sensors, WiFi, and Bluetooth interface!

IngredientsGG (http://www.google.com/glass/start/)Google glass SDK (https://developers.google.com/glass/gdk)Android SDK (http://developer.android.com/sdk/index.html)Zephyr Sensor (http://www.zephyranywhere.com…