ERC777 getting indirect events

Hi @abcoathup, thanks for sharing (Simple ERC777 token example), I have an issue with the Recipient Smart Contract.
In tokensReceived( ) function, if I emit events, they are not fired.

Am I missing something ?

1 Like

Hi @maverick193 welcome to the community :wave:

Great question. I suspect you might be checking for an event to be directly emitted, when the event is emitted from the token received contract after being called by the ERC777 token contract.

I updated the example to include an event being fired to show how this works.

    function tokensReceived(
        address operator,
        address from,
        address to,
        uint256 amount,
        bytes calldata userData,
        bytes calldata operatorData
    ) external {
        require(msg.sender == address(_token), "Simple777Recipient: Invalid token");

        // do stuff
        emit DoneStuff(operator, from, to, amount, userData, operatorData);

We can test for the event being emitted in the transaction using OpenZeppelin test helpers expectEvent.inTransaction which can test for events indirectly emitted. See the documentation for details.

  it('sends to a contract from an externally-owned account', async function () {
    const amount = new BN(1000);
    const receipt = await this.token.send(this.recipient.address, amount, data, { from: holder });

    await expectEvent.inTransaction(receipt.tx, Simple777Recipient, 'DoneStuff', { from: holder, to: this.recipient.address, amount: amount, userData: data, operatorData: null });

    const recipientBalance = await this.token.balanceOf(this.recipient.address);;
1 Like

Hi @abcoathup, awesome !! it works !!
Now I understand my mistake, in my test script, I was expecting to catch the event using expectEvent.inLogs (log checking)

With expectEvent.inTransaction, I am able to test the event (indirectly emitted).

Thanks again for your answer and your time !!

1 Like

Hi @maverick193 glad I could help. I only found out about how to test for indirect events recently.