1

Looking at the open-zeppelin ERC721 Contract's code, I see that the very last thing that its mint function is doing is call emit on the contract's pre-defined Transfer event. My question is: how do I capture and respond to this event in my web3 Dapp?

Here's my code:

    // Set up Event:
    MyApp.NFTtokenTransferEvent = MyApp.NFTContractInstance.Transfer();

    // Now tell my app to LISTEN/WATCH for that Event:
    MyApp.NFTtokenTransferEvent.watch(function (error, result) {
      if (!error) {
        // If a Token was Minted or TRANSFERRED, let us know who, what, where:
        console.log("TRANSFER EVENT RESULTS: " + result);
      }
      else {
        console.log("Error with EVENT and its EMISSION!!! Error is: " + error);
      }
    });
  },

The result I'm getting for is - well, nothing.

When I mint new tokens - which I know is happening successfully cause I am verifying it - I don't get any feedback from the Transfer event. I don't get anything in the result argument - nor do I get any errors. I basically don't get anything.

Any suggestions?

(NOTE: my NFTContract obviously inherits from the ERC721Full Contract, so I'm assuming it/I can access the Transfer event without having to do anything special there, correct? )

1 Answer 1

0

I was also facing your issue and was about to post the question when I saw your question. Now I realized what I was doing wrong. I was using the default provider in truffle-config which will be http. You have to use a websocket provider to listen to events. See here https://web3js.readthedocs.io/en/1.0/web3.html#providers.

This fixed the issue for me:

in truffle-config.js

  networks: {
    development: {
      provider: function() {
        return new Web3.providers.WebsocketProvider("ws://localhost:7545");
      },
      network_id: "5777"
    }
  },

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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