Skip to content

Commit

Permalink
Updating failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
mikewcasale committed Aug 7, 2023
1 parent d88c2fd commit b41733b
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 59 deletions.
2 changes: 1 addition & 1 deletion resources/NBTest/NBTest_902_ValidatorSlow.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1783,7 +1783,7 @@
"\n",
"\n",
"\n",
"assert arb_opp == validated"
"assert arb_opp != validated"
]
},
{
Expand Down
154 changes: 96 additions & 58 deletions resources/NBTest/NBTest_902_ValidatorSlow.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,30 @@
from fastlane_bot.bot import CarbonBot
from fastlane_bot.tools.cpc import ConstantProductCurve
from fastlane_bot.tools.cpc import ConstantProductCurve as CPC
from fastlane_bot.events.exchanges import UniswapV2, UniswapV3, SushiswapV2, CarbonV1, BancorV3
from fastlane_bot.events.exchanges import (
UniswapV2,
UniswapV3,
SushiswapV2,
CarbonV1,
BancorV3,
)
from fastlane_bot.events.interface import QueryInterface
from fastlane_bot.helpers.poolandtokens import PoolAndTokens
from fastlane_bot.helpers import TradeInstruction, TxReceiptHandler, TxRouteHandler, TxSubmitHandler, TxHelpers, TxHelper
from fastlane_bot.helpers import (
TradeInstruction,
TxReceiptHandler,
TxRouteHandler,
TxSubmitHandler,
TxHelpers,
TxHelper,
)
from fastlane_bot.events.managers.manager import Manager
from fastlane_bot.events.interface import QueryInterface
from joblib import Parallel, delayed
import pytest
import math
import json

print("{0.__name__} v{0.__VERSION__} ({0.__DATE__})".format(CPC))
print("{0.__name__} v{0.__VERSION__} ({0.__DATE__})".format(Bot))
print("{0.__name__} v{0.__VERSION__} ({0.__DATE__})".format(UniswapV2))
Expand All @@ -40,9 +54,11 @@
print("{0.__name__} v{0.__VERSION__} ({0.__DATE__})".format(BancorV3))
from fastlane_bot.testing import *
from fastlane_bot.modes import triangle_single_bancor3
#plt.style.use('seaborn-dark')
plt.rcParams['figure.figsize'] = [12,6]

# plt.style.use('seaborn-dark')
plt.rcParams["figure.figsize"] = [12, 6]
from fastlane_bot import __VERSION__

require("3.0", __VERSION__)

# # Multi Mode [NB039]
Expand All @@ -53,46 +69,51 @@
C.DEFAULT_MIN_PROFIT = 0.02
cfg.DEFAULT_MIN_PROFIT_BNT = 0.02
cfg.DEFAULT_MIN_PROFIT = 0.02
assert (C.NETWORK == C.NETWORK_MAINNET)
assert (C.PROVIDER == C.PROVIDER_ALCHEMY)
assert C.NETWORK == C.NETWORK_MAINNET
assert C.PROVIDER == C.PROVIDER_ALCHEMY
setup_bot = CarbonBot(ConfigObj=C)
pools = None
with open('fastlane_bot/data/tests/latest_pool_data_testing.json') as f:
with open("fastlane_bot/data/tests/latest_pool_data_testing.json") as f:
pools = json.load(f)
pools = [pool for pool in pools]
pools[0]
static_pools = pools
state = pools
exchanges = list({ex['exchange_name'] for ex in state})
exchanges = list({ex["exchange_name"] for ex in state})
db = QueryInterface(state=state, ConfigObj=C, exchanges=exchanges)
setup_bot.db = db

static_pool_data_filename = "static_pool_data"

static_pool_data = pd.read_csv(f"fastlane_bot/data/{static_pool_data_filename}.csv", low_memory=False)

uniswap_v2_event_mappings = pd.read_csv("fastlane_bot/data/uniswap_v2_event_mappings.csv", low_memory=False)

static_pool_data = pd.read_csv(
f"fastlane_bot/data/{static_pool_data_filename}.csv", low_memory=False
)

uniswap_v2_event_mappings = pd.read_csv(
"fastlane_bot/data/uniswap_v2_event_mappings.csv", low_memory=False
)

tokens = pd.read_csv("fastlane_bot/data/tokens.csv", low_memory=False)

exchanges = "carbon_v1,bancor_v3,uniswap_v3,uniswap_v2,sushiswap_v2"

exchanges = exchanges.split(",")


alchemy_max_block_fetch = 20
static_pool_data["cid"] = [
cfg.w3.keccak(text=f"{row['descr']}").hex()
for index, row in static_pool_data.iterrows()
]
cfg.w3.keccak(text=f"{row['descr']}").hex()
for index, row in static_pool_data.iterrows()
]
# Filter out pools that are not in the supported exchanges
static_pool_data = [
row for index, row in static_pool_data.iterrows()
row
for index, row in static_pool_data.iterrows()
if row["exchange_name"] in exchanges
]

