我正试图通过使用python进行事务,将SimpleStorage.sol合约部署到ganache本地链。它似乎在连接链条时出现问题。
from solcx import compile_standard
from web3 import Web3
import json
import os
from dotenv import load_dotenv
load_dotenv()
with open("./SimpleStorage.sol", "r") as file:
simple_storage_file = file.read()
compiled_sol = compile_standard(
{
"language": "Solidity",
"sources": {"SimpleStorage.sol": {"content": simple_storage_file}},
"settings": {
"outputSelection": {
"*": {"*": ["abi", "metadata", "evm.bytecode", "evm.sourceMap"]}
}
},
},
solc_version="0.6.0",
)
with open("compiled_code.json", "w") as file:
json.dump(compiled_sol, file)
# get bytecode
bytecode = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["evm"][
"bytecode"
]["object"]
# get ABI
abi = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["abi"]
# to connect to ganache blockchain
w3 = Web3(Web3.HTTPProvider("HTTP://127.0.0.1:7545"))
chain_id = 5777
my_address = "0xca1EA31e644F13E3E36631382686fD471c62267A"
private_key = os.getenv("PRIVATE_KEY")
# create the contract in python
SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)
# get the latest transaction
nonce = w3.eth.getTransactionCount(my_address)
# 1. Build a transaction
# 2. Sign a transaction
# 3. Send a transaction
transaction = SimpleStorage.constructor().buildTransaction(
{"chainId": chain_id, "from": my_address, "nonce": nonce}
)
print(transaction)
它似乎连接到ganache链,因为它打印nonce,但当我构建并尝试打印事务时这是我收到的的全部回溯呼叫
Traceback (most recent call last):
File "C:Usersevensdemosweb3_py_simple_storagedeploy.py", line
52, in <module>
transaction = SimpleStorage.constructor().buildTransaction(
File "C:Python310libsite-packageseth_utilsdecorators.py", line
18, in _wrapper
return self.method(obj, *args, **kwargs)
File "C:UsersevensAppDataRoamingPythonPython310site-
packagesweb3contract.py", line 684, in buildTransaction
return fill_transaction_defaults(self.web3, built_transaction)
File "cytoolz/functoolz.pyx", line 250, in
cytoolz.functoolz.curry.__call__
return self.func(*args, **kwargs)
File "C:UsersevensAppDataRoamingPythonPython310site-
packagesweb3_utilstransactions.py", line 114, in
fill_transaction_defaults
default_val = default_getter(web3, transaction)
File "C:UsersevensAppDataRoamingPythonPython310site-
packagesweb3_utilstransactions.py", line 60, in <lambda>
'gas': lambda web3, tx: web3.eth.estimate_gas(tx),
File "C:UsersevensAppDataRoamingPythonPython310site-
packagesweb3eth.py", line 820, in estimate_gas
return self._estimate_gas(transaction, block_identifier)
File "C:UsersevensAppDataRoamingPythonPython310site-
packagesweb3module.py", line 57, in caller
result = w3.manager.request_blocking(method_str,
File "C:UsersevensAppDataRoamingPythonPython310site-
packagesweb3manager.py", line 197, in request_blocking
response = self._make_request(method, params)
File "C:UsersevensAppDataRoamingPythonPython310site-
packagesweb3manager.py", line 150, in _make_request
return request_func(method, params)
File "cytoolz/functoolz.pyx", line 250, in
cytoolz.functoolz.curry.__call__
return self.func(*args, **kwargs)
File "C:UsersevensAppDataRoamingPythonPython310site-
packagesweb3middlewareformatting.py", line 76, in
apply_formatters
response = make_request(method, params)
File "C:UsersevensAppDataRoamingPythonPython310site-
packagesweb3middlewaregas_price_strategy.py", line 90, in
middleware
return make_request(method, params)
File "cytoolz/functoolz.pyx", line 250, in
cytoolz.functoolz.curry.__call__
return self.func(*args, **kwargs)
File "C:UsersevensAppDataRoamingPythonPython310site-
packagesweb3middlewareformatting.py", line 74, in
apply_formatters
response = make_request(method, formatted_params)
File "C:UsersevensAppDataRoamingPythonPython310site-
packagesweb3middlewareattrdict.py", line 33, in middleware
response = make_request(method, params)
File "cytoolz/functoolz.pyx", line 250, in
cytoolz.functoolz.curry.__call__
return self.func(*args, **kwargs)
File "C:UsersevensAppDataRoamingPythonPython310site-
packagesweb3middlewareformatting.py", line 74, in
apply_formatters
response = make_request(method, formatted_params)
File "cytoolz/functoolz.pyx", line 250, in
cytoolz.functoolz.curry.__call__
return self.func(*args, **kwargs)
File "C:UsersevensAppDataRoamingPythonPython310site-
packagesweb3middlewareformatting.py", line 73, in
apply_formatters
formatted_params = formatter(params)
File "cytoolz/functoolz.pyx", line 503, in
cytoolz.functoolz.Compose.__call__
ret = PyObject_Call(self.first, args, kwargs)
File "cytoolz/functoolz.pyx", line 250, in
cytoolz.functoolz.curry.__call__
return self.func(*args, **kwargs)
File "C:Python310libsite-packageseth_utilsdecorators.py", line
91, in wrapper
return ReturnType(result) # type: ignore
File "C:Python310libsite-packageseth_utilsapplicators.py", line
22, in apply_formatter_at_index
yield formatter(item)
File "cytoolz/functoolz.pyx", line 250, in
cytoolz.functoolz.curry.__call__
File "cytoolz/functoolz.pyx", line 250, in
cytoolz.functoolz.curry.__call__
return self.func(*args, **kwargs)
File "C:Python310libsite-packageseth_utilsapplicators.py", line
72, in apply_formatter_if
return formatter(value)
File "cytoolz/functoolz.pyx", line 250, in
cytoolz.functoolz.curry.__call__
return self.func(*args, **kwargs)
File "C:UsersevensAppDataRoamingPythonPython310site-
packagesweb3middlewarevalidation.py", line 57, in
validate_chain_id
raise ValidationError(
web3.exceptions.ValidationError: The transaction declared chain ID
5777, but the connected node is on 1337
我自己也有这个问题,显然是Ganache CLI错误,但我能找到的最简单的修复方法是通过设置更改Ganache中的网络id>服务器到1337。它会重新启动会话,因此您需要更改地址和私钥变量。
如果这是我正在做的同一个教程,你很可能会在这之后陷入困境。。。交易的代码应该是:
transaction =
SimpleStorage.constructor().buildTransaction( {
"gasPrice": w3.eth.gas_price,
"chainId": chain_id,
"from": my_address,
"nonce": nonce,
})
print(transaction)
否则,如果你不设置gasPrice ,你会得到一个值错误
这行代码是错误的
chain_id = 5777
Ganache链id不是5777。这是网络id。节点使用网络id在同一网络上的节点之间传输数据。网络id不包括在块中,也不用于签署事务或挖掘块。
chain_id = 1377
区块中也不包括链ID,但它在交易签名和验证过程中使用。
它对我有效,我从w3.eth.chain_id 中获得值chain_id
transaction = SimpleStorage.constructor().buildTransaction(
{
"gasPrice": w3.eth.gas_price,
"chainId": w3.eth.chain_id,
"from": my_address,
"nonce": nonce,
}
)
eth_chainId RPC方法的委托
返回EIP-155中引入的当前配置的"链Id"值的整数值。如果没有可用的链Id,则返回None。
嘿,我也遇到过这种情况,你需要构建这样的构造函数
SimpleStorage.constructor().buildTransaction( {
"gasPrice": w3.eth.gas_price,
"chainId": chain_id,
"from": my_address,
"nonce": nonce,
你需要加上汽油价格部分。为我工作
我今天遇到了这个问题,经过调试,我确信这个问题来自于导入的dotenv配置变量。所有变量都被python视为字符串,出于某种原因,如果链id的类型不是整数,那么web3库会交换值5777。
完美的解决方案是在部署智能合约之前将chain_id强制转换为int类型。
希望这能节省你们一些思考的时间,平静下来!
下面的代码段示例:
transaction = SimpleStorage.constructor().buildTransaction(
{
"gasPrice": w3.eth.gas_price,
"chainId": int(chain_id),
"from": my_address,
"nonce": nonce,
}
)
也许您正在学习FreeCodeCamp教程。我遇到了同样的问题
在我的情况下,它在添加天然气价格"gasPrice": w3.eth.gas_price
后起作用
transaction = SimpleStorage.constructor().buildTransaction({ "gasPrice": w3.eth.gas_price, "chainId" : chain_id, "from": my_address, "nonce": nonce})
print(transaction)
您可以使用以下代码检查chainId
const chainId = await wallet.getChainId();
console.log("chain Id",chainId);
对我来说,这是1337