Skip to content

Commit

Permalink
Added test with specific output of get_arb_swaps (#166)
Browse files Browse the repository at this point in the history
* Added test with specific output of get_arb_swaps to compare against actual implementation

* Fix test for github
  • Loading branch information
poliwop authored Dec 14, 2023
1 parent ca3af60 commit 24961c9
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 1 deletion.
34 changes: 34 additions & 0 deletions hydradx/tests/config/arbconfig2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[
{"tkns": ["HDX", "USDT"], "tkn_ids": [0, 10], "exchange": "kraken", "order_book": ["HDX","USD"], "buffer": 0.0010},
{"tkns": ["DOT", "USDT"], "tkn_ids": [5, 10], "exchange": "kraken", "order_book": ["DOT","USDT"], "buffer": 0.0010},
{"tkns": ["WETH", "USDT"], "tkn_ids": [20, 10], "exchange": "kraken", "order_book": ["ETH","USDT"], "buffer": 0.0010},
{"tkns": ["WETH", "USDT"], "tkn_ids": [20, 18], "exchange": "kraken", "order_book": ["ETH","DAI"], "buffer": 0.0010},
{"tkns": ["DOT", "WETH"], "tkn_ids": [5, 20], "exchange": "kraken", "order_book": ["DOT","ETH"], "buffer": 0.0010},
{"tkns": ["WBTC", "USDT"], "tkn_ids": [19, 10], "exchange": "kraken", "order_book": ["BTC","USDT"], "buffer": 0.0010},
{"tkns": ["IBTC", "USDT"], "tkn_ids": [11, 10], "exchange": "kraken", "order_book": ["BTC","USDT"], "buffer": 0.0010},
{"tkns": ["WBTC", "USDT"], "tkn_ids": [19, 18], "exchange": "kraken", "order_book": ["BTC","DAI"], "buffer": 0.0010},
{"tkns": ["IBTC", "USDT"], "tkn_ids": [11, 18], "exchange": "kraken", "order_book": ["BTC","DAI"], "buffer": 0.0010},
{"tkns": ["DOT", "WBTC"], "tkn_ids": [5, 19], "exchange": "kraken", "order_book": ["DOT","BTC"], "buffer": 0.0010},
{"tkns": ["DOT", "IBTC"], "tkn_ids": [5, 11], "exchange": "kraken", "order_book": ["DOT","BTC"], "buffer": 0.0010},
{"tkns": ["WETH", "WBTC"], "tkn_ids": [20, 19], "exchange": "kraken", "order_book": ["ETH","BTC"], "buffer": 0.0010},
{"tkns": ["WETH", "IBTC"], "tkn_ids": [20, 11], "exchange": "kraken", "order_book": ["ETH","BTC"], "buffer": 0.0010},
{"tkns": ["ASTR", "USDT"], "tkn_ids": [9, 10], "exchange": "kraken", "order_book": ["ASTR","USD"], "buffer": 0.0010},
{"tkns": ["CFG", "USDT"], "tkn_ids": [13, 10], "exchange": "kraken", "order_book": ["CFG","USD"], "buffer": 0.0010},
{"tkns": ["BNC", "USDT"], "tkn_ids": [14, 10], "exchange": "kraken", "order_book": ["BNC","USD"], "buffer": 0.0010},
{"tkns": ["GLMR", "USDT"], "tkn_ids": [16, 10], "exchange": "kraken", "order_book": ["GLMR","USD"], "buffer": 0.0010},
{"tkns": ["INTR", "USDT"], "tkn_ids": [17, 10], "exchange": "kraken", "order_book": ["INTR","USD"], "buffer": 0.0010},
{"tkns": ["DOT", "USDT"], "tkn_ids": [5, 10], "exchange": "binance", "order_book": ["DOT","USDT"], "buffer": 0.0010},
{"tkns": ["DOT", "WETH"], "tkn_ids": [5, 20], "exchange": "binance", "order_book": ["DOT","ETH"], "buffer": 0.0010},
{"tkns": ["DOT", "WBTC"], "tkn_ids": [5, 19], "exchange": "binance", "order_book": ["DOT","BTC"], "buffer": 0.0010},
{"tkns": ["DOT", "IBTC"], "tkn_ids": [5, 11], "exchange": "binance", "order_book": ["DOT","BTC"], "buffer": 0.0010},
{"tkns": ["WETH", "USDT"], "tkn_ids": [20, 10], "exchange": "binance", "order_book": ["ETH","USDT"], "buffer": 0.0010},
{"tkns": ["WETH", "DAI"], "tkn_ids": [20, 18], "exchange": "binance", "order_book": ["ETH","DAI"], "buffer": 0.0010},
{"tkns": ["WETH", "WBTC"], "tkn_ids": [20, 19], "exchange": "binance", "order_book": ["ETH","BTC"], "buffer": 0.0010},
{"tkns": ["WETH", "IBTC"], "tkn_ids": [20, 11], "exchange": "binance", "order_book": ["ETH","BTC"], "buffer": 0.0010},
{"tkns": ["WBTC", "USDT"], "tkn_ids": [19, 10], "exchange": "binance", "order_book": ["BTC","USDT"], "buffer": 0.0010},
{"tkns": ["IBTC", "USDT"], "tkn_ids": [11, 10], "exchange": "binance", "order_book": ["BTC","USDT"], "buffer": 0.0010},
{"tkns": ["WBTC", "DAI"], "tkn_ids": [19, 18], "exchange": "binance", "order_book": ["BTC","DAI"], "buffer": 0.0010},
{"tkns": ["IBTC", "DAI"], "tkn_ids": [11, 18], "exchange": "binance", "order_book": ["BTC","DAI"], "buffer": 0.0010},
{"tkns": ["ASTR", "USDT"], "tkn_ids": [9, 10], "exchange": "binance", "order_book": ["ASTR","USDT"], "buffer": 0.0010},
{"tkns": ["GLMR", "USDT"], "tkn_ids": [16, 10], "exchange": "binance", "order_book": ["GLMR","USDT"], "buffer": 0.0010}
]
9 changes: 9 additions & 0 deletions hydradx/tests/config/liqconfig.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{"cex": {"kraken":
{"USD": 1000, "USDT": 1000, "DAI": 1000, "DOT": 200, "BTC": 0.030, "ETH": 0.5, "HDX": 300000, "ASTR": 3000,
"CFG": 350, "INTR": 8000, "BNC": 800, "GLMR": 800},
"binance":
{"USD": 1000, "USDT": 1000, "DAI": 1000, "DOT": 200, "BTC": 0.03, "ETH": 0.5, "HDX": 300000, "ASTR": 3000,
"GLMR": 800}},
"dex": {"USDT": 1000, "DAI": 1000, "DOT": 200, "iBTC": 0.03, "WBTC": 0.03, "WETH": 0.5, "HDX": 300000, "ASTR": 3000,
"CFG": 350, "INTR": 8000, "BNC": 800, "GLMR": 800}
}
1 change: 1 addition & 0 deletions hydradx/tests/output/arb_swaps.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"dex": {"trade": "buy", "buy_asset": "iBTC", "sell_asset": "DAI001", "price": 38706.04, "amount": 0.01453, "max_sell": 514.1799334498644}, "cex": {"trade": "sell", "buy_asset": "DAI", "sell_asset": "BTC", "price": 38686.686980000006, "amount": 0.01453}, "exchange": "binance"}, {"dex": {"trade": "buy", "buy_asset": "iBTC", "sell_asset": "DAI001", "price": 38706.03, "amount": 0.02908, "max_sell": 1074.3511630808114}, "cex": {"trade": "sell", "buy_asset": "DAI", "sell_asset": "BTC", "price": 38686.676985, "amount": 0.02908}, "exchange": "binance"}, {"dex": {"trade": "buy", "buy_asset": "WBTC", "sell_asset": "DAI001", "price": 38706.02, "amount": 0.008306158332824706, "max_sell": 318.3454783473015}, "cex": {"trade": "sell", "buy_asset": "DAI", "sell_asset": "BTC", "price": 38686.66699, "amount": 0.008306158332824706}, "exchange": "binance"}, {"dex": {"trade": "sell", "buy_asset": "USDT", "sell_asset": "CFG", "price": 0.6818, "amount": 41.024, "min_buy": 28.101874021768573}, "cex": {"trade": "buy", "buy_asset": "CFG", "sell_asset": "USD", "price": 0.6821408999999999, "amount": 41.024}, "exchange": "kraken"}, {"dex": {"trade": "sell", "buy_asset": "USDT", "sell_asset": "CFG", "price": 0.682, "amount": 293.267, "min_buy": 200.77441141819955}, "cex": {"trade": "buy", "buy_asset": "CFG", "sell_asset": "USD", "price": 0.682341, "amount": 293.267}, "exchange": "kraken"}, {"dex": {"trade": "buy", "buy_asset": "WBTC", "sell_asset": "USDT", "price": 38821.39, "amount": 0.005976583435058594, "max_sell": 231.60450055789946}, "cex": {"trade": "sell", "buy_asset": "USDT", "sell_asset": "BTC", "price": 38801.979305, "amount": 0.005976583435058594}, "exchange": "binance"}, {"dex": {"trade": "sell", "buy_asset": "USDT", "sell_asset": "CFG", "price": 0.6826, "amount": 15.709000000000003, "min_buy": 10.750916699111462}, "cex": {"trade": "buy", "buy_asset": "CFG", "sell_asset": "USD", "price": 0.6829413, "amount": 15.709000000000003}, "exchange": "kraken"}, {"dex": {"trade": "buy", "buy_asset": "iBTC", "sell_asset": "USDT", "price": 38821.39, "amount": 0.0032848484389733064, "max_sell": 127.30795960825681}, "cex": {"trade": "sell", "buy_asset": "USDT", "sell_asset": "BTC", "price": 38801.979305, "amount": 0.0032848484389733064}, "exchange": "binance"}]
72 changes: 71 additions & 1 deletion hydradx/tests/test_arbitrage_agent.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import copy
from datetime import timedelta
import os
import os, json
import pytest
from hypothesis import given, strategies as st, settings, Phase
from mpmath import mp, mpf
Expand Down Expand Up @@ -1076,3 +1076,73 @@ def test_combine_step():
print(f'Second iteration also gained {iter_profit_total - combined_profit_total}.')
else:
print('Iteration did not improve profit.')


