'Web3py swap tokens path of contracts
I'm trying to swap tokens with web3py using the swapExactETHForTokensSupportingFeeOnTransferTokens function, with some tokens it works perfectly, in others I get the error "execution reverted: PancakeRouter: INSUFFICIENT_OUTPUT_AMOUNT" I'm informing the parameter "amountOutMin (uint256)" to control the slippage . that I saw differently in the tokens that work and what do not work is the path of the contracts used for the swap.
On the poocoin website when I make a trade and the contract path is "WBNB > TOKEN", that is, from BNB direct to TOKEN, it also works using the "swapExactETHForTokensSupportingFeeOnTransferTokens" function when there is some other contract in the middle like "WBNB > WUSD > TOKEN" doesn't work, and these paths change to the same token, see images below:
How poocoin identifies which contracts to use to swap BNB to TOKEN? how to identify the contacts that I should use in the parameter "path (address[])"
swapExactETHForTokensSupportingFeeOnTransferTokens(
amountOutMin,
[WBNB, ????, ???? TOKEN_BUY], # path (address[])
sender_address,
(int(time.time()) + 10000)
thanks!
Solution 1:[1]
I am searching for the same thing, for now, an answer lies in this https://cryptomarketpool.com/use-web3-py-in-python-to-call-uniswap/
More exaclty: you locally get all listed pairs
...
allPairsLength = factory_contract.functions.allPairsLength().call()
...
then you find your route by hand. After collecting locally all the trading pairs, you can find exactly how to get from X to Y because you have all X pairs (X/X1, X/X2 ... X/Xn) and all Y pairs (Y/Y1, Y/X3, ... Y/Yn).
You can get one pair, by it's id by
for i in range(1, PUTTHECOUNTOFTRADINGPAIRSHERE):
allPairs_address = factory_contract.functions.allPairs(i).call()
contract = web3.eth.contract(address=allPairs_address, abi=pairs_abi)
symbol = contract.functions.name().call()
supply = contract.functions.totalSupply().call()
print(allPairs_address, supply)
for example
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | abarbatei |