Skip to content

Commit

Permalink
Merge pull request #559 from bancorprotocol/module-bot-cleanup
Browse files Browse the repository at this point in the history
Module bot cleanup
  • Loading branch information
barakman authored Apr 21, 2024
2 parents b15d962 + 37d48b5 commit 766c735
Show file tree
Hide file tree
Showing 19 changed files with 209 additions and 548 deletions.
477 changes: 119 additions & 358 deletions fastlane_bot/bot.py

Large diffs are not rendered by default.

30 changes: 0 additions & 30 deletions fastlane_bot/events/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,6 @@ def handle_subsequent_iterations(
arb_mode: str,
bot: CarbonBot,
flashloan_tokens: List[str],
polling_interval: int,
randomizer: int,
run_data_validator: bool,
target_tokens: List[str] = None,
Expand All @@ -1013,8 +1012,6 @@ def handle_subsequent_iterations(
The bot object.
flashloan_tokens : List[str]
A list of flashloan tokens.
polling_interval : int
The polling interval.
randomizer : int
The randomizer.
run_data_validator : bool
Expand Down Expand Up @@ -1059,7 +1056,6 @@ def handle_subsequent_iterations(

# Run the bot
bot.run(
polling_interval=polling_interval,
flashloan_tokens=flashloan_tokens,
arb_mode=arb_mode,
run_data_validator=run_data_validator,
Expand Down Expand Up @@ -1802,32 +1798,6 @@ def handle_target_token_addresses(static_pool_data: pd.DataFrame, target_tokens:
return target_token_addresses


def handle_replay_from_block(replay_from_block: int) -> (int, int, bool):
"""
Handle the replay from block flag.
Parameters
----------
replay_from_block : int
The block number to replay from.
Returns
-------
polling_interval : int
The time interval at which the bot polls for new events.
"""
if replay_from_block:
assert (
replay_from_block > 0
), "The block number to replay from must be greater than 0."
reorg_delay = 0
use_cached_events = False
polling_interval = 0
return polling_interval, reorg_delay, use_cached_events


# %%
def get_current_block(
last_block: int,
mgr: Any,
Expand Down
1 change: 1 addition & 0 deletions fastlane_bot/helpers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
from .univ3calc import Univ3Calculator
from .wrap_unwrap_processor import add_wrap_or_unwrap_trades_to_route
from .carbon_trade_splitter import split_carbon_trades
from .routehandler import maximize_last_trade_per_tkn
13 changes: 1 addition & 12 deletions fastlane_bot/helpers/routehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,10 @@ class RouteStruct:
customData: bytes


def maximize_last_trade_per_tkn(route_struct: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
def maximize_last_trade_per_tkn(route_struct: List[Dict[str, Any]]):
"""
Sets the source amount of the last trade to 0 per-token, ensuring that all tokens held will be used in the last trade.
TODO: this function seems to be only used in this module and therefore should
be made a private function (_maximize_last_trade_per_tkn); I also would suggest
to move it to the TxRouteHandler class, either as static or class method.
:param route_struct: the route struct object
Returns:
List[RouteStruct] the route struct object with the sourceAmount adjusted to 0 for each last-trade per token.
"""

tkns_traded = [route_struct[0]["sourceToken"]]
Expand All @@ -99,8 +90,6 @@ def maximize_last_trade_per_tkn(route_struct: List[Dict[str, Any]]) -> List[Dict
route_struct[idx].sourceAmount = 0
tkns_traded.append(trade.sourceToken)

return route_struct


@dataclass
class TxRouteHandler:
Expand Down
8 changes: 4 additions & 4 deletions fastlane_bot/tests/test_039_TestMultiMode.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,14 @@ def test_test_combos_and_tokens():

# +
arb_finder = bot._get_arb_finder("multi")
finder2 = arb_finder(
finder = arb_finder(
flashloan_tokens=flashloan_tokens,
CCm=CCm,
mode="bothin",
result=bot.AO_TOKENS,
result=arb_finder.AO_TOKENS,
ConfigObj=bot.ConfigObj,
)
all_tokens, combos = finder2.find_arbitrage()
all_tokens, combos = finder.find_arbitrage()

# subjected to the length of `TAX_TOKENS`
assert type(all_tokens) == set, f"[NBTest 039 TestMultiMode] all_tokens is wrong data type. Expected set, found: {type(all_tokens)}"
Expand All @@ -210,7 +210,7 @@ def test_test_expected_output():
flashloan_tokens=flashloan_tokens,
CCm=CCm,
mode="bothin",
result=bot.AO_CANDIDATES,
result=arb_finder.AO_CANDIDATES,
ConfigObj=bot.ConfigObj,
)

Expand Down
8 changes: 4 additions & 4 deletions fastlane_bot/tests/test_040_TestSingleMode.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,14 @@ def test_test_tokens_and_combos():

# +
arb_finder = bot._get_arb_finder("single")
finder2 = arb_finder(
finder = arb_finder(
flashloan_tokens=flashloan_tokens,
CCm=CCm,
mode="bothin",
result=bot.AO_TOKENS,
result=arb_finder.AO_TOKENS,
ConfigObj=bot.ConfigObj,
)
all_tokens, combos = finder2.find_arbitrage()
all_tokens, combos = finder.find_arbitrage()

assert type(all_tokens) == set, f"[TestSingleMode] all_tokens is wrong data type. Expected set, found: {type(all_tokens)}"
assert type(combos) == list, f"[TestSingleMode] combos is wrong data type. Expected list, found: {type(combos)}"
Expand All @@ -178,7 +178,7 @@ def test_test_tokens_and_combos():
flashloan_tokens=flashloan_tokens,
CCm=CCm,
mode="bothin",
result=bot.AO_CANDIDATES,
result=arb_finder.AO_CANDIDATES,
ConfigObj=bot.ConfigObj,
)
r = finder.find_arbitrage()
Expand Down
4 changes: 2 additions & 2 deletions fastlane_bot/tests/test_042_TestBancorV3ModeTwoHop.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
"""
from fastlane_bot import Bot, Config
from fastlane_bot.bot import CarbonBot
from fastlane_bot.helpers import TxRouteHandler
from fastlane_bot.tools.cpc import ConstantProductCurve
from fastlane_bot.tools.cpc import ConstantProductCurve as CPC
from fastlane_bot.events.exchanges import UniswapV2, UniswapV3, CarbonV1, BancorV3
from fastlane_bot.events.interface import QueryInterface
from fastlane_bot.helpers import TxRouteHandler
from fastlane_bot.events.managers.manager import Manager
from fastlane_bot.events.interface import QueryInterface
from joblib import Parallel, delayed
Expand Down Expand Up @@ -192,7 +192,7 @@ def test_test_trade_merge():
# Convert the trade instructions
ordered_trade_instructions_objects = bot._convert_trade_instructions(
ordered_scaled_dcts)
tx_route_handler = bot.TxRouteHandlerClass(
tx_route_handler = TxRouteHandler(
trade_instructions=ordered_trade_instructions_objects
)
agg_trade_instructions = (
Expand Down
3 changes: 2 additions & 1 deletion fastlane_bot/tests/test_043_TestEmptyCarbonOrders.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"""
from fastlane_bot import Bot, Config
from fastlane_bot.bot import CarbonBot
from fastlane_bot.helpers import TxRouteHandler
from fastlane_bot.tools.cpc import ConstantProductCurve as CPC
from fastlane_bot.events.exchanges import UniswapV2, UniswapV3, CarbonV1, BancorV3
from fastlane_bot.events.interface import QueryInterface
Expand Down Expand Up @@ -217,7 +218,7 @@ def test_test_empty_carbon_orders_removed():

ordered_trade_instructions_objects = bot._convert_trade_instructions(ordered_scaled_dcts, )
# print(f"ordered_trade_instructions_objects: {ordered_trade_instructions_objects}")
tx_route_handler = bot.TxRouteHandlerClass(
tx_route_handler = TxRouteHandler(
trade_instructions=ordered_trade_instructions_objects
)
agg_trade_instructions = (
Expand Down
2 changes: 1 addition & 1 deletion fastlane_bot/tests/test_045_Validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def test_test_validator():
flashloan_tokens=flashloan_tokens,
CCm=CCm,
mode="bothin",
result=bot.AO_CANDIDATES,
result=arb_finder.AO_CANDIDATES,
ConfigObj=bot.ConfigObj,
)
r = finder.find_arbitrage()
Expand Down
7 changes: 1 addition & 6 deletions fastlane_bot/tests/test_047_Randomizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def test_test_randomizer():
flashloan_tokens=flashloan_tokens,
CCm=CCm,
mode="bothin",
result=bot.AO_CANDIDATES,
result=arb_finder.AO_CANDIDATES,
ConfigObj=bot.ConfigObj,
)
r = finder.find_arbitrage()
Expand All @@ -161,7 +161,6 @@ def test_test_randomizer():
arb_opp_2 = bot.randomize(arb_opps=r, randomizer=1)
arb_opp_3 = bot.randomize(arb_opps=r, randomizer=1)
arb_opp_25 = bot.randomize(arb_opps=r, randomizer=1)
arb_opp_None = bot.randomize(arb_opps=None, randomizer=5)

assert len(arb_opp_0) == 5, f"[NB047 Randomizer], expected 1 arb back from randomizer with length of 5, found length of {len(arb_opp_0)}"
assert len(arb_opp_1) == 5, f"[NB047 Randomizer], expected 1 arb back from randomizer with length of 5, found length of {len(arb_opp_1)}"
Expand All @@ -176,16 +175,12 @@ def test_test_randomizer():
assert isinstance(np.float64(arb_opp_3[0]), np.floating), f"[NB047 Randomizer], expected first value back from randomizer to be of type np.float64, found type {type(arb_opp_3[0])}"
assert isinstance(np.float64(arb_opp_25[0]), np.floating), f"[NB047 Randomizer], expected first value back from randomizer to be of type np.float64, found type {type(arb_opp_25[0])}"

arb_opp_0[2]

assert type(arb_opp_0[2]) == tuple, f"[NB047 Randomizer], expected third value back from randomizer to be of type list, found type {type(arb_opp_0[2])}"
assert type(arb_opp_1[2]) == tuple, f"[NB047 Randomizer], expected third value back from randomizer to be of type list, found type {type(arb_opp_1[2])}"
assert type(arb_opp_2[2]) == tuple, f"[NB047 Randomizer], expected third value back from randomizer to be of type list, found type {type(arb_opp_2[2])}"
assert type(arb_opp_3[2]) == tuple, f"[NB047 Randomizer], expected third value back from randomizer to be of type list, found type {type(arb_opp_3[2])}"
assert type(arb_opp_25[2]) == tuple, f"[NB047 Randomizer], expected third value back from randomizer to be of type list, found type {type(arb_opp_25[2])}"

assert arb_opp_None == None, f"[NB047 Randomizer], expected randomizer to return None when it receives None, but it returned {type(arb_opp_None)}"


# ------------------------------------------------------------
# Test 047
Expand Down
13 changes: 7 additions & 6 deletions fastlane_bot/tests/test_050_TestBancorV2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"""
from fastlane_bot import Bot, Config
from fastlane_bot.bot import CarbonBot
from fastlane_bot.helpers import TxRouteHandler
from fastlane_bot.tools.cpc import ConstantProductCurve as CPC
from fastlane_bot.events.exchanges import UniswapV2, UniswapV3, CarbonV1, BancorV3
from fastlane_bot.events.interface import QueryInterface
Expand Down Expand Up @@ -155,14 +156,14 @@ def test_test_combos_and_tokens():
# ------------------------------------------------------------

arb_finder = bot._get_arb_finder("multi")
finder2 = arb_finder(
finder = arb_finder(
flashloan_tokens=flashloan_tokens,
CCm=CCm,
mode="bothin",
result=bot.AO_TOKENS,
result=arb_finder.AO_TOKENS,
ConfigObj=bot.ConfigObj,
)
all_tokens, combos = finder2.find_arbitrage()
all_tokens, combos = finder.find_arbitrage()
assert type(all_tokens) == set, f"[NBTest_50_TestBancorV2] all_tokens is wrong data type. Expected set, found: {type(all_tokens)}"
assert type(combos) == list, f"[NBTest_50_TestBancorV2] combos is wrong data type. Expected list, found: {type(combos)}"
assert len(all_tokens) > 100, f"[NBTest_50_TestBancorV2] Using wrong dataset, expected at least 100 tokens, found {len(all_tokens)}"
Expand All @@ -184,7 +185,7 @@ def test_test_expected_output_bancorv2():
flashloan_tokens=flashloan_tokens,
CCm=CCm,
mode="bothin",
result=bot.AO_CANDIDATES,
result=arb_finder.AO_CANDIDATES,
ConfigObj=bot.ConfigObj,
)
r = finder.find_arbitrage()
Expand All @@ -207,7 +208,7 @@ def test_test_expected_output_bancorv2():
flashloan_tokens=flashloan_tokens,
CCm=CCm,
mode="bothin",
result=bot.AO_CANDIDATES,
result=arb_finder.AO_CANDIDATES,
ConfigObj=bot.ConfigObj,
)
r = finder.find_arbitrage()
Expand Down Expand Up @@ -250,7 +251,7 @@ def test_test_expected_output_bancorv2():
)

# Create the tx route handler
tx_route_handler = bot.TxRouteHandlerClass(
tx_route_handler = TxRouteHandler(
trade_instructions=ordered_trade_instructions_objects
)

Expand Down
50 changes: 20 additions & 30 deletions fastlane_bot/tests/test_053_TknMaxTrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from fastlane_bot import Bot
from fastlane_bot.events.exchanges import UniswapV2, UniswapV3, CarbonV1, BancorV3
from fastlane_bot.helpers.routehandler import RouteStruct, maximize_last_trade_per_tkn
from fastlane_bot.helpers import maximize_last_trade_per_tkn
from fastlane_bot.tools.cpc import ConstantProductCurve as CPC

print("{0.__name__} v{0.__VERSION__} ({0.__DATE__})".format(CPC))
Expand All @@ -39,34 +39,24 @@
# Segment Test_use_0_for_sourceAmount
# ------------------------------------------------------------
def test_maximize_last_trade_per_tkn():
route_struct_0 = [
{"sourceAmount": 10, "sourceToken": "bob_tkn", "minReturn": 10, "targetToken": "fred_tkn"},
{"sourceAmount": 10, "sourceToken": "bob_tkn", "minReturn": 10, "targetToken": "fred_tkn"},
{"sourceAmount": 10, "sourceToken": "fred_tkn", "minReturn": 10, "targetToken": "grog_tkn"},
{"sourceAmount": 10, "sourceToken": "fred_tkn", "minReturn": 10, "targetToken": "grog_tkn"},
{"sourceAmount": 10, "sourceToken": "grog_tkn", "minReturn": 10, "targetToken": "bob_tkn"},
{"sourceAmount": 10, "sourceToken": "grog_tkn", "minReturn": 10, "targetToken": "bob_tkn"},

]

source_token = route_struct_0[0]["sourceToken"]
maximize_test_result = maximize_last_trade_per_tkn(route_struct_0)

assert len(maximize_test_result) == len(route_struct_0)

for trade in maximize_test_result:
route_struct = [
{"sourceAmount": 10, "sourceToken": "bob_tkn", "minReturn": 10, "targetToken": "fred_tkn"},
{"sourceAmount": 10, "sourceToken": "bob_tkn", "minReturn": 10, "targetToken": "fred_tkn"},
{"sourceAmount": 10, "sourceToken": "fred_tkn", "minReturn": 10, "targetToken": "grog_tkn"},
{"sourceAmount": 10, "sourceToken": "fred_tkn", "minReturn": 10, "targetToken": "grog_tkn"},
{"sourceAmount": 10, "sourceToken": "grog_tkn", "minReturn": 10, "targetToken": "bob_tkn"},
{"sourceAmount": 10, "sourceToken": "grog_tkn", "minReturn": 10, "targetToken": "bob_tkn"},
]

source_token = route_struct[0]["sourceToken"]
maximize_last_trade_per_tkn(route_struct)

for trade in route_struct:
if trade["sourceToken"] == source_token:
assert trade["sourceAmount"] > 0
assert maximize_test_result[0]["sourceAmount"] == 10
assert maximize_test_result[1]["sourceAmount"] == 10
assert maximize_test_result[2]["sourceAmount"] == 10
assert maximize_test_result[3]["sourceAmount"] == 0
assert maximize_test_result[4]["sourceAmount"] == 10
assert maximize_test_result[5]["sourceAmount"] == 0

# -





assert route_struct[0]["sourceAmount"] == 10
assert route_struct[1]["sourceAmount"] == 10
assert route_struct[2]["sourceAmount"] == 10
assert route_struct[3]["sourceAmount"] == 0
assert route_struct[4]["sourceAmount"] == 10
assert route_struct[5]["sourceAmount"] == 0
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@
from fastlane_bot.events.exchanges import UniswapV2, UniswapV3, CarbonV1, BancorV3
from fastlane_bot.events.interface import QueryInterface
from fastlane_bot.events.managers.manager import Manager
from fastlane_bot.helpers import (
TxRouteHandler,
TradeInstruction,
)
from fastlane_bot.helpers import TxRouteHandler, TradeInstruction
from fastlane_bot.tools.cpc import ConstantProductCurve as CPC

print("{0.__name__} v{0.__VERSION__} ({0.__DATE__})".format(CPC))
Expand Down Expand Up @@ -177,7 +174,7 @@ def test_test_precision_using_all_tokens_in_carbon():
flashloan_tokens=flashloan_tokens,
CCm=CCm,
mode="bothin",
result=bot.AO_CANDIDATES,
result=arb_finder.AO_CANDIDATES,
ConfigObj=bot.ConfigObj,
)
r = finder.find_arbitrage()
Expand Down Expand Up @@ -386,7 +383,7 @@ def calculate_trade_outputs(tx_route_handler: TxRouteHandler,
)

# Create the tx route handler
tx_route_handler = bot.TxRouteHandlerClass(
tx_route_handler = TxRouteHandler(
trade_instructions=ordered_trade_instructions_objects
)

Expand Down
Loading

0 comments on commit 766c735

Please sign in to comment.