Skip to content

Commit

Permalink
Merge pull request #58 from bancorprotocol/57-change-the-flashloan_to…
Browse files Browse the repository at this point in the history
…kens-flag-type-to-list

Fix to ensure the @click flag for flashloan_tokens is respected
  • Loading branch information
mikewcasale authored Aug 9, 2023
2 parents 1f60586 + 9485fe0 commit 0554e10
Show file tree
Hide file tree
Showing 4 changed files with 484 additions and 116 deletions.
158 changes: 98 additions & 60 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
from fastlane_bot.config import Config
from fastlane_bot.events.interface import QueryInterface
from fastlane_bot.events.managers.manager import Manager
from fastlane_bot.events.utils import complex_handler, filter_latest_events
from fastlane_bot.events.utils import (
complex_handler,
filter_latest_events,
)
from fastlane_bot.tools.cpc import T

load_dotenv()

Expand All @@ -44,18 +48,25 @@
help="Filename of the static pool data.",
)
@click.option(
"--arb_mode", default="multi", help="See arb_mode in bot.py",
type=click.Choice(["single", "multi", "triangle", "multi_triangle", "bancor_v3", "b3_two_hop"])
"--arb_mode",
default="multi",
help="See arb_mode in bot.py",
type=click.Choice(
["single", "multi", "triangle", "multi_triangle", "bancor_v3", "b3_two_hop"]
),
)
@click.option(
"--flashloan_tokens", default=None, type=str, help="See flashloan_tokens in bot.py"
"--flashloan_tokens",
default=f"{T.WETH},{T.DAI},{T.USDC},{T.USDT},{T.WBTC},{T.BNT},{T.NATIVE_ETH}",
type=str,
help="The --flashloan_tokens flag refers to those token denominations which the bot can take a flash loan in. By default, these are [WETH, DAI, USDC, USDT, WBTC, BNT, NATIVE_ETH]. If you override the default to TKN, the search space is decreased for all modes, including the b3_two_hop mode (assuming that --limit_bancor3_flashloan_tokens=True).",
)
@click.option("--config", default=None, type=str, help="See config in config/*")
@click.option("--n_jobs", default=-1, help="Number of parallel jobs to run")
@click.option(
"--exchanges",
default="carbon_v1,bancor_v3,uniswap_v3,uniswap_v2,sushiswap_v2",
help="Comma separated external exchanges. Note that carbon_v1 and bancor_v3 must be included."
help="Comma separated external exchanges. Note that carbon_v1 and bancor_v3 must be included.",
)
@click.option(
"--polling_interval",
Expand All @@ -74,7 +85,7 @@
)
@click.option(
"--logging_path",
default='',
default="",
help="The logging path.",
)
@click.option(
Expand Down Expand Up @@ -126,26 +137,26 @@
help="Set to the timeout in seconds. Set to None for no timeout.",
)
def main(
cache_latest_only: bool,
backdate_pools: bool,
arb_mode: str,
flashloan_tokens: str,
config: str,
n_jobs: int,
exchanges: str,
polling_interval: int,
alchemy_max_block_fetch: int,
static_pool_data_filename: str,
reorg_delay: int,
logging_path: str,
loglevel: str,
static_pool_data_sample_sz: str,
use_cached_events: bool,
run_data_validator: bool,
randomizer: int,
limit_bancor3_flashloan_tokens: bool,
default_min_profit_bnt: int,
timeout: int,
cache_latest_only: bool,
backdate_pools: bool,
arb_mode: str,
flashloan_tokens: str,
config: str,
n_jobs: int,
exchanges: str,
polling_interval: int,
alchemy_max_block_fetch: int,
static_pool_data_filename: str,
reorg_delay: int,
logging_path: str,
loglevel: str,
static_pool_data_sample_sz: str,
use_cached_events: bool,
run_data_validator: bool,
randomizer: int,
limit_bancor3_flashloan_tokens: bool,
default_min_profit_bnt: int,
timeout: int,
):
"""
The main entry point of the program. It sets up the configuration, initializes the web3 and Base objects,
Expand All @@ -155,7 +166,7 @@ def main(
cache_latest_only (bool): Whether to cache only the latest events or not.
backdate_pools (bool): Whether to backdate pools or not. Set to False for quicker testing runs.
arb_mode (str): The arbitrage mode to use.
flashloan_tokens (str): Tokens that the bot can use for flash loans.
flashloan_tokens (str): Comma seperated list of tokens that the bot can use for flash loans.
config (str): The name of the configuration to use.
n_jobs (int): The number of jobs to run in parallel.
exchanges (str): A comma-separated string of exchanges to include.
Expand All @@ -173,6 +184,8 @@ def main(
timeout (int): The timeout in seconds.
"""
start_time = time.time()

