Skip to content

Commit

Permalink
added price slippage to impact calculation to sort trades for processing
Browse files Browse the repository at this point in the history
  • Loading branch information
poliwop committed Jun 17, 2024
1 parent c2fdbb6 commit b45c68d
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
38 changes: 38 additions & 0 deletions hydradx/model/amm/omnix_solver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from hydradx.model.amm.omnipool_amm import OmnipoolState


def calculate_price_slippage_to_impact(omnipool: OmnipoolState, intent: dict) -> float:
tkn_buy, tkn_sell = intent['tkn_buy'], intent['tkn_sell']
sell_price = omnipool.price(omnipool, tkn_sell, tkn_buy)
buy_liquidity, sell_liquidity = omnipool.liquidity[tkn_buy], omnipool.liquidity[tkn_sell]
buy_lrna, sell_lrna = omnipool.lrna[tkn_buy], omnipool.lrna[tkn_sell]

if 'sell_quantity' in intent:
if sell_lrna <= buy_lrna:
impact = intent['sell_quantity'] / sell_liquidity
else:
impact = intent['sell_quantity'] * sell_price / buy_liquidity
limit_price = intent['buy_limit'] / intent['sell_quantity']
else:
if sell_lrna <= buy_lrna:
impact = intent['buy_quantity'] / sell_price / sell_liquidity
else:
impact = intent['buy_quantity'] / buy_liquidity
limit_price = intent['buy_quantity'] / intent['sell_limit']
slippage = (sell_price - limit_price) / sell_price

return slippage / impact


def _calculate_price_slippage_to_impact_sign_tuple(omnipool: OmnipoolState, intent: dict) -> tuple:
x = calculate_price_slippage_to_impact(omnipool, intent)
return (1, x) if x >= 0 else (-1, -x)


def order_initial_intents(intents: list):
# intents = [
# {'agent': agent_alice, 'buy_quantity': 10000, 'sell_limit': 81000, 'tkn_buy': 'DOT', 'tkn_sell': 'USDT'},
# {'agent': agent_bob, 'sell_quantity': 10000, 'buy_limit': 75500, 'tkn_buy': 'USDT', 'tkn_sell': 'DOT'}
# ]

pass
72 changes: 72 additions & 0 deletions hydradx/tests/test_omnix_solver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import pytest
from hypothesis import given, strategies as st, settings
from mpmath import mp, mpf

from hydradx.model.amm.agents import Agent
from hydradx.model.amm.omnipool_amm import OmnipoolState
from hydradx.model.amm.omnix_solver import calculate_price_slippage_to_impact


def test_calculate_price_slippage_to_impact():
omnipool = OmnipoolState(
tokens={
"DOT": {'liquidity': 10000000 / 7.5, 'LRNA': 10000000},
"USDT": {'liquidity': 10000000, 'LRNA': 10000000},
"HDX": {'liquidity': 100000000, 'LRNA': 1000000}
},
preferred_stablecoin='USDT',
asset_fee=0.0,
lrna_fee=0.0
)

intent = {'agent': Agent(), 'buy_quantity': 10000, 'sell_limit': 81000, 'tkn_buy': 'DOT', 'tkn_sell': 'USDT'}
ratio = calculate_price_slippage_to_impact(omnipool, intent)
if ratio != pytest.approx(9.87654321):
raise

intent = {'agent': Agent(), 'buy_quantity': 10000, 'sell_limit': 74000, 'tkn_buy': 'DOT', 'tkn_sell': 'USDT'}
ratio = calculate_price_slippage_to_impact(omnipool, intent)
if ratio != pytest.approx(-1.801801802):
raise

intent = {'agent': Agent(), 'sell_quantity': 10000, 'buy_limit': 74000, 'tkn_buy': 'USDT', 'tkn_sell': 'DOT'}
ratio = calculate_price_slippage_to_impact(omnipool, intent)
if ratio != pytest.approx(1.777777777777):
raise

intent = {'agent': Agent(), 'sell_quantity': 10000, 'buy_limit': 81000, 'tkn_buy': 'USDT', 'tkn_sell': 'DOT'}
ratio = calculate_price_slippage_to_impact(omnipool, intent)
if ratio != pytest.approx(-10.6666666666666):
raise

# doubling liquidity of DOT should not change any results
omnipool = OmnipoolState(
tokens={
"DOT": {'liquidity': 2 * 10000000 / 7.5, 'LRNA': 2 * 10000000},
"USDT": {'liquidity': 10000000, 'LRNA': 10000000},
"HDX": {'liquidity': 100000000, 'LRNA': 1000000}
},
preferred_stablecoin='USDT',
asset_fee=0.0,
lrna_fee=0.0
)

intent = {'agent': Agent(), 'buy_quantity': 10000, 'sell_limit': 81000, 'tkn_buy': 'DOT', 'tkn_sell': 'USDT'}
ratio = calculate_price_slippage_to_impact(omnipool, intent)
if ratio != pytest.approx(9.87654321):
raise

intent = {'agent': Agent(), 'buy_quantity': 10000, 'sell_limit': 74000, 'tkn_buy': 'DOT', 'tkn_sell': 'USDT'}
ratio = calculate_price_slippage_to_impact(omnipool, intent)
if ratio != pytest.approx(-1.801801802):
raise

intent = {'agent': Agent(), 'sell_quantity': 10000, 'buy_limit': 74000, 'tkn_buy': 'USDT', 'tkn_sell': 'DOT'}
ratio = calculate_price_slippage_to_impact(omnipool, intent)
if ratio != pytest.approx(1.777777777777):
raise

intent = {'agent': Agent(), 'sell_quantity': 10000, 'buy_limit': 81000, 'tkn_buy': 'USDT', 'tkn_sell': 'DOT'}
ratio = calculate_price_slippage_to_impact(omnipool, intent)
if ratio != pytest.approx(-10.6666666666666):
raise

0 comments on commit b45c68d

Please sign in to comment.