Skip to content

Commit

Permalink
Merge branch 'dev' into feat/generateChangelog
Browse files Browse the repository at this point in the history
  • Loading branch information
akiraonstarknet authored May 1, 2024
2 parents 76a5a8e + 7c1b4ba commit 479be0c
Show file tree
Hide file tree
Showing 4 changed files with 280 additions and 18 deletions.
4 changes: 4 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"projectName": "<insert the repo's name>",
"projectOwner": "<insert the repo's owner/orgs>"
}
19 changes: 1 addition & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,11 @@ Thanks goes to these wonderful people.
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
<tbody>
<tr></tr>
</tbody>
<tfoot>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/akiraonstarknet"><img src="https://avatars.githubusercontent.com/u/156126180?v=4" width="100px;" alt="Akira"/><br /><sub><b>Akira</b></sub></a><br /><a href="https://github.com/strkfarm/starkfarm-client/commits?author=akiraonstarknet" title="Code">💻</a></td>
<td align="center" size="13px" colspan="7">
<img src="https://raw.githubusercontent.com/all-contributors/all-contributors-cli/1b8533af435da9854653492b1327a23a4dbd0a10/assets/logo-small.svg">
<a href="https://all-contributors.js.org/docs/en/bot/usage">Add your contributions</a>
</img>
</td>
</tr>
</tfoot>
</table>

<!-- markdownlint-restore -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!

[![All Contributors](https://img.shields.io/github/all-contributors/akiraonstarknet/starkfarm-client?color=ee8449&style=flat-square)](#contributors)

## Current Version
CURRENT_VERSION_PLACEHOLDER
140 changes: 140 additions & 0 deletions analysis/uniV3Math.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +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
}
135 changes: 135 additions & 0 deletions analysis/v3IL.ipynb

Large diffs are not rendered by default.

0 comments on commit 479be0c

Please sign in to comment.