0

I'd like to monitor events happening on-chain on a specific Smart Contract. I'd like to know every time a Transfer event happens. I tried to write a code with hardhat, but no events if being logged. Here is my code:

  const abi = [
    "event Transfer(address indexed from, address indexed to, uint256 indexed id)"
  ]

  const MyContract = await new hre.ethers.Contract(
    "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
    abi,
    hre.ethers.provider
  )

  MyContract.on("Transfer", (from, to, id) => {
    console.log("Transfered:", from, to, id)
  })
  console.log("monitoring...")

I know that there are events occurring in the blockchain, but my code is not tracking them. What am I doing wrong?

1

2 Answers 2

0
filter = {
    address: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
    topics: [
        utils.id("Transfer(address,address,uint256)")
    ]
}
provider.on(filter, (log, event) => {
    // Emitted whenever a token transfer occurs
})

Try updating the function like this and you'll be able to monitor the transfer events provided the provider emit such events.

0

Alchemy have some great tools for monitoring, and these all come with a free account for limited testing purposes.

If you want to simply monitor transactions, for instance, their SDK allows a eth_getTransactions call, where you can, for example, specify a to address.

There are plenty of tutorials online depending on what you're looking to achieve.

If you come unstuck at some point, post your question and tag me - I'll do what I can to help :D

Your Answer

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

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