Skip to content

Commit

Permalink
Got old test working again, various bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
poliwop committed Dec 16, 2024
1 parent d285057 commit 4d3d9cc
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 60 deletions.
2 changes: 1 addition & 1 deletion hydradx/model/amm/omnix_solver_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -1873,7 +1873,7 @@ def find_solution_outer_approx(state: OmnipoolState, init_intents: list, amm_lis
break

if Z_U > 0 or best_status in ['PrimalInfeasible', 'DualInfeasible']:
best_omnipool_deltas = [0]*n
best_omnipool_deltas = {tkn: 0.0 for tkn in p.omnipool.asset_list}
best_amm_deltas = [[0]*(len(amm.asset_list) + 1) for amm in amm_list]
best_intent_deltas = [0]*m
y_best = [0]*r
Expand Down
126 changes: 67 additions & 59 deletions hydradx/tests/test_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,73 @@ def test_single_trade_settles():
assert intent_deltas[0][1] == init_intents_full_lrna[0]['buy_quantity']


def test_single_trade_does_not_settle():
agents = [Agent(holdings={'DOT': 100, 'USDT': 0})]

init_intents_partial = [ # selling DOT for $8
{'sell_quantity': mpf(100), 'buy_quantity': mpf(800), 'tkn_sell': 'DOT', 'tkn_buy': 'USDT', 'agent': agents[0], 'partial': True}
]
init_intents_full = [ # selling DOT for $8
{'sell_quantity': mpf(100), 'buy_quantity': mpf(800), 'tkn_sell': 'DOT', 'tkn_buy': 'USDT', 'agent': agents[0], 'partial': False}
]
init_intents_partial_lrna = [
{'sell_quantity': mpf(650), 'buy_quantity': mpf(700), 'tkn_sell': 'LRNA', 'tkn_buy': 'USDT', 'agent': agents[0],
'partial': True}
]
init_intents_full_lrna = [
{'sell_quantity': mpf(650), 'buy_quantity': mpf(700), 'tkn_sell': 'LRNA', 'tkn_buy': 'USDT', 'agent': agents[0],
'partial': False}
]

liquidity = {'HDX': mpf(100000000), 'USDT': mpf(10000000), 'DOT': mpf(10000000/7.5)} # DOT price is $7.50
lrna = {'HDX': mpf(1000000), 'USDT': mpf(10000000), 'DOT': mpf(10000000)}
initial_state = OmnipoolState(
tokens={
tkn: {'liquidity': liquidity[tkn], 'LRNA': lrna[tkn]} for tkn in lrna
},
asset_fee=mpf(0.0025),
lrna_fee=mpf(0.0005)
)
initial_state.last_fee = {tkn: mpf(0.0025) for tkn in lrna}
initial_state.last_lrna_fee = {tkn: mpf(0.0005) for tkn in lrna}

intents = copy.deepcopy(init_intents_partial)
x = find_solution_outer_approx(initial_state, intents)
intent_deltas = x[0]
omnipool_deltas = x[4]
amm_deltas = x[5]
assert validate_and_execute_solution(initial_state.copy(), [], intents, intent_deltas, omnipool_deltas, amm_deltas, "HDX")
assert intent_deltas[0][0] == 0
assert intent_deltas[0][1] == 0

intents = copy.deepcopy(init_intents_full)
x = find_solution_outer_approx(initial_state, intents)
intent_deltas = x[0]
omnipool_deltas = x[4]
amm_deltas = x[5]
assert validate_and_execute_solution(initial_state.copy(), [], intents, intent_deltas, omnipool_deltas, amm_deltas, "HDX")
assert intent_deltas[0][0] == 0
assert intent_deltas[0][1] == 0

intents = copy.deepcopy(init_intents_partial_lrna)
x = find_solution_outer_approx(initial_state, intents)
intent_deltas = x[0]
omnipool_deltas = x[4]
amm_deltas = x[5]
assert validate_and_execute_solution(initial_state.copy(), [], intents, intent_deltas, omnipool_deltas, amm_deltas, "HDX")
assert intent_deltas[0][0] == 0
assert intent_deltas[0][1] == 0

intents = copy.deepcopy(init_intents_full_lrna)
x = find_solution_outer_approx(initial_state, intents)
intent_deltas = x[0]
omnipool_deltas = x[4]
amm_deltas = x[5]
assert validate_and_execute_solution(initial_state.copy(), [], intents, intent_deltas, omnipool_deltas, amm_deltas, "HDX")
assert intent_deltas[0][0] == 0
assert intent_deltas[0][1] == 0



###############
# Other tests #
Expand Down Expand Up @@ -213,65 +280,6 @@ def test_fuzz_single_trade_settles(size_factor: float):
assert valid


def test_single_trade_does_not_settle():
agents = [Agent(holdings={'DOT': 100, 'USDT': 0})]

init_intents_partial = [ # selling DOT for $8
{'sell_quantity': mpf(100), 'buy_quantity': mpf(800), 'tkn_sell': 'DOT', 'tkn_buy': 'USDT', 'agent': agents[0], 'partial': True}
]
init_intents_full = [ # selling DOT for $8
{'sell_quantity': mpf(100), 'buy_quantity': mpf(800), 'tkn_sell': 'DOT', 'tkn_buy': 'USDT', 'agent': agents[0], 'partial': False}
]
init_intents_partial_lrna = [
{'sell_quantity': mpf(650), 'buy_quantity': mpf(700), 'tkn_sell': 'LRNA', 'tkn_buy': 'USDT', 'agent': agents[0],
'partial': True}
]
init_intents_full_lrna = [
{'sell_quantity': mpf(650), 'buy_quantity': mpf(700), 'tkn_sell': 'LRNA', 'tkn_buy': 'USDT', 'agent': agents[0],
'partial': False}
]

liquidity = {'HDX': mpf(100000000), 'USDT': mpf(10000000), 'DOT': mpf(10000000/7.5)} # DOT price is $7.50
lrna = {'HDX': mpf(1000000), 'USDT': mpf(10000000), 'DOT': mpf(10000000)}
initial_state = OmnipoolState(
tokens={
tkn: {'liquidity': liquidity[tkn], 'LRNA': lrna[tkn]} for tkn in lrna
},
asset_fee=mpf(0.0025),
lrna_fee=mpf(0.0005)
)
initial_state.last_fee = {tkn: mpf(0.0025) for tkn in lrna}
initial_state.last_lrna_fee = {tkn: mpf(0.0005) for tkn in lrna}

intents = copy.deepcopy(init_intents_partial)
x = find_solution_outer_approx(initial_state, intents)
intent_deltas = x[0]
assert validate_and_execute_solution(initial_state.copy(), intents, intent_deltas)
assert intent_deltas[0][0] == 0
assert intent_deltas[0][1] == 0

intents = copy.deepcopy(init_intents_full)
x = find_solution_outer_approx(initial_state, intents)
intent_deltas = x[0]
assert validate_and_execute_solution(initial_state.copy(), intents, intent_deltas)
assert intent_deltas[0][0] == 0
assert intent_deltas[0][1] == 0

intents = copy.deepcopy(init_intents_partial_lrna)
x = find_solution_outer_approx(initial_state, intents)
intent_deltas = x[0]
assert validate_and_execute_solution(initial_state.copy(), intents, intent_deltas)
assert intent_deltas[0][0] == 0
assert intent_deltas[0][1] == 0

intents = copy.deepcopy(init_intents_full_lrna)
x = find_solution_outer_approx(initial_state, intents)
intent_deltas = x[0]
assert validate_and_execute_solution(initial_state.copy(), intents, intent_deltas)
assert intent_deltas[0][0] == 0
assert intent_deltas[0][1] == 0


def test_matching_trades_execute_more():
agents = [Agent(holdings={'DOT': 1000, 'LRNA': 7500}), Agent(holdings={'USDT': 7600})]

Expand Down

0 comments on commit 4d3d9cc

Please sign in to comment.