I'm new to solidity and wondering where the emit should stay inside a function. For example:

function getPlayers(address players, bool enabled) external {
        msg.sender == _admin,
    emit setPlayers(players, enabled);
    arr[players] = enabled;

IMO, the emit should stay at the end of the function because there is an operation has not been executed yet. Do you guys think it's really okay if it's like this?

2 Answers 2


It is good treat it as a state change and apply the checks, effects, interactions pattern.

This will prevent services that watch the event log from falling victim to re-entrance style attacks.

It also makes transaction event logs produced chronologically sensible. Consider three contracts in a chain.

You might get 1) Bang! 2) You're 3) Dead or something like ) Dead 2) You're 3) Bang depending or whether you put the emitter before or after the calls and transfers.

Hope it helps.


In terms of runtime:

It doesn't matter because either the function will be executed to completion and all of the state-changes (including the emitted event) will be written into the blockchain, or the function will revert before completion and none of the state-changes (including the emitted event) will be written into the blockchain.

In terms of readability:

It makes more sense to emit the event after applying the related state-change, i.e., in your example:

arr[players] = enabled;
emit setPlayers(players, enabled);

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.