šŸ“ Basic Mathematics

Lets assume a protocol with a token TPT_P. On an initial cycle is going to be paired with an USD-pegged stablecoin such as USDC. In that way, the LP is going to be TPUSDC\frac{T_P}{USDC}, and the bonding curve formula given by Uniswap is:

xā‹…y=K x \cdot y = K

Hence,

TPā‹…USDC=KT_P \cdot USDC = K

This formula is responsible for calculating prices, and deciding how much TPT_P token would be received in exchange for a certain amount of USDC, or vice versa.

The formula states that KK is a constant no matter what the reserves (x or y) are. Every swap increases the reserve of either USDC or TPT_P token and decreases the reserve of the other.

(x+Ī”x)ā‹…(yāˆ’Ī”y)=K(x + \Delta x) \cdot (y - \Delta y) = K

Where Ī”x\Delta x is the amount being provided by the user for sale, and Ī”y\Delta y is the amount the user is receiving from the DEX in exchange for Ī”x\Delta x.

Since KK is a constant, we can asume that:

xā‹…y=(x+Ī”x)ā‹…(yāˆ’Ī”y)x \cdot y = (x + \Delta x) \cdot (y - \Delta y)

Before any swap is being made, we know the exact values of x, y, and Ī”x\Delta x (given by the input). In that way, we are interested in calculating Ī”y\Delta y, which is the amount of USDC or TPT_P token the user will receive:

yāˆ’Ī”y=xā‹…yx+Ī”xy - \Delta y = \frac{x \cdot y}{x + \Delta x}

āˆ’Ī”y=(yā‹…Ī”x)(x+Ī”x)āˆ’y-\Delta y = \frac{(y \cdot \Delta x)}{(x + \Delta x)} - y

āˆ’Ī”y=xā‹…yāˆ’y(x+Ī”x)x+Ī”x-\Delta y = \frac{x \cdot y - y(x + \Delta x) }{x+ \Delta x}

āˆ’Ī”y=xā‹…yāˆ’yā‹…xāˆ’yā‹…Ī”xx+Ī”x-\Delta y = \frac{x \cdot y - y \cdot x - y \cdot \Delta x}{x+\Delta x}

āˆ’Ī”y=āˆ’yā‹…Ī”xx+Ī”x-\Delta y = \frac{-y \cdot \Delta x}{x+ \Delta x}

Hence, after simplyfing the above equation to obtain Ī”y\Delta y , we get the following:

Ī”y=(yā‹…Ī”x)(x+Ī”x)\Delta y = \frac{(y \cdot \Delta x)}{(x + \Delta x)}

Last updated