简体   繁体   English

web3.py swapExactTokensForTokens 交易失败

[英]web3.py swapExactTokensForTokens failing transaction

I am trying to call swapExactTokensForTokens of a router contract, I need to swap tokenA to tokenB.我正在尝试调用路由器合约的 swapExactTokensForTokens,我需要将 tokenA 交换为 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 .当增加 gas 并在buildTransaction中添加from字段时,上面的代码可以正常工作。

  1. Calculate the estimated gas计算预估gas
  2. Provide estimated gas while building transaction在建立交易时提供估计的气体

function = router_contract.functions.swapExactTokensForTokens( amount, function = router_contract.functions.swapExactTokensForTokens(数量,

    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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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