4

According to this answer the return data of a smart contract function call can be accessed, however the same return data can be collated into an event to emit, so when should either method of returning data be used and what are the overall pros and cons of each including gas considerations?

// method 1:
struct Data {
  uint id;
  int val;
}

function returnData() external returns(Data memory);

// method 2:
event Data(uint indexed id, int val);

function emitData() external;

2 Answers 2

3
+50

Let me elaborate a bit on the valid answer from @pbsh.

As mentioned, they behave quite differently and are for different use case. Events can't be monitored by other smart contracts, only by external systems monitoring the blockchain (and the specific contract). Partially the opposite is true for return values.

What I mean by 'partially' is that an external system can't directly access the (possible) return values of a transaction. The typical solution is to emit events with required data and catch those when the transaction is processed. However, if a static call (not a transaction) is used, you have access to the return values.

As for gas considerations, return values are actually cheaper to use than events. In the following test contract executing testReturn costs 21401 gas and executing testEmit costs 22506 gas.

Example code:

pragma solidity ^0.8.0;

contract GasTest {

  event A(uint256);
  
  function testReturn() public returns (uint256) {
    return 5;
  }

  function testEmit() public  {
    emit A(5);
  }
}
5
  • Actually returning values is cheaper than emitting events, but they are used for different goals so I think they cannot be compared in any case. Commented Jan 25, 2022 at 22:44
  • Hmm, interesting. When writing my reply I was trying to google for some measuremenets, but could only find comparisons to using storage. Do you have any reference? Commented Jan 26, 2022 at 6:26
  • I created a test repo here: github.com/InternetOfPeers/test-gas-costs It was for another question, but I added also this case. Check test-events-costs.js and TestEvents.sol Commented Jan 26, 2022 at 13:19
  • 1
    To be clearer, if you compare events to storage than events are cheaper. I was referring to values that clients can read from the contract method, decoding the tx. Commented Jan 26, 2022 at 13:38
  • Thanks, I changed my answer and included an example. Learned something new :) Commented Jan 26, 2022 at 13:39
1

When you return data from a function, the return value can be accessed only if you know the transaction hash in which the function was called. This is because the return value is scoped to that particular transaction. However, you can set up a listener that can be always listening to certain events. And if you want to get all the events emitted by the contract, you can query them. But event logs are not visible to other contacts. If you want to perform some actions based on the return value of some other function, you won't be able to use events. Think of events like console.log() and return as return value of functions.

1
  • Thanks, this is a decent answer, however what about gas considerations?
    – MShakeG
    Commented Jan 23, 2022 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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