django - 交易永远不会被挖掘,使用 web3.py
问题描述
我正在使用 python 3.6、Django 2.1.1、Solidity 和 web3.py v4 开发一个网站。我想将交易添加到 ropsten 测试网,但交易从未得到确认。这是代码:
amount_in_wei = w3.toWei(questionEtherValue,'ether')
nonce=w3.eth.getTransactionCount(w3.toChecksumAddress(questionairAddress))+1
txn_dict = {
'to': contractAddress,
'value': amount_in_wei,
'gas': 2000000,
'gasPrice': w3.toWei('70', 'gwei'),
'nonce': nonce,
'chainId': 3
}
signed_txn = account.signTransaction(txn_dict)
txn_hash = w3.eth.sendRawTransaction(signed_txn.rawTransaction)
try:
txn_receipt = w3.eth.waitForTransactionReceipt(txn_hash, timeout=300)
except Exception:
return {'status': 'failed', 'error': 'timeout'}
else:
return {'status': 'success', 'receipt': txn_receipt}
解决方案
啊,正如@yasaman.h 发现的那样,在nonce
:
# original:
nonce = w3.eth.getTransactionCount(w3.toChecksumAddress(questionairAddress)) + 1
# should be:
nonce = w3.eth.getTransactionCount(w3.toChecksumAddress(questionairAddress))
事务nonce
的数量必须等于先前发送的事务的计数。因此,新帐户发送的第一笔交易的 anonce
为零。
推荐阅读
- python - 相互环绕的递归彩虹色圆圈
- excel - 如何使用 COUNTIF,使其依赖于过滤器和数据透视表?
- python - 将多个文件中的 json 对象逐行合并到一个文件中
- mask - 如何使用来自掩码字段(文本)的图标创建 HMENU/TMENU?
- unity3d - 重命名文件夹后Unity中的空引用异常
- javascript - three.js(版本 102)如何使用 OrbitControls 设置相机的默认位置和旋转
- java - 使用 DataWeave 1.0 在 Mule-3.8.4 中转换为 SalesForce 日期时间格式
- python-2.7 - SQLite 3 使用参数字符串时出错,但在隐式输入时不会出错
- java - 如何对二维数组对角元素进行排序
- c - 带有 case switch 和 enum 的工作日