SaucerSwap
🇬🇧 English
Search…
⌃K

# Swap

## Introduction

SaucerSwap operates through an on-chain system of smart contracts forked from Uniswap v2. This is made possible by leveraging HSCS to include Solidity smart contract integration with the HTS. These contracts implement an automated liquidity protocol based on a constant product formula:
Where, for any given SaucerSwap pair, x and y are the number of tokens and k is the constant product. Traders pay a 30-basis-point fee on swaps, of which 5/6 goes to liquidity providers, while the remaining 1/6 goes to the DAO treasury. Whenever a swap occurs, the ratio of x and y changes such that the invariant, k, is maintained before fees.

## Token Swap

For example, let $AAA and$BBB be the liquidity pair, with token amounts 2500 and 100, respectively. Here, the constant product is 2500 * 100 = 250,000.
Figure 1. AMM-Faciliated Token Swap. Note that rounded numbers were used in the calculations.
In this example, a trader wants to swap 500 $AAA. In order to withdraw some amount of$BBB, they must deposit a proportional amount of $AAA to maintain the constant product before fees. Firstly, we must account for the swap fee. 500$AAA * 0.3% = 1.50 $AAA in fees is deducted and added to the liquidity pool reserves, slightly increasing the value of k, hence 500 - 1.50 = 498.50$AAA is swapped for $BBB. The number of$AAA tokens increases by 498.50, while the number of $BBB tokens decreases by y. After rearranging the equation (2500 + 498.50) * (100 - y) = 250,000 and solving for y, we learn that the trader receives 16.62$BBB on this token swap.

### Pricing Based on Inputs

Let x and y represent the number of $AAA and$BBB tokens in a $AAA-$BBB liquidity pool. If getInputPrice denotes how many $BBB tokens (i.e., $d_y$ ) can be bought by selling a given number of$AAA tokens (i.e.,
$d_x$
), factoring in a 30-basis-point swap fee,
or in code,
getInputPrice(x, y, dx) = (y * 997 * dx) / (1000 * x + 997 * dx)
If getOutputPrice denotes how many $AAA tokens is needed to buy $d_y$$BBB tokens,
or in code,
getOutputPrice(x, y, dy) = (1000 * x * dy) / ((y - dy) * 997) + 1
where / in the above equations denotes divToInteger, which means divide with rounding to floor of the results.

### Price Impact

Price impact is the influence of a user's individual trade over the market price of an underlying token pair. There are two ways in which price impact can be calculated: x, y,
$d_x$
; or x, y,
$d_y$
. One is based on input, while the other is based on output.
or
in code,
price impact(x, y, dx) = (1000 * x)^2 / (1000 * x + 997 * dx)^2 - 1
price impact(x, y, dy) = (y - dy)^2 / y^2 - 1

## Summary

Put more generally, every time a trader buys $AAA, the price of$AAA goes up as there is now less $AAA in the pool than before the purchase. Conversely, the price of$BBB goes down as there is more $BBB in the pool. The pool stays in constant balance, where the total value of$AAA in the pool will always equal the total value of \$BBB in the pool. Only when new liquidity providers join will the pool expand in size.