Skip to content

Commit

Permalink
Get rid of num_format
Browse files Browse the repository at this point in the history
  • Loading branch information
barak manos committed May 25, 2024
1 parent 8fc7eb3 commit ee6faf9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 37 deletions.
14 changes: 7 additions & 7 deletions fastlane_bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
]
Expand Down
12 changes: 2 additions & 10 deletions fastlane_bot/helpers/txhelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion fastlane_bot/tests/test_061_TestWETHConversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 10 additions & 19 deletions fastlane_bot/utils.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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
Expand Down

0 comments on commit ee6faf9

Please sign in to comment.