20

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

 2 years ago
source link: https://learnblockchain.cn/question/3007
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
web3脚本在BSC链上交易经常失败 | 登链社区 | 技术问答

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链上的问题还是说我代码的问题,想请教下有没有大佬能解决下的,先谢谢了。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK