From c4d1d40431833a7d767f2d6ac09fc7172a9d9235 Mon Sep 17 00:00:00 2001 From: barak manos <> Date: Fri, 17 May 2024 11:50:58 +0300 Subject: [PATCH] Code reuse --- fastlane_bot/modes/base.py | 36 +++++++++++++++++++++++++++++ fastlane_bot/modes/base_pairwise.py | 27 +++++----------------- fastlane_bot/modes/base_triangle.py | 27 +++++----------------- 3 files changed, 48 insertions(+), 42 deletions(-) diff --git a/fastlane_bot/modes/base.py b/fastlane_bot/modes/base.py index 76bba3565..24287c859 100644 --- a/fastlane_bot/modes/base.py +++ b/fastlane_bot/modes/base.py @@ -80,6 +80,42 @@ def custom_sort(self, data, sort_sequence): sort_order = self.create_sort_order(sort_sequence) return sorted(data, key=lambda item: self.sort_key(item, sort_order)) + def update_results( + self, + src_token: str, + r, + trade_instructions_dic, + trade_instructions_df, + trade_instructions, + candidates, + best_profit, + ops, + ): + # Calculate the profit + profit = self.calculate_profit(src_token, -r.result, self.CCm) + if str(profit) == "nan": + self.ConfigObj.logger.debug("profit is nan, skipping") + else: + # Handle candidates based on conditions + candidates += self.handle_candidates( + profit, + trade_instructions_df, + trade_instructions_dic, + src_token, + trade_instructions, + ) + # Find the best operations + best_profit, ops = self.find_best_operations( + best_profit, + ops, + profit, + trade_instructions_df, + trade_instructions_dic, + src_token, + trade_instructions, + ) + return best_profit, ops + def calculate_profit( self, src_token: str, diff --git a/fastlane_bot/modes/base_pairwise.py b/fastlane_bot/modes/base_pairwise.py index e807ee1c7..ae2d52d42 100644 --- a/fastlane_bot/modes/base_pairwise.py +++ b/fastlane_bot/modes/base_pairwise.py @@ -47,31 +47,16 @@ def find_arbitrage(self) -> Union[List, Tuple]: if trade_instructions_dic is None or len(trade_instructions_dic) < 2: # Failed to converge continue - - # Calculate the profit - profit = self.calculate_profit(src_token, -r.result, self.CCm) - if str(profit) == "nan": - self.ConfigObj.logger.debug("profit is nan, skipping") - continue - - # Handle candidates based on conditions - candidates += self.handle_candidates( - profit, - trade_instructions_df, - trade_instructions_dic, + # Update the results + best_profit, ops = self.update_results( src_token, + r, + trade_instructions_dic, + trade_instructions_df, trade_instructions, - ) - - # Find the best operations - best_profit, ops = self.find_best_operations( + candidates, best_profit, ops, - profit, - trade_instructions_df, - trade_instructions_dic, - src_token, - trade_instructions, ) return candidates if self.result == self.AO_CANDIDATES else ops diff --git a/fastlane_bot/modes/base_triangle.py b/fastlane_bot/modes/base_triangle.py index 77bc6cf6f..5288a9c93 100644 --- a/fastlane_bot/modes/base_triangle.py +++ b/fastlane_bot/modes/base_triangle.py @@ -38,31 +38,16 @@ def find_arbitrage(self) -> Union[List, Tuple]: if trade_instructions_dic is None or len(trade_instructions_dic) < 3: # Failed to converge continue - - # Calculate the profit - profit = self.calculate_profit(src_token, -r.result, self.CCm) - if str(profit) == "nan": - self.ConfigObj.logger.debug("profit is nan, skipping") - continue - - # Handle candidates based on conditions - candidates += self.handle_candidates( - profit, - trade_instructions_df, - trade_instructions_dic, + # Update the results + best_profit, ops = self.update_results( src_token, + r, + trade_instructions_dic, + trade_instructions_df, trade_instructions, - ) - - # Find the best operations - best_profit, ops = self.find_best_operations( + candidates, best_profit, ops, - profit, - trade_instructions_df, - trade_instructions_dic, - src_token, - trade_instructions, ) return candidates if self.result == self.AO_CANDIDATES else ops