Skip to content

Commit

Permalink
New Feature: Add target_tokens Setting to Narrow Search Space
Browse files Browse the repository at this point in the history
  • Loading branch information
mikewcasale committed Aug 9, 2023
1 parent e56a78d commit feed3f2
Show file tree
Hide file tree
Showing 4 changed files with 473 additions and 2 deletions.
24 changes: 22 additions & 2 deletions fastlane_bot/events/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@ class QueryInterface:
def cfg(self) -> Config:
return self.ConfigObj

def filter_target_tokens(self, target_tokens: List[str]):
"""
Filter the pools to only include pools that are in the target pools list
Parameters
----------
target_tokens: List[str]
The list of tokens to filter pools by. Pools must contain both tokens in the list to be included.
"""
initial_state = self.state.copy()
self.state = [
pool
for pool in self.state
if pool["tkn0_key"] in target_tokens and pool["tkn1_key"] in target_tokens
]

self.cfg.logger.info(
f"Limiting pools by target_tokens. Removed {len(initial_state) - len(self.state)} non target-pools. {len(self.state)} pools remaining"
)

def remove_unsupported_exchanges(self) -> None:
initial_state = self.state.copy()
self.state = [
Expand Down Expand Up @@ -479,8 +499,8 @@ def get_pool(self, **kwargs) -> Optional[PoolAndTokens]:
)
pool.exchange_name
except AttributeError:
if 'cid' in kwargs:
kwargs['cid'] = int(kwargs['cid'])
if "cid" in kwargs:
kwargs["cid"] = int(kwargs["cid"])
pool = next(
(
pool
Expand Down
36 changes: 36 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@
type=int,
help="Set to the timeout in seconds. Set to None for no timeout.",
)
@click.option(
"--target_tokens",
default=None,
type=str,
help="A comma-separated string of tokens to target. Use None to target all tokens. Use `flashloan_tokens` to target only the flashloan tokens.",
)
def main(
cache_latest_only: bool,
backdate_pools: bool,
Expand All @@ -157,6 +163,7 @@ def main(
limit_bancor3_flashloan_tokens: bool,
default_min_profit_bnt: int,
timeout: int,
target_tokens: str,
):
"""
The main entry point of the program. It sets up the configuration, initializes the web3 and Base objects,
Expand All @@ -182,6 +189,7 @@ def main(
limit_bancor3_flashloan_tokens (bool): Whether to limit the flashloan tokens to the ones supported by Bancor v3 or not.
default_min_profit_bnt (int): The default minimum profit in BNT.
timeout (int): The timeout in seconds.
target_tokens (str): A comma-separated string of tokens to target. Use None to target all tokens. Use `flashloan_tokens` to target only the flashloan tokens.
"""
start_time = time.time()
Expand Down Expand Up @@ -221,6 +229,27 @@ def main(
f"Flashloan tokens are set as: {flashloan_tokens}, {type(flashloan_tokens)}"
)

if target_tokens:
if target_tokens == "flashloan_tokens":
target_tokens = flashloan_tokens
else:
target_tokens = target_tokens.split(",")
target_tokens = [
QueryInterface.cleanup_token_key(token) for token in target_tokens
]

# Ensure that the target tokens are a subset of the flashloan tokens
for token in flashloan_tokens:
if token not in target_tokens:
cfg.logger.warning(
f"Falshloan token {token} not in target tokens. Adding it to target tokens."
)
target_tokens.append(token)

cfg.logger.info(
f"Target tokens are set as: {target_tokens}, {type(target_tokens)}"
)

# Set external exchanges
exchanges = exchanges.split(",")
cfg.logger.info(f"Running data fetching for exchanges: {exchanges}")
Expand Down Expand Up @@ -304,6 +333,7 @@ def main(
run_data_validator,
randomizer,
timeout,
target_tokens,
)


Expand All @@ -322,6 +352,7 @@ def run(
run_data_validator: bool,
randomizer: int,
timeout: int,
target_tokens: List[str] or None,
) -> None:
"""
The main function that drives the logic of the program. It uses helper functions to handle specific tasks.
Expand All @@ -341,6 +372,7 @@ def run(
run_data_validator (bool): Whether to run the data validator or not.
randomizer (bool): Whether to randomize the polling interval or not.
timeout (int): The timeout for the polling interval.
target_tokens (List[str]): List of tokens that the bot will target for arbitrage.
"""

def get_event_filters(
Expand Down Expand Up @@ -662,6 +694,10 @@ def update_pools_from_contracts(
bot.db.remove_zero_liquidity_pools()
bot.db.remove_unsupported_exchanges()

# Filter the target tokens
if target_tokens:
bot.db.filter_target_tokens(target_tokens)

# Run the bot
bot.run(
polling_interval=polling_interval,
Expand Down
Loading

0 comments on commit feed3f2

Please sign in to comment.