DeFiPy 1.0.0__tar.gz → 1.0.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- defipy-1.0.2/DeFiPy.egg-info/PKG-INFO +221 -0
- {defipy-1.0.0 → defipy-1.0.2}/DeFiPy.egg-info/SOURCES.txt +5 -1
- {defipy-1.0.0 → defipy-1.0.2}/DeFiPy.egg-info/requires.txt +1 -1
- defipy-1.0.2/PKG-INFO +221 -0
- defipy-1.0.2/README.md +205 -0
- {defipy-1.0.0 → defipy-1.0.2}/python/prod/__init__.py +4 -2
- defipy-1.0.2/python/prod/analytics/risk/__init__.py +1 -0
- defipy-1.0.2/python/prod/analytics/simulate/__init__.py +1 -0
- defipy-1.0.2/python/prod/utils/client/contract/ExecuteScript.py +43 -0
- defipy-1.0.2/python/prod/utils/client/contract/__init__.py +1 -0
- defipy-1.0.2/python/prod/utils/tools/UniswapScriptHelper.py +67 -0
- defipy-1.0.2/python/prod/utils/tools/__init__.py +2 -0
- {defipy-1.0.0 → defipy-1.0.2}/setup.py +5 -3
- defipy-1.0.0/DeFiPy.egg-info/PKG-INFO +0 -200
- defipy-1.0.0/PKG-INFO +0 -200
- defipy-1.0.0/README.md +0 -184
- defipy-1.0.0/python/prod/simulate/__init__.py +0 -1
- defipy-1.0.0/python/prod/utils/tools/__init__.py +0 -1
- {defipy-1.0.0 → defipy-1.0.2}/DeFiPy.egg-info/dependency_links.txt +0 -0
- {defipy-1.0.0 → defipy-1.0.2}/DeFiPy.egg-info/not-zip-safe +0 -0
- {defipy-1.0.0 → defipy-1.0.2}/DeFiPy.egg-info/top_level.txt +0 -0
- {defipy-1.0.0 → defipy-1.0.2}/LICENSE.txt +0 -0
- {defipy-1.0.0 → defipy-1.0.2}/python/prod/erc/__init__.py +0 -0
- {defipy-1.0.0 → defipy-1.0.2}/python/prod/math/basic/__init__.py +0 -0
- {defipy-1.0.0 → defipy-1.0.2}/python/prod/math/interest/__init__.py +0 -0
- {defipy-1.0.0 → defipy-1.0.2}/python/prod/math/interest/ips/__init__.py +0 -0
- {defipy-1.0.0 → defipy-1.0.2}/python/prod/math/interest/ips/aggregate/__init__.py +0 -0
- {defipy-1.0.0 → defipy-1.0.2}/python/prod/math/model/__init__.py +0 -0
- {defipy-1.0.0 → defipy-1.0.2}/python/prod/math/risk/__init__.py +0 -0
- {defipy-1.0.0 → defipy-1.0.2}/python/prod/process/__init__.py +0 -0
- {defipy-1.0.0 → defipy-1.0.2}/python/prod/process/burn/__init__.py +0 -0
- {defipy-1.0.0 → defipy-1.0.2}/python/prod/process/deposit/__init__.py +0 -0
- {defipy-1.0.0 → defipy-1.0.2}/python/prod/process/join/Join.py +0 -0
- {defipy-1.0.0 → defipy-1.0.2}/python/prod/process/join/__init__.py +0 -0
- {defipy-1.0.0 → defipy-1.0.2}/python/prod/process/liquidity/AddLiquidity.py +0 -0
- {defipy-1.0.0 → defipy-1.0.2}/python/prod/process/liquidity/RemoveLiquidity.py +0 -0
- {defipy-1.0.0 → defipy-1.0.2}/python/prod/process/liquidity/__init__.py +0 -0
- {defipy-1.0.0 → defipy-1.0.2}/python/prod/process/mint/__init__.py +0 -0
- {defipy-1.0.0 → defipy-1.0.2}/python/prod/process/swap/Swap.py +0 -0
- {defipy-1.0.0 → defipy-1.0.2}/python/prod/process/swap/__init__.py +0 -0
- {defipy-1.0.0 → defipy-1.0.2}/python/prod/utils/client/__init__.py +0 -0
- {defipy-1.0.0 → defipy-1.0.2}/python/prod/utils/data/__init__.py +0 -0
- {defipy-1.0.0 → defipy-1.0.2}/python/prod/utils/interfaces/__init__.py +0 -0
- {defipy-1.0.0 → defipy-1.0.2}/python/prod/utils/tools/v3/__init__.py +0 -0
- {defipy-1.0.0 → defipy-1.0.2}/setup.cfg +0 -0
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: DeFiPy
|
|
3
|
+
Version: 1.0.2
|
|
4
|
+
Summary: DeFi Analytics with Python
|
|
5
|
+
Home-page: http://github.com/defipy-devs/defipy
|
|
6
|
+
Author: icmoore
|
|
7
|
+
Author-email: defipy.devs@gmail.com
|
|
8
|
+
License: MIT
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE.txt
|
|
11
|
+
Requires-Dist: scipy>=1.7.3
|
|
12
|
+
Requires-Dist: bokeh==3.3.4
|
|
13
|
+
Requires-Dist: uniswappy==1.7.0
|
|
14
|
+
Requires-Dist: stableswappy==1.0.0
|
|
15
|
+
Requires-Dist: balancerpy==1.0.1
|
|
16
|
+
|
|
17
|
+
# DeFiPy: DeFi Analytics with Python (v 1.0.2)
|
|
18
|
+
|
|
19
|
+
Welcome to the worlds first DeFi Python package with all major protocols intergrated into one package! Implement your analytics in one package with DeFiPy. Since DeFiPy is built with a modular design in mind, your can also silo your analytics by protocol
|
|
20
|
+
using:
|
|
21
|
+
* [UniswapPy](https://github.com/defipy-devs/uniswappy)
|
|
22
|
+
* [BalancerPy](https://github.com/defipy-devs/balancerpy)
|
|
23
|
+
* [StableSwapPy](https://github.com/defipy-devs/stableswappy)
|
|
24
|
+
|
|
25
|
+
## Docs
|
|
26
|
+
Visit [DeFiPy docs](https://defipy.org) for full documentation with walk-through tutorials
|
|
27
|
+
|
|
28
|
+
## Install
|
|
29
|
+
Must first install gmpy2 python package to handle the precision within the StableSwap protocol (requires CPython 3.7 or above). To install the latest release with pip:
|
|
30
|
+
```
|
|
31
|
+
> pip install gmpy2
|
|
32
|
+
```
|
|
33
|
+
Also, in many cases will need to have required libraries (GMP, MPFR and MPC) already installed on your system, see [gmpy2 installation docs](https://gmpy2.readthedocs.io/en/latest/install.html) for more info. Once setup, install the latest release of DeFiPy with pip:
|
|
34
|
+
```
|
|
35
|
+
> git clone https://github.com/defipy-devs/defipy
|
|
36
|
+
> pip install .
|
|
37
|
+
```
|
|
38
|
+
or
|
|
39
|
+
```
|
|
40
|
+
> pip install defipy
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Uniswap V2 Example
|
|
44
|
+
--------------------------
|
|
45
|
+
|
|
46
|
+
To setup a liquidity pool, you must first create the tokens in the pair using the `ERC20` object. Next, create a liquidity pool (LP) factory using `IFactory` object. Once this is setup, an unlimited amount of LPs can be created; the procedures for such are as follows:
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
from defipy import *
|
|
50
|
+
|
|
51
|
+
# Step 1: Define tokens
|
|
52
|
+
tkn = ERC20("TKN", "0x111")
|
|
53
|
+
eth = ERC20("ETH", "0x999")
|
|
54
|
+
|
|
55
|
+
# Step 2: Initialize factory
|
|
56
|
+
factory = UniswapFactory("ETH pool factory", "0x2")
|
|
57
|
+
|
|
58
|
+
# Step 3: Set up exchange data for V2
|
|
59
|
+
exch_data = UniswapExchangeData(tkn0=eth, tkn1=tkn, symbol="LP", address="0x3")
|
|
60
|
+
|
|
61
|
+
# Step 4: Deploy pool
|
|
62
|
+
lp = factory.deploy(exch_data)
|
|
63
|
+
|
|
64
|
+
# Step 5: Add initial liquidity
|
|
65
|
+
join = Join()
|
|
66
|
+
join.apply(lp, "user", 1000, 10000)
|
|
67
|
+
|
|
68
|
+
# Step 6: Perform swap
|
|
69
|
+
swap = Swap()
|
|
70
|
+
out = swap.apply(lp, tkn, "user", 10)
|
|
71
|
+
|
|
72
|
+
# Check reserves and liquidity
|
|
73
|
+
lp.summary()
|
|
74
|
+
|
|
75
|
+
# OUTPUT:
|
|
76
|
+
Exchange ETH-TKN (LP)
|
|
77
|
+
Reserves: ETH = 999.00399301896, TKN = 10010.0
|
|
78
|
+
Liquidity: 3162.2776601683795
|
|
79
|
+
|
|
80
|
+
Uniswap V3 Example
|
|
81
|
+
--------------------------
|
|
82
|
+
|
|
83
|
+
from defipy import *
|
|
84
|
+
|
|
85
|
+
# Step 1: Define tokens and parameters
|
|
86
|
+
eth = ERC20("ETH", "0x93")
|
|
87
|
+
tkn = ERC20("TKN", "0x111")
|
|
88
|
+
tick_spacing = 60
|
|
89
|
+
fee = 3000 # 0.3% fee tier
|
|
90
|
+
|
|
91
|
+
# Step 2: Set up exchange data for V3
|
|
92
|
+
exch_data = UniswapExchangeData(tkn0=eth, tkn1=tkn, symbol="LP", address="0x811", version='V3', tick_spacing=tick_spacing, fee=fee)
|
|
93
|
+
|
|
94
|
+
# Step 3: Initialize factory
|
|
95
|
+
factory = UniswapFactory("ETH pool factory", "0x2")
|
|
96
|
+
|
|
97
|
+
# Step 4: Deploy pool
|
|
98
|
+
lp = factory.deploy(exch_data)
|
|
99
|
+
|
|
100
|
+
# Step 5: Add initial liquidity within tick range
|
|
101
|
+
lwr_tick = UniV3Utils.getMinTick(tick_spacing)
|
|
102
|
+
upr_tick = UniV3Utils.getMaxTick(tick_spacing)
|
|
103
|
+
join = Join()
|
|
104
|
+
join.apply(lp, "user", 1000, 10000, lwr_tick, upr_tick)
|
|
105
|
+
|
|
106
|
+
# Step 6: Perform swap
|
|
107
|
+
swap = Swap()
|
|
108
|
+
out = swap.apply(lp, tkn, "user", 10)
|
|
109
|
+
|
|
110
|
+
# Check reserves and liquidity
|
|
111
|
+
lp.summary()
|
|
112
|
+
|
|
113
|
+
# OUTPUT:
|
|
114
|
+
Exchange ETH-TKN (LP)
|
|
115
|
+
Real Reserves: ETH = 999.0039930189599, TKN = 10010.0
|
|
116
|
+
Gross Liquidity: 3162.277660168379
|
|
117
|
+
|
|
118
|
+
Balancer Example
|
|
119
|
+
--------------------------
|
|
120
|
+
|
|
121
|
+
from defipy import *
|
|
122
|
+
|
|
123
|
+
# Step 1: Define tokens
|
|
124
|
+
dai = ERC20("DAI", "0x111")
|
|
125
|
+
usdc = ERC20("USDC", "0x999")
|
|
126
|
+
|
|
127
|
+
# Step 2: Deposit token amounts
|
|
128
|
+
dai.deposit(None, 10000)
|
|
129
|
+
usdc.deposit(None, 20000)
|
|
130
|
+
|
|
131
|
+
# Step 3: Setup vault
|
|
132
|
+
vault = BalancerVault()
|
|
133
|
+
vault.add_token(dai, 10) # Denormalized weight for DAI
|
|
134
|
+
vault.add_token(usdc, 40) # Denormalized weight for WETH
|
|
135
|
+
|
|
136
|
+
# Step 4: Set up exchange data for Balancer
|
|
137
|
+
exch_data = BalancerExchangeData(vault=vault, symbol="BSP", address="0x3")
|
|
138
|
+
|
|
139
|
+
# Step 5: Initialize factor for Balancer
|
|
140
|
+
bfactory = BalancerFactory("WETH pool factory", "0x2")
|
|
141
|
+
|
|
142
|
+
# Step 6: Deploy pool
|
|
143
|
+
lp = bfactory.deploy(exch_data)
|
|
144
|
+
|
|
145
|
+
# Step 7: Join pool with initial liquidity
|
|
146
|
+
join = Join()
|
|
147
|
+
join.apply(lp, "user", 100) # Issue 100 pool shares
|
|
148
|
+
|
|
149
|
+
# Step 8: Perform swap
|
|
150
|
+
swap = Swap(Proc.SWAPIN)
|
|
151
|
+
out = swap.apply(lp, dai, usdc, "user", 10)
|
|
152
|
+
|
|
153
|
+
# Check reserves and liquidity
|
|
154
|
+
lp.summary()
|
|
155
|
+
|
|
156
|
+
#OUTPUT:
|
|
157
|
+
Balancer Exchange: DAI-USDC (BSP)
|
|
158
|
+
Reserves: DAI = 9979.92478694547, USDC = 20010
|
|
159
|
+
Weights: DAI = 0.2, USDC = 0.8
|
|
160
|
+
Pool Shares: 100
|
|
161
|
+
|
|
162
|
+
StableSwap Example
|
|
163
|
+
--------------------------
|
|
164
|
+
|
|
165
|
+
from defipy import *
|
|
166
|
+
|
|
167
|
+
# Step 1: Define stablecoins and parameters
|
|
168
|
+
dai = ERC20("DAI", "0x111", 18)
|
|
169
|
+
usdc = ERC20("USDC", "0x222", 6)
|
|
170
|
+
AMPL_COEFF = 2000
|
|
171
|
+
|
|
172
|
+
# Step 2: Deposit token amounts
|
|
173
|
+
dai.deposit(None, 10000)
|
|
174
|
+
usdc.deposit(None, 20000)
|
|
175
|
+
|
|
176
|
+
# Step 3: Setup Stableswap vault and add tokens
|
|
177
|
+
sgrp = StableswapVault()
|
|
178
|
+
sgrp.add_token(dai)
|
|
179
|
+
sgrp.add_token(usdc)
|
|
180
|
+
|
|
181
|
+
# Step 4: Set up exchange data for Stableswap
|
|
182
|
+
exch_data = StableswapExchangeData(vault = sgrp, symbol="LP", address="0x011")
|
|
183
|
+
|
|
184
|
+
# Step 5: Initialize factor for Balancer
|
|
185
|
+
factory = StableswapFactory("Stableswap factory", "0x2")
|
|
186
|
+
|
|
187
|
+
# Step 6: Deploy pool
|
|
188
|
+
lp = factory.deploy(exch_data)
|
|
189
|
+
|
|
190
|
+
# Step 7: Join pool with initial liquidity
|
|
191
|
+
join = Join()
|
|
192
|
+
join.apply(lp, "user", AMPL_COEFF)
|
|
193
|
+
|
|
194
|
+
# Step 8: Perform swap
|
|
195
|
+
swap = Swap()
|
|
196
|
+
out = swap.apply(lp, dai, usdc, "user", 10)
|
|
197
|
+
|
|
198
|
+
# Check reserves and liquidity
|
|
199
|
+
lp.summary()
|
|
200
|
+
|
|
201
|
+
#OUTPUT:
|
|
202
|
+
Stableswap Exchange: DAI-USDC (LP)
|
|
203
|
+
Reserves: DAI = 10010, USDC = 19989.996791
|
|
204
|
+
Liquidity: 29999.063056285642
|
|
205
|
+
|
|
206
|
+
## 0x Quant Terminal
|
|
207
|
+
|
|
208
|
+
This application utilizes the 0x API to produce a mock Uniswap pool which allows end-users to stress test
|
|
209
|
+
the limitations of a Uniswap pool setup using live price feeds from [0x API](https://0x.org); for backend setup, see
|
|
210
|
+
[notebook](https://github.com/defipy-devs/defipy/blob/main/notebooks/quant_terminal.ipynb)
|
|
211
|
+
|
|
212
|
+
Click [dashboard.defipy.org](https://dashboard.defipy.org/) for live link; for more detail see
|
|
213
|
+
[README](https://github.com/defipy-devs/defipy/tree/main/python/application/quant_terminal#readme)
|
|
214
|
+
|
|
215
|
+

|
|
216
|
+
|
|
217
|
+
### Run application locally
|
|
218
|
+
|
|
219
|
+
```
|
|
220
|
+
> bokeh serve --show python/application/quant_terminal/bokeh_server.py
|
|
221
|
+
```
|
|
@@ -15,6 +15,8 @@ defipy.egg-info/not-zip-safe
|
|
|
15
15
|
defipy.egg-info/requires.txt
|
|
16
16
|
defipy.egg-info/top_level.txt
|
|
17
17
|
python/prod/__init__.py
|
|
18
|
+
python/prod/analytics/risk/__init__.py
|
|
19
|
+
python/prod/analytics/simulate/__init__.py
|
|
18
20
|
python/prod/erc/__init__.py
|
|
19
21
|
python/prod/math/basic/__init__.py
|
|
20
22
|
python/prod/math/interest/__init__.py
|
|
@@ -33,9 +35,11 @@ python/prod/process/liquidity/__init__.py
|
|
|
33
35
|
python/prod/process/mint/__init__.py
|
|
34
36
|
python/prod/process/swap/Swap.py
|
|
35
37
|
python/prod/process/swap/__init__.py
|
|
36
|
-
python/prod/simulate/__init__.py
|
|
37
38
|
python/prod/utils/client/__init__.py
|
|
39
|
+
python/prod/utils/client/contract/ExecuteScript.py
|
|
40
|
+
python/prod/utils/client/contract/__init__.py
|
|
38
41
|
python/prod/utils/data/__init__.py
|
|
39
42
|
python/prod/utils/interfaces/__init__.py
|
|
43
|
+
python/prod/utils/tools/UniswapScriptHelper.py
|
|
40
44
|
python/prod/utils/tools/__init__.py
|
|
41
45
|
python/prod/utils/tools/v3/__init__.py
|
defipy-1.0.2/PKG-INFO
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: DeFiPy
|
|
3
|
+
Version: 1.0.2
|
|
4
|
+
Summary: DeFi Analytics with Python
|
|
5
|
+
Home-page: http://github.com/defipy-devs/defipy
|
|
6
|
+
Author: icmoore
|
|
7
|
+
Author-email: defipy.devs@gmail.com
|
|
8
|
+
License: MIT
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE.txt
|
|
11
|
+
Requires-Dist: scipy>=1.7.3
|
|
12
|
+
Requires-Dist: bokeh==3.3.4
|
|
13
|
+
Requires-Dist: uniswappy==1.7.0
|
|
14
|
+
Requires-Dist: stableswappy==1.0.0
|
|
15
|
+
Requires-Dist: balancerpy==1.0.1
|
|
16
|
+
|
|
17
|
+
# DeFiPy: DeFi Analytics with Python (v 1.0.2)
|
|
18
|
+
|
|
19
|
+
Welcome to the worlds first DeFi Python package with all major protocols intergrated into one package! Implement your analytics in one package with DeFiPy. Since DeFiPy is built with a modular design in mind, your can also silo your analytics by protocol
|
|
20
|
+
using:
|
|
21
|
+
* [UniswapPy](https://github.com/defipy-devs/uniswappy)
|
|
22
|
+
* [BalancerPy](https://github.com/defipy-devs/balancerpy)
|
|
23
|
+
* [StableSwapPy](https://github.com/defipy-devs/stableswappy)
|
|
24
|
+
|
|
25
|
+
## Docs
|
|
26
|
+
Visit [DeFiPy docs](https://defipy.org) for full documentation with walk-through tutorials
|
|
27
|
+
|
|
28
|
+
## Install
|
|
29
|
+
Must first install gmpy2 python package to handle the precision within the StableSwap protocol (requires CPython 3.7 or above). To install the latest release with pip:
|
|
30
|
+
```
|
|
31
|
+
> pip install gmpy2
|
|
32
|
+
```
|
|
33
|
+
Also, in many cases will need to have required libraries (GMP, MPFR and MPC) already installed on your system, see [gmpy2 installation docs](https://gmpy2.readthedocs.io/en/latest/install.html) for more info. Once setup, install the latest release of DeFiPy with pip:
|
|
34
|
+
```
|
|
35
|
+
> git clone https://github.com/defipy-devs/defipy
|
|
36
|
+
> pip install .
|
|
37
|
+
```
|
|
38
|
+
or
|
|
39
|
+
```
|
|
40
|
+
> pip install defipy
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Uniswap V2 Example
|
|
44
|
+
--------------------------
|
|
45
|
+
|
|
46
|
+
To setup a liquidity pool, you must first create the tokens in the pair using the `ERC20` object. Next, create a liquidity pool (LP) factory using `IFactory` object. Once this is setup, an unlimited amount of LPs can be created; the procedures for such are as follows:
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
from defipy import *
|
|
50
|
+
|
|
51
|
+
# Step 1: Define tokens
|
|
52
|
+
tkn = ERC20("TKN", "0x111")
|
|
53
|
+
eth = ERC20("ETH", "0x999")
|
|
54
|
+
|
|
55
|
+
# Step 2: Initialize factory
|
|
56
|
+
factory = UniswapFactory("ETH pool factory", "0x2")
|
|
57
|
+
|
|
58
|
+
# Step 3: Set up exchange data for V2
|
|
59
|
+
exch_data = UniswapExchangeData(tkn0=eth, tkn1=tkn, symbol="LP", address="0x3")
|
|
60
|
+
|
|
61
|
+
# Step 4: Deploy pool
|
|
62
|
+
lp = factory.deploy(exch_data)
|
|
63
|
+
|
|
64
|
+
# Step 5: Add initial liquidity
|
|
65
|
+
join = Join()
|
|
66
|
+
join.apply(lp, "user", 1000, 10000)
|
|
67
|
+
|
|
68
|
+
# Step 6: Perform swap
|
|
69
|
+
swap = Swap()
|
|
70
|
+
out = swap.apply(lp, tkn, "user", 10)
|
|
71
|
+
|
|
72
|
+
# Check reserves and liquidity
|
|
73
|
+
lp.summary()
|
|
74
|
+
|
|
75
|
+
# OUTPUT:
|
|
76
|
+
Exchange ETH-TKN (LP)
|
|
77
|
+
Reserves: ETH = 999.00399301896, TKN = 10010.0
|
|
78
|
+
Liquidity: 3162.2776601683795
|
|
79
|
+
|
|
80
|
+
Uniswap V3 Example
|
|
81
|
+
--------------------------
|
|
82
|
+
|
|
83
|
+
from defipy import *
|
|
84
|
+
|
|
85
|
+
# Step 1: Define tokens and parameters
|
|
86
|
+
eth = ERC20("ETH", "0x93")
|
|
87
|
+
tkn = ERC20("TKN", "0x111")
|
|
88
|
+
tick_spacing = 60
|
|
89
|
+
fee = 3000 # 0.3% fee tier
|
|
90
|
+
|
|
91
|
+
# Step 2: Set up exchange data for V3
|
|
92
|
+
exch_data = UniswapExchangeData(tkn0=eth, tkn1=tkn, symbol="LP", address="0x811", version='V3', tick_spacing=tick_spacing, fee=fee)
|
|
93
|
+
|
|
94
|
+
# Step 3: Initialize factory
|
|
95
|
+
factory = UniswapFactory("ETH pool factory", "0x2")
|
|
96
|
+
|
|
97
|
+
# Step 4: Deploy pool
|
|
98
|
+
lp = factory.deploy(exch_data)
|
|
99
|
+
|
|
100
|
+
# Step 5: Add initial liquidity within tick range
|
|
101
|
+
lwr_tick = UniV3Utils.getMinTick(tick_spacing)
|
|
102
|
+
upr_tick = UniV3Utils.getMaxTick(tick_spacing)
|
|
103
|
+
join = Join()
|
|
104
|
+
join.apply(lp, "user", 1000, 10000, lwr_tick, upr_tick)
|
|
105
|
+
|
|
106
|
+
# Step 6: Perform swap
|
|
107
|
+
swap = Swap()
|
|
108
|
+
out = swap.apply(lp, tkn, "user", 10)
|
|
109
|
+
|
|
110
|
+
# Check reserves and liquidity
|
|
111
|
+
lp.summary()
|
|
112
|
+
|
|
113
|
+
# OUTPUT:
|
|
114
|
+
Exchange ETH-TKN (LP)
|
|
115
|
+
Real Reserves: ETH = 999.0039930189599, TKN = 10010.0
|
|
116
|
+
Gross Liquidity: 3162.277660168379
|
|
117
|
+
|
|
118
|
+
Balancer Example
|
|
119
|
+
--------------------------
|
|
120
|
+
|
|
121
|
+
from defipy import *
|
|
122
|
+
|
|
123
|
+
# Step 1: Define tokens
|
|
124
|
+
dai = ERC20("DAI", "0x111")
|
|
125
|
+
usdc = ERC20("USDC", "0x999")
|
|
126
|
+
|
|
127
|
+
# Step 2: Deposit token amounts
|
|
128
|
+
dai.deposit(None, 10000)
|
|
129
|
+
usdc.deposit(None, 20000)
|
|
130
|
+
|
|
131
|
+
# Step 3: Setup vault
|
|
132
|
+
vault = BalancerVault()
|
|
133
|
+
vault.add_token(dai, 10) # Denormalized weight for DAI
|
|
134
|
+
vault.add_token(usdc, 40) # Denormalized weight for WETH
|
|
135
|
+
|
|
136
|
+
# Step 4: Set up exchange data for Balancer
|
|
137
|
+
exch_data = BalancerExchangeData(vault=vault, symbol="BSP", address="0x3")
|
|
138
|
+
|
|
139
|
+
# Step 5: Initialize factor for Balancer
|
|
140
|
+
bfactory = BalancerFactory("WETH pool factory", "0x2")
|
|
141
|
+
|
|
142
|
+
# Step 6: Deploy pool
|
|
143
|
+
lp = bfactory.deploy(exch_data)
|
|
144
|
+
|
|
145
|
+
# Step 7: Join pool with initial liquidity
|
|
146
|
+
join = Join()
|
|
147
|
+
join.apply(lp, "user", 100) # Issue 100 pool shares
|
|
148
|
+
|
|
149
|
+
# Step 8: Perform swap
|
|
150
|
+
swap = Swap(Proc.SWAPIN)
|
|
151
|
+
out = swap.apply(lp, dai, usdc, "user", 10)
|
|
152
|
+
|
|
153
|
+
# Check reserves and liquidity
|
|
154
|
+
lp.summary()
|
|
155
|
+
|
|
156
|
+
#OUTPUT:
|
|
157
|
+
Balancer Exchange: DAI-USDC (BSP)
|
|
158
|
+
Reserves: DAI = 9979.92478694547, USDC = 20010
|
|
159
|
+
Weights: DAI = 0.2, USDC = 0.8
|
|
160
|
+
Pool Shares: 100
|
|
161
|
+
|
|
162
|
+
StableSwap Example
|
|
163
|
+
--------------------------
|
|
164
|
+
|
|
165
|
+
from defipy import *
|
|
166
|
+
|
|
167
|
+
# Step 1: Define stablecoins and parameters
|
|
168
|
+
dai = ERC20("DAI", "0x111", 18)
|
|
169
|
+
usdc = ERC20("USDC", "0x222", 6)
|
|
170
|
+
AMPL_COEFF = 2000
|
|
171
|
+
|
|
172
|
+
# Step 2: Deposit token amounts
|
|
173
|
+
dai.deposit(None, 10000)
|
|
174
|
+
usdc.deposit(None, 20000)
|
|
175
|
+
|
|
176
|
+
# Step 3: Setup Stableswap vault and add tokens
|
|
177
|
+
sgrp = StableswapVault()
|
|
178
|
+
sgrp.add_token(dai)
|
|
179
|
+
sgrp.add_token(usdc)
|
|
180
|
+
|
|
181
|
+
# Step 4: Set up exchange data for Stableswap
|
|
182
|
+
exch_data = StableswapExchangeData(vault = sgrp, symbol="LP", address="0x011")
|
|
183
|
+
|
|
184
|
+
# Step 5: Initialize factor for Balancer
|
|
185
|
+
factory = StableswapFactory("Stableswap factory", "0x2")
|
|
186
|
+
|
|
187
|
+
# Step 6: Deploy pool
|
|
188
|
+
lp = factory.deploy(exch_data)
|
|
189
|
+
|
|
190
|
+
# Step 7: Join pool with initial liquidity
|
|
191
|
+
join = Join()
|
|
192
|
+
join.apply(lp, "user", AMPL_COEFF)
|
|
193
|
+
|
|
194
|
+
# Step 8: Perform swap
|
|
195
|
+
swap = Swap()
|
|
196
|
+
out = swap.apply(lp, dai, usdc, "user", 10)
|
|
197
|
+
|
|
198
|
+
# Check reserves and liquidity
|
|
199
|
+
lp.summary()
|
|
200
|
+
|
|
201
|
+
#OUTPUT:
|
|
202
|
+
Stableswap Exchange: DAI-USDC (LP)
|
|
203
|
+
Reserves: DAI = 10010, USDC = 19989.996791
|
|
204
|
+
Liquidity: 29999.063056285642
|
|
205
|
+
|
|
206
|
+
## 0x Quant Terminal
|
|
207
|
+
|
|
208
|
+
This application utilizes the 0x API to produce a mock Uniswap pool which allows end-users to stress test
|
|
209
|
+
the limitations of a Uniswap pool setup using live price feeds from [0x API](https://0x.org); for backend setup, see
|
|
210
|
+
[notebook](https://github.com/defipy-devs/defipy/blob/main/notebooks/quant_terminal.ipynb)
|
|
211
|
+
|
|
212
|
+
Click [dashboard.defipy.org](https://dashboard.defipy.org/) for live link; for more detail see
|
|
213
|
+
[README](https://github.com/defipy-devs/defipy/tree/main/python/application/quant_terminal#readme)
|
|
214
|
+
|
|
215
|
+

|
|
216
|
+
|
|
217
|
+
### Run application locally
|
|
218
|
+
|
|
219
|
+
```
|
|
220
|
+
> bokeh serve --show python/application/quant_terminal/bokeh_server.py
|
|
221
|
+
```
|
defipy-1.0.2/README.md
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
# DeFiPy: DeFi Analytics with Python (v 1.0.2)
|
|
2
|
+
|
|
3
|
+
Welcome to the worlds first DeFi Python package with all major protocols intergrated into one package! Implement your analytics in one package with DeFiPy. Since DeFiPy is built with a modular design in mind, your can also silo your analytics by protocol
|
|
4
|
+
using:
|
|
5
|
+
* [UniswapPy](https://github.com/defipy-devs/uniswappy)
|
|
6
|
+
* [BalancerPy](https://github.com/defipy-devs/balancerpy)
|
|
7
|
+
* [StableSwapPy](https://github.com/defipy-devs/stableswappy)
|
|
8
|
+
|
|
9
|
+
## Docs
|
|
10
|
+
Visit [DeFiPy docs](https://defipy.org) for full documentation with walk-through tutorials
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
Must first install gmpy2 python package to handle the precision within the StableSwap protocol (requires CPython 3.7 or above). To install the latest release with pip:
|
|
14
|
+
```
|
|
15
|
+
> pip install gmpy2
|
|
16
|
+
```
|
|
17
|
+
Also, in many cases will need to have required libraries (GMP, MPFR and MPC) already installed on your system, see [gmpy2 installation docs](https://gmpy2.readthedocs.io/en/latest/install.html) for more info. Once setup, install the latest release of DeFiPy with pip:
|
|
18
|
+
```
|
|
19
|
+
> git clone https://github.com/defipy-devs/defipy
|
|
20
|
+
> pip install .
|
|
21
|
+
```
|
|
22
|
+
or
|
|
23
|
+
```
|
|
24
|
+
> pip install defipy
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Uniswap V2 Example
|
|
28
|
+
--------------------------
|
|
29
|
+
|
|
30
|
+
To setup a liquidity pool, you must first create the tokens in the pair using the `ERC20` object. Next, create a liquidity pool (LP) factory using `IFactory` object. Once this is setup, an unlimited amount of LPs can be created; the procedures for such are as follows:
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
from defipy import *
|
|
34
|
+
|
|
35
|
+
# Step 1: Define tokens
|
|
36
|
+
tkn = ERC20("TKN", "0x111")
|
|
37
|
+
eth = ERC20("ETH", "0x999")
|
|
38
|
+
|
|
39
|
+
# Step 2: Initialize factory
|
|
40
|
+
factory = UniswapFactory("ETH pool factory", "0x2")
|
|
41
|
+
|
|
42
|
+
# Step 3: Set up exchange data for V2
|
|
43
|
+
exch_data = UniswapExchangeData(tkn0=eth, tkn1=tkn, symbol="LP", address="0x3")
|
|
44
|
+
|
|
45
|
+
# Step 4: Deploy pool
|
|
46
|
+
lp = factory.deploy(exch_data)
|
|
47
|
+
|
|
48
|
+
# Step 5: Add initial liquidity
|
|
49
|
+
join = Join()
|
|
50
|
+
join.apply(lp, "user", 1000, 10000)
|
|
51
|
+
|
|
52
|
+
# Step 6: Perform swap
|
|
53
|
+
swap = Swap()
|
|
54
|
+
out = swap.apply(lp, tkn, "user", 10)
|
|
55
|
+
|
|
56
|
+
# Check reserves and liquidity
|
|
57
|
+
lp.summary()
|
|
58
|
+
|
|
59
|
+
# OUTPUT:
|
|
60
|
+
Exchange ETH-TKN (LP)
|
|
61
|
+
Reserves: ETH = 999.00399301896, TKN = 10010.0
|
|
62
|
+
Liquidity: 3162.2776601683795
|
|
63
|
+
|
|
64
|
+
Uniswap V3 Example
|
|
65
|
+
--------------------------
|
|
66
|
+
|
|
67
|
+
from defipy import *
|
|
68
|
+
|
|
69
|
+
# Step 1: Define tokens and parameters
|
|
70
|
+
eth = ERC20("ETH", "0x93")
|
|
71
|
+
tkn = ERC20("TKN", "0x111")
|
|
72
|
+
tick_spacing = 60
|
|
73
|
+
fee = 3000 # 0.3% fee tier
|
|
74
|
+
|
|
75
|
+
# Step 2: Set up exchange data for V3
|
|
76
|
+
exch_data = UniswapExchangeData(tkn0=eth, tkn1=tkn, symbol="LP", address="0x811", version='V3', tick_spacing=tick_spacing, fee=fee)
|
|
77
|
+
|
|
78
|
+
# Step 3: Initialize factory
|
|
79
|
+
factory = UniswapFactory("ETH pool factory", "0x2")
|
|
80
|
+
|
|
81
|
+
# Step 4: Deploy pool
|
|
82
|
+
lp = factory.deploy(exch_data)
|
|
83
|
+
|
|
84
|
+
# Step 5: Add initial liquidity within tick range
|
|
85
|
+
lwr_tick = UniV3Utils.getMinTick(tick_spacing)
|
|
86
|
+
upr_tick = UniV3Utils.getMaxTick(tick_spacing)
|
|
87
|
+
join = Join()
|
|
88
|
+
join.apply(lp, "user", 1000, 10000, lwr_tick, upr_tick)
|
|
89
|
+
|
|
90
|
+
# Step 6: Perform swap
|
|
91
|
+
swap = Swap()
|
|
92
|
+
out = swap.apply(lp, tkn, "user", 10)
|
|
93
|
+
|
|
94
|
+
# Check reserves and liquidity
|
|
95
|
+
lp.summary()
|
|
96
|
+
|
|
97
|
+
# OUTPUT:
|
|
98
|
+
Exchange ETH-TKN (LP)
|
|
99
|
+
Real Reserves: ETH = 999.0039930189599, TKN = 10010.0
|
|
100
|
+
Gross Liquidity: 3162.277660168379
|
|
101
|
+
|
|
102
|
+
Balancer Example
|
|
103
|
+
--------------------------
|
|
104
|
+
|
|
105
|
+
from defipy import *
|
|
106
|
+
|
|
107
|
+
# Step 1: Define tokens
|
|
108
|
+
dai = ERC20("DAI", "0x111")
|
|
109
|
+
usdc = ERC20("USDC", "0x999")
|
|
110
|
+
|
|
111
|
+
# Step 2: Deposit token amounts
|
|
112
|
+
dai.deposit(None, 10000)
|
|
113
|
+
usdc.deposit(None, 20000)
|
|
114
|
+
|
|
115
|
+
# Step 3: Setup vault
|
|
116
|
+
vault = BalancerVault()
|
|
117
|
+
vault.add_token(dai, 10) # Denormalized weight for DAI
|
|
118
|
+
vault.add_token(usdc, 40) # Denormalized weight for WETH
|
|
119
|
+
|
|
120
|
+
# Step 4: Set up exchange data for Balancer
|
|
121
|
+
exch_data = BalancerExchangeData(vault=vault, symbol="BSP", address="0x3")
|
|
122
|
+
|
|
123
|
+
# Step 5: Initialize factor for Balancer
|
|
124
|
+
bfactory = BalancerFactory("WETH pool factory", "0x2")
|
|
125
|
+
|
|
126
|
+
# Step 6: Deploy pool
|
|
127
|
+
lp = bfactory.deploy(exch_data)
|
|
128
|
+
|
|
129
|
+
# Step 7: Join pool with initial liquidity
|
|
130
|
+
join = Join()
|
|
131
|
+
join.apply(lp, "user", 100) # Issue 100 pool shares
|
|
132
|
+
|
|
133
|
+
# Step 8: Perform swap
|
|
134
|
+
swap = Swap(Proc.SWAPIN)
|
|
135
|
+
out = swap.apply(lp, dai, usdc, "user", 10)
|
|
136
|
+
|
|
137
|
+
# Check reserves and liquidity
|
|
138
|
+
lp.summary()
|
|
139
|
+
|
|
140
|
+
#OUTPUT:
|
|
141
|
+
Balancer Exchange: DAI-USDC (BSP)
|
|
142
|
+
Reserves: DAI = 9979.92478694547, USDC = 20010
|
|
143
|
+
Weights: DAI = 0.2, USDC = 0.8
|
|
144
|
+
Pool Shares: 100
|
|
145
|
+
|
|
146
|
+
StableSwap Example
|
|
147
|
+
--------------------------
|
|
148
|
+
|
|
149
|
+
from defipy import *
|
|
150
|
+
|
|
151
|
+
# Step 1: Define stablecoins and parameters
|
|
152
|
+
dai = ERC20("DAI", "0x111", 18)
|
|
153
|
+
usdc = ERC20("USDC", "0x222", 6)
|
|
154
|
+
AMPL_COEFF = 2000
|
|
155
|
+
|
|
156
|
+
# Step 2: Deposit token amounts
|
|
157
|
+
dai.deposit(None, 10000)
|
|
158
|
+
usdc.deposit(None, 20000)
|
|
159
|
+
|
|
160
|
+
# Step 3: Setup Stableswap vault and add tokens
|
|
161
|
+
sgrp = StableswapVault()
|
|
162
|
+
sgrp.add_token(dai)
|
|
163
|
+
sgrp.add_token(usdc)
|
|
164
|
+
|
|
165
|
+
# Step 4: Set up exchange data for Stableswap
|
|
166
|
+
exch_data = StableswapExchangeData(vault = sgrp, symbol="LP", address="0x011")
|
|
167
|
+
|
|
168
|
+
# Step 5: Initialize factor for Balancer
|
|
169
|
+
factory = StableswapFactory("Stableswap factory", "0x2")
|
|
170
|
+
|
|
171
|
+
# Step 6: Deploy pool
|
|
172
|
+
lp = factory.deploy(exch_data)
|
|
173
|
+
|
|
174
|
+
# Step 7: Join pool with initial liquidity
|
|
175
|
+
join = Join()
|
|
176
|
+
join.apply(lp, "user", AMPL_COEFF)
|
|
177
|
+
|
|
178
|
+
# Step 8: Perform swap
|
|
179
|
+
swap = Swap()
|
|
180
|
+
out = swap.apply(lp, dai, usdc, "user", 10)
|
|
181
|
+
|
|
182
|
+
# Check reserves and liquidity
|
|
183
|
+
lp.summary()
|
|
184
|
+
|
|
185
|
+
#OUTPUT:
|
|
186
|
+
Stableswap Exchange: DAI-USDC (LP)
|
|
187
|
+
Reserves: DAI = 10010, USDC = 19989.996791
|
|
188
|
+
Liquidity: 29999.063056285642
|
|
189
|
+
|
|
190
|
+
## 0x Quant Terminal
|
|
191
|
+
|
|
192
|
+
This application utilizes the 0x API to produce a mock Uniswap pool which allows end-users to stress test
|
|
193
|
+
the limitations of a Uniswap pool setup using live price feeds from [0x API](https://0x.org); for backend setup, see
|
|
194
|
+
[notebook](https://github.com/defipy-devs/defipy/blob/main/notebooks/quant_terminal.ipynb)
|
|
195
|
+
|
|
196
|
+
Click [dashboard.defipy.org](https://dashboard.defipy.org/) for live link; for more detail see
|
|
197
|
+
[README](https://github.com/defipy-devs/defipy/tree/main/python/application/quant_terminal#readme)
|
|
198
|
+
|
|
199
|
+

|
|
200
|
+
|
|
201
|
+
### Run application locally
|
|
202
|
+
|
|
203
|
+
```
|
|
204
|
+
> bokeh serve --show python/application/quant_terminal/bokeh_server.py
|
|
205
|
+
```
|
|
@@ -12,11 +12,12 @@ from defipy.process.liquidity import *
|
|
|
12
12
|
from defipy.process.mint import *
|
|
13
13
|
from defipy.process.swap import *
|
|
14
14
|
from defipy.process.join import *
|
|
15
|
-
from defipy.simulate import *
|
|
15
|
+
from defipy.analytics.simulate import *
|
|
16
|
+
from defipy.analytics.risk import *
|
|
16
17
|
from defipy.utils.interfaces import *
|
|
17
18
|
from defipy.utils.data import *
|
|
18
19
|
from defipy.utils.client import *
|
|
19
|
-
from defipy.utils.
|
|
20
|
+
from defipy.utils.client.contract import *
|
|
20
21
|
from defipy.utils.tools import *
|
|
21
22
|
|
|
22
23
|
from uniswappy.cpt.exchg import *
|
|
@@ -25,6 +26,7 @@ from uniswappy.cpt.index import *
|
|
|
25
26
|
from uniswappy.cpt.quote import *
|
|
26
27
|
from uniswappy.cpt.vault import *
|
|
27
28
|
from uniswappy.cpt.wallet import *
|
|
29
|
+
from uniswappy.utils.tools.v3 import *
|
|
28
30
|
|
|
29
31
|
from stableswappy.quote import *
|
|
30
32
|
from stableswappy.vault import *
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from uniswappy.analytics.risk import *
|