# Set config
loglevel = (
Config.LOGLEVEL_DEBUG
Expand All @@ -199,6 +212,15 @@ def main(
cfg.DEFAULT_MIN_PROFIT_BNT = Decimal(str(default_min_profit_bnt))
cfg.DEFAULT_MIN_PROFIT = Decimal(str(default_min_profit_bnt))

# Log the flashloan tokens
flashloan_tokens = flashloan_tokens.split(",")
flashloan_tokens = [
QueryInterface.cleanup_token_key(token) for token in flashloan_tokens
]
cfg.logger.info(
f"Flashloan tokens are set as: {flashloan_tokens}, {type(flashloan_tokens)}"
)

# Set external exchanges
exchanges = exchanges.split(",")
cfg.logger.info(f"Running data fetching for exchanges: {exchanges}")
Expand All @@ -219,10 +241,10 @@ def main(
if static_pool_data_sample_sz != "max":
bancor3_pool_data = static_pool_data[
static_pool_data["exchange_name"] == "bancor_v3"
]
]
non_bancor3_pool_data = static_pool_data[
static_pool_data["exchange_name"] != "bancor_v3"
]
]
non_bancor3_pool_data = non_bancor3_pool_data.sample(
n=int(static_pool_data_sample_sz)
)
Expand All @@ -238,6 +260,10 @@ def main(
cfg.logger.error("Error parsing the CSV file")
raise

if timeout == 1:
cfg.logger.info("Timeout to test the bot flags")
return

# Initialize web3
static_pool_data["cid"] = [
cfg.w3.keccak(text=f"{row['descr']}").hex()
Expand Down Expand Up @@ -277,25 +303,25 @@ def main(
use_cached_events,
run_data_validator,
randomizer,
timeout
timeout,
)


def run(
cache_latest_only: bool,
backdate_pools: bool,
mgr: Manager,
n_jobs: int,
polling_interval: int,
alchemy_max_block_fetch: int,
arb_mode: str,
flashloan_tokens: List[str] or None,
reorg_delay: int,
logging_path: str,
use_cached_events: bool,
run_data_validator: bool,
randomizer: int,
timeout: int
cache_latest_only: bool,
backdate_pools: bool,
mgr: Manager,
n_jobs: int,
polling_interval: int,
alchemy_max_block_fetch: int,
arb_mode: str,
flashloan_tokens: List[str] or None,
reorg_delay: int,
logging_path: str,
use_cached_events: bool,
run_data_validator: bool,
randomizer: int,
timeout: int,
) -> None:
"""
The main function that drives the logic of the program. It uses helper functions to handle specific tasks.
Expand All @@ -318,7 +344,7 @@ def run(
"""

def get_event_filters(
start_block: int, current_block: int, reorg_delay: int
start_block: int, current_block: int, reorg_delay: int
) -> Any:
"""
Creates event filters for the specified block range.
Expand Down Expand Up @@ -354,7 +380,7 @@ def get_all_events(event_filters: Any) -> List[Any]:
)

def save_events_to_json(
latest_events: List[Any], start_block: int, current_block: int
latest_events: List[Any], start_block: int, current_block: int
) -> None:
"""
Saves the given events to a JSON file.
Expand All @@ -373,8 +399,8 @@ def save_events_to_json(
try:
with open(path, "w") as f:
latest_events = [
_["args"].pop("contextId", None) for _ in latest_events
] and latest_events
_["args"].pop("contextId", None) for _ in latest_events
] and latest_events
f.write(json.dumps(latest_events))
except Exception as e:
mgr.cfg.logger.error(f"Error saving events to JSON: {e}")
Expand Down Expand Up @@ -412,7 +438,7 @@ def write_pool_data_to_disk(current_block: int) -> None:
mgr.cfg.logger.error(f"Error writing pool data to disk: {e}")

def parse_bancor3_rows_to_update(
rows_to_update: List[Hashable],
rows_to_update: List[Hashable],
) -> Tuple[List[Hashable], List[Hashable]]:
"""
Parses the rows to update for Bancor v3 pools.
Expand Down Expand Up @@ -465,10 +491,10 @@ def init_bot(mgr: Manager) -> CarbonBot:
return bot

def update_pools_from_contracts(
n_jobs,
rows_to_update: List[int],
not_bancor_v3: bool = True,
current_block: int = None,
n_jobs,
rows_to_update: List[int],
not_bancor_v3: bool = True,
current_block: int = None,
) -> None:
"""
Updates the pools with the given indices by calling the contracts.
Expand Down Expand Up @@ -514,7 +540,8 @@ def update_pools_from_contracts(

# Get current block number, then adjust to the block number reorg_delay blocks ago to avoid reorgs
start_block = (
max(block["last_updated_block"] for block in mgr.pool_data) - reorg_delay
max(block["last_updated_block"] for block in mgr.pool_data)
- reorg_delay
if last_block != 0
else mgr.web3.eth.blockNumber - reorg_delay - alchemy_max_block_fetch
)
Expand Down Expand Up @@ -577,8 +604,11 @@ def update_pools_from_contracts(
)

for rows_to_update in [bancor3_pool_rows, other_pool_rows]:
update_pools_from_contracts(n_jobs=n_jobs, rows_to_update=rows_to_update,
current_block=current_block)
update_pools_from_contracts(
n_jobs=n_jobs,
rows_to_update=rows_to_update,
current_block=current_block,
)

elif last_block == 0 and "bancor_v3" in mgr.exchanges:
# Update the pool data on disk
Expand All @@ -588,8 +618,12 @@ def update_pools_from_contracts(
for idx, pool in enumerate(mgr.pool_data)
if pool["exchange_name"] == "bancor_v3"
]
update_pools_from_contracts(n_jobs=n_jobs, rows_to_update=rows_to_update, not_bancor_v3=False,
current_block=current_block)
update_pools_from_contracts(
n_jobs=n_jobs,
rows_to_update=rows_to_update,
not_bancor_v3=False,
current_block=current_block,
)
elif last_block == 0 and "carbon_v1" in mgr.exchanges:
# Update the pool data on disk
mgr.get_rows_to_update(start_block)
Expand All @@ -607,12 +641,16 @@ def update_pools_from_contracts(
del bot
bot = init_bot(mgr)

assert bot.ConfigObj.DEFAULT_MIN_PROFIT == mgr.cfg.DEFAULT_MIN_PROFIT, "bot failed to update min profit"
assert (
bot.ConfigObj.DEFAULT_MIN_PROFIT == mgr.cfg.DEFAULT_MIN_PROFIT
), "bot failed to update min profit"
mgr.cfg.logger.debug("Bot successfully updated min profit")

# Compare the initial state to the final state, and update the state if it has changed
final_state = mgr.pool_data.copy()
assert bot.db.state == final_state, "\n *** bot failed to update state *** \n"
assert (
bot.db.state == final_state
), "\n *** bot failed to update state *** \n"
if initial_state != final_state:
mgr.cfg.logger.info("State has changed...")

Expand Down
Loading

0 comments on commit 0554e10

Please sign in to comment.