Skip to content

Commit

Permalink
Code reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
barak manos authored and platonfloria committed May 17, 2024
1 parent 9e5d3ae commit c4d1d40
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 42 deletions.
36 changes: 36 additions & 0 deletions fastlane_bot/modes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
27 changes: 6 additions & 21 deletions fastlane_bot/modes/base_pairwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 6 additions & 21 deletions fastlane_bot/modes/base_triangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c4d1d40

Please sign in to comment.