From 24961c98ffec3dcbbb3097315c0bb484de7a300e Mon Sep 17 00:00:00 2001 From: Colin Date: Thu, 14 Dec 2023 04:07:56 -0600 Subject: [PATCH] Added test with specific output of get_arb_swaps (#166) * Added test with specific output of get_arb_swaps to compare against actual implementation * Fix test for github --- hydradx/tests/config/arbconfig2.txt | 34 +++++++++++++ hydradx/tests/config/liqconfig.txt | 9 ++++ hydradx/tests/output/arb_swaps.json | 1 + hydradx/tests/test_arbitrage_agent.py | 72 ++++++++++++++++++++++++++- 4 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 hydradx/tests/config/arbconfig2.txt create mode 100644 hydradx/tests/config/liqconfig.txt create mode 100644 hydradx/tests/output/arb_swaps.json diff --git a/hydradx/tests/config/arbconfig2.txt b/hydradx/tests/config/arbconfig2.txt new file mode 100644 index 00000000..372dfdef --- /dev/null +++ b/hydradx/tests/config/arbconfig2.txt @@ -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} +] diff --git a/hydradx/tests/config/liqconfig.txt b/hydradx/tests/config/liqconfig.txt new file mode 100644 index 00000000..509814ca --- /dev/null +++ b/hydradx/tests/config/liqconfig.txt @@ -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} +} \ No newline at end of file diff --git a/hydradx/tests/output/arb_swaps.json b/hydradx/tests/output/arb_swaps.json new file mode 100644 index 00000000..c91b1f2a --- /dev/null +++ b/hydradx/tests/output/arb_swaps.json @@ -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"}] \ No newline at end of file diff --git a/hydradx/tests/test_arbitrage_agent.py b/hydradx/tests/test_arbitrage_agent.py index fc579f6c..5a1b8ea8 100644 --- a/hydradx/tests/test_arbitrage_agent.py +++ b/hydradx/tests/test_arbitrage_agent.py @@ -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 @@ -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