Links

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+SjtotalSlippage = Si + Sj
Si=g(r)g(r)rrSi = \frac{-g(r') - g(r)} {r' - r}
Sj=g(r)g(r)rrSj = \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=800010000=0.8r = \frac{8000}{10000} = 0.8
r=8000+100010000=0.9r' = \frac{8000 + 1000}{10000} = 0.9
g(r)=0.000020.87=0.00009536743g(r) = \frac{0.00002}{0.8^7} = 0.00009536743
g(r)=0.000020.97=0.00004181503g(r') = \frac{0.00002}{0.9^7} = 0.00004181503
Si=0.000041815030.000095367430.90.8=0.0535524Si = -\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.
amountpostTradingFee=1000.53353(10.015%)=1000.383amount_{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=1011=0.909091r =\frac{ 10}{11} = 0.909091
r=100.3334611=0.87878r' =\frac{10 - 0.33346}{11} = 0.87878
g(r)=0.000020.9090917=0.00003897g(r) = \frac{0.00002}{0.9090917}= 0.00003897
g(r)=0.000020.878787=0.00004942g(r') = \frac{0.00002}{0.878787} = 0.00004942
Sj=0.000049420.000038970.878780.909091=0.034475Sj = \frac{0.00004942 - 0.00003897}{0.87878 - 0.909091} = - 0.034475%
amountpostSlippage=0.33346(10.034475%)=0.33335amount_{postSlippage} = 0.33346 * (1 - 0.034475\%) = 0.33335
Third, the trading fees are applied.
tradingFee=0.015%(1+3060)=0.0225%tradingFee = 0.015\% * (1 + \frac{30}{60}) =0.0225\%
amountpostTradingFee=0.33335(10.0225%)=0.33328amount_{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.