📠Basic Mathematics
Lets assume a protocol with a token TP. 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 USDCTP, and the bonding curve formula given by Uniswap is:
x⋅y=K
Hence,
TP⋅USDC=K
This formula is responsible for calculating prices, and deciding how much TP token would be received in exchange for a certain amount of USDC, or vice versa.
The formula states that K is a constant no matter what the reserves (x or y) are. Every swap increases the reserve of either USDC or TP token and decreases the reserve of the other.
(x+Δx)⋅(y−Δy)=K
Where Δx is the amount being provided by the user for sale, and Δy is the amount the user is receiving from the DEX in exchange for Δx.
Since K is a constant, we can asume that:
x⋅y=(x+Δx)⋅(y−Δy)
Before any swap is being made, we know the exact values of x, y, and Δx (given by the input). In that way, we are interested in calculating Δy, which is the amount of USDC or TP token the user will receive:
y−Δy=x+Δxx⋅y
−Δy=(x+Δx)(y⋅Δx)−y
−Δy=x+Δxx⋅y−y(x+Δx)
−Δy=x+Δxx⋅y−y⋅x−y⋅Δx
−Δy=x+Δx−y⋅Δx
Hence, after simplyfing the above equation to obtain Δy , we get the following:
Δy=(x+Δx)(y⋅Δx)
Last updated