python - 在 python 中与 ganache-cli 同步
问题描述
我想测试一个简单的以太坊智能合约 ganache 以小写形式打印帐户,web3 给了我一个错误:
web3.exceptions.InvalidAddress: ('Web3.py 只接受校验和地址。给你这个非校验和地址的软件应该被认为是不安全的,请将其作为错误提交到他们的平台上。尝试使用 ENS 名称。或者,如果您必须接受较低的安全性,请使用 Web3.toChecksumAddress(lower_case_address).', '0xfcad0b19bb29d4674531d6f115237e16afce377c')
然后我使用以下方法将地址转换为混合地址:
Web3.toChecksumAddress(the_lower_case_ganache_address)
它会出现错误:
文件“/usr/local/lib/python3.7/site-packages/web3/contract.py”,第 1385 行,在 call_contract_function 中从 e web3.exceptions.BadFunctionCallOutput 引发 BadFunctionCallOutput(msg):无法与/调用合约函数进行交易,是否正确部署了合约并同步了链?127.0.0.1 - - [25/Jan/2019 21:35:21] “POST /blockchain/user HTTP/1.1”500 -
这是我的python代码:
def check_gender(data):
valid_list = ["male", "female"]
if data not in valid_list:
raise ValidationError(
'Invalid gender. Valid choices are'+ valid_list
)
class UserSchema(Schema):
name = fields.String(required=True)
gender = fields.String(required=True, validate=check_gender)
app = Flask(__name__)
# api to set new user every api call
@app.route("/blockchain/user", methods=['POST'])
def transaction():
w3.eth.defaultAccount = w3.eth.accounts[0]
with open("data.json", 'r') as f:
datastore = json.load(f)
abi = datastore["abi"]
contract_address = datastore["contract_address"]
# Create the contract instance with the newly-deployed address
user = w3.eth.contract(
address=contract_address, abi=abi,
)
body = request.get_json()
result, error = UserSchema().load(body)
if error:
return jsonify(error), 422
tx_hash = user.functions.setUser(
result['name'], result['gender']
)
tx_hash = tx_hash.transact()
# Wait for transaction to be mined...
w3.eth.waitForTransactionReceipt(tx_hash)
user_data = user.functions.getUser().call()
return jsonify({"data": user_data}), 200
if __name__ == '__main__':
app.run()
` 和 json 文件:
{
"abi": [
{
"constant": false,
"inputs": [
{
"name": "name",
"type": "string"
},
{
"name": "gender",
"type": "string"
}
],
"name": "setUser",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [],
"name": "getUser",
"outputs": [
{
"name": "",
"type": "string"
},
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
],
"contract_address": "0xFCAd0B19bB29D4674531d6f115237E16AfCE377c"
}
解决方案
该错误表明 Ganache 找不到要与之交互的已部署合约。
您的代码似乎有效,但错误可能发生在这一行:
tx_hash = user.functions.setUser(
result['name'], result['gender']
)
代码尝试设置用户,但找不到与之交互的合约(即使 ABI 和合约实例有效)。
如果您使用的是 Ganache,您可能会在每次运行代码时重新部署合约,因此如果您从静态文件中提取,以下行可能无法正常工作:
contract_address = datastore["contract_address"]
如果您每次都部署它,您将需要从 Ganache 动态获取合约地址。
推荐阅读
- python - 如何在 python 中转换时间戳以克服 1..12 问题
- spring - 应用令牌过滤器后如何显示每个控制器的响应
- html - 尽管在 html 文件中给出了绝对路径,但浏览器没有找到我的 css 文件
- ruby - 使用 :on 在 ruby 中进行多次验证
- asp.net-mvc - MVC ReportViewer 不加载大量报告
- angular - 如何在 ag-grid 中实现多行拖动(角度)
- tensorflow - 没有onnx,如何手动将pytorch模型转成tensorflow模型?
- sql-server - SQL Server:从一个表中获取所有数据,但条件是第二个表列
- rcpp - Rcpp 中“as”的内存问题
- sap-cloud-sdk - 将 OData 结果传输到销售订单模拟实体时,销售订单模拟实体中缺少导航值 to_ScheduleLine