0

My NFT project architecture in such way that only when a minting event has occurred, a minted event is emitted and then I want to listen to that event so I trigger an upload of the JSON content that would be then visible to token owner (since anyone can potentially access the content of all the token by basically going to baseURI/tokenID and I want to keep the rarity of the tokens a secret until minted).

I want to create a function that continuously listens to new events from the minted event. I'm trying to access new events by using the following implementation, because I couldn't find an implementation that continuously listens to events from the contract.

async function listener() {
    events = nftContract.getPastEvents("minted",{fromBlock: 1}, function(err,res) {
        console.log(res);
    })

What is the best way to create a function that continues to poll events from the contract?

Soragim
  • 317
  • 7
  • 19
  • 1
    I refer you to my answer here: [Getting all newly minted ERC721 contracts with an ethereum node](https://stackoverflow.com/questions/68919064/getting-all-newly-minted-erc721-contracts-with-an-ethereum-node/68963641#68963641) – Ahmad Gorji Oct 12 '21 at 19:13

1 Answers1

0

The full code of the solution is so:

const ethers = require('ethers');
const CONTRACT_ADDRESS = "0x10820dB......";
const ABIJSON = JSON.parse('{"_format": "hh-sol-artifact-1", "contractName": "testcontract", "....}')
provider = new ethers.providers.AlchemyProvider("ropsten");
const contract = new ethers.Contract(CONTRACT_ADDRESS, ABIJSON.abi, provider);

contract.on("eventName", ( caller,tokenID) => {
      //this section is called every time an event is emitted 
})
Soragim
  • 317
  • 7
  • 19