Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fails when swapping USDT=>WBNB #363

Copy link
Copy link
Open
@RoscoeTheDog

Description

@RoscoeTheDog
Issue body actions

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.