Zaza
Zaza

Reputation: 51

web3.py swapExactTokensForTokens failing transaction

I am trying to call swapExactTokensForTokens of a router contract, I need to swap tokenA to tokenB. Here is the code of the transaction:

router = '0x0000000'
abi = '[{"inputs":[{"internalType":"address","name":"_factory","type":"address"},.... {"stateMutability":"payable","type":"receive"}]'

router_contract = w3.eth.contract(router, abi=abi)

tokenA = '0x000...'
tokenB = '0x000...'

path = [tokenA, tokenB]


txn = router_contract.functions.swapExactTokensForTokens(amount, min_amount, path, personal_wallet, (int(time()) +1000) ).buildTransaction({
        'gas': 81000,
        'gasPrice': w3.toWei('10', 'gwei'),
        'nonce':  w3.eth.get_transaction_count(personal_wallet),
    })

I get the following error in transaction:

screenshot of the transaction

Upvotes: 3

Views: 3272

Answers (2)

Rizwan Syal
Rizwan Syal

Reputation: 19

  1. Calculate the estimated gas
  2. Provide estimated gas while building transaction

function = router_contract.functions.swapExactTokensForTokens( amount,

    0,

    [

        tokenA,


        tokenB


    ],


    wallet,


    int(time.time()) + 10 * 60)


estimated_gas = function.estimateGas({'from':'0x10780b34025a93927ae62776Fe43419166ec88D2'})


tx_params = {


    'from': wallet,


    'gas': estimated_gas,


    'gasPrice': conf.w3.toWei('10', 'gwei'),


    'nonce': conf.w3.eth.getTransactionCount(wallet)


}

transaction = function.buildTransaction(tx_params)

Upvotes: 0

Zaza
Zaza

Reputation: 51

Update:

The above code works fine when increasing the gas and adding the from field in buildTransaction.

Upvotes: 2

Related Questions