I have a smart contract with some members which should get modified on certain events. For simplicity, let's imagine we have a greeter contract.
If I call greeter.greet()
in the JavaScript console, it responds with something like Hello World
. But what if I want to count the number of greetings and store them inside the contract?
My first try was the following:
contract greeter
{
string greeting;
uint calls;
function greeter ( string _greeting ) public
{
greeting = _greeting;
calls = 0;
}
function greet ( ) constant returns ( string )
{
calls = calls + 1; // does not work
return greeting;
}
}
The calls variable was not updated. My first guess is that I need to somehow create a transaction to pay for the gas. I used web3.js
to deploy the contract. Should I use web3 also for calling greet()
with a transaction?
Or could I fund the contract account with some Ether to enable the contract to pay its own gas? How to manipulate data in a Solidity smart contract?