# Swapping

Swapping between tokens is always a direct path from tokenIn to tokenOut, as compared to other AMMs where users have to be routed through multistep paths. Furthermore, since Singularity maintains collateralization ratios for each pool, users may be able to even earn positive slippage when the pool is favorable to trade through.
We can break down a swap from tokenA to tokenB into two parts, swapping in tokenA (1) and swapping out tokenB (2) where:
• S_i is the slippage for tokenA
• S_j is the slippage for tokenB
• r is the collateralization ratio before the swap
• r’ is the collateralization ratio after the swap
$totalSlippage = Si + Sj$
$Si = \frac{-g(r') - g(r)} {r' - r}$
$Sj = \frac{g(r') - g(r) }{r' - r}$

### Part 1 (Swapping in tokenA)

Since tokenA is now being sent to the pool, the pool’s assets increase by the number of tokenA tokens being swapped in (amountA) while liabilities stays the same. This means slippage through part 1 is always positive. It will never be negative slippage since the pool can only get “healthier”.
• First, the slippage formula is applied (above).
• Next, the trading fee is applied (more details in the Trading Fee Section).
• After slippage is applied, the final amountA is converted into USD value (usdAmountA) based on the oracle’s feed.

### Part 2 (Swapping out tokenB)

Since tokenB is now being sent out of the pool, the pool’s assets decrease by the number of tokens being swapped out (amountB) while liabilities stays the same. This means slippage through part 2 is always negative. It will never be positive slippage since the pool can only get “unhealthier”.
• First, usdAmountA is converted into the corresponding amount of B tokens (amountB) based on the oracle’s feed.
• Next, the slippage formula is applied (above).
• Lastly, the trading fee is applied (above).
• After all fees are applied, the final amountB is sent to the user, completing the trade.

#### Example Part 1

Let’s say Bob wants to swap 1000 USDC to ETH. For part 1, assume the USDC pool has the following parameters:
• Assets = 8000
• Liabilities = 10000
• Base fee = 0.015% (stablecoin)
• Oracle price: $1/USDC First, the slippage formula is applied. $r = \frac{8000}{10000} = 0.8$ $r' = \frac{8000 + 1000}{10000} = 0.9$ $g(r) = \frac{0.00002}{0.8^7} = 0.00009536743$ $g(r') = \frac{0.00002}{0.9^7} = 0.00004181503$ $Si = -\frac{0.00004181503 - 0.00009536743}{0.9 - 0.8} = 0.0535524%$ Second, the trading fees are applied. Since USDC is a stablecoin, the trading fee is the base fee (0.015%). You can see this outlined below, integrated into the formula. $amount_{postTradingFee} = 1000.53353 * (1 - 0.015\%) =1000.383$ Lastly, newAmountA is converted into the USD equivalent. #### Example Part 2: Assume the ETH pool has the following parameters: • Assets = 10 • Liabilities = 11 • Base fee = 0.015% (non-stablecoin) • Oracle price =$3000/ETH
• Last updated time = 30 seconds ago
First, amount_usd is converted to the ETH equivalent.
Second, the slippage formula is applied.
$r =\frac{ 10}{11} = 0.909091$
$r' =\frac{10 - 0.33346}{11} = 0.87878$
$g(r) = \frac{0.00002}{0.9090917}= 0.00003897$
$g(r') = \frac{0.00002}{0.878787} = 0.00004942$
$Sj = \frac{0.00004942 - 0.00003897}{0.87878 - 0.909091} = - 0.034475%$
$amount_{postSlippage} = 0.33346 * (1 - 0.034475\%) = 0.33335$
Third, the trading fees are applied.
$tradingFee = 0.015\% * (1 + \frac{30}{60}) =0.0225\%$
$amount_{postTradingFee} = 0.33335 * (1 - 0.0225\%) = 0.33328$
The swap from USDC to ETH is now complete, with Bob receiving 0.33328 ETH (\$999.83) with 0.04% slippage.