首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >事务声明为链式ID 5777,但连接节点位于1337。

事务声明为链式ID 5777,但连接节点位于1337。
EN

Stack Overflow用户
提问于 2022-01-16 15:27:15
回答 7查看 6.4K关注 0票数 11

我正在尝试通过使用python进行事务将我的SimpleStorage.sol合同部署到一个ganache本地链。它似乎很难连接到链条上。

代码语言:javascript
运行
复制
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链的,因为它打印的是当前,但是当我构建并试图打印事务时,这里是我正在接收的整个回溯呼叫。

代码语言:javascript
运行
复制
Traceback (most recent call last):
File "C:\Users\evens\demos\web3_py_simple_storage\deploy.py", line 
52, in <module>
transaction = SimpleStorage.constructor().buildTransaction(
File "C:\Python310\lib\site-packages\eth_utils\decorators.py", line 
18, in _wrapper
return self.method(obj, *args, **kwargs)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site- 
packages\web3\contract.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:\Users\evens\AppData\Roaming\Python\Python310\site- 
packages\web3\_utils\transactions.py", line 114, in 
fill_transaction_defaults
default_val = default_getter(web3, transaction)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site- 
packages\web3\_utils\transactions.py", line 60, in <lambda>
'gas': lambda web3, tx: web3.eth.estimate_gas(tx),
File "C:\Users\evens\AppData\Roaming\Python\Python310\site- 
packages\web3\eth.py", line 820, in estimate_gas
return self._estimate_gas(transaction, block_identifier)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site- 
packages\web3\module.py", line 57, in caller
result = w3.manager.request_blocking(method_str,
File "C:\Users\evens\AppData\Roaming\Python\Python310\site- 
packages\web3\manager.py", line 197, in request_blocking
response = self._make_request(method, params)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site- 
packages\web3\manager.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:\Users\evens\AppData\Roaming\Python\Python310\site- 
packages\web3\middleware\formatting.py", line 76, in 
apply_formatters
response = make_request(method, params)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site- 
packages\web3\middleware\gas_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:\Users\evens\AppData\Roaming\Python\Python310\site- 
packages\web3\middleware\formatting.py", line 74, in 
apply_formatters
response = make_request(method, formatted_params)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site- 
packages\web3\middleware\attrdict.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:\Users\evens\AppData\Roaming\Python\Python310\site- 
packages\web3\middleware\formatting.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:\Users\evens\AppData\Roaming\Python\Python310\site- 
packages\web3\middleware\formatting.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:\Python310\lib\site-packages\eth_utils\decorators.py", line 
91, in wrapper
return ReturnType(result)  # type: ignore
File "C:\Python310\lib\site-packages\eth_utils\applicators.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:\Python310\lib\site-packages\eth_utils\applicators.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:\Users\evens\AppData\Roaming\Python\Python310\site- 
packages\web3\middleware\validation.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
EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2022-01-17 18:17:05

如果我自己也有这个问题,显然是某种Ganache CLI错误,但我能找到的最简单的修复方法是通过settings>server将Ganache中的网络id更改为1337。它重新启动会话,因此您需要更改地址和私钥变量。

如果这是我正在做的同一篇教程,你很可能会在这之后脱困.交易代码应该是:

代码语言:javascript
运行
复制
transaction = 
 SimpleStorage.constructor().buildTransaction( {
    "gasPrice": w3.eth.gas_price, 
    "chainId": chain_id, 
    "from": my_address, 
    "nonce": nonce, 
})
print(transaction)

否则,如果不设置gasPrice,则会出现值错误。

票数 33
EN

Stack Overflow用户

发布于 2022-01-17 19:14:19

这一行代码错了

代码语言:javascript
运行
复制
chain_id = 5777

加纳奇链id不是5777。这是网络标识。网络id由节点用来在同一网络上的节点之间传输数据。网络id不包含在块中,也不用于对事务或挖掘块进行签名。

代码语言:javascript
运行
复制
 chain_id = 1377

链ID也不包含在块中,但它在事务签名和验证过程中使用。

票数 11
EN

Stack Overflow用户

发布于 2022-03-10 16:36:48

它对我有用,我从w3.eth.chain_id那里得到了value w3.eth.chain_id

代码语言:javascript
运行
复制
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。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70731492

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档