Skip to article content 0xprotocol

0xprotocol swaps through Swap API reflect gas costs and fees

Updated on

0xprotocol trading through Swap API reflects both token prices and estimated execution costs. Swap API samples liquidity from decentralized exchanges and market makers, accounting for gas consumption and applicable fees. A nominal price advantage can disappear when a route needs more expensive execution. Comparing quotes therefore requires consistent trade inputs and gas assumptions, together with the fee amounts returned for the selected trade.

Gas estimates change the route ranking

Swap API adjusts sampled liquidity prices for gas consumption and associated fees before selecting a combination of available orders. An attractive exchange rate describes the token exchange, while execution also consumes network resources. A route with a better nominal price can lose its advantage once its gas requirements enter the comparison.

Expected gas consumption and the gas price used for quoting determine the execution-gas estimate. The relationship is gas * gasPrice : gas measures the required execution units, while gas price supplies the cost per unit. Expressing that estimate in the network's native token keeps its denomination separate from the tokens exchanged. OP Mainnet's total network fees also include an L1 data fee and, after Isthmus, an operator fee. Comparing the full native-token cost with token proceeds requires a consistent valuation basis.

Swap API uses the request's gas price or a fast default, so its estimate reflects the quoting assumptions.

Changing that assumption can change the relative attraction of routes with different execution requirements. Token amounts and prices also affect whether an execution-cost difference outweighs a price advantage. Comparing quotes with different gas assumptions can obscure why the routing choice changed, even when both requests involve the same token pair.

Market makers choose whether to respond to quote requests. Aggregation therefore compares the liquidity available for that quote.


Does splitting a swap always improve its outcome?

Splitting a swap does not always improve its cost-adjusted outcome. Swap API can divide a trade across liquidity sources and evaluates combinations while accounting for gas and fees. Access to another pool can improve the token price, while the combined execution requirements affect the overall comparison. A route using more sources therefore needs no automatic preference over a simpler route. The selected composition reflects the quote's inputs and available liquidity.

Does splitting a swap always improve its outcome? (0xprotocol)
Image: Visual summary: Does splitting a swap always improve its outcome?

Open full-size image


API charges and protocol fees have different scopes

Native protocol fees, network gas and API integration charges apply at different layers of a trade. Exchange V3 and V4 protocol fees were removed on September 29, 2021.

For teams on the Standard tier, 0x charges an onchain fee on swaps involving selected tokens. When applicable, the API reports that charge as zeroExFee . Applications can also collect affiliate fees across pricing plans. The integration specifies the recipient and a rate in basis points. It can choose either the buy token or sell token for fees, or let the API select one. These application fees affect the user's trade terms.

The quote returns an application fee through fees.integratorFee . For multiple application fees, fees.integratorFees contains every charge; fees.integratorFee contains only the first. Each amount uses its fee token's base units, so token decimals matter when displaying the charge. A raw integer without its token denomination cannot communicate the actual fee. Keeping network gas, the API charge and the application's fee distinct makes the quoted cost understandable.

Gasless swaps change how gas costs are paid

A 0x relayer pays gas upfront. Ordinarily, the relayed trade covers that cost using part of the user's sell token. An application can instead sponsor transactions and pay 0x directly; under that arrangement, the user pays no onchain gas charge for the sponsored swap.

A gasless quote can report gas reimbursement within the trade. Adding another native-gas charge for that same relayed swap would count its execution cost twice. A separate standard approval, when required, remains a distinct transaction cost. Any affiliate fee in the quote also remains a separate charge.

Gasless swaps support selling non-native tokens. Native-coin sales require ordinary Swap API execution or wrapping the coin before using Gasless API. Gasless approval support is a separate question, because a token may support the relayed swap without supporting a permit-based approval.

A non-null approval object signals gasless approval support; approval.type identifies the permit format.


Approval paths change the cost of reaching execution

A standard onchain approval requires its own network gas when the chosen path lacks sufficient allowance. In API v2, AllowanceHolder and Permit2 are the supported allowance targets. The response identifies the relevant spender in issues.allowance.spender or allowanceTarget. The transaction object's to field specifies its destination separately. Never approve the Settler contract directly.

Illustration: 0xprotocol: Approval paths change the cost of reaching execution

Open full-size image

