💱Swap Mechanism

Swaps are a fundamental aspect of interacting with the DeFi World protocol. For users, the process of swapping is straightforward: one selects an ERC-20 token they possess and another they wish to acquire. Executing a swap involves selling the owned tokens for a proportionate amount of the desired tokens, minus a swap fee that is allocated to liquidity providers. Swapping via the DeFi World protocol is a permissionless activity.

Note: Utilizing web interfaces to conduct swaps through the DeFi World protocol can introduce additional permission structures and may lead to different execution behaviors compared to direct protocol use. For more insights into the differences between the protocol and a web interface, refer to the relevant documentation.

Swaps in the DeFi World protocol differ from traditional order book trades. Instead of executing against discrete orders on a first-come, first-served basis, swaps are executed against a passive liquidity pool, with liquidity providers earning fees in proportion to their committed capital.

Price Impact: In traditional order-book markets, a large market-buy order might exhaust the available liquidity at a certain limit-sell price, continuing to execute against a higher-priced limit-sell order. The final execution price of the order typically falls between these two limit-sell prices.

Price impact in an automated market maker (AMM) like DeFi World similarly affects the execution price of a swap. However, it results from a different dynamic where the relative value of one asset in terms of the other continuously shifts during the swap execution. This leaves the final execution price somewhere between the starting and ending relative prices.

This dynamic is inherent in every swap using the DeFi World protocol, as it is an integral part of AMM design. The price impact for a given swap size will vary depending on the liquidity available at different price points. More liquidity at a given price results in lower price impact for a swap of a certain size, and vice versa.

The DeFi World interface anticipates approximate price impact in real-time, issuing warnings for unusually high impacts during a swap. This allows anyone executing a swap to assess the potential price impact beforehand.

Slippage: Another critical aspect to consider in swaps with the DeFi World protocol is slippage. Slippage refers to changes in the expected price that may occur while a transaction is pending. The execution order of transactions on the blockchain is determined by the "gas" fee offered. Transactions with lower gas fees may remain pending for an indeterminate period, during which the price environment may change due to ongoing swaps.

Slippage tolerances set a range of acceptable price changes beyond the initial impact. If the execution price remains within this range, the transaction proceeds. If it falls outside the accepted range, the transaction fails, and the swap does not occur.

Safety Checks: To safeguard users from significant changes in the execution environment of their swap, the DeFi World protocol incorporates several safety checks:

  1. Expired: This error occurs if a swap is pending beyond a predetermined deadline, after which the swap is canceled to protect against long pending periods and associated price changes.

  2. INSUFFICIENT_OUTPUT_AMOUNT: When a swap is initiated, the interface provides an estimate of the expected output. If the actual output does not match the estimate within the slippage tolerance, the swap is canceled to protect the user from unfavorable price changes during the pending period.

These mechanisms ensure that users are shielded from drastic shifts in market conditions while their transactions are in process. For more detailed information on liquidity provision, users are advised to consult the liquidity user guide. While the DeFi World interface informs users about their swap conditions, it is important to note that these are not guaranteed outcomes.

Last updated