我写了下面的代码。它似乎与signed_txn = w3.eth.account.sign_transaction(transaction, private_key=private_key部分有问题。每次部署时,我都会面对下面的错误:
“回溯”(最近一次调用):文件“E:\区块链-开发\部署. 18”,第81行,在signed_txn =w3.eth.account.sign_transaction(事务,private_key=private_key)文件的第18行,在_wrapper返回self.method(obj,*args,**kwargs)文件"C:\Users\Rezli\AppData\Local\Programs\Python\Python310\lib\site-packages\eth_account\account.py",行734,在sign_transaction raise中(“from必须匹配键的%s,但它是%s”%)( TypeError: from:from必须匹配密钥的0x0A651A5976Ee3cfB68719Dcf6E65f7a691f803,但它是0x95ac4081466F6Bc79B56D7EE1a002D0407820“
我在ganache中使用的地址是0x95ac4081466196F6Bc79B56D7EE1a002D0407820。
from solcx import compile_standard, install_solc
import json
from web3 import Web3
import os
from dotenv import load_dotenv
load_dotenv()
with open("./SimpleStorage.sol", "r") as file:
simple_storage_file = file.read()
print("Installing...")
install_solc("0.6.0")
# compile our solidity
install_solc("0.6.0")
compiled_sol = compile_standard(
{
"language": "Solidity",
"sources": {"SimpleStorage.sol": {"content": simple_storage_file}},
"settings": {
"outputSelection": {
"*": {
"*": ["abi", "metadata", "evm.bytecode", "evm.bytecode.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 = json.loads(
compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["metadata"]
)["output"]["abi"]
# for connecting to ganache
w3 = Web3(Web3.HTTPProvider("HTTP://127.0.0.1:7545"))
chain_id = 5777
my_address = "0x95ac4081466196F6Bc79B56D7EE1a002D0407820"
private_key = os.getenv("PRIVATE_KEY")
print(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
transaction = SimpleStorage.constructor().buildTransaction(
{
"chainID": chain_id,
"gasPrice": w3.eth.gas_price,
"from": my_address,
"nonce": nonce,
}
)
# print(transaction)
signed_txn = w3.eth.account.sign_transaction(transaction, private_key=private_key)
print(signed_txn)
print("Deploying Contract!")发布于 2022-01-14 17:20:27
我相信你的私钥和地址不匹配。这是你的加纳赫地址
my_address = "0x95ac4081466196F6Bc79B56D7EE1a002D0407820"所以私钥应该是它的私钥。
private_key = os.getenv("PRIVATE_KEY")chainId也不是"5777",而是1377。
发布于 2022-07-12 23:41:16
在你的终端(mac)
unset PRIVATE_KEY如果有具有不同值的冲突环境变量名,则此操作有效。
在我的例子中,我使用终端设置了一个PRIVATE_KEY环境变量,然后在我工作目录中的PRIVATE_KEY文件中设置了第二个PRIVATE_KEY(同名不同值)环境变量。
因此,当使用python3运行程序时,将使用终端中的PRIVATE_KEY集,该值用于另一个地址。因此,正如您在错误消息底部看到的那样,地址和私钥不匹配:
File "/usr/local/lib/python3.9/site-packages/eth_account/account.py", line 735, in sign_transaction
raise TypeError("from field must match key's %s, but it was %s" % (
TypeError: from field must match key's 0x623E2E33250d72C1588E50933bA93Ab23B0936e2, but it was 0xFcc971aa122b3BCbA8379bf53015C725F4F93BE5发布于 2022-04-19 11:58:35
如果您还存在此问题,您似乎在私钥之间发生了冲突。您可能已经在“PRIVATE_KEY”文件之外定义了一个环境变量.env。这里的问题是您有两个私钥,一个是在设置".env“文件之前定义的,另一个是在".env”文件中定义的。因此,考虑删除先前定义的一个。
注意:对于ganache,您必须使用1337作为chainid,而不是使用5777 (即网络id)或1377。
https://stackoverflow.com/questions/70713096
复制相似问题