From 9bd45cf9c26b19c029cddb8be722e6ca884b356c Mon Sep 17 00:00:00 2001 From: barak manos <> Date: Tue, 21 May 2024 10:54:43 +0300 Subject: [PATCH] Simplify function `routerhandler._get_output_trade_by_source_carbon` --- fastlane_bot/helpers/routehandler.py | 55 +++------------------------- 1 file changed, 5 insertions(+), 50 deletions(-) diff --git a/fastlane_bot/helpers/routehandler.py b/fastlane_bot/helpers/routehandler.py index 01be8f756..9adabda1a 100644 --- a/fastlane_bot/helpers/routehandler.py +++ b/fastlane_bot/helpers/routehandler.py @@ -727,46 +727,6 @@ def decode_decimal_adjustment(self, value: Decimal, tkn_in_decimals: int or str, (tkn_in_decimals - tkn_out_decimals) / Decimal("2") ) - @staticmethod - def _get_input_trade_by_target_carbon( - y, z, A, B, fee, tkns_out: Decimal, trade_by_source: bool = True - ) -> Tuple[Decimal, Decimal]: - """ - Refactored get input trade by target fastlane_bot. - - Parameters - ---------- - y: Decimal - The y. - z: Decimal - The z. - A: Decimal - The A. - B: Decimal - The B. - fee: Decimal - The fee. - tkns_out: Decimal - The tokens out. - - Returns - ------- - Tuple[Decimal, Decimal] - The tokens in and tokens out. - """ - # Fee set to 0 to avoid - fee = Decimal(str(fee)) - tkns_out = min(tkns_out, y) - tkns_in = ( - (tkns_out * z ** 2) / ((A * y + B * z) * (A * y + B * z - A * tkns_out)) - ) - - if not trade_by_source: - # Only taking fee if calculating by trade by target. Otherwise fee will be calculated in trade by source function. - tkns_in = tkns_in * Decimal(1 - fee) - - return tkns_in, tkns_out - def _get_output_trade_by_source_carbon( self, y, z, A, B, fee, tkns_in: Decimal ) -> Tuple[Decimal, Decimal]: @@ -794,18 +754,13 @@ def _get_output_trade_by_source_carbon( The tuple of tokens in and tokens out. """ - fee = Decimal(str(fee)) - tkns_out = Decimal( - (tkns_in * (B * z + A * y) ** 2) - / (tkns_in * (B * A * z + A ** 2 * y) + z ** 2) - ) + tkns_out = (tkns_in * (B * z + A * y) ** 2) / (tkns_in * (B * A * z + A ** 2 * y) + z ** 2) + if tkns_out > y: - tkns_in, tkns_out = self._get_input_trade_by_target_carbon( - y=y, z=z, A=A, B=B, fee=fee, tkns_out=y, trade_by_source=True - ) + tkns_out = y + tkns_in = (y * z ** 2) / ((A * y + B * z) * (A * y + B * z - A * y)) - tkns_out = tkns_out * (Decimal("1") - fee) - return tkns_in, tkns_out + return tkns_in, tkns_out * (1 - fee) def _calc_carbon_output( self, curve: Pool, tkn_in: str, tkn_in_decimals: int, tkn_out_decimals: int, amount_in: Decimal