Here is my code whenever i try to buy token when its launch its give error transfer failed or just fail
idk why its happening but after some munites i try its working on same contract .
how to solve it?
import time
from web3 import Web3
import datetime
for x in range(100):
try:
PancakeABI = open('pancakeABI.py','r').read().replace('\n','')
ts = datetime.datetime.now().timestamp()
print(ts)
bsc="https://bsc-dataseed.binance.org/"
web3 = Web3(Web3.HTTPProvider(bsc))
print(web3.isConnected())
#My own address to swap from
sender_address = "0x13AE8Bb065407595681034dDA97361424AF14C00"
#This is global Pancake V2 Swap router address
router_address = "0x10ED43C718714eb63d5aA57B78B54704E256024E"
#always spend using Wrapped BNB (currency code)
#I guess you want to use other coins to swap you can do that, but for me I used Wrapped BNB
spend = web3.toChecksumAddress("0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c")
#This is your private key info
private=""
#Print out your balances just for checking
balance = web3.eth.get_balance(sender_address)
print(balance)
humanReadable = web3.fromWei(balance,'ether')
print(humanReadable)
#BUSD 0xe9e7cea3dedca5984780bafc599bd69add087d56
#BNB 0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c
#Contract id is the new token we are swaping to (this only new purchase) #
contract_id = web3.toChecksumAddress("0x09867de1bedee82d8bbc1555f9286888297c5eb0")
#Setup the PancakeSwap contract
contract = web3.eth.contract(address=router_address, abi=PancakeABI)
nonce = web3.eth.get_transaction_count(sender_address)
print(web3.toWei('0.01','ether'))
pancakeswap2_txn = contract.functions.swapExactETHForTokens(
0, # here setup the minimum destination token you want to have, you can do some math, or you can put a 0 if you don't want to care
[spend,contract_id],
sender_address,
(int(time.time()) + 1000000)
).buildTransaction({
'from': sender_address,
'value': web3.toWei(0.01,'ether'),#This is the Token(BNB) amount you want to Swap from
'gas': 500000, #500000 duriWng launch
'gasPrice': web3.toWei('15','gwei'), #15 during launch
'nonce': nonce,
})
signed_txn = web3.eth.account.sign_transaction(pancakeswap2_txn, private_key=private)
tx_token = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
print(web3.toHex(tx_token))
end = datetime.datetime.now().timestamp()
print(end)
# time.sleep(2)
except:
print("Some error occured")