TransWikia.com

Web3 - OR condition between multiple filters in getPastEvents

Ethereum Asked by Deci on January 22, 2021

Take as an example the event PairCreated(address indexed token0, address indexed token1, address pair, uint); defined in UniswapV2Factory’s contract.

Let’s say that I want to fetch all the past events where one of the tokens used to create the pair was WETH.
I would write something like this:

await factory.getPastEvents("PairCreated", {
  filter: { token0: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2" },
  fromBlock: 0,
  toBlock: "latest",
});

The problem with the code above is that it fetches only the events where WETH was token0, ignoring the events in which WETH was token1.
A solution to this issue would be to do something like this:

const firstPart = await factory.getPastEvents("PairCreated", {
  filter: { token0: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2" },
  fromBlock: 0,
  toBlock: "latest",
});

const secondPart = await factory.getPastEvents("PairCreated", {
  filter: { token1: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2" },
  fromBlock: 0,
  toBlock: "latest",
});

const allEvents = firstPart.concat(secondPart);

The code above works fine, the problem with it is that it performs two calls.

Is there a way to do everything in only one call? I tried specifying filter as an array of objects (I.E.: [{ token0: "0x1234..." }, { token1: "0x1234..." }]) but Web3 just ignores it.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP