0

Hey I am currently trying to listen for a NFT token transfer on an account. So if someone sends a NFT to a walletAdress this gets triggered and logs the event ? I have tried it like that any ideas ?

const Web3 = require("web3");
const abi = require("./abi.json").abi;
const contractAddress = "someContractAdress";
const web3 = new Web3(
  new Web3.providers.WebsocketProvider(
    "wss://matic-mainnet.dagger.matic.network"
  )
);

const TokenContract = new web3.eth.Contract(abi, contractAddress);
TokenContract.events.allEvents({ fromBlock: "latest" }, console.log);


const event = TokenContract.events.Transfer();
const options = {
  // filter: {
  //   _from:  process.env.WALLET_FROM,
  //   _to:    process.env.WALLET_TO,
  //   _value: process.env.AMOUNT
  // },
  fromBlock: "latest",
};

TokenContract.once("Transfer", {}, function (error, event) {
  console.log(event);
});

TokenContract.events.Transfer(options, async (error, event) => {
  if (error) {
    console.log(error);
    return;
  }

  console.log("Found incoming transaction from " + txHash);

  // Initiate transaction confirmation
  confirmEtherTransaction(event.transactionHash);

  return;
});

const confirmEtherTransaction = (txHash, confirmations = 10) => {
  setTimeout(async () => {
    // Get current number of confirmations and compare it with sought-for value
    const trxConfirmations = await getConfirmations(txHash);

    console.log(
      "Transaction with hash " +
        txHash +
        " has " +
        trxConfirmations +
        " confirmation(s)"
    );

    if (trxConfirmations >= confirmations) {
      // Handle confirmation event according to your business logic

      console.log(
        "Transaction with hash " + txHash + " has been successfully confirmed"
      );

      return;
    }
    // Recursive call
    return confirmEtherTransaction(txHash, confirmations);
  }, 30 * 1000);
};

```
1
  • Did you check if the NFT has generated the event? I see you are listening the matic network, did you check if the network provider support events? I'll suggest trying first in a ganache instance to discard issues with the provider.
    – Ismael
    Jun 12 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.