web3脚本在BSC链上交易经常失败

错误信息: Status: Fail A Status code indicating if the top-level call succeeded or failed (applicable for Post BYZANTIUM blocks only)

Warning! Error encountered during contract execution [execution reverted]

脚本代码如下

def transactionInBSC(targetAddress, data, address, privateKey, gasRate):
    nonce = w3_main.eth.getTransactionCount(address)
    gasPrice = w3_main.eth.gasPrice
    value = '0x0'
    gas = w3_main.eth.estimateGas({'from': address, 'to': targetAddress, 'value': value, 'data': data})
    transaction = {
        'from': address,
        'to': targetAddress,
        'nonce': nonce,
        'gasPrice': int(gasPrice * gasRate),
        'gas': gas,
        'value': value,
        'data': data
    }

    signed_tx = w3_main.eth.account.signTransaction(transaction, privateKey)
    txn_hash = w3_main.eth.sendRawTransaction(signed_tx.rawTransaction)
    print(Web3.toHex(txn_hash))
    while w3_main.eth.getTransactionCount(address) == nonce:
        time.sleep(sleep_time)

    receipt = w3_main.eth.get_transaction_receipt(txn_hash)
    if not receipt['status']:
        print(('[-]Transaction Fail.'))

    return Web3.toHex(txn_hash)

交易的合约为gamefi类的项目。也不是全部失败,问题是当我发起交易多的时候发现经常会出现失败的情况,不知道是BSC链上的问题还是说我代码的问题,想请教下有没有大佬能解决下的,先谢谢了。

请先 登录 后评论

1 个回答

pan
请先 登录 后评论
  • 1 关注
  • 0 收藏,5305 浏览
  • JunkDoge 提出于 2022-01-17 13:08