For math people:
Constant product automated market maker formula, first used by Uniswap:
tokenA_balance(p) * tokenB_balance(p) = k
which translates to ⇒
$$ x * y = k $$
the price function of the token is a 1st order derivative of the function
$$ x * y = k ⇒ y = k / x ⇒ dy/dx = - k / x^2 $$
amm(tokenA, tokenB):
k = tokenA * tokenB
d_tokenB = k / (tokenA ** 2)
return d_tokenB
in order to find the liquidity required to sustain a certain price within the pool, we can use a recursive algorithm which you can see below. This formula allows us to calculate the necessary amount of usd_liquidity in order to sustain the desired_price given mytoken_supply.
example:
desired_price = [1, 2, 3, 4]
mytoken_supply = [111, 222, 333, 444]
usd_liquidity = []
for i in range(0, 4):
usd_liquidity.append((adjust_liquidity(desired_price[i], 5, 1,
mytoken_supply[i])))
print(usd_liquidity)
if you run the program correctly your will get:
$$ [110.99465880497492, 443.99219575220167, 998.985104895405, 1775.9875068669867] $$
With this method, you can model liquidity flows based on your revenue forecasts.
*in order to increase accuracy, we can set lower adjusting criteria, I suggest not to do this unless the token price is below decimal points. If you run into a recursion error or wanna get very precise use this (don’t shoot yourself in the foot!)
# to fix the error
import sys
sys.setrecursionlimit(50000)
# to ger precision
import os
os.nice(-19)