Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IL Calculator: Bug fixes #40

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148,049 changes: 148,049 additions & 0 deletions .yarn/releases/yarn-1.22.22.cjs

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions .yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


yarn-path ".yarn/releases/yarn-1.22.22.cjs"
278 changes: 139 additions & 139 deletions analysis/uniV3Math.ipynb
Original file line number Diff line number Diff line change
@@ -1,140 +1,140 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Uniswap math functions\n",
"# https://youtu.be/_asFkMz4zhw?t=25 - Uniswap V3 - Liquidity | DeFi\n",
"\n",
"def get_liquidity_0(x, sp, sb):\n",
" return x * sp * sb / (sb - sp)\n",
"\n",
"def get_liquidity_1(y, sp, sa):\n",
" return y / (sp - sa)\n",
"\n",
"def get_liquidity(x, y, sp, sa, sb):\n",
" if sp <= sa:\n",
" liquidity = get_liquidity_0(x, sp, sb)\n",
" elif sp < sb:\n",
" liquidity0 = get_liquidity_0(x, sp, sb)\n",
" liquidity1 = get_liquidity_1(y, sp, sa)\n",
" liquidity = min(liquidity0, liquidity1)\n",
" else:\n",
" liquidity = get_liquidity_1(y, sp, sa)\n",
" return liquidity\n",
"\n",
"\n",
"#\n",
"# Calculate x and y given liquidity and price range\n",
"#\n",
"def calculate_x(L, sp, sa, sb):\n",
" sp = max(min(sp, sb), sa) # if the price is outside the range, use the range endpoints instead\n",
" return L * (sb - sp) / (sp * sb)\n",
"\n",
"def calculate_y(L, sp, sa, sb):\n",
" sp = max(min(sp, sb), sa) # if the price is outside the range, use the range endpoints instead\n",
" return L * (sp - sa)\n",
"\n",
"def calculate_a1(L, sp, sb, x, y):\n",
" # https://www.wolframalpha.com/input/?i=solve+L+%3D+y+%2F+%28sqrt%28P%29+-+a%29+for+a\n",
" # sqrt(a) = sqrt(P) - y / L\n",
" return (sp - y / L) ** 2\n",
"\n",
"def calculate_a2(sp, sb, x, y):\n",
" # https://www.wolframalpha.com/input/?i=solve+++x+sqrt%28P%29+sqrt%28b%29+%2F+%28sqrt%28b%29++-+sqrt%28P%29%29+%3D+y+%2F+%28sqrt%28P%29+-+a%29%2C+for+a\n",
" # sqrt(a) = (y/sqrt(b) + sqrt(P) x - y/sqrt(P))/x\n",
" # simplify:\n",
" # sqrt(a) = y/(sqrt(b) x) + sqrt(P) - y/(sqrt(P) x)\n",
" sa = y / (sb * x) + sp - y / (sp * x)\n",
" return sa ** 2\n",
"\n",
"#\n",
"# Two different ways how to calculate p_b. calculate_b1() uses liquidity as an input, calculate_b2() does not.\n",
"#\n",
"def calculate_b1(L, sp, sa, x, y):\n",
" # https://www.wolframalpha.com/input/?i=solve+L+%3D+x+sqrt%28P%29+sqrt%28b%29+%2F+%28sqrt%28b%29+-+sqrt%28P%29%29+for+b\n",
" # sqrt(b) = (L sqrt(P)) / (L - sqrt(P) x)\n",
" return ((L * sp) / (L - sp * x)) ** 2\n",
"\n",
"def calculate_b2(sp, sa, x, y):\n",
" # find the square root of b:\n",
" # https://www.wolframalpha.com/input/?i=solve+++x+sqrt%28P%29+b+%2F+%28b++-+sqrt%28P%29%29+%3D+y+%2F+%28sqrt%28P%29+-+sqrt%28a%29%29%2C+for+b\n",
" # sqrt(b) = (sqrt(P) y)/(sqrt(a) sqrt(P) x - P x + y)\n",
" P = sp ** 2\n",
" return (sp * y / ((sa * sp - P) * x + y)) ** 2\n",
"\n",
"\n",
"\n",
"def calculate_P(x,y,sa,sb):\n",
" p = sb*x\n",
" q = y-x*sa*sb\n",
" r = -sb*y\n",
" return (math.pow(-1*q+(q**2 - 4*p*r),0.5)/(2*p) )\n",
"\n",
"def tick_price(t):\n",
" return 1.0001**t\n",
"\n",
"def price_tick(p):\n",
" return math.log(p,1.0001)\n",
"\n",
"def getYFromX(x,sa,sb,sp):\n",
" return x * (sp - sa) * sp *sb /(sb - sp)\n",
"\n",
"# ? P is Y/X\n",
"def simulator_calc_L(x,pa,pb,p):\n",
" sa = pa ** 0.5\n",
" sb = pb ** 0.5\n",
" sp = p ** 0.5\n",
" if p >= pb:\n",
" y=x\n",
" x=0\n",
" L = get_liquidity(x, y, sp, sa, sb)\n",
" elif p <= pa:\n",
" y = 0\n",
" else:\n",
" print(\"sim2\", sa, sb, sp, x)\n",
" y = getYFromX(x, sa, sb, sp)\n",
" L = get_liquidity(x, y, sp, sa, sb)\n",
"\n",
" return L,y\n",
"\n",
"# ! Requires X token to be a stable token\n",
"def getXY(capitalDollar, token0Decimals, token1Decimals, Pxusd, Pyusd, sp, sa, sb):\n",
" # captal = x * Pxusd / xdecimals + y * Pyusd / yDecimals ----- eq1\n",
" # replace y with x from getYFromX\n",
"\n",
" factor1 = Pxusd / (10 ** token0Decimals)\n",
" factor2 = Pyusd / (10 ** token1Decimals)\n",
" factor3 = (sp - sa) * sp * sb / (sb - sp)\n",
"\n",
" x = capitalDollar / (factor1 + factor3 * factor2)\n",
" y = getYFromX(x, sa, sb, sp)\n",
" return x, y"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Uniswap math functions\n",
"# https://youtu.be/_asFkMz4zhw?t=25 - Uniswap V3 - Liquidity | DeFi\n",
"\n",
"def get_liquidity_0(x, sp, sb):\n",
" return x * sp * sb / (sb - sp)\n",
"\n",
"def get_liquidity_1(y, sp, sa):\n",
" return y / (sp - sa)\n",
"\n",
"def get_liquidity(x, y, sp, sa, sb):\n",
" if sp <= sa:\n",
" liquidity = get_liquidity_0(x, sp, sb)\n",
" elif sp < sb:\n",
" liquidity0 = get_liquidity_0(x, sp, sb)\n",
" liquidity1 = get_liquidity_1(y, sp, sa)\n",
" liquidity = min(liquidity0, liquidity1)\n",
" else:\n",
" liquidity = get_liquidity_1(y, sp, sa)\n",
" return liquidity\n",
"\n",
"\n",
"#\n",
"# Calculate x and y given liquidity and price range\n",
"#\n",
"def calculate_x(L, sp, sa, sb):\n",
" sp = max(min(sp, sb), sa) # if the price is outside the range, use the range endpoints instead\n",
" return L * (sb - sp) / (sp * sb)\n",
"\n",
"def calculate_y(L, sp, sa, sb):\n",
" sp = max(min(sp, sb), sa) # if the price is outside the range, use the range endpoints instead\n",
" return L * (sp - sa)\n",
"\n",
"def calculate_a1(L, sp, sb, x, y):\n",
" # https://www.wolframalpha.com/input/?i=solve+L+%3D+y+%2F+%28sqrt%28P%29+-+a%29+for+a\n",
" # sqrt(a) = sqrt(P) - y / L\n",
" return (sp - y / L) ** 2\n",
"\n",
"def calculate_a2(sp, sb, x, y):\n",
" # https://www.wolframalpha.com/input/?i=solve+++x+sqrt%28P%29+sqrt%28b%29+%2F+%28sqrt%28b%29++-+sqrt%28P%29%29+%3D+y+%2F+%28sqrt%28P%29+-+a%29%2C+for+a\n",
" # sqrt(a) = (y/sqrt(b) + sqrt(P) x - y/sqrt(P))/x\n",
" # simplify:\n",
" # sqrt(a) = y/(sqrt(b) x) + sqrt(P) - y/(sqrt(P) x)\n",
" sa = y / (sb * x) + sp - y / (sp * x)\n",
" return sa ** 2\n",
"\n",
"#\n",
"# Two different ways how to calculate p_b. calculate_b1() uses liquidity as an input, calculate_b2() does not.\n",
"#\n",
"def calculate_b1(L, sp, sa, x, y):\n",
" # https://www.wolframalpha.com/input/?i=solve+L+%3D+x+sqrt%28P%29+sqrt%28b%29+%2F+%28sqrt%28b%29+-+sqrt%28P%29%29+for+b\n",
" # sqrt(b) = (L sqrt(P)) / (L - sqrt(P) x)\n",
" return ((L * sp) / (L - sp * x)) ** 2\n",
"\n",
"def calculate_b2(sp, sa, x, y):\n",
" # find the square root of b:\n",
" # https://www.wolframalpha.com/input/?i=solve+++x+sqrt%28P%29+b+%2F+%28b++-+sqrt%28P%29%29+%3D+y+%2F+%28sqrt%28P%29+-+sqrt%28a%29%29%2C+for+b\n",
" # sqrt(b) = (sqrt(P) y)/(sqrt(a) sqrt(P) x - P x + y)\n",
" P = sp ** 2\n",
" return (sp * y / ((sa * sp - P) * x + y)) ** 2\n",
"\n",
"\n",
"\n",
"def calculate_P(x,y,sa,sb):\n",
" p = sb*x\n",
" q = y-x*sa*sb\n",
" r = -sb*y\n",
" return (math.pow(-1*q+(q**2 - 4*p*r),0.5)/(2*p) )\n",
"\n",
"def tick_price(t):\n",
" return 1.0001**t\n",
"\n",
"def price_tick(p):\n",
" return math.log(p,1.0001)\n",
"\n",
"def getYFromX(x,sa,sb,sp):\n",
" return x * (sp - sa) * sp *sb /(sb - sp)\n",
"\n",
"# ? P is Y/X\n",
"def simulator_calc_L(x,pa,pb,p):\n",
" sa = pa ** 0.5\n",
" sb = pb ** 0.5\n",
" sp = p ** 0.5\n",
" if p >= pb:\n",
" y=x\n",
" x=0\n",
" L = get_liquidity(x, y, sp, sa, sb)\n",
" elif p <= pa:\n",
" y = 0\n",
" else:\n",
" print(\"sim2\", sa, sb, sp, x)\n",
" y = getYFromX(x, sa, sb, sp)\n",
" L = get_liquidity(x, y, sp, sa, sb)\n",
"\n",
" return L,y\n",
"\n",
"# ! Requires X token to be a stable token\n",
"def getXY(capitalDollar, token0Decimals, token1Decimals, Pxusd, Pyusd, sp, sa, sb):\n",
" # captal = x * Pxusd / xdecimals + y * Pyusd / yDecimals ----- eq1\n",
" # replace y with x from getYFromX\n",
"\n",
" factor1 = Pxusd / (10 ** token0Decimals)\n",
" factor2 = Pyusd / (10 ** token1Decimals)\n",
" factor3 = (sp - sa) * sp * sb / (sb - sp)\n",
"\n",
" x = capitalDollar / (factor1 + factor3 * factor2)\n",
" y = getYFromX(x, sa, sb, sp)\n",
" return x, y"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading
Loading