Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails when swapping USDT=>WBNB #363

Open
RoscoeTheDog opened this issue Dec 24, 2023 · 1 comment
Open

Fails when swapping USDT=>WBNB #363

RoscoeTheDog opened this issue Dec 24, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@RoscoeTheDog
Copy link

I was trying to swap from USDT => BNB but had incorrectly set the address of BNB to WBNB address by mistake. I came across an error from pancake swap saying IDENTICAL ADDRESSES and after stepping through the call stack I could see when its building the txn the second element is the same as the last 3rd element in the routing list. This doesn't cause problems when swapping from USDT to native BNB, but it does cause problems when swapping USDT => WBNB.

Is there a reason weth have to be in the routing chain? Can't it just be a chain input/output?

#v2 uniswap being used here
        elif self.version == 2:
            min_tokens_bought = int(
                (1 - slippage)
                * self._get_token_token_input_price(
                    input_token, output_token, qty, fee=fee
                )
            )
            if fee_on_transfer:
                func = (
                    self.router.functions.swapExactTokensForTokensSupportingFeeOnTransferTokens
                )
            else:
                func = self.router.functions.swapExactTokensForTokens

            return self._build_and_send_tx(
                func(
                    qty,
                    min_tokens_bought,
                    [input_token, self.get_weth_address(), output_token],
                    recipient,
                    self._deadline(),
                ),
            )

Full function

    def _token_to_token_swap_input(
        self,
        input_token: AddressLike,
        output_token: AddressLike,
        qty: int,
        recipient: Optional[AddressLike],
        fee: int,
        slippage: float,
        fee_on_transfer: bool = False,
    ) -> HexBytes:
        """Convert tokens to tokens given an input amount."""
        # Balance check
        input_balance = self.get_token_balance(input_token)
        if qty > input_balance:
            raise InsufficientBalance(input_balance, qty)

        if recipient is None:
            recipient = self.address

        if input_token == ETH_ADDRESS:
            raise ValueError
        elif output_token == ETH_ADDRESS:
            raise ValueError

        if self.version == 1:
            token_funcs = self._exchange_contract(input_token).functions
            # TODO: This might not be correct
            min_tokens_bought, min_eth_bought = self._calculate_max_output_token(
                input_token, qty, output_token
            )
            func_params = [
                qty,
                min_tokens_bought,
                min_eth_bought,
                self._deadline(),
                output_token,
            ]
            if not recipient:
                function = token_funcs.tokenToTokenSwapInput(*func_params)
            else:
                func_params.insert(len(func_params) - 1, recipient)
                function = token_funcs.tokenToTokenTransferInput(*func_params)
            return self._build_and_send_tx(function)
        elif self.version == 2:
            min_tokens_bought = int(
                (1 - slippage)
                * self._get_token_token_input_price(
                    input_token, output_token, qty, fee=fee
                )
            )
            if fee_on_transfer:
                func = (
                    self.router.functions.swapExactTokensForTokensSupportingFeeOnTransferTokens
                )
            else:
                func = self.router.functions.swapExactTokensForTokens
    
            return self._build_and_send_tx(
                func(
                    qty,
                    min_tokens_bought,
                    [input_token, self.get_weth_address(), output_token],
                    recipient,
                    self._deadline(),
                ),
            )
        elif self.version == 3:
            if fee_on_transfer:
                raise Exception("fee on transfer not supported by Uniswap v3")

            min_tokens_bought = int(
                (1 - slippage)
                * self._get_token_token_input_price(
                    input_token, output_token, qty, fee=fee
                )
            )
            sqrtPriceLimitX96 = 0

            return self._build_and_send_tx(
                self.router.functions.exactInputSingle(
                    {
                        "tokenIn": input_token,
                        "tokenOut": output_token,
                        "fee": fee,
                        "recipient": recipient,
                        "deadline": self._deadline(),
                        "amountIn": qty,
                        "amountOutMinimum": min_tokens_bought,
                        "sqrtPriceLimitX96": sqrtPriceLimitX96,
                    }
                ),
                self._get_tx_params(),
            )
        else:
            raise ValueError  # pragma: no cover
@RoscoeTheDog RoscoeTheDog added the bug Something isn't working label Dec 24, 2023
@kancom
Copy link

kancom commented Jan 27, 2024

same thing for Etherium's Uniswap. Majory of new pairs are WETH quoted with..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants