0

I have a simple code using web3.py that sells BEP20 tokens:

deadline = int(time.time()) + 1200;
nonce = w3.eth.get_transaction_count(w3.eth.default_account) 
sellTxn = self.web3Context.routerContract.functions.swapExactTokensForETHSupportingFeeOnTransferTokens(amountToSell, 1000000000000000, 
           [self.tokenAddress, self.web3Context.wrappedEthAddress], 
            account.address, deadline).buildTransaction({
              'chainId': self.web3Context.chainId,
              'nonce': nonce,
              'gas': w3.toHex(500000),
              'gasPrice': w3.toHex(int(w3.eth.gasPrice*2)),
         })

signed_sellTxn = account.signTransaction(sellTxn)
sell_txid = w3.toHex(self.web3Context.w3.eth.sendRawTransaction(signed_sellTxn.rawTransaction))
sellReceipt = w3.eth.wait_for_transaction_receipt(sell_txid)
...

It's all uses pancakeswap V2 stuff, like router address, WBNB address, etc.

It does work with majority of tokens I tried it with. However, occasionally it fails with

Fail with error 'TransferHelper: TRANSFER_FROM_FAILED'

The token is approved before hand. This code is not called if a token is not approved, and I also checked that the transaction to approve it was confirmed before the above code was executed.

If it happens with a particular token, it will be failing no matter how many times I try to run it. However, if I sell manually using Pancakeswap, it works.

I compared the data I send with the data Pancakeswap sends, and I do not see any difference except in amountOutMin. When it works, however, I can even pass 0.

So, here is an example when the transaction fails:

Function: swapExactTokensForETHSupportingFeeOnTransferTokens(uint256 amountIn, uint256 amountOutMin, address[] path, address to, uint256 deadline)
0   amountIn        uint256     1001887138366157231924
1   amountOutMin    uint256     1000000000000000
2   path            address[]   ef155b9bcdbf4bd54143cf2ec7e47045cbbd2ebb
                                bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c
3   to              address     3c94454135e91ab01bb4c1b816d4d6f9399b9a7d

And this is the transaction when it's submitted from Pancakeswap, and it works:

Function: swapExactTokensForETHSupportingFeeOnTransferTokens(uint256 amountIn, uint256 amountOutMin, address[] path, address to, uint256 deadline)
0   amountIn        uint256     1004644755416763858179
1   amountOutMin    uint256     24525898036735865
2   path            address[]   ef155b9bcdbf4bd54143cf2ec7e47045cbbd2ebb
                                bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c
3   to              address     3c94454135e91ab01bb4c1b816d4d6f9399b9a7d

So, the only difference is the amountOutMin value.

What is the best way to troubleshoot or debug this? Is there anything specific to look for in the token's contract?

So far I've seen it with handful of tokens: https://bscscan.com/address/0x0952ddffde60786497c7ced1f49b4a14cf527f76 https://bscscan.com/address/0x35ca09d3c28c24f1f32742ab91d794d2a524519e https://bscscan.com/address/0xd55eb92c40b8e19f5ff97cbbbdb721889a7b034a https://bscscan.com/address/0xd9f21eb27474981f419a0845ddfccc14e3fd4ecc https://bscscan.com/address/0xe68efedf3e1c08af12de6bdec585f89794a75c52 https://bscscan.com/address/0xef155b9bcdbf4bd54143cf2ec7e47045cbbd2ebb

Thanks!

5
  • Try with more gas, better use estimateGas to calculate gas needed.
    – Ismael
    Jun 15 at 3:32
  • I do not think it's an issue with gas. When it fails, I can always sell manually via pancakeswap using was less gas. But it's a good suggestion. I will check the transactions that succeeded around the same time to see how much gas they used.
    – Peter L
    Jun 16 at 4:07
  • Have the exact same issue. Tried with using estimateGas to set the gas price as suggested above and still get the TRANSFER_FROM_FAILED error.
    – Bojorón
    Jun 16 at 21:53
  • Having the same issue tried hardcoding in the same exact amountIn and amountOutMin of a successful transaction which made me avoid the TRANSFER_FROM_FAILED but got Failed which is even more pricy on gas... Did you manage to find the solution or has anyone? Unfortunately, we need this method to trade tokens that have a transaction fee for example SafeMoon takes 10% of every buy and sell order so a normal method wouldn't work. If you have found any solutions would be nice to know!
    – Jesus
    Jul 5 at 21:52
  • No, I did not find a solution yet. I'm using double the gas price usually, so it's possible that some tokens can't be sold if you use higher gas price. I know that there are some tokens that you can't buy unless your gas price is 5. However, this is just a guess.
    – Peter L
    Jul 6 at 22:40
0

I had the same issue, the only solutions that I could think of are:

  1. Get the price of the token and calculate the slippage on the fly
  2. Set amountOutMin to 0, at first I thought it would give me 0 bnb but on all the tokens that I have used it, it only took the tax specified on the contract.
    token_tx = pancake.functions.swapExactTokensForETHSupportingFeeOnTransferTokens(tokens_amount,0 , path, reciever[0],expire ).buildTransaction(
        {
            'chainId':56,
            'gas': 500000,
            'gasPrice': web3.toWei(gas,'gwei'),
            'nonce':nonce,
        }
    )

My guess is that when you try to sell and the tokens received < amountOutMin it fails

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.