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.

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.

2
  • thank you so much for making it clear to me Sep 15 '20 at 13:59
  • @alfo888_ibg: You're welcome. Sep 15 '20 at 14:56
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.

2
  • which is the way to make a call with truffle? Sep 15 '20 at 14:00
  • 1
    I think you should open a new question to clarify what exactly you want.
    – clement
    Sep 16 '20 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.