简体   繁体   中英

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

Update:

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

  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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM