ethers-io / ethers.js

Complete Ethereum library and wallet implementation in JavaScript.
https://ethers.org/
MIT License
7.91k stars 1.84k forks source link

Multiple execution of event-listener's call back function #4828

Open parva-jain opened 3 weeks ago

parva-jain commented 3 weeks ago

Ethers Version

6.13.0

Search Terms

event-listener

Describe the Problem

I have written an event listener that listens to the smart contract event - ReceivedToken. However the callback function of the event listener is executed multiple times on the single event emission.

Code Snippet

export default async (contract: ethers.Contract, provider: ethers.JsonRpcProvider) => {
  const { chainId } = await provider.getNetwork()

  contract.on(
    'ReceivedToken',
    async (tokenAddress: string, sender: string, amount: number, purchase: boolean, timestamp: number, data: any) => {
      console.log('Received Funds In Endowment Pool')

      console.log({
        tokenAddress,
        sender,
        amount: Number(amount),
        purchase,
        timestamp,
        txHash: data.log.transactionHash,
      })
    },
  )
}

Contract ABI

Insignificant here

Errors

Im getting multiple logs which shouldn't happen

Received Funds In Endowment Pool
{
  tokenAddress: '0x0000000000000000000000000000000000000000',
  sender: '0x0035cd0CA79A5b156d5443b698655DBDc5403B45',
  amount: 33404937000000000,
  purchase: true,
  timestamp: 1725608730n,
  txHash: '0xa22f31ae4d4e7c69efdc201f0e1d4e05e86b36307f22c6aecbe832180bc16c1b'
}
Received Funds In Endowment Pool
{
  tokenAddress: '0x0000000000000000000000000000000000000000',
  sender: '0x0035cd0CA79A5b156d5443b698655DBDc5403B45',
  amount: 33404937000000000,
  purchase: true,
  timestamp: 1725608730n,
  txHash: '0xa22f31ae4d4e7c69efdc201f0e1d4e05e86b36307f22c6aecbe832180bc16c1b'
}
Received Funds In Endowment Pool
{
  tokenAddress: '0x0000000000000000000000000000000000000000',
  sender: '0x0035cd0CA79A5b156d5443b698655DBDc5403B45',
  amount: 33404937000000000,
  purchase: true,
  timestamp: 1725608730n,
  txHash: '0xa22f31ae4d4e7c69efdc201f0e1d4e05e86b36307f22c6aecbe832180bc16c1b'
}
Received Funds In Endowment Pool
{
  tokenAddress: '0x0000000000000000000000000000000000000000',
  sender: '0x0035cd0CA79A5b156d5443b698655DBDc5403B45',
  amount: 33404937000000000,
  purchase: true,
  timestamp: 1725608730n,
  txHash: '0xa22f31ae4d4e7c69efdc201f0e1d4e05e86b36307f22c6aecbe832180bc16c1b'
}
Received Funds In Endowment Pool
{
  tokenAddress: '0x0000000000000000000000000000000000000000',
  sender: '0x0035cd0CA79A5b156d5443b698655DBDc5403B45',
  amount: 33404937000000000,
  purchase: true,
  timestamp: 1725608730n,
  txHash: '0xa22f31ae4d4e7c69efdc201f0e1d4e05e86b36307f22c6aecbe832180bc16c1b'
}

Environment

Altcoin - Please specify (e.g. Polygon), node.js (v12 or newer)

Environment (Other)

Alt - polygon mainnet, polygon amoy, filecoin mainnet, filecoin calibration testnet