Skip to content

Swaps

Hot Path

WARNING

Note: Hot path swaps are not guaranteed to be available. Any integrations using the hot path swap should include a fallback to a cold path swap call or use the cold path swap exclusively.

The most common operation on a DEX is a vanilla swap. Therefore, BEX contains the entire logic for a full swap inside the core contract. This avoids the gas overhead of DELEGATECALL to another proxy contract.

For more information on how to perform a hot path swap, refer to the DEX Contract Interface.

Cold Path

The same swap functionality is available using the standard userCmd() method call rather than the swap() call. The behavior is identical, except the swap code is called on a proxy contract, which imposes an overhead of approximately 5000 gas.

The swap path is called with callpath index 1. The bytestring command is the standard ABI packing of the swap parameters:

solidity
userCmd(1, abi.encode(
   base,
   quote,
   poolIdx,
   isBuy,
   inBaseQty,
   qty,
   tip,
   limitPrice,
   minOut,
   settleFlags
))

Parameters

NameTypeDescription
baseaddressThe base token in the swap pair
quoteaddressThe quote token in the swap pair
poolIdxuint256The index of the pool to swap in
isBuyboolTrue if the user wants to pay base token and receive quote token. False if the user wants to receive base token and pay quote token
inBaseQtyboolIf true, the qty parameter is denominated in base tokens. If false, it's denominated in quote tokens
qtyuint128The quantity of tokens to swap
tipuint16The tip amount for the relayer executing the swap
limitPriceuint128Represents the worse possible price the user is willing to accept. If buying this represents an upper bound.
minOutuint128The minimum amount of output tokens the user is willing to receive
reserveFlagsuint8Flags indicating whether to use the user's surplus collateral balance for the base and/or quote token (see Type Conventions)

Note on limitPrice

Limit price is based on the price of the curve, not the fill price of the swap. If the swap pushes the price of the curve to the limit price, then the swap will stop and leave the remaining quantity unfilled (rather than reverting the transaction). Traditional slippage limits should be set with the minOut parameter.

Price value is based on a specific encoding of the square root of the pool price in Q64.64 fixed point format

If the caller is uninterested in partial fills and slippage is set with minOut parameter value, this value can be set to "max values" below based on the direction of the swap:

isBuy=false : 65538

isBuy=true : 21267430153580247136652501917186561137