v3-subgraph icon indicating copy to clipboard operation
v3-subgraph copied to clipboard

Improving computation of amountUSD.

Open AngeloCa opened this issue 4 years ago • 1 comments

The currently computed amountUSD of a swap is sometimes wrong, Sometimes the resulting output is 0, and sometimes it is divided by 2 when it shouldn't.

USD prices are computed in src/utils/pricing.ts this way:

let price0USD = token0.derivedETH.times(bundle.ethPriceUSD)
let price1USD = token1.derivedETH.times(bundle.ethPriceUSD)

When both tokens are whitelisted both amounts are supposed to be non-zero. However, sometimes the computed one of both outputs price{0-1}USD is 0, either because token0.derivedETH is 0 or bundle.ethPriceUSD is zero. ( I didn't find the root cause of the issue ). In this case, when computing the amount USD price, when both tokens are whitelisted, you could avoid returning the wrong price in

  // both are whitelist tokens, return sum of both amounts
  if (WHITELIST_TOKENS.includes(token0.id) && WHITELIST_TOKENS.includes(token1.id)) {
    return tokenAmount0.times(price0USD).plus(tokenAmount1.times(price1USD))
  }

By first checking if one of both price{0-1}USD is 0. If this is the case you should return the non-zero prices multiplied by 2, instead of returning one of the two prices + 0. Otherwise, it will wrongly be divided by 2 when calling the function.

In the other cases when only one of both tokens is whitelisted sometimes a price of 0 is returned. I am not sure where this issue comes from ...

AngeloCa avatar Oct 18 '21 12:10 AngeloCa

Good point here - will take a look and try to sync with a test subgraph

ianlapham avatar Oct 19 '21 15:10 ianlapham