From ee6faf95801b863a542cb8b77b5fe08e940cbec3 Mon Sep 17 00:00:00 2001 From: barak manos <> Date: Sat, 25 May 2024 23:30:52 +0300 Subject: [PATCH] Get rid of `num_format` --- fastlane_bot/bot.py | 14 ++++----- fastlane_bot/helpers/txhelpers.py | 12 ++------ .../tests/test_061_TestWETHConversion.py | 1 - fastlane_bot/utils.py | 29 +++++++------------ 4 files changed, 19 insertions(+), 37 deletions(-) diff --git a/fastlane_bot/bot.py b/fastlane_bot/bot.py index e7a18b2b8..a93da2bb8 100644 --- a/fastlane_bot/bot.py +++ b/fastlane_bot/bot.py @@ -74,7 +74,7 @@ from .modes.triangle_multi_complete import ArbitrageFinderTriangleMultiComplete from .modes.triangle_bancor_v3_two_hop import ArbitrageFinderTriangleBancor3TwoHop from .modes.base import get_prices_simple, custom_sort -from .utils import num_format, rand_item +from .utils import rand_item @dataclass @@ -478,19 +478,19 @@ def _handle_trade_instructions( # Log the calculated arbitrage arb_info = [ f"arb mode = {arb_mode}", - f"gas profit = {num_format(best_profit_gastkn)}", - f"usd profit = {num_format(best_profit_usd)}", + f"gas profit = {best_profit_gastkn}", + f"usd profit = {best_profit_usd}", f"flashloan token = {fl_token_symbol}", - f"flashloan amount = {num_format(calculated_trade_instructions[0].amtin)}", - f"flashloan profit = {num_format(flashloan_tkn_profit)}" + f"flashloan amount = {calculated_trade_instructions[0].amtin}", + f"flashloan profit = {flashloan_tkn_profit}" ] arb_ti_info = [ { "exchange": trade.exchange_name, "tkn_in": {trade.tknin_symbol: trade.tknin} if trade.tknin_symbol != trade.tknin else trade.tknin, - "amt_in": num_format(trade.amtin), + "amt_in": trade.amtin, "tkn_out": {trade.tknout_symbol: trade.tknout} if trade.tknout_symbol != trade.tknout else trade.tknout, - "amt_out": num_format(trade.amtout) + "amt_out": trade.amtout } for trade in calculated_trade_instructions ] diff --git a/fastlane_bot/helpers/txhelpers.py b/fastlane_bot/helpers/txhelpers.py index 0fdf166fc..b44ce98fe 100644 --- a/fastlane_bot/helpers/txhelpers.py +++ b/fastlane_bot/helpers/txhelpers.py @@ -25,7 +25,6 @@ from web3.exceptions import TimeExhausted from fastlane_bot.config import Config -from fastlane_bot.utils import num_format from fastlane_bot.data.abi import ERC20_ABI MAX_UINT256 = 2 ** 256 - 1 @@ -81,13 +80,6 @@ def validate_and_submit_transaction( """ self.cfg.logger.info("[helpers.txhelpers.validate_and_submit_transaction] Validating trade...") - self.cfg.logger.debug( - f"[helpers.txhelpers.validate_and_submit_transaction]:\n" - f"- Routes: {route_struct}\n" - f"- Source amount: {src_amt}\n" - f"- Source token: {src_address}\n" - f"- Expected profit: {num_format(expected_profit_gastkn)} GAS token ({num_format(expected_profit_usd)} USD)\n" - ) if self.cfg.SELF_FUND: fn_name = "fundAndArb" @@ -122,8 +114,8 @@ def validate_and_submit_transaction( self.cfg.logger.info( f"[helpers.txhelpers.validate_and_submit_transaction]:\n" - f"- Expected cost: {num_format(gas_cost_eth)} GAS token ({num_format(gas_cost_usd)} USD)\n" - f"- Expected gain: {num_format(gas_gain_eth)} GAS token ({num_format(gas_gain_usd)} USD)\n" + f"- Expected cost: {gas_cost_eth} GAS token ({gas_cost_usd} USD)\n" + f"- Expected gain: {gas_gain_eth} GAS token ({gas_gain_usd} USD)\n" ) if gas_gain_eth > gas_cost_eth: diff --git a/fastlane_bot/tests/test_061_TestWETHConversion.py b/fastlane_bot/tests/test_061_TestWETHConversion.py index 6eb3c8b6f..083a6e12e 100644 --- a/fastlane_bot/tests/test_061_TestWETHConversion.py +++ b/fastlane_bot/tests/test_061_TestWETHConversion.py @@ -23,7 +23,6 @@ from fastlane_bot.helpers import TxRouteHandler from fastlane_bot.events.exchanges import UniswapV2, UniswapV3, CarbonV1, BancorV3 -from fastlane_bot.utils import num_format from fastlane_bot.helpers import add_wrap_or_unwrap_trades_to_route, split_carbon_trades from fastlane_bot.events.managers.manager import Manager from dataclasses import asdict diff --git a/fastlane_bot/utils.py b/fastlane_bot/utils.py index e0a337ff5..f8a0aa144 100644 --- a/fastlane_bot/utils.py +++ b/fastlane_bot/utils.py @@ -1,34 +1,25 @@ """ Various utility functions -NOTE: Those functions would more naturally fit in `helpers` -TODO-MIKE, TODO-KEVIN: check how hard this is - --- (c) Copyright Bprotocol foundation 2023-24. All rights reserved. Licensed under MIT. """ -import glob -import random -import os.path +from glob import glob +from random import choice +from os.path import join from decimal import Decimal -def safe_int(value: int or float) -> int: - assert value == int(value), f"non-integer `float` value {value}" - return int(value) - - -def num_format(value: int or float) -> str: - try: - return "{0:.4f}".format(value) - except Exception: - return str(value) +def safe_int(value) -> int: + int_value = int(value) + assert value == int_value, f"non-integer `float` value {value}" + return int_value def rand_item(list_of_items: list, num_of_items: int) -> any: - return random.choice(list_of_items[:min(max(num_of_items, 1), len(list_of_items))]) + return choice(list_of_items[:min(max(num_of_items, 1), len(list_of_items))]) def find_latest_timestamped_folder(logging_path=None): @@ -42,8 +33,8 @@ def find_latest_timestamped_folder(logging_path=None): str: Path to the latest timestamped folder, or None if no folder is found. """ search_path = logging_path if logging_path else "." - search_path = os.path.join(search_path, "logs/*") - list_of_folders = glob.glob(search_path) + search_path = join(search_path, "logs/*") + list_of_folders = glob(search_path) if not list_of_folders: return None