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))


#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 = web3.eth.get_balance(sender_address)
humanReadable = web3.fromWei(balance,'ether')

#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')

#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))


#build transaction
pancakeswap2_txn = contract.functions.swapExactTokensForTokens(
  tokenValue, # 
  (int(time.time()) + 1000000)
  '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)

#Wait for transaction receipt

receipt = web3.eth.wait_for_transaction_receipt(tx_token, timeout=300, poll_latency=0.1)

Thank you in advance.

1 Answer 1


this might solve your issue. first, instead of passing 1 as a number into the output amount, the router contract has a public function that you can use to get the exact output amount and the result can be passed into where you need to put the output amount.


function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);

for the failed transaction from what I found out here: https://bscscan.com/vmtrace?txhash=0xae08ebec2a945a727a8d5f158befa6baf943a7f149fad049eedb3f07908fe43a&type=gethtrace2 you can increase the amount of gas or use the getgasPrice from web3.py.

Also the function you are calling doesn't require you to pass in value, here:

 function swapExactTokensForTokens(
    uint amountIn,
    uint amountOutMin,
    address[] calldata path,
    address to,
    uint deadline
) external returns (uint[] memory amounts);

as require first is -input amount

  • output amount (you can use the getAmountsOut() for that)

  • [busd address(which is the token to swap note you must approve this first), token to get address]

  • recipient address

  • deadline

    pancakeswap2_txn = contract.functions.swapExactTokensForTokens( busd amount (Note: must be approved), getAmountOut, [busd address, token to get address], sender_address, (int(time.time()) + 1000000) ).buildTransaction({ 'from': sender_address, 'gas': 500000, 'gasPrice': web3.toWei('10','gwei'), 'nonce': nonce,

hope this help.

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.