DeFiPy 1.0.9__py3-none-any.whl
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/__init__.py +51 -0
- defipy/agents/ImpermanentLossAgent.py +182 -0
- defipy/agents/PriceThresholdSwapAgent.py +169 -0
- defipy/agents/TVLBasedLiquidityExitAgent.py +174 -0
- defipy/agents/VolumeSpikeNotifierAgent.py +190 -0
- defipy/agents/__init__.py +4 -0
- defipy/agents/config/ImpermanentLossConfig.py +28 -0
- defipy/agents/config/PriceThresholdConfig.py +27 -0
- defipy/agents/config/TVLExitConfig.py +28 -0
- defipy/agents/config/VolumeSpikeConfig.py +27 -0
- defipy/agents/config/__init__.py +7 -0
- defipy/agents/data/UniswapPoolData.py +26 -0
- defipy/agents/data/__init__.py +1 -0
- defipy/analytics/risk/__init__.py +1 -0
- defipy/analytics/simulate/__init__.py +1 -0
- defipy/erc/__init__.py +1 -0
- defipy/math/basic/__init__.py +1 -0
- defipy/math/interest/__init__.py +1 -0
- defipy/math/interest/ips/__init__.py +1 -0
- defipy/math/interest/ips/aggregate/__init__.py +1 -0
- defipy/math/model/__init__.py +1 -0
- defipy/math/risk/__init__.py +1 -0
- defipy/process/__init__.py +1 -0
- defipy/process/burn/__init__.py +1 -0
- defipy/process/deposit/__init__.py +1 -0
- defipy/process/join/Join.py +57 -0
- defipy/process/join/__init__.py +2 -0
- defipy/process/liquidity/AddLiquidity.py +57 -0
- defipy/process/liquidity/RemoveLiquidity.py +57 -0
- defipy/process/liquidity/__init__.py +2 -0
- defipy/process/mint/__init__.py +1 -0
- defipy/process/swap/Swap.py +57 -0
- defipy/process/swap/__init__.py +2 -0
- defipy/utils/client/__init__.py +1 -0
- defipy/utils/client/contract/ExecuteScript.py +57 -0
- defipy/utils/client/contract/__init__.py +1 -0
- defipy/utils/data/__init__.py +1 -0
- defipy/utils/interfaces/__init__.py +1 -0
- defipy/utils/tools/UniswapScriptHelper.py +81 -0
- defipy/utils/tools/__init__.py +2 -0
- defipy/utils/tools/v3/__init__.py +1 -0
- defipy-1.0.9.dist-info/METADATA +247 -0
- defipy-1.0.9.dist-info/RECORD +47 -0
- defipy-1.0.9.dist-info/WHEEL +5 -0
- defipy-1.0.9.dist-info/licenses/LICENSE +178 -0
- defipy-1.0.9.dist-info/licenses/NOTICE +16 -0
- defipy-1.0.9.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
2
|
+
# Apache 2.0 License (DeFiPy)
|
|
3
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
4
|
+
# Copyright 2023–2025 Ian Moore
|
|
5
|
+
# Email: defipy.devs@gmail.com
|
|
6
|
+
#
|
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
# you may not use this file except in compliance with the License.
|
|
9
|
+
# You may obtain a copy of the License at
|
|
10
|
+
#
|
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
#
|
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
# See the License for the specific language governing permissions and
|
|
17
|
+
# limitations under the License.
|
|
18
|
+
|
|
19
|
+
import subprocess
|
|
20
|
+
|
|
21
|
+
FORGE_CMD = 'forge'
|
|
22
|
+
SCRIPT_CMD = 'script'
|
|
23
|
+
SILENT_OPT = '--silent'
|
|
24
|
+
SKIP_SIM_OPT = '--skip-simulation'
|
|
25
|
+
SIG_OPT = '--sig'
|
|
26
|
+
SIG_OPT = '--sig'
|
|
27
|
+
RPC_OPT = '--rpc-url'
|
|
28
|
+
BROADCAST_OPT = '--broadcast'
|
|
29
|
+
|
|
30
|
+
class ExecuteScript():
|
|
31
|
+
|
|
32
|
+
def __init__(self, test_directory):
|
|
33
|
+
self.test_directory = test_directory
|
|
34
|
+
pass
|
|
35
|
+
|
|
36
|
+
def apply(self, script_path, rpc_url, args = [], verbose = True, skip_sim = True):
|
|
37
|
+
shell_cmd = self._exe_cmd(script_path, rpc_url, args=args, verbose=verbose, skip_sim=skip_sim)
|
|
38
|
+
p = subprocess.Popen(shell_cmd, cwd=self.test_directory)
|
|
39
|
+
p.wait()
|
|
40
|
+
|
|
41
|
+
def _exe_cmd(self, script_path, rpc_url, verbose = True, skip_sim = True, args = []):
|
|
42
|
+
silent_cmd = SILENT_OPT if not verbose else ''
|
|
43
|
+
skip_sim_cmd = SKIP_SIM_OPT if skip_sim else ''
|
|
44
|
+
deploy_sig = [] if len(args) == 0 else [SIG_OPT]
|
|
45
|
+
deploy_arg_type = [] if len(args) == 0 else ['run('+','.join('uint256' for e in args)+')']
|
|
46
|
+
deploy_args = [] if len(args) == 0 else [str(e) for e in args]
|
|
47
|
+
|
|
48
|
+
shell_cmd1 = [FORGE_CMD, SCRIPT_CMD, script_path]
|
|
49
|
+
shell_cmd2 = deploy_args + deploy_sig + deploy_arg_type
|
|
50
|
+
shell_cmd3 = [ '--ignored-error-codes', '2072',
|
|
51
|
+
'--ignored-error-codes', '6321',
|
|
52
|
+
'--ignored-error-codes', '5667',
|
|
53
|
+
'--ignored-error-codes', '5574',
|
|
54
|
+
'--ignored-error-codes', '2018']
|
|
55
|
+
shell_cmd4 = [RPC_OPT, rpc_url, BROADCAST_OPT, silent_cmd, skip_sim_cmd]
|
|
56
|
+
shell_cmd = shell_cmd1 + shell_cmd2 + shell_cmd3 + shell_cmd4
|
|
57
|
+
return shell_cmd
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .ExecuteScript import ExecuteScript
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from uniswappy.utils.data import *
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from uniswappy.utils.interfaces import *
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
2
|
+
# Apache 2.0 License (DeFiPy)
|
|
3
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
4
|
+
# Copyright 2023–2025 Ian Moore
|
|
5
|
+
# Email: defipy.devs@gmail.com
|
|
6
|
+
#
|
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
# you may not use this file except in compliance with the License.
|
|
9
|
+
# You may obtain a copy of the License at
|
|
10
|
+
#
|
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
#
|
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
# See the License for the specific language governing permissions and
|
|
17
|
+
# limitations under the License.
|
|
18
|
+
|
|
19
|
+
from uniswappy.utils.tools.v3 import UniV3Helper
|
|
20
|
+
from uniswappy.process.swap import WithdrawSwap
|
|
21
|
+
from uniswappy.process.deposit import SwapDeposit
|
|
22
|
+
|
|
23
|
+
class UniswapScriptHelper():
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
""" Uniswap script helper functions
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
def __init__(self):
|
|
30
|
+
pass
|
|
31
|
+
|
|
32
|
+
def calc_arb(self, lp, sDel, tkn_x, tkn_y, user_nm, p):
|
|
33
|
+
swap_dx, swap_dy = sDel.calc(p, 1, 1)
|
|
34
|
+
direction = 0;
|
|
35
|
+
|
|
36
|
+
if(swap_dx >= 0):
|
|
37
|
+
expected_amount_dep = SwapDeposit().apply(lp, tkn_x, user_nm, abs(swap_dx))
|
|
38
|
+
expected_amount_out = WithdrawSwap().apply(lp, tkn_y, user_nm, abs(swap_dy))
|
|
39
|
+
dep_tkn_x = abs(swap_dx); wd_tkn_x = 0
|
|
40
|
+
wd_tkn_y = abs(swap_dy); dep_tkn_y = 0
|
|
41
|
+
direction = 0
|
|
42
|
+
elif(swap_dy >= 0):
|
|
43
|
+
expected_amount_dep = SwapDeposit().apply(lp, tkn_y, user_nm, abs(swap_dy))
|
|
44
|
+
expected_amount_out = WithdrawSwap().apply(lp, tkn_x, user_nm, abs(swap_dx))
|
|
45
|
+
dep_tkn_y = abs(swap_dy); wd_tkn_y = 0
|
|
46
|
+
wd_tkn_x = abs(swap_dx); dep_tkn_x = 0
|
|
47
|
+
direction = 1
|
|
48
|
+
|
|
49
|
+
dep_tkn_x = UniV3Helper().dec2gwei(dep_tkn_x)
|
|
50
|
+
dep_tkn_y = UniV3Helper().dec2gwei(dep_tkn_y)
|
|
51
|
+
wd_tkn_x = UniV3Helper().dec2gwei(wd_tkn_x)
|
|
52
|
+
wd_tkn_y = UniV3Helper().dec2gwei(wd_tkn_y)
|
|
53
|
+
|
|
54
|
+
return (dep_tkn_x, dep_tkn_y, wd_tkn_x, wd_tkn_y, direction)
|
|
55
|
+
|
|
56
|
+
def calc_arb_contract(self, lp, sDel, tkn_x, tkn_y, user_nm, p):
|
|
57
|
+
swap_dx, swap_dy = sDel.calc(p, 1, 1)
|
|
58
|
+
direction = 0;
|
|
59
|
+
|
|
60
|
+
if(swap_dx >= 0):
|
|
61
|
+
dep_tkn_x = abs(swap_dx); wd_tkn_x = 0
|
|
62
|
+
wd_tkn_y = abs(swap_dy); dep_tkn_y = 0
|
|
63
|
+
direction = 0
|
|
64
|
+
elif(swap_dy >= 0):
|
|
65
|
+
dep_tkn_y = abs(swap_dy); wd_tkn_y = 0
|
|
66
|
+
wd_tkn_x = abs(swap_dx); dep_tkn_x = 0
|
|
67
|
+
direction = 1
|
|
68
|
+
|
|
69
|
+
dep_tkn_x = UniV3Helper().dec2gwei(dep_tkn_x)
|
|
70
|
+
dep_tkn_y = UniV3Helper().dec2gwei(dep_tkn_y)
|
|
71
|
+
wd_tkn_x = UniV3Helper().dec2gwei(wd_tkn_x)
|
|
72
|
+
wd_tkn_y = UniV3Helper().dec2gwei(wd_tkn_y)
|
|
73
|
+
|
|
74
|
+
return (dep_tkn_x, dep_tkn_y, wd_tkn_x, wd_tkn_y, direction)
|
|
75
|
+
|
|
76
|
+
def pool_state(self, pool_contract, verbose=False):
|
|
77
|
+
reserves = pool_contract.functions.getReserves().call()
|
|
78
|
+
lp_amt = pool_contract.functions.totalSupply().call()
|
|
79
|
+
if(verbose):
|
|
80
|
+
print(f'TKN0 {UniV3Helper().gwei2dec(reserves[0]):.2f} / TKN1 {UniV3Helper().gwei2dec(reserves[1]):.2f} / Liquidity {UniV3Helper().gwei2dec(lp_amt):.2f}')
|
|
81
|
+
return (reserves[0], reserves[1], lp_amt)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from uniswappy.utils.tools.v3 import *
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: DeFiPy
|
|
3
|
+
Version: 1.0.9
|
|
4
|
+
Summary: Python SDK for DeFi Analytics, Simulation, and Agents
|
|
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
|
|
18
|
+
License-File: NOTICE
|
|
19
|
+
Requires-Dist: scipy>=1.7.3
|
|
20
|
+
Requires-Dist: pydantic>=2.11.0
|
|
21
|
+
Requires-Dist: bokeh==3.3.4
|
|
22
|
+
Requires-Dist: uniswappy>=1.7.4
|
|
23
|
+
Requires-Dist: stableswappy>=1.0.3
|
|
24
|
+
Requires-Dist: balancerpy>=1.0.4
|
|
25
|
+
Dynamic: author
|
|
26
|
+
Dynamic: author-email
|
|
27
|
+
Dynamic: classifier
|
|
28
|
+
Dynamic: description
|
|
29
|
+
Dynamic: description-content-type
|
|
30
|
+
Dynamic: home-page
|
|
31
|
+
Dynamic: license
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
Dynamic: requires-dist
|
|
34
|
+
Dynamic: summary
|
|
35
|
+
|
|
36
|
+
# DeFiPy: Python SDK for DeFi Analytics and Agents
|
|
37
|
+
|
|
38
|
+
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:
|
|
39
|
+
|
|
40
|
+
* [UniswapPy](https://github.com/defipy-devs/uniswappy)
|
|
41
|
+
* [BalancerPy](https://github.com/defipy-devs/balancerpy)
|
|
42
|
+
* [StableSwapPy](https://github.com/defipy-devs/stableswappy)
|
|
43
|
+
|
|
44
|
+
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.
|
|
45
|
+
|
|
46
|
+
## Docs
|
|
47
|
+
Visit [DeFiPy docs](https://defipy.org) for full documentation with walk-through tutorials
|
|
48
|
+
|
|
49
|
+
## Install
|
|
50
|
+
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:
|
|
51
|
+
```
|
|
52
|
+
> pip install gmpy2
|
|
53
|
+
```
|
|
54
|
+
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:
|
|
55
|
+
```
|
|
56
|
+
> git clone https://github.com/defipy-devs/defipy
|
|
57
|
+
> pip install .
|
|
58
|
+
```
|
|
59
|
+
or
|
|
60
|
+
```
|
|
61
|
+
> pip install defipy
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Uniswap V2 Example
|
|
65
|
+
--------------------------
|
|
66
|
+
|
|
67
|
+
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:
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
from defipy import *
|
|
71
|
+
|
|
72
|
+
# Step 1: Define tokens
|
|
73
|
+
tkn = ERC20("TKN", "0x111")
|
|
74
|
+
eth = ERC20("ETH", "0x999")
|
|
75
|
+
|
|
76
|
+
# Step 2: Initialize factory
|
|
77
|
+
factory = UniswapFactory("ETH pool factory", "0x2")
|
|
78
|
+
|
|
79
|
+
# Step 3: Set up exchange data for V2
|
|
80
|
+
exch_data = UniswapExchangeData(tkn0=eth, tkn1=tkn, symbol="LP", address="0x3")
|
|
81
|
+
|
|
82
|
+
# Step 4: Deploy pool
|
|
83
|
+
lp = factory.deploy(exch_data)
|
|
84
|
+
|
|
85
|
+
# Step 5: Add initial liquidity
|
|
86
|
+
join = Join()
|
|
87
|
+
join.apply(lp, "user", 1000, 10000)
|
|
88
|
+
|
|
89
|
+
# Step 6: Perform swap
|
|
90
|
+
swap = Swap()
|
|
91
|
+
out = swap.apply(lp, tkn, "user", 10)
|
|
92
|
+
|
|
93
|
+
# Check reserves and liquidity
|
|
94
|
+
lp.summary()
|
|
95
|
+
|
|
96
|
+
# OUTPUT:
|
|
97
|
+
Exchange ETH-TKN (LP)
|
|
98
|
+
Reserves: ETH = 999.00399301896, TKN = 10010.0
|
|
99
|
+
Liquidity: 3162.2776601683795
|
|
100
|
+
|
|
101
|
+
Uniswap V3 Example
|
|
102
|
+
--------------------------
|
|
103
|
+
|
|
104
|
+
from defipy import *
|
|
105
|
+
|
|
106
|
+
# Step 1: Define tokens and parameters
|
|
107
|
+
eth = ERC20("ETH", "0x93")
|
|
108
|
+
tkn = ERC20("TKN", "0x111")
|
|
109
|
+
tick_spacing = 60
|
|
110
|
+
fee = 3000 # 0.3% fee tier
|
|
111
|
+
|
|
112
|
+
# Step 2: Set up exchange data for V3
|
|
113
|
+
exch_data = UniswapExchangeData(tkn0=eth, tkn1=tkn, symbol="LP", address="0x811", version='V3', tick_spacing=tick_spacing, fee=fee)
|
|
114
|
+
|
|
115
|
+
# Step 3: Initialize factory
|
|
116
|
+
factory = UniswapFactory("ETH pool factory", "0x2")
|
|
117
|
+
|
|
118
|
+
# Step 4: Deploy pool
|
|
119
|
+
lp = factory.deploy(exch_data)
|
|
120
|
+
|
|
121
|
+
# Step 5: Add initial liquidity within tick range
|
|
122
|
+
lwr_tick = UniV3Utils.getMinTick(tick_spacing)
|
|
123
|
+
upr_tick = UniV3Utils.getMaxTick(tick_spacing)
|
|
124
|
+
join = Join()
|
|
125
|
+
join.apply(lp, "user", 1000, 10000, lwr_tick, upr_tick)
|
|
126
|
+
|
|
127
|
+
# Step 6: Perform swap
|
|
128
|
+
swap = Swap()
|
|
129
|
+
out = swap.apply(lp, tkn, "user", 10)
|
|
130
|
+
|
|
131
|
+
# Check reserves and liquidity
|
|
132
|
+
lp.summary()
|
|
133
|
+
|
|
134
|
+
# OUTPUT:
|
|
135
|
+
Exchange ETH-TKN (LP)
|
|
136
|
+
Real Reserves: ETH = 999.0039930189599, TKN = 10010.0
|
|
137
|
+
Gross Liquidity: 3162.277660168379
|
|
138
|
+
|
|
139
|
+
Balancer Example
|
|
140
|
+
--------------------------
|
|
141
|
+
|
|
142
|
+
from defipy import *
|
|
143
|
+
|
|
144
|
+
# Step 1: Define tokens
|
|
145
|
+
dai = ERC20("DAI", "0x111")
|
|
146
|
+
usdc = ERC20("USDC", "0x999")
|
|
147
|
+
|
|
148
|
+
# Step 2: Deposit token amounts
|
|
149
|
+
dai.deposit(None, 10000)
|
|
150
|
+
usdc.deposit(None, 20000)
|
|
151
|
+
|
|
152
|
+
# Step 3: Setup vault
|
|
153
|
+
vault = BalancerVault()
|
|
154
|
+
vault.add_token(dai, 10) # Denormalized weight for DAI
|
|
155
|
+
vault.add_token(usdc, 40) # Denormalized weight for WETH
|
|
156
|
+
|
|
157
|
+
# Step 4: Set up exchange data for Balancer
|
|
158
|
+
exch_data = BalancerExchangeData(vault=vault, symbol="BSP", address="0x3")
|
|
159
|
+
|
|
160
|
+
# Step 5: Initialize factor for Balancer
|
|
161
|
+
bfactory = BalancerFactory("WETH pool factory", "0x2")
|
|
162
|
+
|
|
163
|
+
# Step 6: Deploy pool
|
|
164
|
+
lp = bfactory.deploy(exch_data)
|
|
165
|
+
|
|
166
|
+
# Step 7: Join pool with initial liquidity
|
|
167
|
+
join = Join()
|
|
168
|
+
join.apply(lp, "user", 100) # Issue 100 pool shares
|
|
169
|
+
|
|
170
|
+
# Step 8: Perform swap
|
|
171
|
+
swap = Swap(Proc.SWAPIN)
|
|
172
|
+
out = swap.apply(lp, dai, usdc, "user", 10)
|
|
173
|
+
|
|
174
|
+
# Check reserves and liquidity
|
|
175
|
+
lp.summary()
|
|
176
|
+
|
|
177
|
+
# OUTPUT:
|
|
178
|
+
Balancer Exchange: DAI-USDC (BSP)
|
|
179
|
+
Reserves: DAI = 9979.92478694547, USDC = 20010
|
|
180
|
+
Weights: DAI = 0.2, USDC = 0.8
|
|
181
|
+
Pool Shares: 100
|
|
182
|
+
|
|
183
|
+
StableSwap Example
|
|
184
|
+
--------------------------
|
|
185
|
+
|
|
186
|
+
from defipy import *
|
|
187
|
+
|
|
188
|
+
# Step 1: Define stablecoins and parameters
|
|
189
|
+
dai = ERC20("DAI", "0x111", 18)
|
|
190
|
+
usdc = ERC20("USDC", "0x222", 6)
|
|
191
|
+
AMPL_COEFF = 2000
|
|
192
|
+
|
|
193
|
+
# Step 2: Deposit token amounts
|
|
194
|
+
dai.deposit(None, 10000)
|
|
195
|
+
usdc.deposit(None, 20000)
|
|
196
|
+
|
|
197
|
+
# Step 3: Setup Stableswap vault and add tokens
|
|
198
|
+
sgrp = StableswapVault()
|
|
199
|
+
sgrp.add_token(dai)
|
|
200
|
+
sgrp.add_token(usdc)
|
|
201
|
+
|
|
202
|
+
# Step 4: Set up exchange data for Stableswap
|
|
203
|
+
exch_data = StableswapExchangeData(vault = sgrp, symbol="LP", address="0x011")
|
|
204
|
+
|
|
205
|
+
# Step 5: Initialize factor for Balancer
|
|
206
|
+
factory = StableswapFactory("Stableswap factory", "0x2")
|
|
207
|
+
|
|
208
|
+
# Step 6: Deploy pool
|
|
209
|
+
lp = factory.deploy(exch_data)
|
|
210
|
+
|
|
211
|
+
# Step 7: Join pool with initial liquidity
|
|
212
|
+
join = Join()
|
|
213
|
+
join.apply(lp, "user", AMPL_COEFF)
|
|
214
|
+
|
|
215
|
+
# Step 8: Perform swap
|
|
216
|
+
swap = Swap()
|
|
217
|
+
out = swap.apply(lp, dai, usdc, "user", 10)
|
|
218
|
+
|
|
219
|
+
# Check reserves and liquidity
|
|
220
|
+
lp.summary()
|
|
221
|
+
|
|
222
|
+
# OUTPUT:
|
|
223
|
+
Stableswap Exchange: DAI-USDC (LP)
|
|
224
|
+
Reserves: DAI = 10010, USDC = 19989.996791
|
|
225
|
+
Liquidity: 29999.063056285642
|
|
226
|
+
|
|
227
|
+
## 0x Quant Terminal
|
|
228
|
+
|
|
229
|
+
This application utilizes the 0x API to produce a mock Uniswap pool which allows end-users to stress test
|
|
230
|
+
the limitations of a Uniswap pool setup using live price feeds from [0x API](https://0x.org); for backend setup, see
|
|
231
|
+
[notebook](https://github.com/defipy-devs/defipy/blob/main/notebooks/quant_terminal.ipynb)
|
|
232
|
+
|
|
233
|
+
Click [dashboard.defipy.org](https://dashboard.defipy.org/) for live link; for more detail see
|
|
234
|
+
[README](https://github.com/defipy-devs/defipy/tree/main/python/application/quant_terminal#readme)
|
|
235
|
+
|
|
236
|
+

|
|
237
|
+
|
|
238
|
+
### Run application locally
|
|
239
|
+
|
|
240
|
+
```
|
|
241
|
+
> bokeh serve --show python/application/quant_terminal/bokeh_server.py
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
## License
|
|
245
|
+
Licensed under the Apache License, Version 2.0.
|
|
246
|
+
See [LICENSE](./LICENSE) and [NOTICE](./NOTICE) for details.
|
|
247
|
+
Portions of this project may include code from third-party projects under compatible open-source licenses.
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
defipy/__init__.py,sha256=zpvOH7sicTLAExJYOIVqCHqVuiTqe58ARb7CaHapTTo,1574
|
|
2
|
+
defipy/agents/ImpermanentLossAgent.py,sha256=bKfDVqsZM-kHiZ40aTjeQPsw9hav674JYSBxMarSc-Y,7747
|
|
3
|
+
defipy/agents/PriceThresholdSwapAgent.py,sha256=pxfUrtrVMWncvn7TVI-UaRI6WMbCSiwBAFqkK0_qfZ0,7136
|
|
4
|
+
defipy/agents/TVLBasedLiquidityExitAgent.py,sha256=4JxmHgtt5pOR_RKoGEJ6Jftpi29szcKh8o13Vj7CEDk,7402
|
|
5
|
+
defipy/agents/VolumeSpikeNotifierAgent.py,sha256=A5Z4l8IN3hX9JY85CirxrKnNRJ1xExv84NyXQTqdc6E,7983
|
|
6
|
+
defipy/agents/__init__.py,sha256=OuV06cRnX5A7M4vjtlGPbfHEK-NAkO-wxK3OQBZZuhQ,246
|
|
7
|
+
defipy/agents/config/ImpermanentLossConfig.py,sha256=Kd2pd34nrrKTF49bTtTSVWfLO4wDzA9j3CeExzWl6ME,1694
|
|
8
|
+
defipy/agents/config/PriceThresholdConfig.py,sha256=US8hyYFzVsJEtDdOw_KlSDvuJs2-EZiMQwk5nsR16EI,1543
|
|
9
|
+
defipy/agents/config/TVLExitConfig.py,sha256=3KP2YbQpJ6Y4qSx3bwG6AuSsSEkTrGreMCu_WWWzp0Y,1585
|
|
10
|
+
defipy/agents/config/VolumeSpikeConfig.py,sha256=SIywGS0jWAAMnnNo3vMGpv9-cl3ZH2oUiDJNGOZbE_g,1578
|
|
11
|
+
defipy/agents/config/__init__.py,sha256=JZi3-bHSbUgcSbwtEYpvAFXvPugKVqlgJjFvauK9yFo,205
|
|
12
|
+
defipy/agents/data/UniswapPoolData.py,sha256=8xPTrAxX0kiqo_TRcE4viHoOaf0mK_tCSHLWNwOhEVY,1282
|
|
13
|
+
defipy/agents/data/__init__.py,sha256=uKsiHjQaIG9IcLWTtQ0Ew4e7yH2wV0AZMvoLAAvUDPw,44
|
|
14
|
+
defipy/analytics/risk/__init__.py,sha256=eFoOoZ7Nsq5vqPaNlcRlqWjHA5kWw1SFOKK30gPjo5M,38
|
|
15
|
+
defipy/analytics/simulate/__init__.py,sha256=W5qXe3ypyruhYgg5ojqrEZWKgc6Z5yLsydJKz2I9uyk,43
|
|
16
|
+
defipy/erc/__init__.py,sha256=-3pjCJPmp3oJBEK5B44A-9pEfYlpPbNT4f2h5G3ZAW0,27
|
|
17
|
+
defipy/math/basic/__init__.py,sha256=A8f3r6KaUk78waBWDURRgnLgE0-FI1EE07le_v00oSI,34
|
|
18
|
+
defipy/math/interest/__init__.py,sha256=CtJCu5XuQEEYUsDsI4Pb5cWnwnG-1uJMub8ghoUqUHA,37
|
|
19
|
+
defipy/math/interest/ips/__init__.py,sha256=6Lb7GZ4q0vjxFZE6es2ttJNLliNMGalKM1m17e3cljU,41
|
|
20
|
+
defipy/math/interest/ips/aggregate/__init__.py,sha256=aNpUOgzKHKUx9CyocvTPk1cyrqdyG85TZs7Yeic-OAY,51
|
|
21
|
+
defipy/math/model/__init__.py,sha256=guq0so38r4JDlldXLL0nIDpWyDtfKFvPA7rQ67lGeU4,35
|
|
22
|
+
defipy/math/risk/__init__.py,sha256=mjFISYNM_Uja-ys5HTMTyF4nskWvobj9SYvO7XRYZkE,33
|
|
23
|
+
defipy/process/__init__.py,sha256=oOdW7epkXTic6-HWbwuCB6iCi511fnmBb_wdmB8KX7A,31
|
|
24
|
+
defipy/process/burn/__init__.py,sha256=ut6uyIoE8aJTY1au9MhGwCq9L-qqWfYs5OJS91kOd0w,37
|
|
25
|
+
defipy/process/deposit/__init__.py,sha256=qW8sIg-qDzApgvt483kwu4UOK4uSIBl_LjbmRnIAw1s,39
|
|
26
|
+
defipy/process/join/Join.py,sha256=9NPtQiJ2xI1vq6dIsRzG6niLMoaEOrDtjsM1cy3qUmg,2433
|
|
27
|
+
defipy/process/join/__init__.py,sha256=z_4RbhqO8-c0UJXH6nb-W10s3CABdCrTVUmY3vOOl_w,66
|
|
28
|
+
defipy/process/liquidity/AddLiquidity.py,sha256=cgz6Q00VUtxxUSEzlb81wmXqlwf-wbNzC1Em61jIyiU,2617
|
|
29
|
+
defipy/process/liquidity/RemoveLiquidity.py,sha256=aDq341STB-9wfevJI3eUbeVzzLHSBvlL8PfA5DBuRqs,2682
|
|
30
|
+
defipy/process/liquidity/__init__.py,sha256=7Oa0KRyW8hzitrXI21d7GQFT0bCVGZzVr-a0rhVFYBg,84
|
|
31
|
+
defipy/process/mint/__init__.py,sha256=lbND5rQdiNpj6YTftTo6xEZ8zNwa2kgzN2n2UzMWmWc,36
|
|
32
|
+
defipy/process/swap/Swap.py,sha256=xhLH3j-TRnz4QgrMmPrgchJWXNOJh8AMX5uVhTQklAw,2511
|
|
33
|
+
defipy/process/swap/__init__.py,sha256=jqva_TiVUslmRSENX28ID31A5duPZiIZHNo-jraVcCY,70
|
|
34
|
+
defipy/utils/client/__init__.py,sha256=HPYLVZCAMK3ke9-V7YbbHbqalxYbsWNTTta9a5I7CKk,36
|
|
35
|
+
defipy/utils/client/contract/ExecuteScript.py,sha256=yC96NdsLYQtqRh0qZKuazjoFytgwuGOvT4sRazB_01Y,2730
|
|
36
|
+
defipy/utils/client/contract/__init__.py,sha256=nz9-8LuC6Xy1M5CB1vq83-zfDPjqAUXcNpqh9NCSTSY,40
|
|
37
|
+
defipy/utils/data/__init__.py,sha256=4LFhM7PtOvuG7aDvUJsCylRgM4Raged6n__EfUk__1A,34
|
|
38
|
+
defipy/utils/interfaces/__init__.py,sha256=Oaprl7gRQ_WmP7ULoAzdjAjFRerl1VOsSV8S7-R5Ibk,40
|
|
39
|
+
defipy/utils/tools/UniswapScriptHelper.py,sha256=EZBFYsPEWh4DayBPBuZqk3Ez0ohDYXPpOhKBpHlRY04,3675
|
|
40
|
+
defipy/utils/tools/__init__.py,sha256=OfufE9zwn8V6gWb2WDkWcDnCRwOF2C1zTqGX6AB4jCA,88
|
|
41
|
+
defipy/utils/tools/v3/__init__.py,sha256=wqpXAbXrZx4DGN05Ov9CCus-V-GvRQOngTZOGFEKAoE,38
|
|
42
|
+
defipy-1.0.9.dist-info/licenses/LICENSE,sha256=x1Jk6eHP_RT875HuG_YBjQYS3cxj50ekubbbFs5mDtM,10184
|
|
43
|
+
defipy-1.0.9.dist-info/licenses/NOTICE,sha256=-n7yliH-P1iroNFQlIEomAkNATyOGzosZjD1H2M_27A,699
|
|
44
|
+
defipy-1.0.9.dist-info/METADATA,sha256=YNMrcRmOUWcTvxumH5uWhF840CdkBAPumLNJ9l46rIA,8288
|
|
45
|
+
defipy-1.0.9.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
46
|
+
defipy-1.0.9.dist-info/top_level.txt,sha256=kYm1UIZjTea-emV9NGgzVd_HmDKPeq5I9ckAF5K27aY,7
|
|
47
|
+
defipy-1.0.9.dist-info/RECORD,,
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
Copyright 2023–2025 DeFiPy contributors
|
|
2
|
+
|
|
3
|
+
Apache License
|
|
4
|
+
Version 2.0, January 2004
|
|
5
|
+
http://www.apache.org/licenses/
|
|
6
|
+
|
|
7
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
8
|
+
|
|
9
|
+
1. Definitions.
|
|
10
|
+
|
|
11
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
12
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
13
|
+
|
|
14
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
15
|
+
the copyright owner that is granting the License.
|
|
16
|
+
|
|
17
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
18
|
+
other entities that control, are controlled by, or are under common
|
|
19
|
+
control with that entity. For the purposes of this definition,
|
|
20
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
21
|
+
direction or management of such entity, whether by contract or
|
|
22
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
23
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
24
|
+
|
|
25
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
26
|
+
exercising permissions granted by this License.
|
|
27
|
+
|
|
28
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
29
|
+
including but not limited to software source code, documentation
|
|
30
|
+
source, and configuration files.
|
|
31
|
+
|
|
32
|
+
"Object" form shall mean any form resulting from mechanical
|
|
33
|
+
transformation or translation of a Source form, including but
|
|
34
|
+
not limited to compiled object code, generated documentation,
|
|
35
|
+
and conversions to other media types.
|
|
36
|
+
|
|
37
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
38
|
+
Object form, made available under the License, as indicated by a
|
|
39
|
+
copyright notice that is included in or attached to the work
|
|
40
|
+
(an example is provided in the Appendix below).
|
|
41
|
+
|
|
42
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
43
|
+
form, that is based on (or derived from) the Work and for which the
|
|
44
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
45
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
46
|
+
of this License, Derivative Works shall not include works that remain
|
|
47
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
48
|
+
the Work and Derivative Works thereof.
|
|
49
|
+
|
|
50
|
+
"Contribution" shall mean any work of authorship, including
|
|
51
|
+
the original version of the Work and any modifications or additions
|
|
52
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
53
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
54
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
55
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
56
|
+
means any form of electronic, verbal, or written communication sent
|
|
57
|
+
to the Licensor or its representatives, including but not limited to
|
|
58
|
+
communication on electronic mailing lists, source code control systems,
|
|
59
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
60
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
61
|
+
excluding communication that is conspicuously marked or otherwise
|
|
62
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
63
|
+
|
|
64
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
65
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
66
|
+
subsequently incorporated within the Work.
|
|
67
|
+
|
|
68
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
69
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
70
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
71
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
72
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
73
|
+
Work and such Derivative Works in Source or Object form.
|
|
74
|
+
|
|
75
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
76
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
77
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
78
|
+
(except as stated in this section) patent license to make, have made,
|
|
79
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
80
|
+
where such license applies only to those patent claims licensable
|
|
81
|
+
by such Contributor that are necessarily infringed by their
|
|
82
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
83
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
84
|
+
institute patent litigation against any entity (including a
|
|
85
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
86
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
87
|
+
or contributory patent infringement, then any patent licenses
|
|
88
|
+
granted to You under this License for that Work shall terminate
|
|
89
|
+
as of the date such litigation is filed.
|
|
90
|
+
|
|
91
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
92
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
93
|
+
modifications, and in Source or Object form, provided that You
|
|
94
|
+
meet the following conditions:
|
|
95
|
+
|
|
96
|
+
(a) You must give any other recipients of the Work or
|
|
97
|
+
Derivative Works a copy of this License; and
|
|
98
|
+
|
|
99
|
+
(b) You must cause any modified files to carry prominent notices
|
|
100
|
+
stating that You changed the files; and
|
|
101
|
+
|
|
102
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
103
|
+
that You distribute, all copyright, patent, trademark, and
|
|
104
|
+
attribution notices from the Source form of the Work,
|
|
105
|
+
excluding those notices that do not pertain to any part of
|
|
106
|
+
the Derivative Works; and
|
|
107
|
+
|
|
108
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
109
|
+
distribution, then any Derivative Works that You distribute must
|
|
110
|
+
include a readable copy of the attribution notices contained
|
|
111
|
+
within such NOTICE file, excluding those notices that do not
|
|
112
|
+
pertain to any part of the Derivative Works, in at least one
|
|
113
|
+
of the following places: within a NOTICE text file distributed
|
|
114
|
+
as part of the Derivative Works; within the Source form or
|
|
115
|
+
documentation, if provided along with the Derivative Works; or,
|
|
116
|
+
within a display generated by the Derivative Works, if and
|
|
117
|
+
wherever such third-party notices normally appear. The contents
|
|
118
|
+
of the NOTICE file are for informational purposes only and
|
|
119
|
+
do not modify the License. You may add Your own attribution
|
|
120
|
+
notices within Derivative Works that You distribute, alongside
|
|
121
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
122
|
+
that such additional attribution notices cannot be construed
|
|
123
|
+
as modifying the License.
|
|
124
|
+
|
|
125
|
+
You may add Your own copyright statement to Your modifications and
|
|
126
|
+
may provide additional or different license terms and conditions
|
|
127
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
128
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
129
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
130
|
+
the conditions stated in this License.
|
|
131
|
+
|
|
132
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
133
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
134
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
135
|
+
this License, without any additional terms or conditions.
|
|
136
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
137
|
+
the terms of any separate license agreement you may have executed
|
|
138
|
+
with Licensor regarding such Contributions.
|
|
139
|
+
|
|
140
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
141
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
142
|
+
except as required for describing the origin of the Work and
|
|
143
|
+
reproducing the content of the NOTICE file.
|
|
144
|
+
|
|
145
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
146
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
147
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
148
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
149
|
+
implied, including, without limitation, any warranties or conditions
|
|
150
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
151
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
152
|
+
appropriateness of using or redistributing the Work and assume any
|
|
153
|
+
risks associated with Your exercise of permissions under this License.
|
|
154
|
+
|
|
155
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
156
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
157
|
+
unless required by applicable law (such as deliberate and grossly
|
|
158
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
159
|
+
liable to You for damages, including any direct, indirect, special,
|
|
160
|
+
incidental, or consequential damages of any character arising as a
|
|
161
|
+
result of this License or out of the use or inability to use the
|
|
162
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
163
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
164
|
+
other commercial damages or losses), even if such Contributor
|
|
165
|
+
has been advised of the possibility of such damages.
|
|
166
|
+
|
|
167
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
168
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
169
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
170
|
+
or other liability obligations and/or rights consistent with this
|
|
171
|
+
License. However, in accepting such obligations, You may act only
|
|
172
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
173
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
174
|
+
defend, and hold each Contributor harmless for any liability
|
|
175
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
176
|
+
of your accepting any such warranty or additional liability.
|
|
177
|
+
|
|
178
|
+
END OF TERMS AND CONDITIONS
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
This software includes code developed by the DeFiPy contributors (https://defipy.org)
|
|
2
|
+
|
|
3
|
+
Copyright 2023–2025 DeFiPy contributors
|
|
4
|
+
|
|
5
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
you may not use this file except in compliance with the License.
|
|
7
|
+
You may obtain a copy of the License at
|
|
8
|
+
|
|
9
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
|
|
11
|
+
Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
|
|
15
|
+
We kindly request that if you use DeFiPy in your project, you credit us by
|
|
16
|
+
retaining this notice and optionally linking to https://defipy.org.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
defipy
|