Skip to content

Commit

Permalink
Added min_profit to eliminate rounding errors. Fixed test_single_trad…
Browse files Browse the repository at this point in the history
…e_does_not_settle
  • Loading branch information
poliwop committed Dec 27, 2024
1 parent 3d35f63 commit 8a7a202
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 24 deletions.
3 changes: 2 additions & 1 deletion hydradx/model/amm/omnix_solver_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -1835,7 +1835,8 @@ def find_solution_outer_approx(state: OmnipoolState, init_intents: list, amm_lis
if not valid:
break

if Z_U > 0 or best_status in ['PrimalInfeasible', 'DualInfeasible']:
min_profit = 1 # denominated in profit_tkn, i.e. HDX
if Z_U > -min_profit or best_status in ['PrimalInfeasible', 'DualInfeasible']:
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
Expand Down
85 changes: 62 additions & 23 deletions hydradx/tests/test_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,55 @@ def get_token_list(omnipool: OmnipoolState, amm_list: list[StableSwapPoolState])
return token_list


def get_markets_minimal():
prices = {'HDX': 0.013, 'DOT': 9, 'vDOT': 13, '2-Pool': 1.01, '4-Pool': 1.01, 'LRNA': 1, 'USDT': 1, 'USDC': 1,
'USDT2': 1, 'DAI': 1}
weights = {'HDX': 0.08, 'DOT': 0.5, 'vDOT': 0.2, '2-Pool': 0.2, '4-Pool': 0.02}
total_lrna = 65000000

lrna = {tkn: weights[tkn] * total_lrna for tkn in weights}
liquidity = {tkn: lrna[tkn] / prices[tkn] for tkn in lrna}

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}

ss_fee = 0.0005

sp_tokens = {
"USDT": 7600000,
"USDC": 9200000
}
stablepool = StableSwapPoolState(
tokens=sp_tokens,
amplification=1000,
trade_fee=ss_fee,
unique_id="2-Pool"
)

sp4_tokens = {
"USDC": 600000,
"USDT": 340000,
"DAI": 365000,
"USDT2": 330000
}
stablepool4 = StableSwapPoolState(
tokens=sp4_tokens,
amplification=1000,
trade_fee=ss_fee,
unique_id="4-Pool"
)

amm_list = [stablepool, stablepool4]
return initial_state, amm_list


def test_no_intent_arbitrage():

# test where stablepool shares have different values
Expand Down Expand Up @@ -250,11 +299,11 @@ def test_single_trade_settles():
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_partial = [ # selling DOT for $10
{'sell_quantity': mpf(100), 'buy_quantity': mpf(1000), '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_full = [ # selling DOT for $10
{'sell_quantity': mpf(100), 'buy_quantity': mpf(1000), '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],
Expand All @@ -265,51 +314,41 @@ def test_single_trade_does_not_settle():
'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}
initial_state, amm_list = get_markets_minimal()

intents = copy.deepcopy(init_intents_partial)
x = find_solution_outer_approx(initial_state, intents)
x = find_solution_outer_approx(initial_state, intents, amm_list)
intent_deltas = x['deltas']
omnipool_deltas = x['omnipool_deltas']
amm_deltas = x['amm_deltas']
assert validate_and_execute_solution(initial_state.copy(), [], intents, intent_deltas, omnipool_deltas, amm_deltas, "HDX")
assert validate_and_execute_solution(initial_state.copy(), copy.deepcopy(amm_list), 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)
x = find_solution_outer_approx(initial_state, intents, amm_list)
intent_deltas = x['deltas']
omnipool_deltas = x['omnipool_deltas']
amm_deltas = x['amm_deltas']
assert validate_and_execute_solution(initial_state.copy(), [], intents, intent_deltas, omnipool_deltas, amm_deltas, "HDX")
assert validate_and_execute_solution(initial_state.copy(), copy.deepcopy(amm_list), 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)
x = find_solution_outer_approx(initial_state, intents, amm_list)
intent_deltas = x['deltas']
omnipool_deltas = x['omnipool_deltas']
amm_deltas = x['amm_deltas']
assert validate_and_execute_solution(initial_state.copy(), [], intents, intent_deltas, omnipool_deltas, amm_deltas, "HDX")
assert validate_and_execute_solution(initial_state.copy(), copy.deepcopy(amm_list), 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)
x = find_solution_outer_approx(initial_state, intents, amm_list)
intent_deltas = x['deltas']
omnipool_deltas = x['omnipool_deltas']
amm_deltas = x['amm_deltas']
assert validate_and_execute_solution(initial_state.copy(), [], intents, intent_deltas, omnipool_deltas, amm_deltas, "HDX")
assert validate_and_execute_solution(initial_state.copy(), copy.deepcopy(amm_list), intents, intent_deltas, omnipool_deltas, amm_deltas, "HDX")
assert intent_deltas[0][0] == 0
assert intent_deltas[0][1] == 0

Expand Down

0 comments on commit 8a7a202

Please sign in to comment.