From 94ffa9883c19dbcf4b608fa770e5d06652decaf4 Mon Sep 17 00:00:00 2001 From: icmoore Date: Sat, 30 Sep 2023 19:15:10 -0700 Subject: [PATCH] cleanup --- notebooks/tutorials/pairingcode.ipynb | 1124 ------------------------- 1 file changed, 1124 deletions(-) delete mode 100644 notebooks/tutorials/pairingcode.ipynb diff --git a/notebooks/tutorials/pairingcode.ipynb b/notebooks/tutorials/pairingcode.ipynb deleted file mode 100644 index 45ec036..0000000 --- a/notebooks/tutorials/pairingcode.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 -}