自动 BNB 转账机器人在第一笔交易后抛出错误
import config
from decimal import Decimal
from web3 import Web3
while True:
bsc = "https://bsc-dataseed.binance.org/"
web3 = Web3(Web3.HTTPProvider(bsc))
balance_wei=web3.eth.get_balance(config.address1)
balance=web3.fromWei(balance_wei,'ether')
nonce=web3.eth.getTransactionCount(config.address1)
gas=21000
max_transfer=balance-Decimal(0.000105)
print (max_transfer)
if max_transfer>0:
tx= {
'nonce':nonce,
'to':config.address2,
'value':web3.toWei(max_transfer,'ether'),
'gas':gas,
'gasPrice':web3.toWei('5','gwei')
}
signed_tx=web3.eth.account.signTransaction(tx, config.private_key)
tx_hash=web3.eth.sendRawTransaction(signed_tx.rawTransaction)
print(web3.toHex(tx_hash))
我正在构建一个自动从我最近被黑客攻击的帐户转移 BNB 的机器人。使用 while 语句设置连续循环并运行代码后,第一笔交易一切正常。我在自动传输 bnb 并终止程序后立即抛出以下错误。我必须再次手动运行该程序。我想要实现的是一个连续的程序,它将继续运行 24/7,而无需我每次进行交易时都重新运行。欢迎提供善意的帮助来保护我的宝贵资产。
第一次交易后生成错误。
return self.formatted_response(response,
File "C:\Users\m2jto\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\manager.py", line 171, in formatted_response
raise ValueError(response["error"])
ValueError: {'code': -32000, 'message': 'already known'}
import config
from decimal import Decimal
from web3 import Web3
while True:
bsc = "https://bsc-dataseed.binance.org/"
web3 = Web3(Web3.HTTPProvider(bsc))
balance_wei=web3.eth.get_balance(config.address1)
balance=web3.fromWei(balance_wei,'ether')
nonce=web3.eth.getTransactionCount(config.address1)
gas=21000
max_transfer=balance-Decimal(0.000105)
print (max_transfer)
if max_transfer>0:
tx= {
'nonce':nonce,
'to':config.address2,
'value':web3.toWei(max_transfer,'ether'),
'gas':gas,
'gasPrice':web3.toWei('5','gwei')
}
signed_tx=web3.eth.account.signTransaction(tx, config.private_key)
tx_hash=web3.eth.sendRawTransaction(signed_tx.rawTransaction)
print(web3.toHex(tx_hash))
I am building a bot that auto transfer BNB from my account that was recently hacked.After setting the continuous loop using the while statement and run the code, everything works fine for just the first transaction. I throws the below error immediately after auto transferring the bnb and kills the program. I have to manually run the program again. What I am trying to achieve is a continuous program that will keep on running 24/7 without me having to rerun each time a transaction is made. A kind assistance will be welcomed to protect my valuable assets.
Generated error after first transaction.
return self.formatted_response(response,
File "C:\Users\m2jto\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\manager.py", line 171, in formatted_response
raise ValueError(response["error"])
ValueError: {'code': -32000, 'message': 'already known'}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只需将 tx_Hash 放入 try:, except: 块并添加睡眠函数即可解决该问题。这很神奇,因为我多次发送 bnb 来测试我的机器人,它会自动发送出去而不会崩溃。我相信这种使用 try 和 except 块与 time.sleep() 函数的方法可以用来解决许多 valueError 。见下文
I was able to solve the problem by just putting the tx_Hash in a try:,except: block and adding a sleep function. This did the magic as I tested my bot severally by sending in bnb many times and it auto send out without it crashing. I believe this method of using try and except block with time.sleep() function can be used to solve many valueError. see below