Search code examples
ethereumsolidity

Failing to fill 0x quote


I'm trying to fill a quote -from within my contract- that I get from the 0x API v1:

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

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

When the quote has one order, the swaps runs, but when it has two or more orders (like in my quote), it reverts:

One order:

enter image description here

Two orders (reverts):

enter image description here

My fillquote function:

function fillQuote(
        address sellToken,
        address buyToken,
        address spender,
        address swapTarget,
        bytes calldata swapCallData
    ) public   
    {        
        require(MyIERC20(sellToken).approve(spender, type(uint).max));
        (bool success, bytes memory returnData) = swapTarget.call(swapCallData);
        if (!success) {
            console.log(Helpers._getRevertMsg(returnData));
        }
        require(success, 'SWAP_CALL_FAILED');
    }

Solution

  • Solution:

    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.