0

I am using the 0x Price and Quote API for my project and encountered the following Problem which could be a bug in the API.

Example request: https://api.0x.org/swap/v1/quote?sellToken=0x514cdb9cd8a2fb2bdcf7a3b8ddd098caf466e548&buyToken=0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce&sellAmount=91852168466729

Token Decimals:

  • 0x514cdb9cd8a2fb2bdcf7a3b8ddd098caf466e548 has 9 Decimals
  • 0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce has 18 Decimals

The response includes the following values among others:

  • "buyAmount": "17621716298148718"
  • "sellAmount": "91852168466729"
  • "sellTokenToEthRate": "681573019064660.96173915"
  • "buyTokenToEthRate": "131484453.43337870913186545"

sellAmount is corrently based on 9 Decimals --> sellAmount / sellTokenToEthRate = 0.13476497146 ETH which is correct.

However buyAmount is also based on 9 Decimals --> buyAmount / buyTokenToEthRate = 134141724.605 which is not correct and 9 decimals off the correct value.

Is this a know issue or am I doing something wrong here? Appreciate the help!

2
  • Early in your question you say one token has 9 decimals and the other 18, then later you say buyAmount and sellAmount are both 9 decimals. That wouldn't make sense. They will have the number of decimals that their their respective asset has
    – natewelch_
    Apr 5, 2022 at 20:01
  • This is why I asked the question. I guess that it's a bug in the API. Shouldn't the results of sellAmount / sellTokenToEthRate and buyAmount / buyTokenToEthRate be about the same? Apr 6, 2022 at 5:26

1 Answer 1

0

As temporary workaround I'm using a multiplier for calculating the eth value of the buyAmount:

const sellTokenToBuyTokenDecimalMultiplier: BigDecimal = 
  new BigDecimal(`1e${sellTokenDecimals}`,)
    .divide(new BigDecimal(`1e${buyTokenDecimals}`), 19);

const buyValueInEth: BigDecimal = 
      new BigDecimal(buyAmount.toString())
        .divide(buyTokenToEthRate, 18)
        .multiply(sellTokenToBuyTokenDecimalMultiplier);

(Hint: BigDecimal is https://www.npmjs.com/package/js-big-decimal)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.