forked from rsksmart/rskj
-
Notifications
You must be signed in to change notification settings - Fork 0
Module 3 – Adding simple functionality (20 minutes)
herrerameri edited this page Dec 1, 2017
·
4 revisions
Let's add to our smart contract 2 functions or methods:
- getGreeting: it's going to return a greeting, by default 'Hello smart world!'
- setGreeting: for changing the current greeting
Note: Upon creation, each transaction is charged with a certain amount of gas, whose purpose is to limit the amount of work that is needed to execute the transaction and to pay for this execution. In Solidity, we have to write code taking it into account.
We need a field to save the greeting. The getter is going to return it. The setter is going to change it. So, the resultant code is like this:
pragma solidity ^0.4.4;
contract HelloWorld {
string greeting;
function HelloWorld() {
greeting = "Hello smart world!";
}
function getGreeting () constant returns (string) {
return greeting;
}
function setGreeting(string _newGreeting) returns (bool success) {
greeting = _newGreeting;
return true;
}
}
It's a good idea to write the code in Remix (or any online compiler) and then copy+paste it into the Truffle project.
Now, using the following commands the contract will be re-deployed:
truffle compile
truffle migrate --reset
In the next Module, we are going to interact with the contract.