From 17f2075c4e88f7b77626c6121a1bac305f2be0fd Mon Sep 17 00:00:00 2001 From: Lesigh-3100 Date: Tue, 14 Nov 2023 21:04:04 +0200 Subject: [PATCH 1/5] Fix to use wei format for generating flashloans to avoid precision or rounding errors --- fastlane_bot/helpers/routehandler.py | 26 +++-- .../NBTest_051_BalancerFlashloans.ipynb | 100 +++++++++--------- .../NBTest/NBTest_051_BalancerFlashloans.py | 18 ++-- 3 files changed, 78 insertions(+), 66 deletions(-) diff --git a/fastlane_bot/helpers/routehandler.py b/fastlane_bot/helpers/routehandler.py index affecf59d..961a9c9fb 100644 --- a/fastlane_bot/helpers/routehandler.py +++ b/fastlane_bot/helpers/routehandler.py @@ -355,6 +355,8 @@ def get_custom_address( return pool.anchor elif pool.exchange_name in self.ConfigObj.UNI_V2_FORKS: return self.ConfigObj.UNI_V2_ROUTER_MAPPING[pool.exchange_name] + elif pool.exchange_name in self.ConfigObj.CARBON_FORKS: + return self.ConfigObj.CARBON_CONTROLLER_ADDRESS elif pool.exchange_name in self.ConfigObj.UNI_V3_FORKS: return self.ConfigObj.UNI_V3_ROUTER_MAPPING[pool.exchange_name] else: @@ -394,15 +396,14 @@ def _get_flashloan_struct(self, trade_instructions_objects: List[TradeInstructio Turns an object containing trade instructions into a struct with flashloan tokens and amounts ready to send to the smart contract. :param flash_tokens: an object containing flashloan tokens in the format {tkn: {"tkn": tkn_address, "flash_amt": tkn_amt}} """ - flash_tokens = self._extract_flashloan_tokens(trade_instructions=trade_instructions_objects) + flash_tokens = self._extract_single_flashloan_token(trade_instructions=trade_instructions_objects) flashloans = [] balancer = {"platformId": 7, "sourceTokens": [], "sourceAmounts": []} has_balancer = False for tkn in flash_tokens.keys(): platform_id = self._get_flashloan_platform_id(tkn) source_token = flash_tokens[tkn]["tkn"] - source_amounts = TradeInstruction._convert_to_wei(abs(flash_tokens[tkn]["flash_amt"]), - flash_tokens[tkn]["decimals"]) + source_amounts = abs(flash_tokens[tkn]["flash_amt"]) if platform_id == 7: has_balancer = True balancer["sourceTokens"].append(source_token) @@ -436,6 +437,16 @@ def wrapped_gas_token_to_native(self, tkn: str): else: return tkn + def _extract_single_flashloan_token(self, trade_instructions: List[TradeInstruction]) -> Dict: + """ + Generate a flashloan tokens and amounts. + :param trade_instructions: A list of trade instruction objects + """ + flash_tokens = {trade_instructions[0].tknin_key: {"tkn": self.wrapped_gas_token_to_native(trade_instructions[0]._tknin_address), + "flash_amt": trade_instructions[0].amtin_wei, + "decimals": trade_instructions[0].tknin_decimals}} + return flash_tokens + def _extract_flashloan_tokens(self, trade_instructions: List[TradeInstruction]) -> Dict: """ Generate a list of the flashloan tokens and amounts. @@ -454,14 +465,15 @@ def _extract_flashloan_tokens(self, trade_instructions: List[TradeInstruction]) tknin_key = self.wrapped_gas_token_to_native(trade.tknin_key) tknout_key = self.wrapped_gas_token_to_native(trade.tknout_key) - token_change[tknin_key]["amtin"] = token_change[tknin_key]["amtin"] + Decimal(str(trade.amtin)) - token_change[tknin_key]["balance"] = token_change[tknin_key]["balance"] - Decimal(str(trade.amtin)) - token_change[tknout_key]["amtout"] = token_change[tknout_key]["amtout"] + Decimal(str(trade.amtout)) - token_change[tknout_key]["balance"] = token_change[tknout_key]["balance"] + Decimal(str(trade.amtout)) + token_change[tknin_key]["amtin"] = token_change[tknin_key]["amtin"] + trade.amtin_wei + token_change[tknin_key]["balance"] = token_change[tknin_key]["balance"] - trade.amtin_wei + token_change[tknout_key]["amtout"] = token_change[tknout_key]["amtout"] + trade.amtout_wei + token_change[tknout_key]["balance"] = token_change[tknout_key]["balance"] + trade.amtout_wei if token_change[tknin_key]["balance"] < 0: flash_tokens[tknin_key] = {"tkn": trade._tknin_address, "flash_amt": token_change[tknin_key]["amtin"], "decimals": trade.tknin_decimals} + return flash_tokens def get_arb_contract_args( diff --git a/resources/NBTest/NBTest_051_BalancerFlashloans.ipynb b/resources/NBTest/NBTest_051_BalancerFlashloans.ipynb index 2e9ffa43c..9a10e278f 100644 --- a/resources/NBTest/NBTest_051_BalancerFlashloans.ipynb +++ b/resources/NBTest/NBTest_051_BalancerFlashloans.ipynb @@ -25,7 +25,7 @@ "name": "stderr", "output_type": "stream", "text": [ - "C:\\Users\\Kveen\\AppData\\Local\\Temp\\ipykernel_41036\\3597301263.py:29: MatplotlibDeprecationWarning: The seaborn styles shipped by Matplotlib are deprecated since 3.6, as they no longer correspond to the styles shipped by seaborn. However, they will remain available as 'seaborn-v0_8-