def test_get_arb_swaps_output():

prefix = './'
if not os.path.exists(prefix + "data"):
prefix = 'hydradx/tests/'

asset_list, asset_map, tokens, fees = get_omnipool_data_from_file(prefix + "data/")

arb_file = "arbconfig2.txt"
with open(prefix + 'config/' + arb_file, 'r') as json_file:
cfg = json.load(json_file)

for d in cfg:
d['tkns'] = tuple(d['tkns'])
d['tkn_ids'] = tuple(d['tkn_ids'])
d['order_book'] = tuple(d['order_book'])

order_book_assets = {}

ob_objs = get_orderbooks_from_file(prefix + "data/")

for arb_cfg in cfg:
tkn_pair = arb_cfg['order_book']
exchange = arb_cfg['exchange']
for tkn in tkn_pair:
if exchange not in order_book_assets:
order_book_assets[exchange] = []
if tkn not in order_book_assets[exchange]:
order_book_assets[exchange].append(tkn)

arb_cfg['tkn_pair'] = (asset_map[arb_cfg['tkn_ids'][0]], asset_map[arb_cfg['tkn_ids'][1]])

cex_fees = {
'kraken': 0.0016,
'binance': 0.0010
}

lrna_fee = {asset: fees[asset]['protocol_fee'] for asset in asset_list}
asset_fee = {asset: fees[asset]['asset_fee'] for asset in asset_list}

cex_dict = {}
for exchange in ob_objs:
cex_dict[exchange] = CentralizedMarket(
order_book=ob_objs[exchange],
asset_list=order_book_assets[exchange],
trade_fee=cex_fees[exchange]
)

op_state = OmnipoolState(
tokens=tokens,
lrna_fee=lrna_fee,
asset_fee=asset_fee,
preferred_stablecoin='USDT',
)

liq_file = "liqconfig.txt"
with open(prefix + 'config/' + liq_file, 'r') as json_file:
max_liquidity = json.load(json_file)

all_swaps = get_arb_swaps(op_state, cex_dict, cfg, max_liquidity=max_liquidity)

# with open(f'./output/arb_swaps.json', 'w') as output_file:
# json.dump(all_swaps, output_file)

with open(prefix + 'output/arb_swaps.json', 'r') as output_file:
loaded_swaps = json.load(output_file)

assert all_swaps == loaded_swaps

0 comments on commit 24961c9

Please sign in to comment.