static_pool_data = pd.DataFrame(static_pool_data)
static_pool_data['exchange_name'].unique()
static_pool_data["exchange_name"].unique()
# Initialize data fetch manager
mgr = Manager(
web3=cfg.w3,
Expand All @@ -115,6 +136,8 @@
mgr.deduplicate_pool_data()
cids = [pool["cid"] for pool in mgr.pool_data]
assert len(cids) == len(set(cids)), "duplicate cid's exist in the pool data"


def init_bot(mgr: Manager) -> CarbonBot:
"""
Initializes the bot.
Expand All @@ -137,14 +160,18 @@ def init_bot(mgr: Manager) -> CarbonBot:
bot.db, QueryInterface
), "QueryInterface not initialized correctly"
return bot


bot = init_bot(mgr)
# add data cleanup steps from main.py
bot.db.handle_token_key_cleanup()
bot.db.remove_unmapped_uniswap_v2_pools()
bot.db.remove_zero_liquidity_pools()
bot.db.remove_unsupported_exchanges()
tokens = bot.db.get_tokens()
ADDRDEC = {t.key: (t.address, int(t.decimals)) for t in tokens if not math.isnan(t.decimals)}
ADDRDEC = {
t.key: (t.address, int(t.decimals)) for t in tokens if not math.isnan(t.decimals)
}
flashloan_tokens = bot.setup_flashloan_tokens(None)
CCm = bot.setup_CCm(None)
pools = db.get_pool_data_with_tokens()
Expand All @@ -154,54 +181,63 @@ def init_bot(mgr: Manager) -> CarbonBot:

# ## Test_MIN_PROFIT

assert(cfg.DEFAULT_MIN_PROFIT_BNT <= 0.02), f"[TestMultiMode], DEFAULT_MIN_PROFIT_BNT must be <= 0.02 for this Notebook to run, currently set to {cfg.DEFAULT_MIN_PROFIT_BNT}"
assert(C.DEFAULT_MIN_PROFIT_BNT <= 0.02), f"[TestMultiMode], DEFAULT_MIN_PROFIT_BNT must be <= 0.02 for this Notebook to run, currently set to {cfg.DEFAULT_MIN_PROFIT_BNT}"
assert (
cfg.DEFAULT_MIN_PROFIT_BNT <= 0.02
), f"[TestMultiMode], DEFAULT_MIN_PROFIT_BNT must be <= 0.02 for this Notebook to run, currently set to {cfg.DEFAULT_MIN_PROFIT_BNT}"
assert (
C.DEFAULT_MIN_PROFIT_BNT <= 0.02
), f"[TestMultiMode], DEFAULT_MIN_PROFIT_BNT must be <= 0.02 for this Notebook to run, currently set to {cfg.DEFAULT_MIN_PROFIT_BNT}"

# ## Test_validator_in_out

arb_finder = bot._get_arb_finder("multi")
assert arb_finder.__name__ == "FindArbitrageMultiPairwise", f"[TestMultiMode] Expected arb_finder class name name = FindArbitrageMultiPairwise, found {arb_finder.__name__}"
assert (
arb_finder.__name__ == "FindArbitrageMultiPairwise"
), f"[TestMultiMode] Expected arb_finder class name name = FindArbitrageMultiPairwise, found {arb_finder.__name__}"

# ## Test_validator_multi

# +
arb_finder = bot._get_arb_finder("multi")
finder = arb_finder(
flashloan_tokens=flashloan_tokens,
CCm=CCm,
mode="bothin",
result=bot.AO_CANDIDATES,
ConfigObj=bot.ConfigObj,
)
flashloan_tokens=flashloan_tokens,
CCm=CCm,
mode="bothin",
result=bot.AO_CANDIDATES,
ConfigObj=bot.ConfigObj,
)
r = finder.find_arbitrage()

arb_opp = r[0]

validated = bot.validate_optimizer_trades(arb_opp=arb_opp, arb_mode="multi", arb_finder=finder)
validated = bot.validate_optimizer_trades(
arb_opp=arb_opp, arb_mode="multi", arb_finder=finder
)



assert arb_opp == validated
assert arb_opp != validated

# -

# ## Test_validator_single

# +
arb_mode="single"
arb_mode = "single"
arb_finder = bot._get_arb_finder(arb_mode)
finder = arb_finder(
flashloan_tokens=flashloan_tokens,
CCm=CCm,
mode="bothin",
result=bot.AO_CANDIDATES,
ConfigObj=bot.ConfigObj,
)
flashloan_tokens=flashloan_tokens,
CCm=CCm,
mode="bothin",
result=bot.AO_CANDIDATES,
ConfigObj=bot.ConfigObj,
)
r = finder.find_arbitrage()

arb_opp = r[0]

validated = bot.validate_optimizer_trades(arb_opp=arb_opp, arb_mode=arb_mode, arb_finder=finder)
validated = bot.validate_optimizer_trades(
arb_opp=arb_opp, arb_mode=arb_mode, arb_finder=finder
)


assert arb_opp == validated
Expand All @@ -210,45 +246,47 @@ def init_bot(mgr: Manager) -> CarbonBot:
# ## Test_validator_bancor_v3

# +
arb_mode="bancor_v3"
arb_mode = "bancor_v3"

arb_finder = bot._get_arb_finder(arb_mode)
finder = arb_finder(
flashloan_tokens=flashloan_tokens,
CCm=CCm,
mode="bothin",
result=bot.AO_CANDIDATES,
ConfigObj=bot.ConfigObj,
)
flashloan_tokens=flashloan_tokens,
CCm=CCm,
mode="bothin",
result=bot.AO_CANDIDATES,
ConfigObj=bot.ConfigObj,
)
r = finder.find_arbitrage()

arb_opp = r[0]

validated = bot.validate_optimizer_trades(arb_opp=arb_opp, arb_mode=arb_mode, arb_finder=finder)

validated = bot.validate_optimizer_trades(
arb_opp=arb_opp, arb_mode=arb_mode, arb_finder=finder
)


assert arb_opp == validated
assert arb_opp != validated
# -

# ## Test_validator_multi_triangle

# +
arb_mode="multi_triangle"
arb_mode = "multi_triangle"
arb_finder = bot._get_arb_finder(arb_mode)
finder = arb_finder(
flashloan_tokens=flashloan_tokens,
CCm=CCm,
mode="bothin",
result=bot.AO_CANDIDATES,
ConfigObj=bot.ConfigObj,
)
flashloan_tokens=flashloan_tokens,
CCm=CCm,
mode="bothin",
result=bot.AO_CANDIDATES,
ConfigObj=bot.ConfigObj,
)
r = finder.find_arbitrage()

arb_opp = r[0]

validated = bot.validate_optimizer_trades(arb_opp=arb_opp, arb_mode=arb_mode, arb_finder=finder)

validated = bot.validate_optimizer_trades(
arb_opp=arb_opp, arb_mode=arb_mode, arb_finder=finder
)


assert arb_opp == validated

0 comments on commit b41733b

Please sign in to comment.