0

I have the following event:

event WaitingPlayer2(address indexed player2);

Which I call through:

function partecipateAsPlayer2(address player2) public{
        emit WaitingPlayer2(player2);
}

Is it possible to wait in a smart contract function for that even to be fired? Like this:

function startGame(address requiredPlayer2){
    //get the WaitingPlayer2 event with requiredPlayer2 address
}
0

There is a short answer and an obscure answer.

Short answer

You can't. Events are not accessible internally, so use state variables for logic and emit events to inform external clients about changes.

Obscure answer

Access may be possible with assembly. I came across this pattern some time ago, and since events are much cheaper than state storage, it seemed intriguing. It hasn't really caught on, and I'm unsure about risks that might exist with future forks, etc. Certainly not a supported approach, if there is such a thing.

It seems to rely on a client first observing an event externally, and then by passing knowledge of that event into the verify function a contract can confirm such an event does indeed exist. That offers some interesting possibilities, if it works.

It doesn't allow browsing, counting or enumerating events.

NO WARRANTY.

https://github.com/figs999/Ethereum/blob/master/EventStorage.sol

Hope it helps.

| improve this answer | |
  • Thank you very much for the obscure answer, I was trying to mimize the use of the storage so this is quite the intriguing answer. – iLikeKFC Jan 20 at 15:20

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