DeFiPy 1.0.1__tar.gz → 1.0.3__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.3/DeFiPy.egg-info/PKG-INFO +230 -0
- {defipy-1.0.1 → defipy-1.0.3}/DeFiPy.egg-info/SOURCES.txt +3 -0
- defipy-1.0.3/LICENSE.txt +120 -0
- defipy-1.0.3/PKG-INFO +230 -0
- defipy-1.0.3/README.md +207 -0
- {defipy-1.0.1 → defipy-1.0.3}/python/prod/__init__.py +2 -1
- {defipy-1.0.1 → defipy-1.0.3}/python/prod/process/join/Join.py +13 -2
- {defipy-1.0.1 → defipy-1.0.3}/python/prod/process/liquidity/AddLiquidity.py +13 -2
- {defipy-1.0.1 → defipy-1.0.3}/python/prod/process/liquidity/RemoveLiquidity.py +13 -2
- {defipy-1.0.1 → defipy-1.0.3}/python/prod/process/swap/Swap.py +13 -2
- defipy-1.0.3/python/prod/utils/client/contract/ExecuteScript.py +43 -0
- defipy-1.0.3/python/prod/utils/client/contract/__init__.py +1 -0
- defipy-1.0.3/python/prod/utils/tools/UniswapScriptHelper.py +67 -0
- defipy-1.0.3/python/prod/utils/tools/__init__.py +2 -0
- {defipy-1.0.1 → defipy-1.0.3}/setup.py +12 -2
- defipy-1.0.1/DeFiPy.egg-info/PKG-INFO +0 -200
- defipy-1.0.1/LICENSE.txt +0 -21
- defipy-1.0.1/PKG-INFO +0 -200
- defipy-1.0.1/README.md +0 -184
- defipy-1.0.1/python/prod/utils/tools/__init__.py +0 -1
- {defipy-1.0.1 → defipy-1.0.3}/DeFiPy.egg-info/dependency_links.txt +0 -0
- {defipy-1.0.1 → defipy-1.0.3}/DeFiPy.egg-info/not-zip-safe +0 -0
- {defipy-1.0.1 → defipy-1.0.3}/DeFiPy.egg-info/requires.txt +0 -0
- {defipy-1.0.1 → defipy-1.0.3}/DeFiPy.egg-info/top_level.txt +0 -0
- {defipy-1.0.1 → defipy-1.0.3}/python/prod/analytics/risk/__init__.py +0 -0
- {defipy-1.0.1 → defipy-1.0.3}/python/prod/analytics/simulate/__init__.py +0 -0
- {defipy-1.0.1 → defipy-1.0.3}/python/prod/erc/__init__.py +0 -0
- {defipy-1.0.1 → defipy-1.0.3}/python/prod/math/basic/__init__.py +0 -0
- {defipy-1.0.1 → defipy-1.0.3}/python/prod/math/interest/__init__.py +0 -0
- {defipy-1.0.1 → defipy-1.0.3}/python/prod/math/interest/ips/__init__.py +0 -0
- {defipy-1.0.1 → defipy-1.0.3}/python/prod/math/interest/ips/aggregate/__init__.py +0 -0
- {defipy-1.0.1 → defipy-1.0.3}/python/prod/math/model/__init__.py +0 -0
- {defipy-1.0.1 → defipy-1.0.3}/python/prod/math/risk/__init__.py +0 -0
- {defipy-1.0.1 → defipy-1.0.3}/python/prod/process/__init__.py +0 -0
- {defipy-1.0.1 → defipy-1.0.3}/python/prod/process/burn/__init__.py +0 -0
- {defipy-1.0.1 → defipy-1.0.3}/python/prod/process/deposit/__init__.py +0 -0
- {defipy-1.0.1 → defipy-1.0.3}/python/prod/process/join/__init__.py +0 -0
- {defipy-1.0.1 → defipy-1.0.3}/python/prod/process/liquidity/__init__.py +0 -0
- {defipy-1.0.1 → defipy-1.0.3}/python/prod/process/mint/__init__.py +0 -0
- {defipy-1.0.1 → defipy-1.0.3}/python/prod/process/swap/__init__.py +0 -0
- {defipy-1.0.1 → defipy-1.0.3}/python/prod/utils/client/__init__.py +0 -0
- {defipy-1.0.1 → defipy-1.0.3}/python/prod/utils/data/__init__.py +0 -0
- {defipy-1.0.1 → defipy-1.0.3}/python/prod/utils/interfaces/__init__.py +0 -0
- {defipy-1.0.1 → defipy-1.0.3}/python/prod/utils/tools/v3/__init__.py +0 -0
- {defipy-1.0.1 → defipy-1.0.3}/setup.cfg +0 -0
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: DeFiPy
|
|
3
|
+
Version: 1.0.3
|
|
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: Apache-2.0
|
|
9
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
14
|
+
Classifier: Topic :: Scientific/Engineering :: Information Analysis
|
|
15
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE.txt
|
|
18
|
+
Requires-Dist: scipy>=1.7.3
|
|
19
|
+
Requires-Dist: bokeh==3.3.4
|
|
20
|
+
Requires-Dist: uniswappy==1.7.0
|
|
21
|
+
Requires-Dist: stableswappy==1.0.0
|
|
22
|
+
Requires-Dist: balancerpy==1.0.1
|
|
23
|
+
|
|
24
|
+
# DeFiPy: Python SDK for DeFi Analytics and Agents
|
|
25
|
+
|
|
26
|
+
DeFiPy is the first unified Python SDK for DeFi analytics, simulation, and autonomous agents. Built with modularity in mind, DeFiPy lets you isolate and extend your analytics by protocol using:
|
|
27
|
+
|
|
28
|
+
* [UniswapPy](https://github.com/defipy-devs/uniswappy)
|
|
29
|
+
* [BalancerPy](https://github.com/defipy-devs/balancerpy)
|
|
30
|
+
* [StableSwapPy](https://github.com/defipy-devs/stableswappy)
|
|
31
|
+
|
|
32
|
+
For onchain event access and scripting, pair it with [Web3Scout](https://github.com/defipy-devs/web3scout) — a companion tool for [decoding pool events](https://defipy.readthedocs.io/en/latest/onchain/pool_events.html) and [interfacing with Solidity contracts](https://defipy.readthedocs.io/en/latest/onchain/testnet_sim_univ2.html). Whether you’re building dashboards, simulations, or agent-based trading systems, DeFiPy + Web3Scout deliver a uniquely powerful toolset — unlike anything else in the ecosystem.
|
|
33
|
+
|
|
34
|
+
## Docs
|
|
35
|
+
Visit [DeFiPy docs](https://defipy.org) for full documentation with walk-through tutorials
|
|
36
|
+
|
|
37
|
+
## Install
|
|
38
|
+
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:
|
|
39
|
+
```
|
|
40
|
+
> pip install gmpy2
|
|
41
|
+
```
|
|
42
|
+
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:
|
|
43
|
+
```
|
|
44
|
+
> git clone https://github.com/defipy-devs/defipy
|
|
45
|
+
> pip install .
|
|
46
|
+
```
|
|
47
|
+
or
|
|
48
|
+
```
|
|
49
|
+
> pip install defipy
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Uniswap V2 Example
|
|
53
|
+
--------------------------
|
|
54
|
+
|
|
55
|
+
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:
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
from defipy import *
|
|
59
|
+
|
|
60
|
+
# Step 1: Define tokens
|
|
61
|
+
tkn = ERC20("TKN", "0x111")
|
|
62
|
+
eth = ERC20("ETH", "0x999")
|
|
63
|
+
|
|
64
|
+
# Step 2: Initialize factory
|
|
65
|
+
factory = UniswapFactory("ETH pool factory", "0x2")
|
|
66
|
+
|
|
67
|
+
# Step 3: Set up exchange data for V2
|
|
68
|
+
exch_data = UniswapExchangeData(tkn0=eth, tkn1=tkn, symbol="LP", address="0x3")
|
|
69
|
+
|
|
70
|
+
# Step 4: Deploy pool
|
|
71
|
+
lp = factory.deploy(exch_data)
|
|
72
|
+
|
|
73
|
+
# Step 5: Add initial liquidity
|
|
74
|
+
join = Join()
|
|
75
|
+
join.apply(lp, "user", 1000, 10000)
|
|
76
|
+
|
|
77
|
+
# Step 6: Perform swap
|
|
78
|
+
swap = Swap()
|
|
79
|
+
out = swap.apply(lp, tkn, "user", 10)
|
|
80
|
+
|
|
81
|
+
# Check reserves and liquidity
|
|
82
|
+
lp.summary()
|
|
83
|
+
|
|
84
|
+
# OUTPUT:
|
|
85
|
+
Exchange ETH-TKN (LP)
|
|
86
|
+
Reserves: ETH = 999.00399301896, TKN = 10010.0
|
|
87
|
+
Liquidity: 3162.2776601683795
|
|
88
|
+
|
|
89
|
+
Uniswap V3 Example
|
|
90
|
+
--------------------------
|
|
91
|
+
|
|
92
|
+
from defipy import *
|
|
93
|
+
|
|
94
|
+
# Step 1: Define tokens and parameters
|
|
95
|
+
eth = ERC20("ETH", "0x93")
|
|
96
|
+
tkn = ERC20("TKN", "0x111")
|
|
97
|
+
tick_spacing = 60
|
|
98
|
+
fee = 3000 # 0.3% fee tier
|
|
99
|
+
|
|
100
|
+
# Step 2: Set up exchange data for V3
|
|
101
|
+
exch_data = UniswapExchangeData(tkn0=eth, tkn1=tkn, symbol="LP", address="0x811", version='V3', tick_spacing=tick_spacing, fee=fee)
|
|
102
|
+
|
|
103
|
+
# Step 3: Initialize factory
|
|
104
|
+
factory = UniswapFactory("ETH pool factory", "0x2")
|
|
105
|
+
|
|
106
|
+
# Step 4: Deploy pool
|
|
107
|
+
lp = factory.deploy(exch_data)
|
|
108
|
+
|
|
109
|
+
# Step 5: Add initial liquidity within tick range
|
|
110
|
+
lwr_tick = UniV3Utils.getMinTick(tick_spacing)
|
|
111
|
+
upr_tick = UniV3Utils.getMaxTick(tick_spacing)
|
|
112
|
+
join = Join()
|
|
113
|
+
join.apply(lp, "user", 1000, 10000, lwr_tick, upr_tick)
|
|
114
|
+
|
|
115
|
+
# Step 6: Perform swap
|
|
116
|
+
swap = Swap()
|
|
117
|
+
out = swap.apply(lp, tkn, "user", 10)
|
|
118
|
+
|
|
119
|
+
# Check reserves and liquidity
|
|
120
|
+
lp.summary()
|
|
121
|
+
|
|
122
|
+
# OUTPUT:
|
|
123
|
+
Exchange ETH-TKN (LP)
|
|
124
|
+
Real Reserves: ETH = 999.0039930189599, TKN = 10010.0
|
|
125
|
+
Gross Liquidity: 3162.277660168379
|
|
126
|
+
|
|
127
|
+
Balancer Example
|
|
128
|
+
--------------------------
|
|
129
|
+
|
|
130
|
+
from defipy import *
|
|
131
|
+
|
|
132
|
+
# Step 1: Define tokens
|
|
133
|
+
dai = ERC20("DAI", "0x111")
|
|
134
|
+
usdc = ERC20("USDC", "0x999")
|
|
135
|
+
|
|
136
|
+
# Step 2: Deposit token amounts
|
|
137
|
+
dai.deposit(None, 10000)
|
|
138
|
+
usdc.deposit(None, 20000)
|
|
139
|
+
|
|
140
|
+
# Step 3: Setup vault
|
|
141
|
+
vault = BalancerVault()
|
|
142
|
+
vault.add_token(dai, 10) # Denormalized weight for DAI
|
|
143
|
+
vault.add_token(usdc, 40) # Denormalized weight for WETH
|
|
144
|
+
|
|
145
|
+
# Step 4: Set up exchange data for Balancer
|
|
146
|
+
exch_data = BalancerExchangeData(vault=vault, symbol="BSP", address="0x3")
|
|
147
|
+
|
|
148
|
+
# Step 5: Initialize factor for Balancer
|
|
149
|
+
bfactory = BalancerFactory("WETH pool factory", "0x2")
|
|
150
|
+
|
|
151
|
+
# Step 6: Deploy pool
|
|
152
|
+
lp = bfactory.deploy(exch_data)
|
|
153
|
+
|
|
154
|
+
# Step 7: Join pool with initial liquidity
|
|
155
|
+
join = Join()
|
|
156
|
+
join.apply(lp, "user", 100) # Issue 100 pool shares
|
|
157
|
+
|
|
158
|
+
# Step 8: Perform swap
|
|
159
|
+
swap = Swap(Proc.SWAPIN)
|
|
160
|
+
out = swap.apply(lp, dai, usdc, "user", 10)
|
|
161
|
+
|
|
162
|
+
# Check reserves and liquidity
|
|
163
|
+
lp.summary()
|
|
164
|
+
|
|
165
|
+
# OUTPUT:
|
|
166
|
+
Balancer Exchange: DAI-USDC (BSP)
|
|
167
|
+
Reserves: DAI = 9979.92478694547, USDC = 20010
|
|
168
|
+
Weights: DAI = 0.2, USDC = 0.8
|
|
169
|
+
Pool Shares: 100
|
|
170
|
+
|
|
171
|
+
StableSwap Example
|
|
172
|
+
--------------------------
|
|
173
|
+
|
|
174
|
+
from defipy import *
|
|
175
|
+
|
|
176
|
+
# Step 1: Define stablecoins and parameters
|
|
177
|
+
dai = ERC20("DAI", "0x111", 18)
|
|
178
|
+
usdc = ERC20("USDC", "0x222", 6)
|
|
179
|
+
AMPL_COEFF = 2000
|
|
180
|
+
|
|
181
|
+
# Step 2: Deposit token amounts
|
|
182
|
+
dai.deposit(None, 10000)
|
|
183
|
+
usdc.deposit(None, 20000)
|
|
184
|
+
|
|
185
|
+
# Step 3: Setup Stableswap vault and add tokens
|
|
186
|
+
sgrp = StableswapVault()
|
|
187
|
+
sgrp.add_token(dai)
|
|
188
|
+
sgrp.add_token(usdc)
|
|
189
|
+
|
|
190
|
+
# Step 4: Set up exchange data for Stableswap
|
|
191
|
+
exch_data = StableswapExchangeData(vault = sgrp, symbol="LP", address="0x011")
|
|
192
|
+
|
|
193
|
+
# Step 5: Initialize factor for Balancer
|
|
194
|
+
factory = StableswapFactory("Stableswap factory", "0x2")
|
|
195
|
+
|
|
196
|
+
# Step 6: Deploy pool
|
|
197
|
+
lp = factory.deploy(exch_data)
|
|
198
|
+
|
|
199
|
+
# Step 7: Join pool with initial liquidity
|
|
200
|
+
join = Join()
|
|
201
|
+
join.apply(lp, "user", AMPL_COEFF)
|
|
202
|
+
|
|
203
|
+
# Step 8: Perform swap
|
|
204
|
+
swap = Swap()
|
|
205
|
+
out = swap.apply(lp, dai, usdc, "user", 10)
|
|
206
|
+
|
|
207
|
+
# Check reserves and liquidity
|
|
208
|
+
lp.summary()
|
|
209
|
+
|
|
210
|
+
# OUTPUT:
|
|
211
|
+
Stableswap Exchange: DAI-USDC (LP)
|
|
212
|
+
Reserves: DAI = 10010, USDC = 19989.996791
|
|
213
|
+
Liquidity: 29999.063056285642
|
|
214
|
+
|
|
215
|
+
## 0x Quant Terminal
|
|
216
|
+
|
|
217
|
+
This application utilizes the 0x API to produce a mock Uniswap pool which allows end-users to stress test
|
|
218
|
+
the limitations of a Uniswap pool setup using live price feeds from [0x API](https://0x.org); for backend setup, see
|
|
219
|
+
[notebook](https://github.com/defipy-devs/defipy/blob/main/notebooks/quant_terminal.ipynb)
|
|
220
|
+
|
|
221
|
+
Click [dashboard.defipy.org](https://dashboard.defipy.org/) for live link; for more detail see
|
|
222
|
+
[README](https://github.com/defipy-devs/defipy/tree/main/python/application/quant_terminal#readme)
|
|
223
|
+
|
|
224
|
+

|
|
225
|
+
|
|
226
|
+
### Run application locally
|
|
227
|
+
|
|
228
|
+
```
|
|
229
|
+
> bokeh serve --show python/application/quant_terminal/bokeh_server.py
|
|
230
|
+
```
|
|
@@ -36,7 +36,10 @@ python/prod/process/mint/__init__.py
|
|
|
36
36
|
python/prod/process/swap/Swap.py
|
|
37
37
|
python/prod/process/swap/__init__.py
|
|
38
38
|
python/prod/utils/client/__init__.py
|
|
39
|
+
python/prod/utils/client/contract/ExecuteScript.py
|
|
40
|
+
python/prod/utils/client/contract/__init__.py
|
|
39
41
|
python/prod/utils/data/__init__.py
|
|
40
42
|
python/prod/utils/interfaces/__init__.py
|
|
43
|
+
python/prod/utils/tools/UniswapScriptHelper.py
|
|
41
44
|
python/prod/utils/tools/__init__.py
|
|
42
45
|
python/prod/utils/tools/v3/__init__.py
|
defipy-1.0.3/LICENSE.txt
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License.
|
|
67
|
+
|
|
68
|
+
Subject to the terms and conditions of this License, each Contributor
|
|
69
|
+
hereby grants to You a perpetual, worldwide, non-exclusive,
|
|
70
|
+
no-charge, royalty-free, irrevocable copyright license to reproduce,
|
|
71
|
+
prepare Derivative Works of, publicly display, publicly perform,
|
|
72
|
+
sublicense, and distribute the Work and such Derivative Works in
|
|
73
|
+
Source or Object form.
|
|
74
|
+
|
|
75
|
+
3. Grant of Patent License.
|
|
76
|
+
|
|
77
|
+
Subject to the terms and conditions of this License, each Contributor
|
|
78
|
+
hereby grants to You a perpetual, worldwide, non-exclusive,
|
|
79
|
+
no-charge, royalty-free, irrevocable (except as stated in this
|
|
80
|
+
section) patent license to make, have made, use, offer to sell, sell,
|
|
81
|
+
import, and otherwise transfer the Work, where such license applies
|
|
82
|
+
only to those patent claims licensable by such Contributor that are
|
|
83
|
+
necessarily infringed by their Contribution(s) alone or by combination
|
|
84
|
+
of their Contribution(s) with the Work to which such Contribution(s)
|
|
85
|
+
was submitted. If You institute patent litigation against any entity
|
|
86
|
+
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
|
87
|
+
the Work or a Contribution incorporated within the Work constitutes
|
|
88
|
+
direct or contributory patent infringement, then any patent licenses
|
|
89
|
+
granted to You under this License for that Work shall terminate as of
|
|
90
|
+
the date such litigation is filed.
|
|
91
|
+
|
|
92
|
+
4. Redistribution.
|
|
93
|
+
|
|
94
|
+
You may reproduce and distribute copies of the Work or Derivative
|
|
95
|
+
Works thereof in any medium, with or without modifications, and in
|
|
96
|
+
Source or Object form, provided that You meet the following conditions:
|
|
97
|
+
|
|
98
|
+
(a) You must give any other recipients of the Work or
|
|
99
|
+
Derivative Works a copy of this License; and
|
|
100
|
+
|
|
101
|
+
(b) You must cause any modified files to carry prominent notices
|
|
102
|
+
stating that You changed the files; and
|
|
103
|
+
|
|
104
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
105
|
+
that You distribute, all copyright, patent, trademark, and
|
|
106
|
+
attribution notices from the Source form of the Work,
|
|
107
|
+
excluding those notices that do not pertain to any part of
|
|
108
|
+
the Derivative Works; and
|
|
109
|
+
|
|
110
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
111
|
+
distribution, then any Derivative Works that You distribute must
|
|
112
|
+
include a readable copy of the attribution notices contained
|
|
113
|
+
within such NOTICE file, excluding those notices that do not
|
|
114
|
+
pertain to any part of the Derivative Works, in at least one
|
|
115
|
+
of the following places: within a NOTICE text file distributed
|
|
116
|
+
as part of the Derivative Works; within the Source form or
|
|
117
|
+
documentation, if provided along with the Derivative Works; or,
|
|
118
|
+
within a display generated by the Derivative Works, if and
|
|
119
|
+
wherever such third-party notices normally appear. The contents
|
|
120
|
+
of the NOTICE file are for inform
|
defipy-1.0.3/PKG-INFO
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: DeFiPy
|
|
3
|
+
Version: 1.0.3
|
|
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: Apache-2.0
|
|
9
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
14
|
+
Classifier: Topic :: Scientific/Engineering :: Information Analysis
|
|
15
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE.txt
|
|
18
|
+
Requires-Dist: scipy>=1.7.3
|
|
19
|
+
Requires-Dist: bokeh==3.3.4
|
|
20
|
+
Requires-Dist: uniswappy==1.7.0
|
|
21
|
+
Requires-Dist: stableswappy==1.0.0
|
|
22
|
+
Requires-Dist: balancerpy==1.0.1
|
|
23
|
+
|
|
24
|
+
# DeFiPy: Python SDK for DeFi Analytics and Agents
|
|
25
|
+
|
|
26
|
+
DeFiPy is the first unified Python SDK for DeFi analytics, simulation, and autonomous agents. Built with modularity in mind, DeFiPy lets you isolate and extend your analytics by protocol using:
|
|
27
|
+
|
|
28
|
+
* [UniswapPy](https://github.com/defipy-devs/uniswappy)
|
|
29
|
+
* [BalancerPy](https://github.com/defipy-devs/balancerpy)
|
|
30
|
+
* [StableSwapPy](https://github.com/defipy-devs/stableswappy)
|
|
31
|
+
|
|
32
|
+
For onchain event access and scripting, pair it with [Web3Scout](https://github.com/defipy-devs/web3scout) — a companion tool for [decoding pool events](https://defipy.readthedocs.io/en/latest/onchain/pool_events.html) and [interfacing with Solidity contracts](https://defipy.readthedocs.io/en/latest/onchain/testnet_sim_univ2.html). Whether you’re building dashboards, simulations, or agent-based trading systems, DeFiPy + Web3Scout deliver a uniquely powerful toolset — unlike anything else in the ecosystem.
|
|
33
|
+
|
|
34
|
+
## Docs
|
|
35
|
+
Visit [DeFiPy docs](https://defipy.org) for full documentation with walk-through tutorials
|
|
36
|
+
|
|
37
|
+
## Install
|
|
38
|
+
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:
|
|
39
|
+
```
|
|
40
|
+
> pip install gmpy2
|
|
41
|
+
```
|
|
42
|
+
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:
|
|
43
|
+
```
|
|
44
|
+
> git clone https://github.com/defipy-devs/defipy
|
|
45
|
+
> pip install .
|
|
46
|
+
```
|
|
47
|
+
or
|
|
48
|
+
```
|
|
49
|
+
> pip install defipy
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Uniswap V2 Example
|
|
53
|
+
--------------------------
|
|
54
|
+
|
|
55
|
+
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:
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
from defipy import *
|
|
59
|
+
|
|
60
|
+
# Step 1: Define tokens
|
|
61
|
+
tkn = ERC20("TKN", "0x111")
|
|
62
|
+
eth = ERC20("ETH", "0x999")
|
|
63
|
+
|
|
64
|
+
# Step 2: Initialize factory
|
|
65
|
+
factory = UniswapFactory("ETH pool factory", "0x2")
|
|
66
|
+
|
|
67
|
+
# Step 3: Set up exchange data for V2
|
|
68
|
+
exch_data = UniswapExchangeData(tkn0=eth, tkn1=tkn, symbol="LP", address="0x3")
|
|
69
|
+
|
|
70
|
+
# Step 4: Deploy pool
|
|
71
|
+
lp = factory.deploy(exch_data)
|
|
72
|
+
|
|
73
|
+
# Step 5: Add initial liquidity
|
|
74
|
+
join = Join()
|
|
75
|
+
join.apply(lp, "user", 1000, 10000)
|
|
76
|
+
|
|
77
|
+
# Step 6: Perform swap
|
|
78
|
+
swap = Swap()
|
|
79
|
+
out = swap.apply(lp, tkn, "user", 10)
|
|
80
|
+
|
|
81
|
+
# Check reserves and liquidity
|
|
82
|
+
lp.summary()
|
|
83
|
+
|
|
84
|
+
# OUTPUT:
|
|
85
|
+
Exchange ETH-TKN (LP)
|
|
86
|
+
Reserves: ETH = 999.00399301896, TKN = 10010.0
|
|
87
|
+
Liquidity: 3162.2776601683795
|
|
88
|
+
|
|
89
|
+
Uniswap V3 Example
|
|
90
|
+
--------------------------
|
|
91
|
+
|
|
92
|
+
from defipy import *
|
|
93
|
+
|
|
94
|
+
# Step 1: Define tokens and parameters
|
|
95
|
+
eth = ERC20("ETH", "0x93")
|
|
96
|
+
tkn = ERC20("TKN", "0x111")
|
|
97
|
+
tick_spacing = 60
|
|
98
|
+
fee = 3000 # 0.3% fee tier
|
|
99
|
+
|
|
100
|
+
# Step 2: Set up exchange data for V3
|
|
101
|
+
exch_data = UniswapExchangeData(tkn0=eth, tkn1=tkn, symbol="LP", address="0x811", version='V3', tick_spacing=tick_spacing, fee=fee)
|
|
102
|
+
|
|
103
|
+
# Step 3: Initialize factory
|
|
104
|
+
factory = UniswapFactory("ETH pool factory", "0x2")
|
|
105
|
+
|
|
106
|
+
# Step 4: Deploy pool
|
|
107
|
+
lp = factory.deploy(exch_data)
|
|
108
|
+
|
|
109
|
+
# Step 5: Add initial liquidity within tick range
|
|
110
|
+
lwr_tick = UniV3Utils.getMinTick(tick_spacing)
|
|
111
|
+
upr_tick = UniV3Utils.getMaxTick(tick_spacing)
|
|
112
|
+
join = Join()
|
|
113
|
+
join.apply(lp, "user", 1000, 10000, lwr_tick, upr_tick)
|
|
114
|
+
|
|
115
|
+
# Step 6: Perform swap
|
|
116
|
+
swap = Swap()
|
|
117
|
+
out = swap.apply(lp, tkn, "user", 10)
|
|
118
|
+
|
|
119
|
+
# Check reserves and liquidity
|
|
120
|
+
lp.summary()
|
|
121
|
+
|
|
122
|
+
# OUTPUT:
|
|
123
|
+
Exchange ETH-TKN (LP)
|
|
124
|
+
Real Reserves: ETH = 999.0039930189599, TKN = 10010.0
|
|
125
|
+
Gross Liquidity: 3162.277660168379
|
|
126
|
+
|
|
127
|
+
Balancer Example
|
|
128
|
+
--------------------------
|
|
129
|
+
|
|
130
|
+
from defipy import *
|
|
131
|
+
|
|
132
|
+
# Step 1: Define tokens
|
|
133
|
+
dai = ERC20("DAI", "0x111")
|
|
134
|
+
usdc = ERC20("USDC", "0x999")
|
|
135
|
+
|
|
136
|
+
# Step 2: Deposit token amounts
|
|
137
|
+
dai.deposit(None, 10000)
|
|
138
|
+
usdc.deposit(None, 20000)
|
|
139
|
+
|
|
140
|
+
# Step 3: Setup vault
|
|
141
|
+
vault = BalancerVault()
|
|
142
|
+
vault.add_token(dai, 10) # Denormalized weight for DAI
|
|
143
|
+
vault.add_token(usdc, 40) # Denormalized weight for WETH
|
|
144
|
+
|
|
145
|
+
# Step 4: Set up exchange data for Balancer
|
|
146
|
+
exch_data = BalancerExchangeData(vault=vault, symbol="BSP", address="0x3")
|
|
147
|
+
|
|
148
|
+
# Step 5: Initialize factor for Balancer
|
|
149
|
+
bfactory = BalancerFactory("WETH pool factory", "0x2")
|
|
150
|
+
|
|
151
|
+
# Step 6: Deploy pool
|
|
152
|
+
lp = bfactory.deploy(exch_data)
|
|
153
|
+
|
|
154
|
+
# Step 7: Join pool with initial liquidity
|
|
155
|
+
join = Join()
|
|
156
|
+
join.apply(lp, "user", 100) # Issue 100 pool shares
|
|
157
|
+
|
|
158
|
+
# Step 8: Perform swap
|
|
159
|
+
swap = Swap(Proc.SWAPIN)
|
|
160
|
+
out = swap.apply(lp, dai, usdc, "user", 10)
|
|
161
|
+
|
|
162
|
+
# Check reserves and liquidity
|
|
163
|
+
lp.summary()
|
|
164
|
+
|
|
165
|
+
# OUTPUT:
|
|
166
|
+
Balancer Exchange: DAI-USDC (BSP)
|
|
167
|
+
Reserves: DAI = 9979.92478694547, USDC = 20010
|
|
168
|
+
Weights: DAI = 0.2, USDC = 0.8
|
|
169
|
+
Pool Shares: 100
|
|
170
|
+
|
|
171
|
+
StableSwap Example
|
|
172
|
+
--------------------------
|
|
173
|
+
|
|
174
|
+
from defipy import *
|
|
175
|
+
|
|
176
|
+
# Step 1: Define stablecoins and parameters
|
|
177
|
+
dai = ERC20("DAI", "0x111", 18)
|
|
178
|
+
usdc = ERC20("USDC", "0x222", 6)
|
|
179
|
+
AMPL_COEFF = 2000
|
|
180
|
+
|
|
181
|
+
# Step 2: Deposit token amounts
|
|
182
|
+
dai.deposit(None, 10000)
|
|
183
|
+
usdc.deposit(None, 20000)
|
|
184
|
+
|
|
185
|
+
# Step 3: Setup Stableswap vault and add tokens
|
|
186
|
+
sgrp = StableswapVault()
|
|
187
|
+
sgrp.add_token(dai)
|
|
188
|
+
sgrp.add_token(usdc)
|
|
189
|
+
|
|
190
|
+
# Step 4: Set up exchange data for Stableswap
|
|
191
|
+
exch_data = StableswapExchangeData(vault = sgrp, symbol="LP", address="0x011")
|
|
192
|
+
|
|
193
|
+
# Step 5: Initialize factor for Balancer
|
|
194
|
+
factory = StableswapFactory("Stableswap factory", "0x2")
|
|
195
|
+
|
|
196
|
+
# Step 6: Deploy pool
|
|
197
|
+
lp = factory.deploy(exch_data)
|
|
198
|
+
|
|
199
|
+
# Step 7: Join pool with initial liquidity
|
|
200
|
+
join = Join()
|
|
201
|
+
join.apply(lp, "user", AMPL_COEFF)
|
|
202
|
+
|
|
203
|
+
# Step 8: Perform swap
|
|
204
|
+
swap = Swap()
|
|
205
|
+
out = swap.apply(lp, dai, usdc, "user", 10)
|
|
206
|
+
|
|
207
|
+
# Check reserves and liquidity
|
|
208
|
+
lp.summary()
|
|
209
|
+
|
|
210
|
+
# OUTPUT:
|
|
211
|
+
Stableswap Exchange: DAI-USDC (LP)
|
|
212
|
+
Reserves: DAI = 10010, USDC = 19989.996791
|
|
213
|
+
Liquidity: 29999.063056285642
|
|
214
|
+
|
|
215
|
+
## 0x Quant Terminal
|
|
216
|
+
|
|
217
|
+
This application utilizes the 0x API to produce a mock Uniswap pool which allows end-users to stress test
|
|
218
|
+
the limitations of a Uniswap pool setup using live price feeds from [0x API](https://0x.org); for backend setup, see
|
|
219
|
+
[notebook](https://github.com/defipy-devs/defipy/blob/main/notebooks/quant_terminal.ipynb)
|
|
220
|
+
|
|
221
|
+
Click [dashboard.defipy.org](https://dashboard.defipy.org/) for live link; for more detail see
|
|
222
|
+
[README](https://github.com/defipy-devs/defipy/tree/main/python/application/quant_terminal#readme)
|
|
223
|
+
|
|
224
|
+

|
|
225
|
+
|
|
226
|
+
### Run application locally
|
|
227
|
+
|
|
228
|
+
```
|
|
229
|
+
> bokeh serve --show python/application/quant_terminal/bokeh_server.py
|
|
230
|
+
```
|