AllowanceHolder acts as both spender and execution entry point for its integration path. With Permit2, approvals go to Permit2 and execution enters through Settler. These v2 instructions differ from native Exchange V4 orders, whose allowance instructions target the Exchange Proxy. Applying the native-order approval rule to a v2 quote can select the wrong spender. An existing sufficient allowance also changes whether another approval is necessary.

Surplus allocation changes who receives extra buy tokens

Positive slippage describes execution yielding more buy tokens than the quoted amount, while the pricing plan determines who receives that surplus. Eligible custom-plan integrations can direct it through tradeSurplusRecipient ; when set, that recipient receives surplus in the buy token up to the collection cap. API v2 expresses that cap through tradeSurplusMaxBps or tradeSurplusMaxPpm, mutually exclusive parameters measured against the buy amount. The default cap equals the buy amount. Without that parameter, surplus goes to the taker for those integrations. For integrations outside a custom plan, 0x collects the surplus. A higher realized output therefore need not all reach the trader.

Which route risks remain after gas adjustment?

A swap based on a gas-adjusted quote can still fail because of liquidity, slippage or order validity. An expired market-maker quote no longer supplies executable terms. AMM prices can move between quoting and execution, while insufficient balances or allowances can prevent settlement. The slippage setting controls accepted price movement, while allowance controls permission to spend the sell token.

RFQ protection also has a narrower scope than the overall aggregation service. Private, atomic RFQ fills protect against sandwiching and front-running in that order flow. AMM trades using public mempools remain exposed to the usual MEV risks.

Conditions for comparing two quoted routes

Comparable quotes need matching trade inputs before their different costs can explain a routing choice. A displayed price advantage and a gas-adjusted advantage describe different quantities. Use the conditions below to decide whether two quoted outcomes support the same comparison.

  • Match the network, token pair, sell amount and taker when comparing valid quotes.
  • Use consistent gas-price assumptions when execution requirements differ.
  • Read each returned fee amount in its stated token and base units.
  • Separate user-funded gas from a charge already included in gasless execution.
  • Include a separate approval cost only if the selected path needs a standard onchain approval.
  • Check the pricing plan's surplus allocation before treating improved execution as extra trader proceeds.

Comparing the trader's total cost avoids choosing a nominal price advantage already consumed by execution costs.

Visual summary: 0xprotocol - Conditions for comparing two quoted routes

Open full-size image

Things people ask

How does including the taker improve a swap's gas estimate?

Providing the taker address lets Swap API estimate gas for the account expected to execute the quote. A successful estimate reflects that account's execution context. A gas-estimation error can reveal a problem affecting that taker before submission. Supplying the address helps diagnose executability; it does not turn the estimate into a guaranteed final gas charge.

When should an application refresh an RFQ quote on Ethereum mainnet?

Refreshing RFQ quotes about every 30 seconds helps applications avoid using expired market-maker terms on Ethereum mainnet. Such quotes typically remain valid for roughly 60 seconds, so that period is approximate. The order's actual expiry sets its time limit. These timings concern mainnet RFQ quoting and do not establish a universal lifetime for every route or network.

Can a successful swap contain internal order reverts?

A successful aggregate swap can contain internal orders that reverted without filling. Timing and pricing changes can leave some orders already filled or expired. Fallback orders can still let the overall transaction succeed. An internal revert therefore does not establish that the whole swap failed. Its transaction outcome and executed fills determine how to interpret that trace.

Why can a small Gasless API trade fail before submission?

Gasless API can reject a trade when its amount is too small for the current gas conditions on that chain. The rejection includes an estimated minimum for that request. That amount can change with the network and gas conditions, so a fixed application-wide threshold does not describe every gasless quote.

Which quote field signals insufficient liquidity for a requested swap?

Swap API returns liquidityAvailable=false when it cannot find enough liquidity for the requested quote. In that state, it omits other response parameters. This describes the requested trade under its supplied conditions; it does not establish that the token has no liquidity anywhere. A nominal price comparison cannot supply an executable route when the quote reports insufficient liquidity.

Can a Swap API trade pay affiliate fees to multiple recipients?

Swap API supports multiple affiliate-fee recipients within one request, each with an independently specified fee amount. The comma-separated swapFeeRecipient and swapFeeBps lists must have equal lengths and match by position. Each specified swapFeeToken value must be either the buy token or sell token; multiple token values must match the fee list's length. If omitted, the API selects the fee token. These parameters allocate the application's trading fees; they do not redirect the network's gas payment.