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.")
swapExactETHForTokens
only works when paying with ETH/BNB, if you want to pay with an ERC-20 token you should callswapExactTokensForTokens
orswapExactTokensForTokensSupportingFeeOnTransferTokens
.