From af92205e4d156d84179aca438d0e012ab6e0acab Mon Sep 17 00:00:00 2001 From: icmoore Date: Sat, 30 Sep 2023 18:59:43 -0700 Subject: [PATCH] clean-up --- .../deposit-checkpoint.ipynb | 642 ---------- .../liqleak-checkpoint.ipynb | 413 ------ .../liquidity-checkpoint.ipynb | 360 ------ .../pairingcode-checkpoint.ipynb | 1124 ----------------- .../revenue-checkpoint.ipynb | 297 ----- .../.ipynb_checkpoints/swap-checkpoint.ipynb | 177 --- .../withdraw-checkpoint.ipynb | 518 -------- 7 files changed, 3531 deletions(-) delete mode 100644 notebooks/tutorials/.ipynb_checkpoints/deposit-checkpoint.ipynb delete mode 100644 notebooks/tutorials/.ipynb_checkpoints/liqleak-checkpoint.ipynb delete mode 100644 notebooks/tutorials/.ipynb_checkpoints/liquidity-checkpoint.ipynb delete mode 100644 notebooks/tutorials/.ipynb_checkpoints/pairingcode-checkpoint.ipynb delete mode 100644 notebooks/tutorials/.ipynb_checkpoints/revenue-checkpoint.ipynb delete mode 100644 notebooks/tutorials/.ipynb_checkpoints/swap-checkpoint.ipynb delete mode 100644 notebooks/tutorials/.ipynb_checkpoints/withdraw-checkpoint.ipynb diff --git a/notebooks/tutorials/.ipynb_checkpoints/deposit-checkpoint.ipynb b/notebooks/tutorials/.ipynb_checkpoints/deposit-checkpoint.ipynb deleted file mode 100644 index bdf32df..0000000 --- a/notebooks/tutorials/.ipynb_checkpoints/deposit-checkpoint.ipynb +++ /dev/null @@ -1,642 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "id": "4087412d-29ba-47b1-b413-c8b7d70ee84f", - "metadata": {}, - "outputs": [], - "source": [ - "import os\n", - "import copy\n", - "import numpy as np\n", - "import pandas as pd\n", - "import time\n", - "import datetime\n", - "import math\n", - "import pytest\n", - "from termcolor import colored\n", - "import matplotlib.pyplot as plt\n", - "cwd = os.getcwd().replace(\"notebooks/tutorials\",\"\")\n", - "os.chdir(cwd)" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "fe75cbd4-1a13-45f2-bbe9-c9d22c87bbcc", - "metadata": {}, - "outputs": [], - "source": [ - "from uniswappy.math.model import TokenDeltaModel\n", - "from uniswappy.math.model import EventSelectionModel\n", - "from uniswappy.cpt.factory import Factory\n", - "from uniswappy.cpt.exchg import Exchange\n", - "from uniswappy.erc import ERC20\n", - "from uniswappy.process.deposit import SwapDeposit\n", - "from uniswappy.process.swap import WithdrawSwap\n", - "from uniswappy.process.liquidity import RemoveLiquidity\n", - "from uniswappy.process.liquidity import AddLiquidity\n", - "from uniswappy.process.swap import Swap" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "24d1d9fc-ce68-48ba-96ee-7ac69d8e5f4e", - "metadata": {}, - "outputs": [], - "source": [ - "user_nm = 'user0'\n", - "eth_amount = 1000\n", - "tkn_amount = 100000" - ] - }, - { - "cell_type": "markdown", - "id": "5556e7fb-7685-49a3-b28f-4f6382938b97", - "metadata": {}, - "source": [ - "#### SwapDeposit (incl. fee):\n", - "\n", - "* A ```SwapDeposit``` is where a certain amount of a specific token is deposited into the LP under one operation\n", - "* Includes two steps:\n", - " * (step 1) perform approx. 50% swap for opposing token\n", - " * (step 2) using amt from step 1, perform 1:1 deposit\n", - "* A portion of the incoming funds are swapped to achieve equal portions of both assets\n", - "* These portions are then deposited into the LP\n", - "* To ensure all the funds are deposited, we must determine the portion ($\\alpha$) of $s_{in}$ that must first get swapped\n", - "\n", - "> $ \\Delta x =\\Delta x_{swap} + \\Delta x_{deposit}$\n", - "\n", - "> $ \\Delta x =\\alpha \\Delta x + \\Delta x_{deposit}$\n", - "\n", - "\n", - "#### Follows this system of equations:\n", - "\n", - "> (Eq. 1) $\\Delta y = \\frac{997 y \\alpha \\Delta x }{1000x + 997\\alpha\\Delta x }$\n", - "\n", - "> (Eq. 2) $\\Delta x = \\alpha\\Delta x + \\frac{\\Delta y(x + \\alpha\\Delta x )}{y - \\Delta y}$\n", - "\n", - "where \n", - "* $\\Delta x$ -> amt token in \n", - "* $\\Delta y$ -> amt opposing token out after swap\n", - "* $\\alpha$ -> portion of $\\Delta x$ swapped in\n", - "* x -> reserve0\n", - "* y -> reserve1" - ] - }, - { - "cell_type": "markdown", - "id": "cc6e5fc9-59af-415b-a705-19d95e45608a", - "metadata": {}, - "source": [ - "### Let's highlight why the above considerations are important ..." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "7d8b1466-fb14-4bc5-bd04-8968f20821db", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "***\n", - "Initial LP\n", - "***\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "***\n", - "LP post step 1\n", - "***\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1050.0 | TKN = 95251.7026241844\n", - "Liquidity: 10000.0 \n", - "\n", - "***\n", - "LP post step 2\n", - "***\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1100.0 | TKN = 99787.4979872408\n", - "Liquidity: 10476.190476190477 \n", - "\n", - "Given 100 initial ETH:\n", - " (step 1) 50.0 ETH must first get swapped for 4748.297375815593 TKN\n", - " (step 2) The received TKN gets deposited along with the remaining 50.0 ETH\n", - "\n", - "Total deposited is 50.000000 + 50.000000 = 100.000000 ETH:\n", - "However, we lost \u001b[1m\u001b[31m212.50201275919972\u001b[0m TKN which need to be considered when using a 50/50 split\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "\n", - "print('***\\nInitial LP\\n***')\n", - "lp.info()\n", - "\n", - "s_in = 100\n", - "alpha = 0.5\n", - "s_out = Swap().apply(lp, eth, user_nm, alpha*s_in)\n", - "\n", - "print('***\\nLP post step 1\\n***')\n", - "lp.info()\n", - " \n", - "balance0 = alpha*s_in\n", - "balance1 = lp.quote(balance0, lp.reserve0, lp.reserve1)\n", - "lp.add_liquidity(user_nm, balance0, balance1, balance0, balance1) \n", - "\n", - "print('***\\nLP post step 2\\n***')\n", - "lp.info() \n", - "\n", - "\n", - "print('Given {} initial ETH:'.format(s_in))\n", - "print(' (step 1) {} ETH must first get swapped for {} TKN'.format(alpha*s_in, s_out))\n", - "print(' (step 2) The received TKN gets deposited along with the remaining {} ETH'.format(balance0))\n", - "\n", - "print('\\nTotal deposited is {:.6f} + {:.6f} = {:.6f} ETH:'.format(alpha*s_in, balance0, alpha*s_in + balance0))\n", - "print('However, we lost {} TKN which need to be considered when using a 50/50 split'.format(colored(str(tkn_amount-lp.reserve1), 'red', attrs=['bold'])))" - ] - }, - { - "cell_type": "markdown", - "id": "88de0c9b-2f8f-40c1-937a-51fca2da214f", - "metadata": {}, - "source": [ - "### Let's now address this problem ..." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "3f1ef04b-fd23-4152-a192-72e67216e850", - "metadata": {}, - "outputs": [], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "\n", - "s_in = 100\n", - "alpha = 0.5\n", - "y = lp.reserve1\n", - "x = lp.reserve0" - ] - }, - { - "cell_type": "markdown", - "id": "3aabe39e-d397-4dae-86f9-69082cdd144f", - "metadata": {}, - "source": [ - "#### Plug above into equation (1), and see how many TKN we get when 50% of ETH is swapped in for step (1)" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "f9b4b136-71f1-4a01-a817-ceed7ec35d48", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "For 100 ETH, we get 4748.30 TKN with a 50% portion\n" - ] - } - ], - "source": [ - "s_out = (997*alpha*s_in*y)/(1000*x + 997*alpha*s_in)\n", - "print('For {} ETH, we get {:.2f} TKN with a 50% portion'.format(s_in, s_out))" - ] - }, - { - "cell_type": "markdown", - "id": "a3ce783e-8d67-49cb-a0c1-68e3fae8eee9", - "metadata": {}, - "source": [ - "#### Now, lets check how many ETH gets SwapDeposited in when 50% of ETH is swapped in for step (1)" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "id": "bb851bec-6a25-41ee-949c-7b1e160c5ea8", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Instead of 100 ETH, we get 102.34 ETH under a 50% portion\n" - ] - } - ], - "source": [ - "a1_out = alpha*s_in + s_out*(x + alpha*s_in)/(y - s_out)\n", - "print('Instead of {} ETH, we get {:.2f} ETH under a 50% portion'.format(s_in, a1_out))" - ] - }, - { - "cell_type": "markdown", - "id": "103b8dd4-80a1-4455-84ea-3275025a6141", - "metadata": {}, - "source": [ - "We can see that there is an imbalance in the system under a 50% distribution for step (1); \n", - "* we need to solve the system above for $\\alpha$ to get the proper distribution\n", - "* plug (1) into (2) and we get:\n", - "\n", - "$\\Delta x = \\alpha\\Delta x + \\left(\\frac{997 y\\alpha\\Delta x}{1000x + 997\\alpha\\Delta x} \\right) \\left(\\frac{ x + \\alpha\\Delta x}{y - \\frac{997 y \\alpha \\Delta x}{1000x + 997\\alpha\\Delta x}} \\right)$\n", - "\n", - "reduces to: \n", - "\n", - "$\\alpha^2 \\frac{997 \\Delta x^2}{1000x} + \\alpha\\frac{1997\\Delta x}{1000} - \\Delta x = 0$\n", - "\n", - "#### Now, solve for $\\alpha$, and we can calculate the correct distribution using ```calc_deposit_dist``` " - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "5c4f50bd-a286-4f3e-94e2-ad1ce20d6ed8", - "metadata": {}, - "outputs": [], - "source": [ - "def calc_deposit_portion(lp, token_in, dx):\n", - " \n", - " if(token_in.token_name == lp.token0):\n", - " tkn_supply = lp.reserve0\n", - " else: \n", - " tkn_supply = lp.reserve1\n", - " \n", - " a = 997*(dx**2)/(1000*tkn_supply)\n", - " b = dx*(1997/1000)\n", - " c = -dx\n", - "\n", - " alpha = -(b - math.sqrt(b*b - 4*a*c)) / (2*a)\n", - " return alpha " - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "id": "3d7a0bdb-6bf1-4589-8bba-94ce022155ba", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "The correct swap distrbution (for step 1) is 0.4888217399419355\n" - ] - } - ], - "source": [ - "alpha = calc_deposit_portion(lp, eth, s_in)\n", - "print('The correct swap distrbution (for step 1) is {}'.format(alpha))" - ] - }, - { - "cell_type": "markdown", - "id": "0333ae75-ab5a-4302-8103-c6e5e1024755", - "metadata": {}, - "source": [ - "#### Now, check against our reduced quadratic, and we should expect to get 0" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "id": "b55b12cf-4e4a-4ca5-9cd5-6ad3a6f79369", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "-5.684341886080802e-14" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "997*(alpha**2)*(s_in**2)/(1000*x) + alpha*s_in*(1997/1000) - s_in" - ] - }, - { - "cell_type": "markdown", - "id": "d3739ee7-2541-488b-a070-bbbeae216a4c", - "metadata": {}, - "source": [ - "#### Finally, lets run through the steps to a ```SwapDeposit``` and compare above" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "id": "5459e4b6-5ff9-47c0-9276-91ebe20fe5b3", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "***\n", - "Initial LP\n", - "***\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "***\n", - "LP post step 1\n", - "***\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1048.8821739941936 | TKN = 95352.92490856306\n", - "Liquidity: 10000.0 \n", - "\n", - "***\n", - "LP post step 2\n", - "***\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1100.0 | TKN = 100000.0\n", - "Liquidity: 10487.35527472211 \n", - "\n", - "Given 100 initial ETH:\n", - " (step 1) 48.88217399419355 ETH must first get swapped for 4647.075091436944 TKN\n", - " (step 2) The received TKN gets deposited along with the remaining 51.11782600580638 ETH\n", - "\n", - "Total deposited is 48.882174 + 51.117826 = 100.000000 ETH:\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "\n", - "print('***\\nInitial LP\\n***')\n", - "lp.info()\n", - "\n", - "s_in = 100\n", - "alpha = calc_deposit_portion(lp, eth, s_in)\n", - "s_out = Swap().apply(lp, eth, user_nm, alpha*s_in)\n", - "\n", - "print('***\\nLP post step 1\\n***')\n", - "lp.info()\n", - " \n", - "balance1 = s_out\n", - "balance0 = lp.quote(s_out, lp.reserve1, lp.reserve0)\n", - "lp.add_liquidity(user_nm, balance0, balance1, balance0, balance1) \n", - "\n", - "print('***\\nLP post step 2\\n***')\n", - "lp.info() \n", - "\n", - "\n", - "print('Given {} initial ETH:'.format(s_in))\n", - "print(' (step 1) {} ETH must first get swapped for {} TKN'.format(alpha*s_in, s_out))\n", - "print(' (step 2) The received TKN gets deposited along with the remaining {} ETH'.format(balance0))\n", - "\n", - "print('\\nTotal deposited is {:.6f} + {:.6f} = {:.6f} ETH:'.format(alpha*s_in, balance0, alpha*s_in + balance0))" - ] - }, - { - "cell_type": "markdown", - "id": "bfb02a28-21ee-451e-8085-a6d021744fd2", - "metadata": {}, - "source": [ - "#### Let's check the opposing token" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "id": "d03bc54b-e95d-4325-b008-e7888a3094e1", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "***\n", - "Initial LP\n", - "***\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "***\n", - "LP post step 1\n", - "***\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 995.0446086357448 | TKN = 100499.50547221022\n", - "Liquidity: 10000.0 \n", - "\n", - "***\n", - "LP post step 2\n", - "***\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000.0 | TKN = 101000.0\n", - "Liquidity: 10049.80069557936 \n", - "\n", - "Given 1000 initial ETH:\n", - " (step 1) 499.5054722102269 ETH must first get swapped for 4.955391364255177 TKN\n", - " (step 2) The received TKN gets deposited along with the remaining 4.955391364255177 ETH\n", - "\n", - "Total deposited is 499.505472 + 500.494528 = 1000.000000 TKN:\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "\n", - "print('***\\nInitial LP\\n***')\n", - "lp.info()\n", - "\n", - "s_in = 1000\n", - "alpha = calc_deposit_portion(lp, tkn, s_in)\n", - "s_out = Swap().apply(lp, tkn, user_nm, alpha*s_in)\n", - "\n", - "print('***\\nLP post step 1\\n***')\n", - "lp.info()\n", - " \n", - "balance1 = lp.quote(s_out, lp.reserve0, lp.reserve1) \n", - "balance0 = s_out\n", - "lp.add_liquidity(user_nm, balance0, balance1, balance0, balance1) \n", - "\n", - "print('***\\nLP post step 2\\n***')\n", - "lp.info() \n", - "\n", - "\n", - "print('Given {} initial ETH:'.format(s_in))\n", - "print(' (step 1) {} ETH must first get swapped for {} TKN'.format(alpha*s_in, s_out))\n", - "print(' (step 2) The received TKN gets deposited along with the remaining {} ETH'.format(balance0))\n", - "\n", - "print('\\nTotal deposited is {:.6f} + {:.6f} = {:.6f} TKN:'.format(alpha*s_in, balance1, alpha*s_in + balance1))" - ] - }, - { - "cell_type": "markdown", - "id": "7c209dd5-e720-48c0-8f8c-f147a23a1e91", - "metadata": {}, - "source": [ - "#### Finally, let's check when our solution is integrated into ```SwapDeposit```" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "id": "1a759f47-2c3e-4c91-82bf-f338e0bc0b3e", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1100.0 | TKN = 100000.0\n", - "Liquidity: 10487.35527472211 \n", - "\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp.info()\n", - "\n", - "s_in = 100\n", - "dep = SwapDeposit().apply(lp, eth, user_nm, s_in)\n", - "lp.info()" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "id": "54e4120a-fcbb-4015-9d2d-6f922dbd1581", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp.info()" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "id": "79e56404-365c-4388-8192-33517d91d6e0", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000.0 | TKN = 100100.0\n", - "Liquidity: 10004.991241237401 \n", - "\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000.0 | TKN = 99999.99999999997\n", - "Liquidity: 9999.984988734976 \n", - "\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp.info()\n", - "\n", - "dep = SwapDeposit().apply(lp, tkn, user_nm, 100)\n", - "lp.info()\n", - "\n", - "out = WithdrawSwap().apply(lp, tkn, user_nm, 100)\n", - "lp.info()" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "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.9.7" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/notebooks/tutorials/.ipynb_checkpoints/liqleak-checkpoint.ipynb b/notebooks/tutorials/.ipynb_checkpoints/liqleak-checkpoint.ipynb deleted file mode 100644 index 2be4a58..0000000 --- a/notebooks/tutorials/.ipynb_checkpoints/liqleak-checkpoint.ipynb +++ /dev/null @@ -1,413 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "bbadac2a-bf42-4db1-8acb-ad918c7c89f5", - "metadata": {}, - "source": [ - "## Test for Liquidity Leaks" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "id": "79912f6d-1d23-4c62-9dfe-1d0ead235941", - "metadata": {}, - "outputs": [], - "source": [ - "import os\n", - "import copy\n", - "import numpy as np\n", - "import pandas as pd\n", - "import time\n", - "import datetime\n", - "import math\n", - "import matplotlib.pyplot as plt\n", - "cwd = os.getcwd().replace(\"notebooks/tutorials\",\"\")\n", - "os.chdir(cwd)" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "b45c9861-544e-4bef-ad6e-dafd21eb3460", - "metadata": {}, - "outputs": [], - "source": [ - "from uniswappy.math.model import TokenDeltaModel\n", - "from uniswappy.math.model import EventSelectionModel\n", - "from uniswappy.cpt.factory import Factory\n", - "from uniswappy.cpt.exchg import Exchange\n", - "from uniswappy.erc import ERC20\n", - "from uniswappy.process.deposit import SwapDeposit\n", - "from uniswappy.process.swap import WithdrawSwap\n", - "from uniswappy.process.liquidity import RemoveLiquidity\n", - "from uniswappy.process.liquidity import AddLiquidity\n", - "from uniswappy.process.swap import Swap\n", - "from uniswappy.cpt.quote import LPQuote" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "26e771c9-c5f2-4dde-8d7b-4564813f089f", - "metadata": {}, - "outputs": [], - "source": [ - "user_nm = 'user0'\n", - "dai_amount = 10000\n", - "sys_amount = 100000" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "0de99129-fc47-48cc-b85d-e9f5c3f0ad52", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 1000 | DAI = 1000\n", - "Liquidity: 1000.0 \n", - "\n", - "lp tokens: 1000.000000 sqrt(k): 1000.000000\n", - "\n", - "-----------------------------------------\n", - "1 LP token is worth 1.996006 DAI\n", - "-----------------------------------------\n", - "\n", - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 900.0 | DAI = 900.0\n", - "Liquidity: 900.0 \n", - "\n", - "(sub liquidity) lp tokens: 900.000000 sqrt(k) 900.000000\n", - "\n", - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 1000.0 | DAI = 1000.0\n", - "Liquidity: 1000.0 \n", - "\n", - "(add liquidity) lp tokens: 1000.000000 sqrt(k): 1000.000000\n", - "-----------------------------------------\n", - "\n", - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 900.0 | DAI = 900.0\n", - "Liquidity: 900.0 \n", - "\n", - "(sub liquidity) lp tokens: 900.000000 sqrt(k) 900.000000\n", - "\n", - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 1000.0 | DAI = 1000.0\n", - "Liquidity: 1000.0 \n", - "\n", - "(add liquidity) lp tokens: 1000.000000 sqrt(k): 1000.000000\n", - "-----------------------------------------\n", - "\n", - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 900.0 | DAI = 900.0\n", - "Liquidity: 900.0 \n", - "\n", - "(sub liquidity) lp tokens: 900.000000 sqrt(k) 900.000000\n", - "\n", - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 1000.0 | DAI = 1000.0\n", - "Liquidity: 1000.0 \n", - "\n", - "(add liquidity) lp tokens: 1000.000000 sqrt(k): 1000.000000\n", - "-----------------------------------------\n", - "\n", - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 900.0 | DAI = 900.0\n", - "Liquidity: 900.0 \n", - "\n", - "(sub liquidity) lp tokens: 900.000000 sqrt(k) 900.000000\n", - "\n", - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 1000.0 | DAI = 1000.0\n", - "Liquidity: 1000.0 \n", - "\n", - "(add liquidity) lp tokens: 1000.000000 sqrt(k): 1000.000000\n", - "-----------------------------------------\n", - "\n", - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 900.0 | DAI = 900.0\n", - "Liquidity: 900.0 \n", - "\n", - "(sub liquidity) lp tokens: 900.000000 sqrt(k) 900.000000\n", - "\n", - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 1000.0 | DAI = 1000.0\n", - "Liquidity: 1000.0 \n", - "\n", - "(add liquidity) lp tokens: 1000.000000 sqrt(k): 1000.000000\n", - "-----------------------------------------\n", - "\n", - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 900.0 | DAI = 900.0\n", - "Liquidity: 900.0 \n", - "\n", - "(sub liquidity) lp tokens: 900.000000 sqrt(k) 900.000000\n", - "\n", - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 1000.0 | DAI = 1000.0\n", - "Liquidity: 1000.0 \n", - "\n", - "(add liquidity) lp tokens: 1000.000000 sqrt(k): 1000.000000\n", - "-----------------------------------------\n", - "\n", - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 900.0 | DAI = 900.0\n", - "Liquidity: 900.0 \n", - "\n", - "(sub liquidity) lp tokens: 900.000000 sqrt(k) 900.000000\n", - "\n", - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 1000.0 | DAI = 1000.0\n", - "Liquidity: 1000.0 \n", - "\n", - "(add liquidity) lp tokens: 1000.000000 sqrt(k): 1000.000000\n", - "-----------------------------------------\n", - "\n", - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 900.0 | DAI = 900.0\n", - "Liquidity: 900.0 \n", - "\n", - "(sub liquidity) lp tokens: 900.000000 sqrt(k) 900.000000\n", - "\n", - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 1000.0 | DAI = 1000.0\n", - "Liquidity: 1000.0 \n", - "\n", - "(add liquidity) lp tokens: 1000.000000 sqrt(k): 1000.000000\n", - "-----------------------------------------\n", - "\n", - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 900.0 | DAI = 900.0\n", - "Liquidity: 900.0 \n", - "\n", - "(sub liquidity) lp tokens: 900.000000 sqrt(k) 900.000000\n", - "\n", - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 1000.0 | DAI = 1000.0\n", - "Liquidity: 1000.0 \n", - "\n", - "(add liquidity) lp tokens: 1000.000000 sqrt(k): 1000.000000\n", - "-----------------------------------------\n", - "\n", - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 900.0 | DAI = 900.0\n", - "Liquidity: 900.0 \n", - "\n", - "(sub liquidity) lp tokens: 900.000000 sqrt(k) 900.000000\n", - "\n", - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 1000.0 | DAI = 1000.0\n", - "Liquidity: 1000.0 \n", - "\n", - "(add liquidity) lp tokens: 1000.000000 sqrt(k): 1000.000000\n", - "-----------------------------------------\n", - "\n", - "-----------------------------------------\n", - "1 LP token is worth 1.996006 DAI\n", - "-----------------------------------------\n", - "\n" - ] - } - ], - "source": [ - "dai1 = ERC20(\"DAI\", \"0x111\")\n", - "sys1 = ERC20(\"SYS\", \"0x09\")\n", - "\n", - "factory = Factory(\"SYS pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(sys1, dai1, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, 1000, 1000, 1000, 1000)\n", - "lp.info()\n", - "print('lp tokens: {:.6f} sqrt(k): {:.6f}\\n'.format(lp.total_supply, np.sqrt(lp.reserve0*lp.reserve1)))\n", - "\n", - "amt = 100\n", - "\n", - "print('-----------------------------------------')\n", - "print('1 LP token is worth {:.6f} DAI'.format(LPQuote(False).get_amount_from_lp(lp, dai1, 1)))\n", - "print('-----------------------------------------\\n')\n", - "\n", - "for k in range(10):\n", - " \n", - " RemoveLiquidity().apply(lp, dai1, user_nm, amt) \n", - " lp.info()\n", - " print('(sub liquidity) lp tokens: {:.6f} sqrt(k) {:.6f}\\n'.format(lp.total_supply, np.sqrt(lp.reserve0*lp.reserve1))) \n", - " \n", - " AddLiquidity().apply(lp, dai1, user_nm, amt) \n", - " lp.info()\n", - " print('(add liquidity) lp tokens: {:.6f} sqrt(k): {:.6f}'.format(lp.total_supply, np.sqrt(lp.reserve0*lp.reserve1)))\n", - "\n", - "\n", - " print('-----------------------------------------\\n')\n", - " \n", - "print('-----------------------------------------')\n", - "print('1 LP token is worth {:.6f} DAI'.format(LPQuote(False).get_amount_from_lp(lp, dai1, 1)))\n", - "print('-----------------------------------------\\n') " - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "5e4b406c-d670-443b-83fa-c88c126f96b6", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 100000 | DAI = 10000\n", - "Liquidity: 31622.776601683792 \n", - "\n" - ] - } - ], - "source": [ - "dai1 = ERC20(\"DAI\", \"0x111\")\n", - "sys1 = ERC20(\"SYS\", \"0x09\")\n", - "\n", - "\n", - "factory = Factory(\"SYS pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(sys1, dai1, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, sys_amount, dai_amount, sys_amount, dai_amount)\n", - "lp.info()" - ] - }, - { - "cell_type": "markdown", - "id": "bb14717d-649b-4ccf-8f86-e720c567e2b9", - "metadata": {}, - "source": [ - "### Liquidity leak test-batch #1" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "fe0eba62-61bd-45a6-9666-de5c96f7a4d9", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 100000 | DAI = 10000\n", - "Liquidity: 31622.776601683792 \n", - "\n", - "Pre-tests:\n", - "- 1 LP token is 0.631497 DAI\n", - "- we are starting with 31622.776602 LP token\n" - ] - } - ], - "source": [ - "lp.info()\n", - "dai_amt = LPQuote(False).get_amount_from_lp(lp, dai1, 1)\n", - "print('Pre-tests:')\n", - "print('- 1 LP token is {:.6f} DAI'.format(dai_amt))\n", - "print('- we are starting with {:.6f} LP token'.format(lp.liquidity_providers[user_nm]))" - ] - }, - { - "cell_type": "markdown", - "id": "7ffba19e-41b1-45db-9b3c-5dfc3aae6b5a", - "metadata": {}, - "source": [ - "#### Test 1: AddLiquidity / RemoveLiquidity" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "id": "55985bb0-0282-4c7f-8b77-9f5ddf4f31b8", - "metadata": {}, - "outputs": [], - "source": [ - "for k in range(10):\n", - " vault_lp1_amt = 2000\n", - " dai_amount1 = LPQuote(True).get_amount(lp, dai1, vault_lp1_amt) \n", - " AddLiquidity().apply(lp, dai1, user_nm, vault_lp1_amt) \n", - " RemoveLiquidity().apply(lp, dai1, user_nm, abs(vault_lp1_amt)) " - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "155cd1c8-7359-4399-a95f-89ed7cd16ea1", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 100000.0 | DAI = 10000.0\n", - "Liquidity: 31622.776601683792 \n", - "\n", - "Post test:\n", - "- 1 LP token is 0.631497 DAI\n", - "- we have 31622.776602 LP token remaining\n" - ] - } - ], - "source": [ - "lp.info()\n", - "dai_amt = LPQuote(False).get_amount_from_lp(lp, dai1, 1)\n", - "print('Post test:')\n", - "print('- 1 LP token is {:.6f} DAI'.format(dai_amt))\n", - "print('- we have {:.6f} LP token remaining'.format(lp.liquidity_providers[user_nm]))" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "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.9.7" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/notebooks/tutorials/.ipynb_checkpoints/liquidity-checkpoint.ipynb b/notebooks/tutorials/.ipynb_checkpoints/liquidity-checkpoint.ipynb deleted file mode 100644 index cc894df..0000000 --- a/notebooks/tutorials/.ipynb_checkpoints/liquidity-checkpoint.ipynb +++ /dev/null @@ -1,360 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "1810cab6-3b3c-4c72-94da-a66ab8e42bb8", - "metadata": {}, - "source": [ - "## Adding / Removing Liquidity" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "id": "1e790717-d489-461d-bd90-3f154dcad138", - "metadata": {}, - "outputs": [], - "source": [ - "import os\n", - "import copy\n", - "import numpy as np\n", - "import pandas as pd\n", - "import time\n", - "import datetime\n", - "import math\n", - "import matplotlib.pyplot as plt\n", - "cwd = os.getcwd().replace(\"notebooks/tutorials\",\"\")\n", - "os.chdir(cwd)" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "9e1efcbc-f52d-4c8a-a4e1-9d2b40680a15", - "metadata": {}, - "outputs": [], - "source": [ - "from uniswappy.math.model import TokenDeltaModel\n", - "from uniswappy.math.model import EventSelectionModel\n", - "from uniswappy.cpt.factory import Factory\n", - "from uniswappy.cpt.exchg import Exchange\n", - "from uniswappy.erc import ERC20\n", - "from uniswappy.process.deposit import SwapDeposit\n", - "from uniswappy.process.swap import WithdrawSwap\n", - "from uniswappy.process.liquidity import RemoveLiquidity\n", - "from uniswappy.process.liquidity import AddLiquidity\n", - "from uniswappy.process.swap import Swap\n", - "from uniswappy.cpt.quote import LPQuote" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "26c71fd0-d596-4254-befc-c08df6bcf9a5", - "metadata": {}, - "outputs": [], - "source": [ - "user_nm = 'user0'\n", - "eth_amount = 1000\n", - "tkn_amount = 100000" - ] - }, - { - "cell_type": "markdown", - "id": "0d1f8e7c-4cd8-4425-8045-42f3eaa03ce9", - "metadata": {}, - "source": [ - "**AddLiquidity()**: add LP based on eth " - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "2fd42006-249a-4ef2-a986-dde545e5aee3", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1010 | TKN = 101000.0\n", - "Liquidity: 10100.0 \n", - "\n" - ] - }, - { - "data": { - "text/plain": [ - "1010" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp.info()\n", - "\n", - "liq = AddLiquidity().apply(lp, eth, user_nm, 10)\n", - "lp.info()\n", - "\n", - "lp.reserve0" - ] - }, - { - "cell_type": "markdown", - "id": "00770ce4-f309-4b66-89d0-a87e265ffa3d", - "metadata": {}, - "source": [ - "**AddLiquidity()**: add LP based on tkn " - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "98276d82-c205-4da5-bec4-99762dea129d", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 999.999999999993 | TKN = 100000.0\n", - "Liquidity: 9992.547380633905 \n", - "\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp.info()\n", - "\n", - "liq_tot = 0\n", - "N = 50\n", - "for k in range(0,N):\n", - " dep = SwapDeposit().apply(lp, eth, user_nm, 10)\n", - " WithdrawSwap().apply(lp, eth, user_nm, 10) \n", - " \n", - "lp.info()" - ] - }, - { - "cell_type": "markdown", - "id": "0213077b-5715-4c8a-aa2d-b3a6c99ae6ac", - "metadata": {}, - "source": [ - "**RemoveLiquidity()**: remove LP based on eth " - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "f8ad7e1c-9e42-4e88-baed-975e1a09a8e6", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 990.0 | TKN = 99000.0\n", - "Liquidity: 9900.0 \n", - "\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp.info()\n", - "\n", - "liq = RemoveLiquidity().apply(lp, eth, user_nm, 10)\n", - "lp.info()" - ] - }, - { - "cell_type": "markdown", - "id": "215ead0a-7cb1-46c0-9aab-67e80f7a3c1f", - "metadata": {}, - "source": [ - "**RemoveLiquidity(tkn)**: remove LP based on eth " - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "id": "628dc800-226b-42d9-911d-50748c717f49", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n" - ] - }, - { - "data": { - "text/plain": [ - "10000.0" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp.info()\n", - "\n", - "lp.total_supply" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "518c22d1-7c97-4b3b-bcd9-805811ddb79c", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 990.0 | TKN = 99000.0\n", - "Liquidity: 9900.0 \n", - "\n" - ] - }, - { - "data": { - "text/plain": [ - "9900.0" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "liq = RemoveLiquidity().apply(lp, tkn, user_nm, 1000)\n", - "lp.info()\n", - "\n", - "lp.total_supply" - ] - }, - { - "cell_type": "markdown", - "id": "4cb68aea-eaae-4581-a6b3-59cfe6dd1da7", - "metadata": {}, - "source": [ - "**RemoveLiquidity(tkn)**: remove LP based on eth " - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "id": "3f0407b9-b7b4-4f48-a7c9-63282504fe5b", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1030.0 | TKN = 103000.0\n", - "Liquidity: 10300.0 \n", - "\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp.info()\n", - "\n", - "liq = AddLiquidity().apply(lp, tkn, user_nm, 1000)\n", - "#liq = RemoveLiquidity().apply(lp, tkn, user_nm, 1000)\n", - "liq = AddLiquidity().apply(lp, tkn, user_nm, 1000)\n", - "#liq = RemoveLiquidity().apply(lp, tkn, user_nm, 1000)\n", - "liq = AddLiquidity().apply(lp, tkn, user_nm, 1000)\n", - "#liq = RemoveLiquidity().apply(lp, tkn, user_nm, 1000)\n", - "lp.info()" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "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.9.7" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/notebooks/tutorials/.ipynb_checkpoints/pairingcode-checkpoint.ipynb b/notebooks/tutorials/.ipynb_checkpoints/pairingcode-checkpoint.ipynb deleted file mode 100644 index 45ec036..0000000 --- a/notebooks/tutorials/.ipynb_checkpoints/pairingcode-checkpoint.ipynb +++ /dev/null @@ -1,1124 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "8ee9b46b-75c6-4f77-b113-98766534604c", - "metadata": {}, - "source": [ - "## Pairing Code Abstractions\n", - "\n", - "**Swap()**: swaps X for Y (and vice verse)
\n", - "**AddLiquidity()**: adds liquidity using only X or Y amounts
\n", - "**RemoveLiquidity()**: removes liquidity using only X or Y amounts
\n", - "**SwapDeposit()**: deposit desired token -> perform approx. 50% swap -> perform approx. 50/50 deposit (exact percentages are calculated)
\n", - "**WithdrawSwap()**: perform approx. 50/50 withdraw -> swap remaining approx. 50% -> return desired token (exact percentages are calculated)
" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "id": "744eb40c-fdbb-4a30-9a4d-22b6e12f86ff", - "metadata": {}, - "outputs": [], - "source": [ - "import os\n", - "import copy\n", - "import numpy as np\n", - "import pandas as pd\n", - "import time\n", - "import datetime\n", - "import math\n", - "import matplotlib.pyplot as plt\n", - "cwd = os.getcwd().replace(\"notebooks/tutorials\",\"\")\n", - "os.chdir(cwd)" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "440f92b5-615b-4550-83d6-a3e8c2875184", - "metadata": {}, - "outputs": [], - "source": [ - "from uniswappy.math.model import TokenDeltaModel\n", - "from uniswappy.math.model import EventSelectionModel\n", - "from uniswappy.cpt.factory import Factory\n", - "from uniswappy.cpt.exchg import Exchange\n", - "from uniswappy.erc import ERC20\n", - "from uniswappy.process.deposit import SwapDeposit\n", - "from uniswappy.process.swap import WithdrawSwap\n", - "from uniswappy.process.liquidity import RemoveLiquidity\n", - "from uniswappy.process.liquidity import AddLiquidity\n", - "from uniswappy.process.swap import Swap\n", - "from uniswappy.cpt.quote import LPQuote" - ] - }, - { - "cell_type": "markdown", - "id": "620d431e-1409-4270-bd18-ebb2b2cc7318", - "metadata": {}, - "source": [ - "### Setup pool" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "1e68a541-b6c0-4733-949d-d6ac123f2b72", - "metadata": {}, - "outputs": [], - "source": [ - "user_nm = 'user0'\n", - "eth_amount = 1000\n", - "tkn_amount = 100000" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "c1de04eb-8852-430d-9273-8c6833cddd7d", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(\"user0\", eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp.info()" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "e6b8acd3-31ad-4926-aea3-98752f139dcd", - "metadata": {}, - "outputs": [], - "source": [ - "tDel = TokenDeltaModel(50)\n", - "ev = EventSelectionModel()" - ] - }, - { - "cell_type": "markdown", - "id": "4d1a3d37-a5b7-42c9-84f6-35a049bf4826", - "metadata": {}, - "source": [ - "**Swap():** swap tkn for eth" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "b1d1f2c2-a3c5-40dc-956b-bc6390fd7789", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 990.1284196560293 | TKN = 101000\n", - "Liquidity: 10000.0 \n", - "\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp.info()\n", - "\n", - "out = Swap().apply(lp, tkn, user_nm, 1000)\n", - "lp.info()" - ] - }, - { - "cell_type": "markdown", - "id": "865bc323-fd14-42b5-9cfd-26d923a39f23", - "metadata": {}, - "source": [ - "**Swap():** swap eth for tkn" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "id": "901298ea-eb06-479d-b05f-711da5d80331", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1010 | TKN = 99012.84196560294\n", - "Liquidity: 10000.0 \n", - "\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp.info()\n", - "\n", - "out = Swap().apply(lp, eth, user_nm, 10)\n", - "lp.info()" - ] - }, - { - "cell_type": "markdown", - "id": "1f6e9045-2637-4a29-8577-95d07ba9343e", - "metadata": {}, - "source": [ - "**AddLiquidity()**: add LP based on eth " - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "1fd83152-c93e-491d-89bf-e780ba079dd5", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1010 | TKN = 101000.0\n", - "Liquidity: 10100.0 \n", - "\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp.info()\n", - "\n", - "AddLiquidity().apply(lp, eth, user_nm, 10)\n", - "lp.info()" - ] - }, - { - "cell_type": "markdown", - "id": "7667ae3b-93a9-4253-948e-27653c7852bf", - "metadata": {}, - "source": [ - "**AddLiquidity()**: add LP based on tkn " - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "id": "f65c0db1-f95f-4274-930f-e2633066605c", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1010.0 | TKN = 101000.0\n", - "Liquidity: 10100.0 \n", - "\n" - ] - }, - { - "data": { - "text/plain": [ - "{'0': 1e-15, 'user0': 10100.0}" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp.info()\n", - "\n", - "AddLiquidity().apply(lp, tkn, user_nm, 1000)\n", - "lp.info()\n", - "lp.liquidity_providers" - ] - }, - { - "cell_type": "markdown", - "id": "a05d867f-0e2b-43dc-9a22-23b650290c74", - "metadata": {}, - "source": [ - "**RemoveLiquidity()**: remove LP based on eth " - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "id": "2300eb80-153d-4538-a6f8-dd34839f42c2", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1.0 | TKN = 100.0\n", - "Liquidity: 10.0 \n", - "\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp.info()\n", - "\n", - "RemoveLiquidity().apply(lp, eth, user_nm, 999)\n", - "lp.info()" - ] - }, - { - "cell_type": "markdown", - "id": "33e01ddc-4751-4db3-95c3-0f4b76286cdf", - "metadata": {}, - "source": [ - "**RemoveLiquidity()**: remove LP based on tkn " - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "id": "5cdf6d7b-049d-42cc-b815-ba5978112c83", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 999.0 | TKN = 99900.0\n", - "Liquidity: 9990.0 \n", - "\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp.info()\n", - "\n", - "RemoveLiquidity().apply(lp, tkn, user_nm, 100)\n", - "lp.info()" - ] - }, - { - "cell_type": "markdown", - "id": "a244262b-a3d9-4096-9fdc-73d964c59c4d", - "metadata": {}, - "source": [ - "**SwapDeposit()**: deposit LP with only tkn\n", - "* deposit desired token -> perform 50% swap -> perform 50/50 deposit" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "id": "dd65eee3-b8d0-4b83-8c04-cf99ea55fcc4", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000.0 | TKN = 100100.0\n", - "Liquidity: 10004.991241237401 \n", - "\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp.info()\n", - "\n", - "SwapDeposit().apply(lp, tkn, user_nm, 100)\n", - "lp.info() " - ] - }, - { - "cell_type": "markdown", - "id": "ab3beefc-0392-4ea4-a6f2-6151eda3ea5e", - "metadata": {}, - "source": [ - "**SwapDeposit()**: deposit LP with only eth\n", - "* deposit desired token -> perform 50% swap -> perform 50/50 deposit" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "id": "e411ba09-3688-4998-94b4-c0453f4700d9", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1001.0000000000001 | TKN = 100000.0\n", - "Liquidity: 10004.991241237401 \n", - "\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp.info()\n", - "\n", - "amount_out = SwapDeposit().apply(lp, eth, user_nm, 1)\n", - "lp.info()" - ] - }, - { - "cell_type": "markdown", - "id": "b28f8cfb-5073-4314-9341-17b30bde3886", - "metadata": {}, - "source": [ - "**WithdrawSwap()**: withdraw LP based upon expected amount of eth\n", - "* perform 50/50 withdraw -> swap remaining 50% -> return desired token" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "id": "90fa05cd-f102-4ccc-bf7f-38cfb6d90fe6", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 998.9999999999999 | TKN = 100000.0\n", - "Liquidity: 9994.99123998928 \n", - "\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp.info()\n", - "\n", - "expected_amount_out = WithdrawSwap().apply(lp, eth, user_nm, 1)\n", - "lp.info()" - ] - }, - { - "cell_type": "markdown", - "id": "2a60bf10-f474-4224-b02b-127dcd77b1d6", - "metadata": {}, - "source": [ - "**WithdrawSwap()**: withdraw LP based upon expected amount of tkn" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "id": "e6f8a1eb-d1ea-45bd-a6fa-30f206f5c48b", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000.0 | TKN = 99899.99999999999\n", - "Liquidity: 9994.99123998928 \n", - "\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp.info()\n", - "\n", - "expected_amount_out = WithdrawSwap().apply(lp, tkn, user_nm, 100)\n", - "lp.info()" - ] - }, - { - "cell_type": "markdown", - "id": "1763fba6-925b-4ee9-8588-ab62b39450eb", - "metadata": {}, - "source": [ - "**Swap():** swap lp_tkn for eth" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "id": "66550ca8-0edf-4569-b95b-1e33e833c0f6", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP1)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "Exchange ETHTKN-LP/TKN (LP2)\n", - "Coins: ETHTKN-LP/TKN\n", - "Reserves: ETHTKN-LP = 10000.0 | TKN = 100000\n", - "Liquidity: 31622.776601683792 \n", - "\n", - "Exchange ETHTKN-LP/TKN (LP2)\n", - "Coins: ETHTKN-LP/TKN\n", - "Reserves: ETHTKN-LP = 10100.0 | TKN = 99012.84196560294\n", - "Liquidity: 31622.776601683792 \n", - "\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp_tkn = factory.create_exchange(eth, tkn, symbol=\"LP1\", address=\"0x011\")\n", - "lp_tkn.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp_tkn.info()\n", - "\n", - "tkn2 = ERC20(\"TKN\", \"0x112\")\n", - "lp_tkn_amount = lp_tkn.total_supply\n", - "lp2 = factory.create_exchange(lp_tkn, tkn2, symbol=\"LP2\", address=\"0x012\")\n", - "lp2.add_liquidity(user_nm, lp_tkn_amount, tkn_amount, lp_tkn_amount, tkn_amount)\n", - "lp2.info()\n", - "\n", - "out = Swap().apply(lp2, lp_tkn, user_nm, 100)\n", - "lp2.info()" - ] - }, - { - "cell_type": "markdown", - "id": "cdb1eb2c-0c6d-4589-ac53-59c5bf5734ab", - "metadata": {}, - "source": [ - "**Swap():** swap eth for lp_tkn" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "id": "d695eca9-bf36-4de7-86ce-1a928acfce54", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP1)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "Exchange ETHTKN-LP/TKN (LP2)\n", - "Coins: ETHTKN-LP/TKN\n", - "Reserves: ETHTKN-LP = 10000.0 | TKN = 100000\n", - "Liquidity: 31622.776601683792 \n", - "\n", - "Exchange ETHTKN-LP/TKN (LP2)\n", - "Coins: ETHTKN-LP/TKN\n", - "Reserves: ETHTKN-LP = 9990.039930189601 | TKN = 100100\n", - "Liquidity: 31622.776601683792 \n", - "\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp_tkn = factory.create_exchange(eth, tkn, symbol=\"LP1\", address=\"0x011\")\n", - "lp_tkn.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp_tkn.info()\n", - "\n", - "tkn2 = ERC20(\"TKN\", \"0x112\")\n", - "lp_tkn_amount = lp_tkn.total_supply\n", - "lp2 = factory.create_exchange(lp_tkn, tkn2, symbol=\"LP2\", address=\"0x012\")\n", - "lp2.add_liquidity(user_nm, lp_tkn_amount, tkn_amount, lp_tkn_amount, tkn_amount)\n", - "lp2.info()\n", - "\n", - "out = Swap().apply(lp2, tkn2, user_nm, 100)\n", - "lp2.info()" - ] - }, - { - "cell_type": "markdown", - "id": "66039ec9-86e7-49ec-9e0a-519e395d6418", - "metadata": {}, - "source": [ - "**AddLiquidity()**: add LP based on tkn2 " - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "id": "b06a9541-c36a-4d94-b401-8a075bf601e7", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP1)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "Exchange ETHTKN-LP/TKN (LP2)\n", - "Coins: ETHTKN-LP/TKN\n", - "Reserves: ETHTKN-LP = 10000.0 | TKN = 100000\n", - "Liquidity: 31622.776601683792 \n", - "\n", - "Exchange ETHTKN-LP/TKN (LP2)\n", - "Coins: ETHTKN-LP/TKN\n", - "Reserves: ETHTKN-LP = 10001.0 | TKN = 100010.0\n", - "Liquidity: 31625.93887934396 \n", - "\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp_tkn = factory.create_exchange(eth, tkn, symbol=\"LP1\", address=\"0x011\")\n", - "lp_tkn.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp_tkn.info()\n", - "\n", - "tkn2 = ERC20(\"TKN\", \"0x112\")\n", - "lp_tkn_amount = lp_tkn.total_supply\n", - "lp2 = factory.create_exchange(lp_tkn, tkn2, symbol=\"LP2\", address=\"0x012\")\n", - "lp2.add_liquidity(user_nm, lp_tkn_amount, tkn_amount, lp_tkn_amount, tkn_amount)\n", - "lp2.info()\n", - "\n", - "AddLiquidity().apply(lp2, tkn2, user_nm, 10)\n", - "lp2.info()" - ] - }, - { - "cell_type": "markdown", - "id": "ac2b121d-71df-478f-86c3-72d24764a182", - "metadata": {}, - "source": [ - "**AddLiquidity()**: add LP based on lp_tkn " - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "id": "abf5c45d-5691-40cf-a7ac-bbb202c4f871", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP1)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "Exchange ETHTKN-LP/TKN (LP2)\n", - "Coins: ETHTKN-LP/TKN\n", - "Reserves: ETHTKN-LP = 10000.0 | TKN = 100000\n", - "Liquidity: 31622.776601683792 \n", - "\n", - "Exchange ETHTKN-LP/TKN (LP2)\n", - "Coins: ETHTKN-LP/TKN\n", - "Reserves: ETHTKN-LP = 10010.0 | TKN = 100100.0\n", - "Liquidity: 31654.399378285478 \n", - "\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp_tkn = factory.create_exchange(eth, tkn, symbol=\"LP1\", address=\"0x011\")\n", - "lp_tkn.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp_tkn.info()\n", - "\n", - "tkn2 = ERC20(\"TKN\", \"0x112\")\n", - "lp_tkn_amount = lp_tkn.total_supply\n", - "lp2 = factory.create_exchange(lp_tkn, tkn2, symbol=\"LP2\", address=\"0x012\")\n", - "lp2.add_liquidity(user_nm, lp_tkn_amount, tkn_amount, lp_tkn_amount, tkn_amount)\n", - "lp2.info()\n", - "\n", - "AddLiquidity().apply(lp2, lp_tkn, user_nm, 10)\n", - "lp2.info()" - ] - }, - { - "cell_type": "markdown", - "id": "6b934c73-9ed2-4d6b-9cff-66704cbb89d7", - "metadata": {}, - "source": [ - "**RemoveLiquidity()**: remove LP based on tkn2 " - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "id": "0f626ff4-5432-4e50-8728-f41cc3d7d45a", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP1)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "Exchange ETHTKN-LP/TKN (LP2)\n", - "Coins: ETHTKN-LP/TKN\n", - "Reserves: ETHTKN-LP = 1000 | TKN = 1000\n", - "Liquidity: 1000.0 \n", - "\n", - "Exchange ETHTKN-LP/TKN (LP2)\n", - "Coins: ETHTKN-LP/TKN\n", - "Reserves: ETHTKN-LP = 990.0 | TKN = 990.0\n", - "Liquidity: 990.0 \n", - "\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp_tkn = factory.create_exchange(eth, tkn, symbol=\"LP1\", address=\"0x011\")\n", - "lp_tkn.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp_tkn.info()\n", - "\n", - "tkn2 = ERC20(\"TKN\", \"0x112\")\n", - "lp_tkn_amount = 1000\n", - "lp2 = factory.create_exchange(lp_tkn, tkn2, symbol=\"LP2\", address=\"0x012\")\n", - "lp2.add_liquidity(user_nm, eth_amount, lp_tkn_amount, eth_amount, lp_tkn_amount)\n", - "lp2.info()\n", - "\n", - "RemoveLiquidity().apply(lp2, tkn2, user_nm, 10)\n", - "lp2.info()" - ] - }, - { - "cell_type": "markdown", - "id": "4fe1350d-2795-4bb3-98a6-fb819d2bef0e", - "metadata": {}, - "source": [ - "**RemoveLiquidity(lp_tkn)**: remove LP based on lp_tkn " - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "id": "7999b861-8ee1-474f-a201-ffa3565191ca", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP1)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "Exchange ETHTKN-LP/TKN (LP2)\n", - "Coins: ETHTKN-LP/TKN\n", - "Reserves: ETHTKN-LP = 1000 | TKN = 1000\n", - "Liquidity: 1000.0 \n", - "\n", - "Exchange ETHTKN-LP/TKN (LP2)\n", - "Coins: ETHTKN-LP/TKN\n", - "Reserves: ETHTKN-LP = 990.0 | TKN = 990.0\n", - "Liquidity: 990.0 \n", - "\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp_tkn = factory.create_exchange(eth, tkn, symbol=\"LP1\", address=\"0x011\")\n", - "lp_tkn.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp_tkn.info()\n", - "\n", - "tkn2 = ERC20(\"TKN\", \"0x112\")\n", - "lp_tkn_amount = 1000\n", - "lp2 = factory.create_exchange(lp_tkn, tkn2, symbol=\"LP2\", address=\"0x012\")\n", - "lp2.add_liquidity(user_nm, eth_amount, lp_tkn_amount, eth_amount, lp_tkn_amount)\n", - "lp2.info()\n", - "\n", - "RemoveLiquidity().apply(lp2, lp_tkn, user_nm, 10)\n", - "lp2.info()" - ] - }, - { - "cell_type": "markdown", - "id": "fa24c849-7137-49cf-96ac-c50859bcf4dc", - "metadata": {}, - "source": [ - "**SwapDeposit()**: deposit LP with only tkn2\n", - "* deposit desired token -> perform 50% swap -> perform 50/50 deposit" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "id": "652ef16a-50d3-400e-9f15-acca965f94e0", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP1)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "Exchange ETHTKN-LP/TKN (LP2)\n", - "Coins: ETHTKN-LP/TKN\n", - "Reserves: ETHTKN-LP = 1000 | TKN = 1000\n", - "Liquidity: 1000.0 \n", - "\n", - "Exchange ETHTKN-LP/TKN (LP2)\n", - "Coins: ETHTKN-LP/TKN\n", - "Reserves: ETHTKN-LP = 1000.0 | TKN = 1009.9999999999998\n", - "Liquidity: 1004.9800695579358 \n", - "\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp_tkn = factory.create_exchange(eth, tkn, symbol=\"LP1\", address=\"0x011\")\n", - "lp_tkn.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp_tkn.info()\n", - "\n", - "tkn2 = ERC20(\"TKN\", \"0x112\")\n", - "lp_tkn_amount = 1000\n", - "lp2 = factory.create_exchange(lp_tkn, tkn2, symbol=\"LP2\", address=\"0x012\")\n", - "lp2.add_liquidity(user_nm, eth_amount, lp_tkn_amount, eth_amount, lp_tkn_amount)\n", - "lp2.info()\n", - "\n", - "SwapDeposit().apply(lp2, tkn2, user_nm, 10)\n", - "lp2.info() " - ] - }, - { - "cell_type": "markdown", - "id": "33201379-82f8-49fb-ad60-42f1d4977cf1", - "metadata": {}, - "source": [ - "**SwapDeposit()**: deposit LP with only lp_tkn\n", - "* deposit desired token -> perform 50% swap -> perform 50/50 deposit" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "id": "f27e9a94-1d19-4d6a-82f4-dbf1caa30fcc", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP1)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "Exchange ETHTKN-LP/TKN (LP2)\n", - "Coins: ETHTKN-LP/TKN\n", - "Reserves: ETHTKN-LP = 1000 | TKN = 1000\n", - "Liquidity: 1000.0 \n", - "\n", - "Exchange ETHTKN-LP/TKN (LP2)\n", - "Coins: ETHTKN-LP/TKN\n", - "Reserves: ETHTKN-LP = 1009.9999999999998 | TKN = 1000.0\n", - "Liquidity: 1004.9800695579358 \n", - "\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp_tkn = factory.create_exchange(eth, tkn, symbol=\"LP1\", address=\"0x011\")\n", - "lp_tkn.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp_tkn.info()\n", - "\n", - "tkn2 = ERC20(\"TKN\", \"0x112\")\n", - "lp_tkn_amount = 1000\n", - "lp2 = factory.create_exchange(lp_tkn, tkn2, symbol=\"LP2\", address=\"0x012\")\n", - "lp2.add_liquidity(user_nm, eth_amount, lp_tkn_amount, eth_amount, lp_tkn_amount)\n", - "lp2.info()\n", - "\n", - "SwapDeposit().apply(lp2, lp_tkn, user_nm, 10)\n", - "lp2.info() " - ] - }, - { - "cell_type": "markdown", - "id": "f0f7c60d-b4ea-4568-aafd-ad232bf6edbb", - "metadata": {}, - "source": [ - "**WithdrawSwap()**: withdraw LP based upon expected amount of tkn2\n", - "* perform 50/50 withdraw -> swap remaining 50% -> return desired token" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "id": "8fc42ffe-053b-4394-8cc4-e5f2a420549c", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP1)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "Exchange ETHTKN-LP/TKN (LP2)\n", - "Coins: ETHTKN-LP/TKN\n", - "Reserves: ETHTKN-LP = 1000 | TKN = 1000\n", - "Liquidity: 1000.0 \n", - "\n", - "Exchange ETHTKN-LP/TKN (LP2)\n", - "Coins: ETHTKN-LP/TKN\n", - "Reserves: ETHTKN-LP = 1000.0 | TKN = 989.9999999999998\n", - "Liquidity: 994.9799447405355 \n", - "\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp_tkn = factory.create_exchange(eth, tkn, symbol=\"LP1\", address=\"0x011\")\n", - "lp_tkn.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp_tkn.info()\n", - "\n", - "tkn2 = ERC20(\"TKN\", \"0x112\")\n", - "lp_tkn_amount = 1000\n", - "lp2 = factory.create_exchange(lp_tkn, tkn2, symbol=\"LP2\", address=\"0x012\")\n", - "lp2.add_liquidity(user_nm, eth_amount, lp_tkn_amount, eth_amount, lp_tkn_amount)\n", - "lp2.info()\n", - "\n", - "WithdrawSwap().apply(lp2, tkn2, user_nm, 10)\n", - "lp2.info() " - ] - }, - { - "cell_type": "markdown", - "id": "715e626c-33c2-4f4d-8533-b114b960b30b", - "metadata": {}, - "source": [ - "**WithdrawSwap()**: withdraw LP based upon expected amount of tkn2\n", - "* perform 50/50 withdraw -> swap remaining 50% -> return desired token" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "id": "20f30955-2980-4dd4-bedf-efaa1611b87f", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP1)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "Exchange ETHTKN-LP/TKN (LP2)\n", - "Coins: ETHTKN-LP/TKN\n", - "Reserves: ETHTKN-LP = 1000 | TKN = 1000\n", - "Liquidity: 1000.0 \n", - "\n", - "Exchange ETHTKN-LP/TKN (LP2)\n", - "Coins: ETHTKN-LP/TKN\n", - "Reserves: ETHTKN-LP = 989.9999999999998 | TKN = 1000.0\n", - "Liquidity: 994.9799447405355 \n", - "\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp_tkn = factory.create_exchange(eth, tkn, symbol=\"LP1\", address=\"0x011\")\n", - "lp_tkn.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp_tkn.info()\n", - "\n", - "tkn2 = ERC20(\"TKN\", \"0x112\")\n", - "lp_tkn_amount = 1000\n", - "lp2 = factory.create_exchange(lp_tkn, tkn2, symbol=\"LP2\", address=\"0x012\")\n", - "lp2.add_liquidity(user_nm, eth_amount, lp_tkn_amount, eth_amount, lp_tkn_amount)\n", - "lp2.info()\n", - "\n", - "WithdrawSwap().apply(lp2, lp_tkn, user_nm, 10)\n", - "lp2.info() " - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "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.9.7" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/notebooks/tutorials/.ipynb_checkpoints/revenue-checkpoint.ipynb b/notebooks/tutorials/.ipynb_checkpoints/revenue-checkpoint.ipynb deleted file mode 100644 index 3708ff0..0000000 --- a/notebooks/tutorials/.ipynb_checkpoints/revenue-checkpoint.ipynb +++ /dev/null @@ -1,297 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "da027ba5-8b74-432b-984f-5849abf4b2af", - "metadata": {}, - "source": [ - "## Revenue Generation in Uniswap" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "id": "79912f6d-1d23-4c62-9dfe-1d0ead235941", - "metadata": {}, - "outputs": [], - "source": [ - "import os\n", - "import copy\n", - "import numpy as np\n", - "import pandas as pd\n", - "import time\n", - "import datetime\n", - "import math\n", - "import matplotlib.pyplot as plt\n", - "cwd = os.getcwd().replace(\"notebooks/tutorials\",\"\")\n", - "os.chdir(cwd)" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "b45c9861-544e-4bef-ad6e-dafd21eb3460", - "metadata": {}, - "outputs": [], - "source": [ - "from uniswappy.math.model import TokenDeltaModel\n", - "from uniswappy.math.model import EventSelectionModel\n", - "from uniswappy.cpt.factory import Factory\n", - "from uniswappy.cpt.exchg import Exchange\n", - "from uniswappy.erc import ERC20\n", - "from uniswappy.process.deposit import SwapDeposit\n", - "from uniswappy.process.swap import WithdrawSwap\n", - "from uniswappy.process.liquidity import RemoveLiquidity\n", - "from uniswappy.process.liquidity import AddLiquidity\n", - "from uniswappy.process.swap import Swap\n", - "from uniswappy.cpt.quote import LPQuote\n", - "from uniswappy.cpt.index import RebaseIndexToken" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "26e771c9-c5f2-4dde-8d7b-4564813f089f", - "metadata": {}, - "outputs": [], - "source": [ - "user_nm = 'user0'\n", - "dai_amount = 10000\n", - "sys_amount = 100000" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "5e4b406c-d670-443b-83fa-c88c126f96b6", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 100000 | DAI = 10000\n", - "Liquidity: 31622.776601683792 \n", - "\n" - ] - } - ], - "source": [ - "dai1 = ERC20(\"DAI\", \"0x111\")\n", - "sys1 = ERC20(\"SYS\", \"0x09\")\n", - "\n", - "factory = Factory(\"SYS pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(sys1, dai1, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, sys_amount, dai_amount, sys_amount, dai_amount)\n", - "lp.info()\n" - ] - }, - { - "cell_type": "markdown", - "id": "8ceefaf9-07bc-4813-878c-55702670b09d", - "metadata": {}, - "source": [ - "### Liquidity leak test-batch #1" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "fe0eba62-61bd-45a6-9666-de5c96f7a4d9", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 100000 | DAI = 10000\n", - "Liquidity: 31622.776601683792 \n", - "\n", - "Pre-tests:\n", - "- 1 LP token is 0.631497 DAI\n", - "- we are starting with 31622.776602 LP token\n" - ] - } - ], - "source": [ - "lp.info()\n", - "dai_amt = LPQuote(False).get_amount_from_lp(lp, dai1, 1)\n", - "print('Pre-tests:')\n", - "print('- 1 LP token is {:.6f} DAI'.format(dai_amt))\n", - "print('- we are starting with {:.6f} LP token'.format(lp.liquidity_providers[user_nm]))" - ] - }, - { - "cell_type": "markdown", - "id": "ad518477-9813-461d-8424-a89b3afd9a44", - "metadata": {}, - "source": [ - "#### Test 1: SwapDeposit" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "47eb9b9c-df4f-4b13-93d0-d8390031f271", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 103132.68283987256 | DAI = 10000.0\n", - "Liquidity: 32113.539290307595 \n", - "\n" - ] - } - ], - "source": [ - "requested_liquidity_in = 500\n", - "sys_before_test = lp.reserve0\n", - "sys_settlement_amt = RebaseIndexToken().apply(lp, sys1, requested_liquidity_in) \n", - "dep = SwapDeposit().apply(lp, sys1, user_nm, sys_settlement_amt)\n", - "get_deposit = LPQuote(False).get_amount_from_lp(lp, sys1, lp.last_liquidity_deposit)\n", - "lp.info()" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "id": "d8081086-ea6c-470b-99b7-e56413991602", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 100009.18348322519 | DAI = 10000.0\n", - "Liquidity: 31622.776601683796 \n", - "\n" - ] - } - ], - "source": [ - "out = WithdrawSwap().apply(lp, sys1, user_nm, get_deposit)\n", - "lp.info()" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "42337d8a-3314-475a-83b9-926cb1145e55", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 100009.18348322519 | DAI = 10000.0\n", - "Liquidity: 31622.776601683796 \n", - "\n", - "Post test #1:\n", - "- 1 LP token is 0.631497 DAI\n", - "- we have 31622.776602 total LP token\n", - "- we made a profit of 9.183483 SYS post swapping\n" - ] - } - ], - "source": [ - "lp.info()\n", - "dai_amt = LPQuote(False).get_amount_from_lp(lp, dai1, 1)\n", - "sys_after_test = lp.reserve0\n", - "print('Post test #1:')\n", - "print('- 1 LP token is {:.6f} DAI'.format(dai_amt))\n", - "print('- we have {:.6f} total LP token'.format(lp.liquidity_providers[user_nm]))\n", - "print('- we made a profit of {:.6f} SYS post swapping'.format(sys_after_test-sys_before_test))" - ] - }, - { - "cell_type": "markdown", - "id": "f1e64cb2-ceb7-4e48-b158-8bc0b50bc333", - "metadata": {}, - "source": [ - "#### Test 2: Multiple WithdrawSwap / SwapDeposit" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "id": "2f166ece-0322-435c-aa16-78c67a9df853", - "metadata": {}, - "outputs": [], - "source": [ - "sys_before_test = lp.reserve0\n", - "N = 100\n", - "for k in range(N):\n", - " requested_liquidity_out = 500\n", - " sys_settlement_amt = RebaseIndexToken().apply(lp, sys1, requested_liquidity_in) \n", - " dep = SwapDeposit().apply(lp, sys1, user_nm, sys_settlement_amt)\n", - " get_deposit = LPQuote(False).get_amount_from_lp(lp, sys1, lp.last_liquidity_deposit)\n", - " out = WithdrawSwap().apply(lp, sys1, user_nm, get_deposit)" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "id": "3916ff5e-f1f3-4be3-8247-c63cb82e6868", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange SYS/DAI (LP)\n", - "Coins: SYS/DAI\n", - "Reserves: SYS = 100931.8037282928 | DAI = 10000.0\n", - "Liquidity: 31622.776601683832 \n", - "\n", - "Post test #2:\n", - "- 1 LP token is 0.631497 DAI\n", - "- we have 31622.776602 LP token remaining\n", - "- we made a profit of 922.620245 SYS post swapping\n" - ] - } - ], - "source": [ - "lp.info()\n", - "dai_amt = LPQuote(False).get_amount_from_lp(lp, dai1, 1)\n", - "sys_after_test = lp.reserve0\n", - "print('Post test #2:')\n", - "print('- 1 LP token is {:.6f} DAI'.format(dai_amt))\n", - "print('- we have {:.6f} LP token remaining'.format(lp.liquidity_providers[user_nm]))\n", - "print('- we made a profit of {:.6f} SYS post swapping'.format(sys_after_test-sys_before_test))" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "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.9.7" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/notebooks/tutorials/.ipynb_checkpoints/swap-checkpoint.ipynb b/notebooks/tutorials/.ipynb_checkpoints/swap-checkpoint.ipynb deleted file mode 100644 index 2cd806b..0000000 --- a/notebooks/tutorials/.ipynb_checkpoints/swap-checkpoint.ipynb +++ /dev/null @@ -1,177 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "075b7dc2-ad4e-4ecd-85c7-3cb9e228eac5", - "metadata": {}, - "source": [ - "## Swap() abstraction" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "id": "606231ed-0cd6-4e16-8cb1-fdb006d45c78", - "metadata": {}, - "outputs": [], - "source": [ - "import os\n", - "import copy\n", - "import numpy as np\n", - "import pandas as pd\n", - "import time\n", - "import datetime\n", - "import math\n", - "import matplotlib.pyplot as plt\n", - "cwd = os.getcwd().replace(\"notebooks/tutorials\",\"\")\n", - "os.chdir(cwd)" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "e7af61ea-c200-47a0-9966-413844057de7", - "metadata": {}, - "outputs": [], - "source": [ - "from uniswappy.math.model import TokenDeltaModel\n", - "from uniswappy.math.model import EventSelectionModel\n", - "from uniswappy.cpt.factory import Factory\n", - "from uniswappy.cpt.exchg import Exchange\n", - "from uniswappy.erc import ERC20\n", - "from uniswappy.process.deposit import SwapDeposit\n", - "from uniswappy.process.swap import WithdrawSwap\n", - "from uniswappy.process.liquidity import RemoveLiquidity\n", - "from uniswappy.process.liquidity import AddLiquidity\n", - "from uniswappy.process.swap import Swap\n", - "from uniswappy.cpt.quote import LPQuote" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "b184fd54-c4ed-4989-a8ec-9624937e70d2", - "metadata": {}, - "outputs": [], - "source": [ - "user_nm = 'user0'\n", - "eth_amount = 1000\n", - "tkn_amount = 100000" - ] - }, - { - "cell_type": "markdown", - "id": "e9b294af-bdc8-4b37-bdac-7d792245a98d", - "metadata": {}, - "source": [ - "**swap(tkn):** swap tkn for eth" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "f63d8e75-81ca-457c-a594-7c824d8879af", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1001 | TKN = 99900.399301896\n", - "Liquidity: 10000.0 \n", - "\n", - "0.9979970000000001\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp.info()\n", - "\n", - "out = Swap().apply(lp, eth, user_nm, 1)\n", - "lp.info()\n", - "\n", - "print((out * lp.reserve0) / lp.reserve1)" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "f808ce37-83f5-4edd-a888-01c82c19480a", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000 | TKN = 100000\n", - "Liquidity: 10000.0 \n", - "\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1010 | TKN = 99012.84196560294\n", - "Liquidity: 10000.0 \n", - "\n" - ] - }, - { - "data": { - "text/plain": [ - "987.1580343970613" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, eth_amount, tkn_amount, eth_amount, tkn_amount)\n", - "lp.info()\n", - "\n", - "out = Swap().apply(lp, eth, user_nm, 10)\n", - "lp.info()\n", - "\n", - "out" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "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.9.7" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/notebooks/tutorials/.ipynb_checkpoints/withdraw-checkpoint.ipynb b/notebooks/tutorials/.ipynb_checkpoints/withdraw-checkpoint.ipynb deleted file mode 100644 index 5519d36..0000000 --- a/notebooks/tutorials/.ipynb_checkpoints/withdraw-checkpoint.ipynb +++ /dev/null @@ -1,518 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "c3d183f5-fa7a-49e7-ac7c-b206c7736461", - "metadata": {}, - "source": [ - "## Deep dive into WithdrawSwap()" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "id": "d00b574d-495a-4fa9-8ebc-f6bb916d33e6", - "metadata": {}, - "outputs": [], - "source": [ - "import os\n", - "import copy\n", - "import numpy as np\n", - "import pandas as pd\n", - "import time\n", - "import datetime\n", - "import math\n", - "import matplotlib.pyplot as plt\n", - "cwd = os.getcwd().replace(\"notebooks/tutorials\",\"\")\n", - "os.chdir(cwd)" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "0f7401d3-7183-462f-9a56-8617b6027368", - "metadata": {}, - "outputs": [], - "source": [ - "from uniswappy.math.model import TokenDeltaModel\n", - "from uniswappy.math.model import EventSelectionModel\n", - "from uniswappy.cpt.factory import Factory\n", - "from uniswappy.cpt.exchg import Exchange\n", - "from uniswappy.erc import ERC20\n", - "from uniswappy.process.deposit import SwapDeposit\n", - "from uniswappy.process.swap import WithdrawSwap\n", - "from uniswappy.process.liquidity import RemoveLiquidity\n", - "from uniswappy.process.liquidity import AddLiquidity\n", - "from uniswappy.process.swap import Swap\n", - "from uniswappy.cpt.quote import LPQuote" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "e0f346d8-5662-4128-a81f-940e8ab4c96c", - "metadata": {}, - "outputs": [], - "source": [ - "user_nm = 'user0'\n", - "eth_amount = 1000\n", - "tkn_amount = 100000" - ] - }, - { - "cell_type": "markdown", - "id": "919f04b6-38ac-4f34-8846-1c5166de1ae7", - "metadata": {}, - "source": [ - "#### WithdrawSwap (incl. fee):\n", - "* A ```WithdrawSwap``` is where a certain amount of a specific token is withdraw from LP under one operation\n", - "* Includes two steps:\n", - " * (step 1) split token amount into two parts and perform 50/50 withdraw on one of the parts to receive tknA and tknB\n", - " * (step 2) swap remaining ~50% of tknB (from step 1) to receive only tknA\n", - "* To ensure all the funds are withdrawn, we must determine the portion ($\\alpha$) that requesed token amount is split into " - ] - }, - { - "cell_type": "markdown", - "id": "4a5af836-fe38-41ed-a12e-4962763a9835", - "metadata": {}, - "source": [ - "### Indexing problem (defined)\n", - "\n", - "Given the definition of **_constant product trading_** (CPT) as:\n", - "\n", - "> $(x-\\Delta x)(y - \\gamma\\Delta y) = L^2$\n", - "\n", - "where \n", - "* $x$ -> reserve0 (r0)\n", - "* $y$ -> reserve1 (r1)\n", - "* $\\Delta x$ -> swap x (a0)\n", - "* $\\Delta y$ -> swap y (a1)\n", - "* $L$ -> total supply\n", - "* $\\gamma$ -> fee $\\left(ie, \\frac{997}{1000} \\right)$\n", - "\n", - "We define the **_indexing problem_** via the following linear system of equations:\n", - "\n", - "> (Eq. 1) $\\Delta x = \\frac{\\Delta L x}{L}$\n", - "\n", - "> (Eq. 2) $\\Delta y = \\frac{\\Delta L y}{L}$\n", - "\n", - "> (Eq. 3) $\\Delta y_{(i)} = \\Delta y + \\frac{\\gamma \\Delta x(y-\\Delta y)}{(x - \\Delta x) + \\gamma \\Delta x}$\n", - "\n", - "where \n", - "* $\\Delta y_{(i)}$ -> indexed token\n", - "* $\\Delta L$ -> liquidity deposit\n", - "\n", - "_The above system serves the indexing component to the DAOSYS tokenization platform_" - ] - }, - { - "cell_type": "markdown", - "id": "8f9763d8-16ae-4ae1-9d7e-08292e5ad955", - "metadata": {}, - "source": [ - "#### Let's highlight why the **_indexing problem_** is important ..." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "d18160f3-757e-43fc-8c49-f5031549d428", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "***\n", - "Initial LP\n", - "***\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1100 | TKN = 100000\n", - "Liquidity: 10488.088481701516 \n", - "\n", - "***\n", - "LP post step 1\n", - "***\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1050.0 | TKN = 95454.54545454546\n", - "Liquidity: 10011.35718707872 \n", - "\n", - "***\n", - "LP post step 2\n", - "***\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1002.4094194662908 | TKN = 100000.0\n", - "Liquidity: 10011.35718707872 \n", - "\n", - "Total withdrawn is 50.000000 + 47.590581 = 97.590581 ETH\n", - "Of the requested 100 ETH, a total of 97.590581 ETH has been withdrawn when using a 50/50 split\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, eth_amount+100, tkn_amount, eth_amount+100, tkn_amount)\n", - "\n", - "print('***\\nInitial LP\\n***')\n", - "lp.info()\n", - "\n", - "amt_out = 100\n", - "token_out = eth\n", - "trading_token = tkn\n", - "user_nm1 = 'user0'\n", - "#rate = 0\n", - "\n", - "# Step 1: withdrawal\n", - "p_out = 0.5\n", - "removeLiq = RemoveLiquidity()\n", - "res = removeLiq.apply(lp, token_out, user_nm1, p_out*amt_out)\n", - "\n", - "print('***\\nLP post step 1\\n***')\n", - "lp.info()\n", - "\n", - "# Step 2: swap\n", - "out = Swap().apply(lp, trading_token, user_nm1, res[trading_token.token_name]) \n", - "\n", - "print('***\\nLP post step 2\\n***')\n", - "lp.info() \n", - "\n", - "withdrawn = res[eth.token_name] + out \n", - "\n", - "print('Total withdrawn is {:.6f} + {:.6f} = {:.6f} ETH'.format(p_out*amt_out, out, withdrawn))\n", - "print('Of the requested {} ETH, a total of {:.6f} ETH has been withdrawn when using a 50/50 split'.format(amt_out, withdrawn))" - ] - }, - { - "cell_type": "markdown", - "id": "92acb06f-fc5c-4c46-9cab-1e301e91b86c", - "metadata": {}, - "source": [ - "#### Let's now address the problem ...\n", - "Using the system of equations outlined in the **_indexing problem_**, Eq. 3 can be rearranged as:\n", - "> $(\\Delta y_{(i)}x) - (\\Delta y_{(i)}\\Delta x) + (\\gamma \\Delta y_{(i)} \\Delta x) - (\\Delta y x) + (\\Delta y\\Delta x) - (\\gamma y\\Delta x) = 0$\n", - "\n", - "Plug Eq. 1 and Eq. 2 into above, and we get:\n", - "> $(\\Delta y_{(i)} x) - (\\frac{\\Delta y_{(i)} \\Delta L x}{L}) + (\\frac{\\Delta y_{(i)} \\gamma \\Delta L x}{L}) - (\\frac{\\Delta L xy}{L}) + (\\frac{\\Delta L^2 xy}{L^2}) - (\\frac{\\Delta L \\gamma x y}{L}) = 0$\n", - "\n", - "The above equation gets reduced to the following quadratic:\n", - "> $\\Delta L^2 \\left( \\frac{xy}{L^2} \\right) - \\Delta L \\left(\\frac{1000 \\Delta y_{(i)} x - 997\\Delta y_{(i)} x + 1000xy + 997 xy}{1000L} \\right) + \\Delta y_{(i)} x = 0$\n", - "\n", - "#### Now, solve for $\\Delta L$ using ```calc_lp_settlement``` " - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "d8971261-6c53-434d-8b73-a4f409631cc0", - "metadata": {}, - "outputs": [], - "source": [ - "def calc_lp_settlement(lp, token_in, itkn_amt):\n", - "\n", - " if(token_in.token_name == lp.token1):\n", - " x = lp.reserve0\n", - " y = lp.reserve1\n", - " else: \n", - " x = lp.reserve1\n", - " y = lp.reserve0\n", - "\n", - " L = lp.total_supply\n", - " gamma = 997\n", - "\n", - " a1 = x*y/L\n", - " a2 = L\n", - " a = a1/a2\n", - " b = (1000*itkn_amt*x - itkn_amt*gamma*x + 1000*x*y + x*y*gamma)/(1000*L);\n", - " c = itkn_amt*x;\n", - "\n", - " dL = (b*a2 - a2*math.sqrt(b*b - 4*a1*c/a2)) / (2*a1);\n", - " return dL" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "1a260e48-c3e3-4520-9ce8-64e0e562b16f", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1100 | TKN = 100000\n", - "Liquidity: 10488.088481701516 \n", - "\n", - "A request of 100 ETH requires a settlement of 488.787567 LP token\n" - ] - } - ], - "source": [ - "eth = ERC20(\"ETH\", \"0x09\")\n", - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, eth_amount+100, tkn_amount, eth_amount+100, tkn_amount)\n", - "lp.info()\n", - "\n", - "eth_amt = 100\n", - "dL = calc_lp_settlement(lp, eth, eth_amt)\n", - "\n", - "print('A request of {} ETH requires a settlement of {:.6f} LP token'.format(eth_amt, dL))" - ] - }, - { - "cell_type": "markdown", - "id": "44aaa7ed-1205-4035-ba4f-c3253b27e81a", - "metadata": {}, - "source": [ - "#### Now, check against our reduced quadratic, and we should expect to get 0" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "id": "d0471d6e-8991-4411-9a87-2252568b5220", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "-5.587935447692871e-09" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "y = lp.reserve0\n", - "x = lp.reserve1\n", - "L = lp.total_supply\n", - "gamma = 997\n", - "\n", - "(dL**2)*x*y/(L*L) - dL*((1000*eth_amt*x - eth_amt*gamma*x + 1000*x*y + x*y*gamma)/(1000*L)) + eth_amt*x" - ] - }, - { - "cell_type": "markdown", - "id": "ac0a7f5f-a937-4533-9ed3-fe0db513f163", - "metadata": {}, - "source": [ - "#### Using $\\Delta L$, we can determine the splitting distribution for withdrawal\n", - "\n", - "Reconsidering Eq. 3, we redefine $\\Delta y$ and $\\Delta y_{swap}$ by portion $\\alpha$, thus:\n", - "\n", - "> $ \\Delta y_{(i)} = \\Delta y + \\Delta y_{swap} $\n", - "\n", - "> $ \\Delta y_{(i)} = \\alpha \\Delta y_{(i)} + (1- \\alpha) \\Delta y_{(i)} $ \n", - "\n", - "Therefore, using Eq. 2 we calculate our distribution as:\n", - "\n", - "> $\\alpha =\\frac{\\Delta y}{\\Delta y_{(i)}}= \\frac{\\Delta L y}{\\Delta y_{(i)} L}$\n", - "\n", - "Hence, using the above equation and $L$ from our solver, we can calculate the withdraw distribution $\\alpha$ via ```calc_portion```\n" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "7ca40dbd-ba1c-4c67-9f6c-7655b66c1bf4", - "metadata": {}, - "outputs": [], - "source": [ - "def calc_withdraw_portion(lp, token_in, amt):\n", - " \n", - " if(token_in.token_name == lp.token1):\n", - " x = lp.reserve0\n", - " y = lp.reserve1\n", - " else: \n", - " x = lp.reserve1\n", - " y = lp.reserve0\n", - " \n", - " L = lp.total_supply\n", - " gamma = 997/1000\n", - "\n", - " dL = calc_lp_settlement(lp, token_in, amt) \n", - " dx = dL*x/L\n", - " dy = dL*y/L\n", - " aswap = (gamma*dx)*(y-dy)/(x-dx+gamma*dx)\n", - "\n", - " return dy/amt " - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "id": "e4e695ee-bde3-46fd-a8ef-43a11993929c", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "The correct portion (for step 1) is 0.512645\n" - ] - } - ], - "source": [ - "alpha = calc_withdraw_portion(lp, eth, eth_amt)\n", - "print('The correct portion (for step 1) is {:.6f}'.format(alpha))" - ] - }, - { - "cell_type": "markdown", - "id": "06fb57f1-12c8-4b67-9d55-e97d5381886d", - "metadata": {}, - "source": [ - "#### Finally, lets run through the steps to a ```WithdrawSwap``` and compare above" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "id": "23399fc5-08a1-4430-b8d5-b0a0ee0935e9", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "***\n", - "Initial LP\n", - "***\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1100 | TKN = 100000\n", - "Liquidity: 10488.088481701516 \n", - "\n", - "***\n", - "LP post step 1\n", - "***\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1048.735527472211 | TKN = 95339.59340656463\n", - "Liquidity: 9999.300914574964 \n", - "\n", - "***\n", - "LP post step 2\n", - "***\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000.0 | TKN = 100000.0\n", - "Liquidity: 9999.300914574964 \n", - "\n", - "Total withdrawn is 51.264473 + 48.735527 = 100.000000 ETH\n", - "Of the requested 100 ETH, a total of 100.000000 ETH has been withdrawn\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, eth_amount+100, tkn_amount, eth_amount+100, tkn_amount)\n", - "\n", - "print('***\\nInitial LP\\n***')\n", - "lp.info()\n", - "\n", - "amt_out = 100\n", - "token_out = eth\n", - "user_nm = 'user0'\n", - "\n", - "# Step 1: withdrawal\n", - "p_out = calc_withdraw_portion(lp, token_out, amt_out)\n", - "removeLiq = RemoveLiquidity()\n", - "res = removeLiq.apply(lp, token_out, user_nm, p_out*amt_out)\n", - "\n", - "print('***\\nLP post step 1\\n***')\n", - "lp.info()\n", - "\n", - "# Step 2: swap\n", - "out = Swap().apply(lp, trading_token, user_nm, res[trading_token.token_name]) \n", - "\n", - "print('***\\nLP post step 2\\n***')\n", - "lp.info() \n", - "\n", - "withdrawn = res[eth.token_name] + out \n", - "\n", - "print('Total withdrawn is {:.6f} + {:.6f} = {:.6f} ETH'.format(p_out*amt_out, out, withdrawn))\n", - "print('Of the requested {} ETH, a total of {:.6f} ETH has been withdrawn'.format(amt_out, withdrawn))" - ] - }, - { - "cell_type": "markdown", - "id": "d6c9b05a-1e87-4c81-ae8e-7ef16b8ea399", - "metadata": {}, - "source": [ - "#### Finally, let's check when our solution is integrated into ```WithdrawSwap```" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "id": "3ea65167-15a2-4ea0-8c06-108eb1ca6bee", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1100 | TKN = 100000\n", - "Liquidity: 10488.088481701516 \n", - "\n", - "Exchange ETH/TKN (LP)\n", - "Coins: ETH/TKN\n", - "Reserves: ETH = 1000.0 | TKN = 100000.0\n", - "Liquidity: 9999.300914574964 \n", - "\n" - ] - } - ], - "source": [ - "tkn = ERC20(\"TKN\", \"0x111\")\n", - "eth = ERC20(\"ETH\", \"0x09\")\n", - "factory = Factory(\"ETH pool factory\", \"0x2\")\n", - "lp = factory.create_exchange(eth, tkn, symbol=\"LP\", address=\"0x011\")\n", - "lp.add_liquidity(user_nm, eth_amount+100, tkn_amount, eth_amount+100, tkn_amount)\n", - "lp.info()\n", - "\n", - "out = WithdrawSwap().apply(lp, eth, user_nm, 100)\n", - "lp.info() \n" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "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.9.7" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -}