Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic fee parameters #191

Merged
merged 73 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
1379ef2
notebook to examine different fee parameters
jepidoptera Jul 1, 2024
5398057
change "jan" to "January"
jepidoptera Jul 1, 2024
9c5428c
add frequency paramater to omnipool_arbitrage
jepidoptera Jul 1, 2024
84468fe
simplified notebook version
jepidoptera Jul 1, 2024
1d97c4b
corrected absolute imports to relative
jepidoptera Jul 2, 2024
d2566cd
streamlined output method, show graphs for two scenarios
jepidoptera Jul 2, 2024
a9a1fe9
trouble getting SQLPad data except for this one query
jepidoptera Jul 8, 2024
808114d
notebook examining recorded vs simulated dynamic fees in Omnipool
jepidoptera Jul 11, 2024
64628d0
remove one irrelevant cell and regenerate plots
jepidoptera Jul 11, 2024
733039c
measure price against USDC
jepidoptera Jul 12, 2024
4d8ebaa
update volume_in and volume_out data in lrna_swap
jepidoptera Jul 18, 2024
80eabab
simulate dynamic fee with historical trades, compare with historical …
jepidoptera Jul 18, 2024
00da71b
simulate dynamic fee with historical trades, compare with historical …
jepidoptera Jul 18, 2024
62f06aa
refactor get_omnipool_balance_history
jepidoptera Aug 6, 2024
03673d7
comment out this LRNA holdings check because it makes some test fail
jepidoptera Aug 6, 2024
ec4daf8
add dateutil requirement
jepidoptera Aug 6, 2024
ab7ac65
when loading history, use data from .model directory
jepidoptera Aug 6, 2024
f6ded52
add history data files to model folder
jepidoptera Aug 6, 2024
13b2188
change 'round' to 'int'
jepidoptera Aug 6, 2024
a72d00a
handle end of data gracefully
jepidoptera Aug 6, 2024
af4af35
ran
jepidoptera Aug 6, 2024
eb15db2
download latest
jepidoptera Aug 6, 2024
f1a8270
delete
jepidoptera Aug 6, 2024
097ede2
test balance history load
jepidoptera Aug 6, 2024
2daa5a0
delete last cell
jepidoptera Aug 6, 2024
bad7966
ignore data files in notebooks/Misc
jepidoptera Aug 6, 2024
77dd8ba
run once more
jepidoptera Aug 6, 2024
a5d5598
replace swap_lrna calls with swap
jepidoptera Sep 10, 2024
6334c72
use spot prices for both recorded and simulated fees
jepidoptera Sep 10, 2024
8222b9b
mark _lrna_swap as a private method so oracle update will happen corr…
jepidoptera Sep 10, 2024
f9d5caa
GlobalState.evolve() returns self
jepidoptera Sep 10, 2024
c6f0087
fix test_oracle_one_block_with_swaps
jepidoptera Sep 12, 2024
a692cf1
fix test_stableswap_router_arbitrage
jepidoptera Sep 12, 2024
99aee75
add model/data folder
jepidoptera Sep 13, 2024
8a32f5f
notebook examining difference in cash out value between LPs who bough…
jepidoptera Sep 13, 2024
f458346
fix price curve
jepidoptera Sep 13, 2024
bc1dae9
add axis labels
jepidoptera Sep 13, 2024
ef3b0c1
formatting
jepidoptera Sep 17, 2024
a779c73
add graphs separating DOT and LRNA returns
jepidoptera Sep 17, 2024
30cd807
remove widthdrawal fee
jepidoptera Sep 17, 2024
27cca4a
add additional sanity check to test_remove_liquidity_no_fee
jepidoptera Sep 17, 2024
374626f
add a graph tracking the ratio of agent1_value/agent2/value
jepidoptera Sep 19, 2024
16cda57
add shares option to omnipool setup
jepidoptera Sep 19, 2024
43563ad
increase range and flexibility of simulation
jepidoptera Sep 19, 2024
8b26ac7
delete stale notebook
jepidoptera Sep 23, 2024
05fff8b
include instructions for adding .env file with sqlpad username and pa…
jepidoptera Sep 23, 2024
e2c2c19
delete stale notebook
jepidoptera Sep 23, 2024
c8c0a47
delete large history files
jepidoptera Sep 24, 2024
506ed22
require gdown for fetching history data from google drive
jepidoptera Sep 24, 2024
9bd9f6c
Use smaller files with proper json. Add download from google drive. F…
jepidoptera Sep 24, 2024
602a73c
tests for handing of history data in processing.py
jepidoptera Sep 24, 2024
0ce804e
rename to test_get_current_balance
jepidoptera Sep 24, 2024
bfa2ed9
PyCharm seems to think we need this
jepidoptera Sep 24, 2024
9fb316d
Merge branch 'refs/heads/main' into dynamic_fee_parameters
jepidoptera Sep 24, 2024
c0d77e2
WIP temp notebook for debugging
poliwop Sep 25, 2024
14b16a6
Merge remote-tracking branch 'origin/dynamic_fee_parameters' into dyn…
jepidoptera Sep 26, 2024
3f713bc
support getting a liquidity history for a range of dates
jepidoptera Oct 4, 2024
3d9e5cf
test getting liquidity history for a range of dates
jepidoptera Oct 4, 2024
db7bbd1
tried new stuff and cleaned up some
jepidoptera Oct 4, 2024
6b08c9f
sort file_ls
poliwop Oct 7, 2024
923d185
sort file_ls
poliwop Oct 7, 2024
7b768a1
try to match update behavior in production
jepidoptera Oct 7, 2024
e236e4c
Merge remote-tracking branch 'origin/dynamic_fee_parameters' into dyn…
jepidoptera Oct 7, 2024
5f072c7
corrections
jepidoptera Oct 8, 2024
c9635ad
extend omnipool class, match prod
jepidoptera Oct 8, 2024
1a13a13
prevent double decay in blocks when trades happen
jepidoptera Oct 10, 2024
3a4f843
increase precision on test_volatility_limit
jepidoptera Oct 11, 2024
b9f3eba
clean up fragments
jepidoptera Oct 14, 2024
165da62
use simulate_add_liquidity in test_remove_liquidity_no_fee
jepidoptera Oct 14, 2024
0181087
remove temp notebook
jepidoptera Oct 14, 2024
8f1232a
re-enable LRNA holdings check in _lrna_swap
jepidoptera Oct 14, 2024
226fe65
fix tests that fail due to LRNA holdings check
jepidoptera Oct 14, 2024
1556dfb
fix last test
jepidoptera Oct 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
/.idea/
/hydradx/tests/.hypothesis/
/hydradx/notebooks/Omnipool/data/
/hydradx/notebooks/Misc/data/
/hydradx/other tests/.hypothesis/
/hydradx/model/data/
/.hypothesis/
*.env
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ Installation:
* Clone the repository and navigate to the root folder, HydraDx-simulations
* In terminal, enter 'pip install -r requirements.txt'
* Alternatively, open the project folder in PyCharm, and you'll be prompted to create a virtual environment for this project.
* If you want to pull data from SQLPad, add a .env file to ./hydradx/model/ with the following contents:
```
SQLPAD_USERNAME=<your username>
PASSWORD=<your password>
```
2 changes: 2 additions & 0 deletions hydradx/model/amm/global_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ def evolve(self):
pool.update()
if self._evolve_function:
return self._evolve_function(self)
else:
return self

def execute_swap(
self,
Expand Down
2 changes: 1 addition & 1 deletion hydradx/model/amm/liquidations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import copy

from hydradx.model.amm.agents import Agent
from .agents import Agent


class CDP:
Expand Down
45 changes: 13 additions & 32 deletions hydradx/model/amm/omnipool_amm.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ def __init__(self,
token,
liquidity=pool['liquidity'],
lrna=lrna,
shares=pool['liquidity'],
protocol_shares=pool['liquidity'],
shares=pool['shares'] if 'shares' in pool else pool['liquidity'],
protocol_shares=pool['shares'] if 'shares' in pool else pool['liquidity'],
weight_cap=pool['weight_cap'] if 'weight_cap' in pool else 1
)

Expand Down Expand Up @@ -447,9 +447,9 @@ def swap(
)

elif tkn_sell == 'LRNA':
return_val = self.lrna_swap(agent, buy_quantity, -sell_quantity, tkn_buy, modify_imbalance)
return_val = self._lrna_swap(agent, buy_quantity, -sell_quantity, tkn_buy, modify_imbalance)
elif tkn_buy == 'LRNA':
return_val = self.lrna_swap(agent, -sell_quantity, buy_quantity, tkn_sell, modify_imbalance)
return_val = self._lrna_swap(agent, -sell_quantity, buy_quantity, tkn_sell, modify_imbalance)

elif buy_quantity and not sell_quantity:
# back into correct delta_Ri, then execute sell
Expand Down Expand Up @@ -530,7 +530,7 @@ def swap(
self.current_block.price[tkn_sell] = self.lrna[tkn_sell] / self.liquidity[tkn_sell]
return return_val

def lrna_swap(
def _lrna_swap(
self,
agent: Agent,
delta_ra: float = 0,
Expand All @@ -543,9 +543,10 @@ def lrna_swap(
"""

if delta_qa < 0:
# selling LRNA
asset_fee = self.asset_fee[tkn].compute()
if -delta_qa + self.lrna[tkn] <= 0:
return self.fail_transaction('insufficient lrna in pool', agent)
if delta_qa + agent.holdings['LRNA'] < 0:
return self.fail_transaction('Agent has insufficient lrna', agent)
delta_ra = -self.liquidity[tkn] * delta_qa / (-delta_qa + self.lrna[tkn]) * (1 - asset_fee)

delta_qm = asset_fee * (-delta_qa) / self.lrna[tkn] * (self.lrna[tkn] - delta_qa) * self.lrna_mint_pct
Expand All @@ -559,6 +560,7 @@ def lrna_swap(
self.liquidity[tkn] += -delta_ra

elif delta_ra > 0:
# buying asset
asset_fee = self.asset_fee[tkn].compute()
if -delta_ra + self.liquidity[tkn] <= 0:
return self.fail_transaction('insufficient assets in pool', agent)
Expand All @@ -576,14 +578,15 @@ def lrna_swap(

# buying LRNA
elif delta_qa > 0:
# buying LRNA
lrna_fee = self.lrna_fee[tkn].compute()
delta_qi = -delta_qa / (1 - lrna_fee)
if delta_qi + self.lrna[tkn] <= 0:
return self.fail_transaction('insufficient lrna in pool', agent)
delta_ra = -self.liquidity[tkn] * -delta_qi / (delta_qi + self.lrna[tkn])
if agent.holdings[tkn] < -delta_ra:
return self.fail_transaction('Agent has insufficient assets', agent)
self.lrna[tkn] += delta_qi
self.lrna[tkn] += delta_qi # burn the LRNA fee
self.liquidity[tkn] += -delta_ra
# if modify_imbalance:
# self.lrna_imbalance += - delta_qi * (q + l) / (q + delta_qi) - delta_qi
Expand All @@ -595,8 +598,8 @@ def lrna_swap(
self.lrna["HDX"] += lrna_fee_amt - delta_l

elif delta_ra < 0:
# selling asset
lrna_fee = self.lrna_fee[tkn].compute()
# delta_ri = -delta_ra
if delta_ra > agent.holdings[tkn]:
return self.fail_transaction('agent has insufficient assets', agent)
delta_qi = self.lrna[tkn] * delta_ra / (self.liquidity[tkn] - delta_ra)
Expand Down Expand Up @@ -639,7 +642,7 @@ def stable_swap(
sub_pool = self.sub_pools[sub_pool_buy_id]
# buy a specific quantity of a stableswap asset using LRNA
shares_needed = sub_pool.calculate_withdrawal_shares(tkn_remove=tkn_buy, quantity=buy_quantity)
self.lrna_swap(agent, delta_ra=shares_needed, tkn=sub_pool.unique_id)
self._lrna_swap(agent, delta_ra=shares_needed, tkn=sub_pool.unique_id)
if self.fail:
# if the swap failed, the transaction failed.
return self.fail_transaction(self.fail, agent)
Expand Down Expand Up @@ -1112,11 +1115,6 @@ def remove_liquidity(self, agent: Agent, quantity: float = None, tkn_remove: str
If quantity is unspecified and nft_id is unspecified, remove all liquidity.
"""

# if i is None:
# k = (self.unique_id, tkn_remove)
# else:
# k = (self.unique_id + "_" + str(i), tkn_remove)

k = (self.unique_id, tkn_remove)
if nft_id is not None:
if nft_id not in agent.nfts:
Expand Down Expand Up @@ -1344,23 +1342,6 @@ def weight_i(state: OmnipoolState, i: str) -> float:
return state.lrna[i] / state.lrna_total


def simulate_swap_lrna(
jepidoptera marked this conversation as resolved.
Show resolved Hide resolved
old_state: OmnipoolState,
old_agent: Agent,
delta_ra: float = 0,
delta_qa: float = 0,
tkn: str = '',
modify_imbalance: bool = True
) -> tuple[OmnipoolState, Agent]:
"""Compute new state after LRNA swap"""

new_state = old_state.copy()
new_agent = old_agent.copy()

new_state.lrna_swap(new_agent, delta_ra, delta_qa, tkn, modify_imbalance)
return new_state, new_agent


def simulate_swap(
old_state: OmnipoolState,
old_agent: Agent,
Expand Down
2 changes: 1 addition & 1 deletion hydradx/model/amm/otc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from hydradx.model.amm.agents import Agent
from .agents import Agent


class OTC:
Expand Down
5 changes: 4 additions & 1 deletion hydradx/model/amm/trade_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ def price_after_trade(buy_amount=0, sell_amount=0):
return TradeStrategy(strategy, name=f'constant product pool arbitrage ({pool_id})')


def omnipool_arbitrage(pool_id: str, arb_precision=1, skip_assets=None):
def omnipool_arbitrage(pool_id: str, arb_precision=1, skip_assets=None, frequency=1) -> TradeStrategy:
if skip_assets is None:
skip_assets = []

Expand Down Expand Up @@ -465,6 +465,9 @@ def get_dq_list(dr, reserves: list, lrna: list):
return dq

def strategy(state: GlobalState, agent_id: str) -> GlobalState:
if state.time_step % frequency != 0:
return state

omnipool: OmnipoolState = state.pools[pool_id]
agent: Agent = state.agents[agent_id]
if not isinstance(omnipool, OmnipoolState):
Expand Down
Loading