From 70f241ec0ec883adc7330588135af71f81688484 Mon Sep 17 00:00:00 2001 From: beeb Date: Mon, 30 May 2022 07:47:52 +0200 Subject: [PATCH 1/2] Fixed price impact calculation for tokens with decimals different from 18 --- pancaketrade/network/bsc.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pancaketrade/network/bsc.py b/pancaketrade/network/bsc.py index c39d706..5ef678f 100644 --- a/pancaketrade/network/bsc.py +++ b/pancaketrade/network/bsc.py @@ -392,10 +392,16 @@ def calculate_price_impact( token_price, _ = self.get_token_price(token_address) if self.price_in_usd: # we need price in BNB / token token_price = token_price / self.get_bnb_price() - quote_amount_out = amount_in * token_price if sell else amount_in / token_price + quote_amount_out = amount_in * token_price if sell else amount_in / token_price # with "in" token decimals if swap_path is None or amount_out is None: - swap_path, amount_out = self.get_best_swap_path(token_address, amount_in, sell) - slippage = (quote_amount_out - amount_out) / quote_amount_out + swap_path, amount_out = self.get_best_swap_path(token_address, amount_in, sell) # with "out" token decimals + quote_amount_out_normalized = quote_amount_out * Decimal( + 10 ** (18 - self.get_token_decimals(swap_path[0])) + ) # normalize to 18 decimals + amount_out_normalized = Decimal(amount_out) * Decimal( + 10 ** (18 - self.get_token_decimals(swap_path[-1])) + ) # normalize to 18 decimals + slippage = (quote_amount_out_normalized - amount_out_normalized) / quote_amount_out_normalized lpFee = Decimal("0.0025") if len(swap_path) == 2 else Decimal("0.0049375") # 1 - (1-0.25%)^2 return slippage - lpFee From 94829b53b3d50076faccacab4266b85c46d63288 Mon Sep 17 00:00:00 2001 From: beeb Date: Mon, 30 May 2022 07:49:16 +0200 Subject: [PATCH 2/2] Bumped version number --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 6c411b1..14a355f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "pancaketrade" -version = "0.7.1" +version = "0.7.2" description = "Trading bot for PancakeSwap" authors = ["Valentin Bersier "]