How Tealswap works?

Tealswap is a decentralized exchange with automated market maker on Oasys hub-layer.

Participant

There are two types of participants in Tealswap, Traders and Liquidity Providers.

Trader

A trader is a user who swaps two listed ERC20 tokens(sells a ERC20 token and buys another ERC20 token). Amount of input and output of tokens is determined by constant product formula(see Constant Product Formula). A trader pays swap fees.

Liquidity Provider

A liquidity provider is a user who deposits an equivalent value of two ERC20 tokens into the liquidity pool. Liquidity providers take a risk of impermanent loss(see Impermanent Loss) and are compensated with swap fees.

Glossary

TermDescription

ERC20

ERC20 tokens are fungibile tokens. Currently in Tealswap, stablecoins, USDC and USDT, and OAS(Oasys token) are supported.

Pool

A pair of two tokens makes a pool. When liquidity is added or removed, tokens in a pool are increased or decreased.

Automated Market Maker (AMM)

AMM is a trading mechanism that improved shortcomings of order book method. Instead of order books of buyers and sellers, market is automated with the provided liquidity. It overcomes an economic problem known as the coincidence of wants and enables an exchange to occur immediately. Ratio of swapped tokens is determined in AMM with constant product formula, a.k.a x*y = k. See below for the detailed explanations.

Constant Product Formula

Constant Product Formula is a method to determine the price of tokens when swap is executed. It refers to the equation below: xy=kx*y=k, where xx and yy are reserve balance of two tokens in the pool and kk is a constant (whose value does not change with exchange of tokens). When swap occurs the reserve balance changes: (xδx)(y+δy)=k(x-\delta_x)*(y+\delta_y)= k, where δx\delta_xis amount of outflowing token (token purchased) and δy\delta_y is amount of inflowing token (token sold).

For example, let’s take a look at a case of ETH to USD swap. Let’s assume 1 ETH = 10 USD in market and there are 1,000 ETH and accordingly 10,000 USD token in liquidity pool. Selling 5 ETH, a trader inputs 5 ETH to the liquidity reserve. Then, to satisfy the constant product formula (10,000δx)(1,000+5)=10,0001,000(10,000-\delta_x)*(1,000+5)=10,000*1,000, the reserve outputs δx49.751\delta_x\approx49.751 of USD token.

Price Impact

In the example above, we can see that the trader’s expected price of ETH to USD is 9.95 USD where the initial market price was 10 USD before the swap. This difference between current market price and the expected price is called price impact. In Tealswap, price impact is affected by relative size of an order to the liquidity pool. The larger the order to the pool, the further above the market price will the trade price be.

For example, let’s take a look again at a case of ETH to USD swap. Now there are 100,000 ETH and accordingly 1,000,000 USD token in liquidity pool. Selling 5 ETH, to satisfy the constant product formula (1,000,000δx)(100,000+5)=1,000,000100,000(1,000,000-\delta_x)*(100,000+5)=1,000,000*100,000, the reserve outputs δx49.997\delta_x\approx49.997of USD token. This time, expected price of ETH to USD is 9.999 USD.

Providing Liquidity and Swap Fees

When liquidity is provided to a pool, constant value kk increases accordingly (and vice versa). If α=x/x=y/y\alpha=\triangle x/x=\triangle y/y(ratio of provided/previous liquidity) is provided, the equation is as below: xy=kx'*y'=k', where x=(1+α)x,y=(1+α)yx'=(1+\alpha)x, y'=(1+\alpha)y and accordingly k=(1+α)2kk'=(1+\alpha)^2k

Similarly after swap is executed with swap fees, swap fee is reserved to the pool which increases the value of kk. Redeeming LP token (removing liquidity), they are given ERC20 tokens amount in the same way. Liquidity providers expect swap fees accumulated during their provision period in return for their liquidity.

Impermanent Loss

While lquidity providers expect pool’s growth as a reward, they take a risk of impermanent loss. Impermanent loss is unrealized loss that is calculated as a difference between value of LP tokens (if redeemed) and value of otherwise, not depositing into a pool and holding.

Graph below shows how much of impermanent loss liquidity providers are exposed to as price of two tokens change.

When ETH value decreases (ETH reserve increased and USD reserve decreased), constant product formula changes as below:

Assuming ETH price decreased to 5 USD, the formula becomes (10,000δx)(1,000+δy)=10,0001,000(10,000-\delta_x)*(1,000+\delta_y)=10,000*1,000 and market price becomes 10,000δx1,000+δy=5\frac{10,000-\delta_x} {1,000+\delta_y} = 5. This gives us δx2930,δy415\delta_x \approx 2930, \delta_y \approx 415 which means there are 7,070 USD and 1,415 ETH in pool reserve. We can calculate impermanent loss here by comparing scenario of holding and providing liquidity.

ETHUSDSUM

Holding(Initial)

1,000

10,000

15,000

LP token

1,415

7,070

14,145

Last updated