Price API Problem in case of low liquidity

Their docs say xBTRFLY is deprecated, so that may be why. Canā€™t seem to swap for it on exchanges. If it should be 1 to 1 in reality (as in docs, it was given in return for staked BTRFLY), then you can just use BTRFLY.

Thanks, but this is not super relevant, because we are getting a historical price. xBTRFLY was not deprecated as of the block we are requesting.

The issue is that the Moralis API is returning a wildly incorrect price. If thereā€™s no liquidity, it should return the ā€œNot enough liquidityā€ error, in which case we have code that falls back to proxy tokens (such as BTRFLY in this case). But when Moralis returns a price, we believe itā€¦

I think that sometimes getTokenPrice can return prices like that when the liquidity is low

That is not an acceptable answer.
If we say ā€œsometimes itā€™s wrongā€ that is the same as saying ā€œdonā€™t trust this API.ā€

How do we detect when itā€™s going to return crazy prices?

you could try to use /{pair_address}/reserves endpoint to get the exact liquidity reserves values, not sure if it will work in your case

Can you please check if it will work in this case, since this is a tech support question and you are providing support for a paid product?

If the liquidity is not sufficient to return an accurate price, I think youā€™ve set the liquidity threshold too low in the API.

do you know what were to tokens in the liquidity pool at that time?

No - thatā€™s your job :slight_smile: . We just use the price API Moralis provides.

As the price is obviously incorrect, the alternative is to just use BTRFLY address which according to the protocol should have been matching xBTRFLY at the time - this returns $3209 so in the same range as the chart which is what youā€™re using as a comparison anyway.

If it is user inputted, then you can set a case for it by default where it uses the BTRFLY contract address instead.

We have that case coded. It uses the fallback token (in this case BTRFLY) if the primary token the user inputs (in this case xBTRYFLY) does not return a price from the Moralis price API. However, in this case, it does return a price, and itā€™s a very wrong price. So this code does not kick in.

The other way to code it would be to try the fallback token first, but that is no good because sometimes the fallback token does not trade in line with the primary. For example, if the user wants a quote for stETH and we fall back to ETH, that doesnā€™t work when stETH breaks its peg.

If the user asks for an apple, you always try to give them an apple before falling back to a pear.

What we need is a price API that functions as advertised in the docs: If there is not enough liquidity to return an accurate price, we should get an error instead of a crazy price.

Related question: Iā€™m trying the price API with another token (PolyCub), and getting a liquidity pool errorā€¦
https://deep-index.moralis.io/api/v2/erc20/0x7cc15fef543f205bf21018f038f591c6bada941c/price?chain=polygon

ā€¦even though it has $190k of liquidity on Sushiswap on Polygon right now:

So, sometimes thereā€™s plenty of liquidity and you COULD get a good price but return an error. Other times, it seems like thereā€™s not enough liquidity and instead of returning an error, Moralis returns a crazy price. This is pretty difficult to code around.

Can I make a suggestion? Since the price API already checks the liquidity (to decide whether to return an error or return a price), what donā€™t you just return the liquidity in the API response, along with the price? Then each of us developers can set their own threshold.

You can add this suggestion on roadmap.moralis.io

Thanks. Done.
Would still appreciate an answer to the above.

the problem with that I think is that we only track quickswap on polygon now

  1. I think you are responding to the POLYCUB issue. So whatā€™s the liquidity you are seeing in quickswap?
  2. Please address the xBTRFLY issue as well.

it could be no liquidity in quickswap if there is no trading pair there for that token, it is the same error when there is no trading pair

for xBTRFLY:

https://deep-index.moralis.io/api/v2/0xcc94faf235cc5d3bf4bed3a30db5984306c86abc/0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2/pairAddress?chain=eth&exchange=sushiswapv2

=>

{
  "token0": {
    "address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
    "name": "Wrapped Ether",
    "symbol": "WETH",
    "decimals": "18",
    "logo": "https://cdn.moralis.io/eth/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2.png",
    "logo_hash": "0a7fc292596820fe066ce8ce3fd6e2ad9d479c2993f905e410ef74f2062a83ec",
    "thumbnail": "https://cdn.moralis.io/eth/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2_thumb.png",
    "block_number": null,
    "validated": null,
    "created_at": "2022-01-20T10:39:55.818Z"
  },
  "token1": {
    "address": "0xcc94faf235cc5d3bf4bed3a30db5984306c86abc",
    "name": "xBTRFLY",
    "symbol": "xBTRFLY",
    "decimals": "9",
    "logo": null,
    "logo_hash": null,
    "thumbnail": null,
    "block_number": "13825631",
    "validated": 1,
    "created_at": "2022-01-20T10:39:55.818Z"
  },
  "pairAddress": "0xed9193c3c0a9d5f77cd6d1ca18f14ce9e19ccad9"
}
https://deep-index.moralis.io/api/v2/0xed9193c3c0a9d5f77cd6d1ca18f14ce9e19ccad9/reserves?chain=eth&to_block=13973890

=>

{
  "reserve0": "19729909330800231",
  "reserve1": "535678"
}
  1. For POLYCUB: You said it could be no liquidity. Is it actually no liquidity on Quickswap?
  2. For xBTRYFLY is this enough liquidity? How much is required? Whatā€™s the threshold that throws the error?

Gentle reminder on the two items above. Thanks.

1 Like

Iā€™m not able to find this address 0x7cc15fef543f205bf21018f038f591c6bada941c on quickswap interface: https://info.quickswap.exchange/

for xBTRYFLY, it looks like it has close to 0.2 WETH

The logic is the following one:

  • we calculate the price based on the average of all stablecoin pairs (with at least 250USD liquidity)
  • we look for WETH (or any other whitelisted tokens) to calculate the price. This has no liquidity threshold