0

I have this contract and I've deployed it to ganache

pragma solidity >= 0.5.0;

contract HelloWorld { string public payload;

function setPayload(string memory content) public {
    payload = content;
}

function sayHello() public pure returns (string memory) {
    return 'Hello World!';
}

}

I'm calling sayHello(), but I notice that no new block is created -- why is this?

nz_21
  • 279
  • 5
  • 10

1 Answers1

1

There are two kinds of functions. One, the functions called "Getter" and the other the functions called "Setter". Getter functions do not change the state and used to get some data from smart contract. For this reason, they are always called with this command in Truffle console:

 getterFunctionName.call()

Setter functions change the state, e.g. changing a state variable's value or generally changing the storage. Executing these function with the below command in Truffle console changes the state and ledger:

setterFunctionName.sendTransaction(arguments)

or

setterFunctionName(arguments)

A block is mined when the setter function be executed with one of these two commands. A transaction is recorded in the ledger or a block whenever it changes the state. Calling a function never change the state. So, It is natural to say no transaction sent and sequencely no block mined. Now, whether a function is a Setter or a Getter, if one executes that with .call() no block will be created because no change occurs on state by that.

Your function:

   function sayHello() public pure returns (string memory) {
        return 'Hello World!';
    }
}

never needs to change state because it is a Getter function. For this reason I explained one should not wait for a block being mined for calling this function.

Good Luck.

Alireza
  • 533
  • 5
  • 15
  • thanks for the detailed answer. Does this mean that if I call setPayload it should create a block? I am doing functions.setPayload("wonpfe").call() but it doesn't seem to create a block on ganache – nz_21 Oct 20 '20 at 14:13
  • @nz_21 you're welcome. To create a block just use functions.setPayload("wonpfe") or functions.setPayload.sendTransaction("wonpfe") if you defined your deployed contract as functions. If my answer solves your problem, please mark it to if another questioner has a similar question can find its solution easily. – Alireza Oct 20 '20 at 14:18
  • @nz_21 I suggest you change the question title to "Why is no block created by calling a function?". It clears your mean in title more and helps others to find their solution. – Alireza Oct 27 '20 at 07:51