Page cover image

Swap

Swapping (Trading) on Spookyswap

Introduction

Swaps are the most utilized feature of SpookySwap V3. It is simply trading an ERC-20 token the user owns for another token. Executing a swap grants the user the desired token by selling the proportional amount of the currently owned token, minus the swap fee, which is given to liquidity providers as a reward for providing liquidity.

Swaps on the SpookySwap App differ from traditional order book trades in that they are not executed against individual orders on a first-in, first-out basis. Instead, swaps are executed against a passive liquidity pool.

Order types

SpookySwap offers two different ways of swapping (trading): market orders and range orders, with range orders being a new addition in the SpookySwap V3.

Market order

A market order is the simplest of all. A market order buys or sells a token immediately. This guarantees that the order is executed, but does not guarantee the execution price. The price you receive depends on the liquidity in the pool and the relative value of the assets at the time of the trade, which can change dynamically.

Range order

The new type of order introduced in SpookySwap V3 is a range order. A range order can be seen as a more flexible form of limit orders. Instead of a specific price, a range order allows the user to choose a price range, rather than a single target, where the user wishes to execute the order. Range orders are executed by providing concentrated liquidity.

Unlike some markets where limit orders may incur fees, the range order maker generates fees while the order is filled. This is due to the range order technically being a form of liquidity provisioning rather than a typical swap.

Using a range order over a limit order increases the likelihood of the order executing, especially in fast-moving or volatile markets. It also reduces the chances of missing a good trade.

For example: a limit order set to buy Bitcoin at 64 000 will not execute if the price only reaches 63 900, but a range order set at 64000 with 0,5 % range would execute.

Range orders possibilities

The design of Automated Market Makers (AMMs) allows for some types of limit orders, but others are not possible due to how price spaces are structured in liquidity pools. Here are four examples of range orders and their traditional counterparts, with Take-Profit and Buy Limit orders being possible, and Buy Stop and Stop-Loss orders being impossible:

Take-Profit Orders

  • Example: The current price in a DAI/ETH pool is 1,500 DAI/ETH, and you want to sell your ETH at 1,600 DAI/ETH.

  • Possible: You can provide ETH at 1,600 DAI/ETH, and when the price reaches that level, your ETH will be swapped for DAI.

Buy Limit Orders

  • Example: The price is 1,500 DAI/ETH, and you anticipate ETH will drop to 1,000 DAI/ETH before rebounding. You want to buy ETH at 1,000 DAI/ETH.

  • Possible: You can provide DAI at 1,000 DAI/ETH, and if the price falls to that level, your DAI will be swapped for ETH.

Buy Stop Orders

  • Example: You expect ETH to rise to 3,000 DAI/ETH once it crosses 2,000, so you want to place an order at 2,000 DAI/ETH.

  • Not Possible: The price space above the spot price is denominated in ETH, so you cannot provide the DAI required for this type of order.

Stop-Loss Orders

  • Example: You want to sell ETH for DAI at 1,000 DAI/ETH if the price drops to that level because you expect it to fall further.

  • Not Possible: The price space below the spot price is denominated in DAI, preventing you from allocating ETH at the desired price for the swap.

Additionally, range orders require manual monitoring. If the spot price crosses the given range and then reverses, you’ll need to either withdraw liquidity manually or use a third-party service to ensure the order is filled.

Price Impact

In a traditional order-book market, a large market-buy order may exhaust the liquidity of one limit-sell order and then execute against another limit-sell order at a higher price. As a result, the final execution price lands somewhere between the prices of the two limit-sell orders.

In an automated market maker (AMM) like SpookySwap, price impact affects the execution price similarly but stems from a different mechanism. During a swap, the relative value of one asset compared to another shifts continuously, meaning the final execution price falls between the starting and ending relative prices.

This dynamic is a fundamental aspect of AMM design and affects every swap on the SpookySwap platform. The available liquidity at various price points can differ, so the price impact for a swap depends on the amount of liquidity available at the time. More liquidity at a given price results in lower price impact, while less liquidity leads to higher price impact.

SpookySwap's interface anticipates approximate price impact in real-time, providing warnings if unusually high price impact is expected. This allows users to assess potential price impacts before executing a swap.

Slippage

Another important factor to consider when swapping on SpookySwap V3 is slippage. Slippage refers to the price difference that can occur while a submitted transaction is pending.

When transactions are submitted to the blockchain, their order of execution is determined by the amount of "gas" offered as a fee. The higher the gas fee, the faster the transaction is processed. Transactions with lower gas fees may remain pending for an uncertain amount of time, during which the market price may change as other swaps are executed.

Slippage tolerance sets the acceptable range of price movement beyond the initial price impact. For example, if a user sets a slippage tolerance of 1%, the transaction will proceed as long as the execution price stays within that 1% range. If the price moves outside the tolerance, the transaction will fail, and the swap will not occur.

A similar situation in traditional markets would be a market-buy order experiencing a delay. Although the expected price is known at the time of submission, market conditions may change before the order is executed.

Pending Transactions Safety Checks

Both price impact and slippage can fluctuate while a transaction is pending, which is why SpookySwap has built several safety checks into the protocol to protect users from significant changes in the execution environment of their swaps. Some of the most common safety checks include:

  • Expired: This error occurs if a transaction remains pending beyond a set deadline. The deadline ensures that if a swap is not executed within a reasonable time, it will be automatically canceled to prevent exposure to prolonged price changes that could negatively affect the user.

  • INSUFFICIENT_OUTPUT_AMOUNT: When a user initiates a swap, SpookySwap provides an estimate of the expected token amount they will receive. If the actual output falls outside the acceptable range defined by the slippage tolerance, the swap is canceled. This protects users from unfavorable price shifts that may occur while their transaction is being processed.

Last updated