I'm trying to fill a quote that I get from the 0x API:

const qs = createQueryString({
   sellToken: 'TUSD',
   buyToken: 'WETH',
   sellAmount: BigInt(882693 * 10 ** 18)

..but the transaction reverts, even after successfully getting the quote (taker has the sell balance, allowance was granted, value of the fee is 0).

URL: https://api.0x.org/swap/v1/quote?sellToken=TUSD&buyToken=WETH&sellAmount=882693000000000051118080&slippagePercentage=0.8

If I modify sellAmount with:

  1. BigInt(882693 * 10 ** 12):the swap runs but with less traded amount (obviously since TUSD is 18 decimals).
  2. 882693 * 10 ** 18: doesn't even get the quote (throws an error on sellAmount).
  3. 882693: same problem as 1.

If I add includedSources: 'Uniswap_V2', for example, to the quote, the swap runs but with less traded amount while having just one order in the quote:

enter image description here

But when running the original quote, I get two orders instead so I think here is where it fails.

enter image description here

How do I fill this quote from the API that has two orders ? I looked through the API docs but couldn't find an answer.

Thanks for the help!

1 Answer 1



My contract is a hardhat fork with a pinned block to an old state from mainnet, and the 0x API makes offchain requests to the current state, so the liquidity that appears on the quote is not the same as that the contract aims to fill.

This could be and most likely is the reason why it's failing since when I doubled the gas on the call, and check -with gasleft()- the amount of gas taken on the execution, it was roughly 90k -after failure- when I had passed over 1M in gas to the call.

  • 1
    Did you ever figure out how to fix this problem? I can't figure out how to test in dev. Is there a mode in the API for it?
    – coffeemug
    Sep 7, 2021 at 4:25
  • There's no fix since technically speaking it's not a problem. 0x API makes offchain requests for onchain data, but the request itself is offchain in the end, so it cannot be pinned to an old mainnet fork. I found a deprecated version of 1Inch's aggregator protocol that works and you can use instead of the 0x API, but since it's deprecated (since Aug 2020 I think), there are a lot of exchanges that are not included in the querying mechanism. @coffeemug
    – dNyrM
    Sep 7, 2021 at 16:40
  • So there's basically no viable way to test a valid 0x quote using hardhat? Dec 30, 2021 at 1:44
  • Not with an old mainnet work, if you're looking for a deterministic result @AashayDesai
    – dNyrM
    Dec 30, 2021 at 7:19
  • 1
    Hardhat is always in sync with mainnet. What you can't do is make current offchain requests to old mainnet forks and expect deterministic results since one is offchain and the other onchain @AashayDesai
    – dNyrM
    Dec 30, 2021 at 19:26

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.