📠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:

xy=K x \cdot y = K

Hence,

TPUSDC=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:

xy=(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=xyx+Δ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=xyy(x+Δx)x+Δx-\Delta y = \frac{x \cdot y - y(x + \Delta x) }{x+ \Delta x}

Δy=xyyxyΔ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