3

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

Progman
  • 16,827
  • 6
  • 33
  • 48
Zaza
  • 51
  • 1
  • 3

2 Answers2

2

Update:

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

Zaza
  • 51
  • 1
  • 3
0
  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)
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 07 '22 at 15:56