2

I am trying to swap USDT to another token on pancakeswap. When I use contract tokenToBuy as WBNB it works normally, but when I replace it with contract of USDT, it gives error ": ValueError: {'code': -32000, 'message': 'insufficient funds for gas * price + value'}

How to fix this error? Please give me advice. Thank you

tokenToBuy = web3.toChecksumAddress("0x9fd87aefe02441b123c3c32466cd9db4c578618f") 
spend = web3.toChecksumAddress("0x55d398326f99059ff775485246999027b3197955") 
#0x55d398326f99059ff775485246999027b3197955 USDT contract


contract = web3.eth.contract(address=panRouterContractAddress, abi=panabi)
nonce = web3.eth.get_transaction_count(sender_address)
start = time.time()

pancakeswap2_txn = contract.functions.swapExactETHForTokens(
    int(minimum), 
    [spend, tokenToBuy],
    sender_address,
    (int(time.time()) + 10000) 
).buildTransaction({
    'from': sender_address,
    'value': web3.toWei(float(1), 'ether'),  
    'gas': 250000,
    'gasPrice': web3.toWei(float(gas), 'gwei'), 
    'nonce': nonce,
})
5
  • I am not 100% sure how your app works. But in my Python app, I need to mark the gas value. For example -> "gas": Wei(200000) so it knows how much I am willing to spend. So in your case it would be 'gas': Wei(250000), It's worth a shoot.
    – Sky
    Commented Jan 14, 2022 at 10:03
  • What is the balance of your sender account?
    – Richard
    Commented Jan 14, 2022 at 10:24
  • mine is 100 USDT, and BNB is enough to make transaction
    – Ca Ca
    Commented Jan 14, 2022 at 14:28
  • @CaCa What code are you using? swapExactETHForTokens only works when paying with ETH/BNB, if you want to pay with an ERC-20 token you should call swapExactTokensForTokens or swapExactTokensForTokensSupportingFeeOnTransferTokens.
    – Ismael
    Commented Jan 15, 2022 at 18:51
  • 1
    thanks @ismael , i have used wrong function
    – Ca Ca
    Commented Jan 16, 2022 at 8:24

1 Answer 1

1
def BuyUSDT(hedeftokenAddress, hedeftokenSymbol,walletAddress,private_key,gasAmount,gasPrice,snipeBNBAmount):
    if(hedeftokenAddress != None):
        tokenToBuy = web3.toChecksumAddress(hedeftokenAddress)
        #tokenToBuy2 = web3.toChecksumAddress(tokenAddress)
        spend = web3.toChecksumAddress("0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c")  #wbnb contract address
        token2=web3.toChecksumAddress("0x55d398326f99059fF775485246999027B3197955")
        contract = web3.eth.contract(address=pancakeSwapRouterAddress, abi=pancakeABI)
        # contractpancake = web3.eth.contract(address=pancakeSwapRouterAddress, abi=pancakeABI)
        # amount_out = contractpancake.functions.getAmountsOut(int(snipeBNBAmount*1e18), [spend,tokenToBuy]).call()[-1]
        # min_tokens = int(amount_out * (1 - (40 / 100)))
        #snipeBNBAmounttoBUSD= snipeBNBAmount*400
        pancakeswap2_txn = contract.functions.swapExactETHForTokensSupportingFeeOnTransferTokens(
            0, # Set to 0 or specify min number of tokens - setting to 0 just buys X amount of token at its current price for whatever BNB specified
            [spend,token2,tokenToBuy],
            walletAddress,
            (int(time.time()) + transactionRevertTime)
            ).buildTransaction({
                'from': walletAddress,
                'value': web3.toWei(float(snipeBNBAmount), 'ether'), #This is the Token(BNB) amount you want to Swap from
                'gas': gasAmount,
                'gasPrice': web3.toWei(gasPrice,'gwei'),
                'nonce': web3.eth.get_transaction_count(walletAddress),
                })


        signed_txn = web3.eth.account.sign_transaction(pancakeswap2_txn, private_key)
        tx_token = web3.eth.send_raw_transaction(signed_txn.rawTransaction) #BUY THE TOKEN
        
        txHash = str(web3.toHex(tx_token))
        
        #TOKEN IS BOUGHT

        checkTransactionSuccessURL = "https://api.bscscan.com/api?module=transaction&action=gettxreceiptstatus&txhash=" + txHash + "&apikey=" + bscScanAPIKey
        checkTransactionRequest = requests.get(url = checkTransactionSuccessURL)
        txResult = checkTransactionRequest.json()['status']
        # except:
        #     print(style.RED + currentTimeStamp + " 1.Hesap BUSD - Transaction failed.")
        #     print("") # line break: move onto scanning for next token
    
        try: 
            if(txResult == "1"):
                print(style.GREEN + currentTimeStamp + " 1.Hesap USDT Successfully bought $" + hedeftokenSymbol + " for " + style.BLUE + str(snipeBNBAmount) + style.GREEN + " BNB - TX ID: ", txHash)
                with open('{}\\transferlerV2.txt'.format(ROOT_PATH),'a', encoding='utf8') as conf:
                    conf.writelines(currentTimeStamp + "  " + " 1.Hesap USDT - TX ID: " + txHash)
            else:
                print(style.RED + currentTimeStamp + " 1.Hesap USDT - Alındı ama BSC FAİL ile döndü.")
        except:
            print(style.RED + currentTimeStamp + " 1.Hesap USDT - Transaction failed: likely not enough gas.")
1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented May 26, 2022 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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