1

I have a doubt.

I am using truffle with meta mask to deploy Smart Contract, write transaction and read transaction on Blockchain.

Why do I pay fee for reading data on BlockChain? Should it be free? I have thought it is because I using emit event (so the method is'n view methos).

I am testing on Ropsten Blockchain.

  function getVincitore ()  public returns (string memory, string memory,string memory,string memory )
  {
    require(userCountVincitore>0, "non e` stato sorteggiato nessun vincitore");
    emit Winner ("Il Vincitore", vincitore.username, vincitore.nome, vincitore.cognome,vincitore.email );
    return (vincitore.username, vincitore.nome, vincitore.cognome, vincitore.email);

}

Which are the cases where the transaction are free?

Thanks a lot.

alfo888_ibg
  • 194
  • 1
  • 1
  • 10

2 Answers2

1

A transaction is free when it does not write data to the blockchain. In this case we are talking about a call and not a transaction (see here for more infos : What is the difference between a transaction and a call?).

Events can be seen as logs written in the blocks, they modify the storage. That's why you have to pay fees.

clement
  • 4,302
  • 2
  • 17
  • 35
1

A transaction necessarily changes the state of the blockchain, therefore requires mining, therefore has a gas-cost attached to it, therefore is not free.

Sending a transaction, regardless of what function is called in it, costs gas.

When the function is constant (either pure or view), you do not need to execute it in a transaction, but you can simply call it.

This (also known as RPC) does not cost anything, apart perhaps paying your web3 provider (i.e., the node service), but that payment is outside the context of the blockchain.

In your case, since your function emits an event and events are always written into the blockchain, the function changes the state of the blockchain, therefore cannot be declared constant (either pure or view), therefore can be executed only in a transaction, therefore costs gas.

goodvibration
  • 26,003
  • 5
  • 46
  • 86