i have a problem and hope you could help me.
I try to swap BUSD on pancakeswap against an other token. I have a script, written in web3.py which works perfectly fine with BNB and the function "swapExactETHForTokens". But if i use the function: "swapExactTokensForTokens" I'm not in able to got a succesful transaction.
The script build the transaction, but after it is indexed by bscscan it failed.
Forexample: https://bscscan.com/tx/0xae08ebec2a945a727a8d5f158befa6baf943a7f149fad049eedb3f07908fe43a
I have tried to fix it the last days, but now i dont know further.
Her is the code:
import json
import config
import time
import sys
panabi = open('pancakeABI','r').read().replace('\n','')
sellABI = open('sellABI', 'r').read().replace('\n','')
bsc = "https://bsc-dataseed.binance.org/"
web3 = Web3(Web3.HTTPProvider(bsc))
print(web3.isConnected())
#TokenAddress of holder
sender_address = 'XXXXXX'
#BUSD Address
spend = web3.toChecksumAddress("0x4Fabb145d64652a948d72533023f6E7A623C7C53")
#pancakeswap router
panRouterContractAddress = '0x10ED43C718714eb63d5aA57B78B54704E256024E'
#Address Token
address = web3.toChecksumAddress(input("Token: "))
#Adddress Token
contract = web3.eth.contract(address=address, abi=panabi)
#Balance
balance = web3.eth.get_balance(sender_address)
print(balance)
humanReadable = web3.fromWei(balance,'ether')
print(humanReadable)
#Setup contract pancake
contract = web3.eth.contract(address=panRouterContractAddress, abi=panabi)
nonce = web3.eth.get_transaction_count(sender_address)
start = time.time()
print(web3.toWei('0.02', 'ether'))
#Token instance
sellTokenContract = web3.eth.contract(address, abi=sellABI)
#
tokenValue = web3.toWei(input("Enter amount you want to sell: "), 'ether')
int(tokenValue)
#Approve Token
tokenValue2 = web3.fromWei(tokenValue, 'ether')
start = time.time()
approve = sellTokenContract.functions.approve(panRouterContractAddress, tokenValue).buildTransaction({
'from': sender_address,
'gasPrice': web3.toWei('5','gwei'),
'nonce': web3.eth.get_transaction_count(sender_address),
})
signed_txn = web3.eth.account.sign_transaction(approve, private_key=config.private)
tx_token = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
print("Approved: " + web3.toHex(tx_token))
time.sleep(7)
#build transaction
pancakeswap2_txn = contract.functions.swapExactTokensForTokens(
tokenValue, #
1,
[spend,address],
sender_address,
(int(time.time()) + 1000000)
).buildTransaction({
'from': sender_address,
# 'value': web3.toWei(0,'ether'),
'gas': 500000,
'gasPrice': web3.toWei('10','gwei'),
'nonce': nonce,
})
#sign transaction
signed_txn = web3.eth.account.sign_transaction(pancakeswap2_txn, private_key=config.private)
tx_token = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
print(web3.toHex(tx_token))
#Wait for transaction receipt
print("Wait...")
time.sleep(5)
receipt = web3.eth.wait_for_transaction_receipt(tx_token, timeout=300, poll_latency=0.1)
print(receipt)```
Thank you in advance.