我在 ytb 上学习了这门课程,在编写了与他相同的代码后,我遇到了 solcx 的问题。提前很多坦克。这是错误消息(我是法国人,所以第一行是法语,但我想你会明白的):
Information�: impossible de trouver des fichiers pour le(s) mod�le(s) sp�cifi�(s).
Traceback (most recent call last):
File "d:\Code\Blockchain\demos\web3_py_simpleStorage\deploy.py", line 34, in <module>
bytecode = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["evm"]["bytecode"]["object"]
KeyError: 'evm'
这是我的代码(与课程中的完全相同):
import json
from solcx import compile_standard, install_solc
with open("SimpleStorage.sol", "r") as file:
simple_storage_file = file.read()
# compile solidity project
install_solc("0.8.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.8.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"]
如果您需要,请使用solidity代码,但我认为问题不在这里:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SimpleStorage {
// this will get initialized to 0!
uint256 favoriteNumber;
bool favoriteBool;
struct People {
uint256 favoriteNumber;
string name;
}
People[] public people;
mapping(string => uint256) public nameToFavoriteNumber;
function store(uint256 _favoriteNumber) public {
favoriteNumber = _favoriteNumber;
}
function retrieve() public view returns (uint256) {
return favoriteNumber;
}
function addPerson(string memory _name, uint256 _favoriteNumber) public {
people.push(People(_favoriteNumber, _name));
nameToFavoriteNumber[_name] = _favoriteNumber;
}
}