uniswap-python / uniswap-python

🦄 The unofficial Python client for the Uniswap exchange.
https://uniswap-python.com
MIT License
931 stars 368 forks source link

Question, why [input_token, weth, output_token] #405

Open weestack opened 1 month ago

weestack commented 1 month ago

Hello not a bug, but however a question from a newbie in dex swapping.

https://github.com/uniswap-python/uniswap-python/blob/4f15d2ef7e9d7cb502e6b2ce44772fae46d18781/uniswap/uniswap.py#L1011

There on line 1011: [input_token, self.get_weth_address(), output_token],

why are the default for v2 a 3 way path, I debugged the code yesterday, because I already had some WETH in my meta account, so naturally I set the input to weth, and output to what I wanted to swap i for, which kept failing with "SAME ADDRESS" error.

Are there any perks of using a 3-way route over a 2-way route on Uniswap V2, in my own clumsy and logic (VERY new to defi) it should be faster and cheaper with a 2-way router rather than a 3-way route, right?

Can someone explain the implementation reason to me, as I cant find it in the documentation and neither that it defaults to weth as middle route, perhaps i just used it wrong.... but it would be great to get some perspective on the matter

uniswap = Uniswap(address=address, default_slippage=0.5, private_key=private_key, version=version, provider=provider)

web3 = Web3(Web3.HTTPProvider(mainnet_url))
token_meta = uniswap.get_token(token, 'erc20')
spend_eth = '0.001'
spend = web3.to_wei(spend_eth, 'ether')
Amount = uniswap.get_price_input(WETH_PAIR_ADDRESS, token, spend)
uniswap.make_trade(WETH_PAIR_ADDRESS, token, spend, fee_on_transfer=True)
nigeldelviero commented 1 month ago

Hello not a bug, but however a question from a newbie in dex swapping.

https://github.com/uniswap-python/uniswap-python/blob/4f15d2ef7e9d7cb502e6b2ce44772fae46d18781/uniswap/uniswap.py#L1011

There on line 1011: [input_token, self.get_weth_address(), output_token],

why are the default for v2 a 3 way path, I debugged the code yesterday, because I already had some WETH in my meta account, so naturally I set the input to weth, and output to what I wanted to swap i for, which kept failing with "SAME ADDRESS" error.

Are there any perks of using a 3-way route over a 2-way route on Uniswap V2, in my own clumsy and logic (VERY new to defi) it should be faster and cheaper with a 2-way router rather than a 3-way route, right?

Can someone explain the implementation reason to me, as I cant find it in the documentation and neither that it defaults to weth as middle route, perhaps i just used it wrong.... but it would be great to get some perspective on the matter

uniswap = Uniswap(address=address, default_slippage=0.5, private_key=private_key, version=version, provider=provider)

web3 = Web3(Web3.HTTPProvider(mainnet_url))
token_meta = uniswap.get_token(token, 'erc20')
spend_eth = '0.001'
spend = web3.to_wei(spend_eth, 'ether')
Amount = uniswap.get_price_input(WETH_PAIR_ADDRESS, token, spend)
uniswap.make_trade(WETH_PAIR_ADDRESS, token, spend, fee_on_transfer=True)

I'll try to help answer your question

  1. Why the middle of path is WETH address instead direct into target token? The most significant reason is liquidity, WETH pairs generally have the most liquidity on Uniswap. When you swap from one token to another, going through WETH often gives you access to deeper liquidity pools, which can result in better pricing. For example, if you want to swap Token A for Token B, there might not be a direct pair (or enough liquidity in the direct pair) for that swap. However, both Token A/WETH and WETH/Token B pools might have substantial liquidity, making the swap through WETH more efficient.

  2. By routing through WETH, you reduce the price impact of your trade. Direct swaps between less liquid pairs can cause significant slippage

  3. Can we input route like [input_token, output_token]?

    • If there is sufficient liquidity in the [input_token, output_token] pair, then using this route could potentially be cheaper because you're avoiding an extra swap step. However, this is only beneficial if the liquidity is high enough to avoid significant slippage.
    • If the direct pair is not very liquid, it could be more costly due to slippage, and routing through WETH might actually result in a better price, even with the additional swap step.
    • directly to the target token will be faster? it does not affect the transaction speed

Summary

In my opinion, which is the safest and best option out of them? Default to [input_token, WETH, output_token] Route**