Swaps
The core functionality of BEX is to allow users to exchange one type of token for another. If liquidity exists for a given pair in the DEX contract, users will be able to swap between tokens. The BEX web app provides an interface for general-purpose swaps.
Users can swap tokens on BEX on bArtio here: https://bartio.bex.berachain.com//swap
Single-Hop and Multi-Hop Swaps
If adequate liquidity exists in a given pair, a swap will most likely be executed as a single hop. Single-hop swaps incur less exchange and gas fees, making them the preferred method.
However, When a trade between the input and output token does not provide the best overall price (or the trading pair does not exist as a single pool), BEX can perform a multi-hop swap. Multi-hop swaps involve chaining swaps across multiple pools to find the best overall price. The process of finding a cost-efficient multi-hop path is called routing.
BEX provides an off-chain API for approximately optimal routing. See an example here.
Intermediate Tokens
In a multi-hop swap, intermediate tokens are the tokens held temporarily when swapping through a sequence of pairs. Intermediate tokens in BEX are never transferred because settlement occurs based on the net debit against the entire BEX protocol.
Swap Parameters
When initiating a swap, users can define the following parameters (sensible defaults are chosen by the BEX app):
Quantity: The amount of tokens to swap, which can be specified as either a fixed input quantity or a fixed output quantity.
Slippage: The maximum acceptable difference between the expected and actual price of the trade. Slippage is expressed as a percentage and represents the worst-case price impact the user is willing to accept. If the actual price impact exceeds this threshold, the swap transaction will revert. Slippage can be specified by selecting the gear icon on the swap page:
Settlement: Advanced users interacting with the BEX contract can settle swaps directly with ERC20 token transfers or using their surplus collateral balance. A mix-and-match approach is possible, such as paying the input as an ERC20 token transfer and receiving the output as surplus collateral.