0x API – Confused About All Fees/Costs in the Different Response Params of 0x Swap Quote API?

0xweb3js

I'm wanting to use the 0x swap quote API but I'm struggling to understand some of the response params.

Let's say for example I'm doing an arbitration and I'm starting with 10 WETH, then I sell the WETH for DAI, and then use the 0x swap quote API to find DAI -> WETH swaps that net me more than the 10 original WETH.

How should I be calculacting the actual profit so it considers tx fees, protocol fees, gas, slippage, etc?

The 0x response has a bunch of fees and minimums, estimates, prices, a "value" field that I think maybe encapsulates multiple fees in one.. I'm struggling to understand how I should compute my profit.

It's going to be something like:

let netProfit = quote.buyAmount – originalWETHAmount – (quote.gas * quote.gasPrice) – quote.value – ? – ??

Can anyone help me make sense of this?

Best Answer

That seems right for the best case. For the worst case something like this makes sense: quote.buyAmount * (1-slippagePercentage) - (quote.gas * quote.gasPrice) - quote.value - originalWethAmount, where slippagePercentage is the request param you pass in (defaults to 0.01, 1%)

But this only works when W/ETH is the buyToken. For a more general solution you should convert everything into a common denomination (like USD). Also, if you're selling ETH (not WETH) quote.value = quote.sellAmount + quote.protocolFee, so be careful not to double count your sell amount in calculations.

Related Topic