2

I am asked to provide the value of msg.sender as part of an event emitted in a function.

It is something like:

contract MyContract {
    event MyEvent(address indexed sender, uint256 input, uint256 output);

    function func(uint256 input) external {
        verify(msg.sender);
        uint256 output = compute(input);
        emit MyEvent(msg.sender, input, output);
    }

    function verify(address sender) private view {
        // verify the sender...
        ...
    }

    function compute(uint256 input) private returns (uint256) {
        // compute the output...
    }
}

Is there any point in doing this, or will this value always be present in the block anyway?

For all it matters, this function is called only from the off-chain (i.e., not by any contract).

So msg.sender will always be equal to tx.origin when the event is emitted.

Thank you!

2

It comes down to how you are using the data and what is the most efficient method for you.

It is true that you can get the msg.sender from the block (or transaction) or an event. If you are creating a front end and are populating the page with data from the event, it would likely be best to include it in the event for clean and concise front end code. If you are already storing each block and it's data in your own block explorer, you can use that data as well and will come to the same result.

Update

Looking at it from a gas perspective, you will end up paying more for adding it to the events. The LOG cost (according to the yellow paper) is as follows:

k + unindexedBytes * a + indexedTopics * b

where:

k = 375 (initial `LOG` cost)
a = 8
b = 375 (each additional indexed `LOG`)
  • 1
    I was thinking more on the aspect of gas cost, as this extra (indexed) parameter costs a bit, so we including it if it's already there? Thank you. – goodvibration Nov 7 '18 at 23:13
  • I see. I will add to the answer. – shane Nov 7 '18 at 23:17

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.