Listening to the Transfer event ERC721 token

I have a contract that inherits ERC721Mintable, Ownable. The mint function in my contract calls the inherited _mint(msg.sender, _tokenId). My understanding is that to get the token Id of the created token I have to listen to the Transfer event emited when the token is minted.
Q1. Is this the correct way? Is there any other way?

I tried listening to the transfer event like this:

let instance = await MyToken.deployed();
const event = instance.allEvents();

  event
.on("data", data => {
  console.log(data);
})
.on("error", console.error);
await instance.mint(args);

However no events are being fired and nothing is being logged in the console
Can you help me and point me in the right direction?

1 Like

Got the solution. It was kind of a regression from my side. I was using a http provider. It does not support events

3 Likes

Hi @sanjay well done on solving your own issue. I have been caught by that previously.

For your testing, you can use openzeppelin-test-helpers expectEvent

For an example test, see ERC721.test.js:

1 Like

Moved to from #openzeppelin to #support:openzeppelin

1 Like

Thank you so much. I was writing my tests so far myself. I did not know there were test helpers. I will start using them.

2 Likes