0

I am using ethers.js. I am trying to create a filter and listen to the PairCreated event.

The indexed parameters are token0, token1.

I am trying to take WETH and a custom token and sort them so that I can pass it to my filter.

var token0, token1; // init Vars
const WETH = "0xd0A1E359811322d97991E03f863a0C30C2cF029C" // WETH Kovan Network
const tokenA = "0xe12A7BE0e1637a9ab3A137E0Aa114318aCb58BCd" // Token on my Kovan Fork

if (bigNumber(WETH) < bigNumber(tokenA)) {
   token0 = tokenA; token1 = tokenB;
   console.log('token0 is WETH');
} else {
   token0 = tokenB; token1 = tokenA;
   console.log('token0 is tokenA');
}
const filter = eth.factory.filters.PairCreated(
  token0, // address indexed token0
  null,  // address indexed token1 
  null, // address pair
  null // uint
); // Filter for Pair Created

function bigNumber(data) { return ethers.BigNumber.from(data); }

The above code at times gets the token0 correct, but how would I get it so that it sorts correctly?

1 Answer 1

3
+50

Change :

if (bigNumber(WETH) < bigNumber(tokenA)) {
  /* body */
}

To :

if (bigNumber(WETH).lt(tokenA)) {
      /* body */
}

Otherwise you are using the "fallback" JavaScript comparison operating on strings if i'm not wrong, but it's definitely not what you want anyway.

The ether.js documentation covers everything you need to know about their BigNumber type comparison.

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.