Solcx编译源正在引发错误-执行过程中发生错误



我是区块链技术的新手。我正在尝试部署智能合约。但在编译sol文件时,我总是会遇到以下错误。它在工作,但突然停止了工作。我没有做任何改变。

这是我得到的错误

Traceback (most recent call last):
File ".main.py", line 68, in <module>
main()
File ".main.py", line 57, in main
compiled_sol = compile_source_file('contract.sol')
File ".main.py", line 20, in compile_source_file
return solcx.compile_source(source)
File "C:Program Files (x86)Python37-32libsite-packagessolcxmain.py", line 130, in compile_source
allow_empty=allow_empty,
File "C:Program Files (x86)Python37-32libsite-packagessolcxmain.py", line 277, in _compile_combined_json
combined_json = _get_combined_json_outputs(solc_binary)
File "C:Program Files (x86)Python37-32libsite-packagessolcxmain.py", line 242, in _get_combined_json_outputs
help_str = wrapper.solc_wrapper(solc_binary=solc_binary, help=True)[0].split("n")
File "C:Program Files (x86)Python37-32libsite-packagessolcxwrapper.py", line 163, in solc_wrapper
stderr_data=stderrdata,
solcx.exceptions.SolcError: An error occurred during execution
> command: `C:UsersNikesh.solcxsolc-v0.8.11solc.exe --help -`
> return code: `0`
> stdout:
solc, the Solidity commandline compiler.
This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you
are welcome to redistribute it under certain conditions. See 'solc --license'
for details.
Usage: solc [options] [input_file...]
Compiles the given Solidity input files (or the standard input if none given or
"-" is used as a file name) and outputs the components specified in the options
at standard output or in files in the output directory, if specified.
Imports are automatically read from the filesystem, but it is also possible to
remap paths using the context:prefix=path syntax.
Example:
solc --bin -o /tmp/solcoutput dapp-bin=/usr/local/lib/dapp-bin contract.sol
General Information:
--help               Show help message and exit.
--version            Show version and exit.
--license            Show licensing information and exit.
--input-file arg     input file
Input Options:
--base-path path     Use the given path as the root of the source tree
instead of the root of the filesystem.
--include-path path  Make an additional source directory available to the
default import callback. Use this option if you want to
import contracts whose location is not fixed in relation
to your main source tree, e.g. third-party libraries
installed using a package manager. Can be used multiple
times. Can only be used if base path has a non-empty
value.
--allow-paths path(s)
Allow a given path for imports. A list of paths can be
supplied by separating them with a comma.
--ignore-missing     Ignore missing files.
--error-recovery     Enables additional parser error recovery.

我正在使用web3和solcx

这是contract.sol文件

//SPDX许可证标识符:MIT实用主义稳固性^0.8.11;

contract StoreVar {
uint8 public _myVar;
event MyEvent(uint indexed _var);
function setVar(uint8 _var) public {
_myVar = _var;
emit MyEvent(_var);
}
function getVar() public view returns (uint8) {
return _myVar;
}
}

这是的主文件

from web3 import Web3
from web3.middleware import geth_poa_middleware
# from solcx import compile_source, install_solc
import solcx
"""
pip install py-solc-x
pip install web3
"""
def compile_source_file(file_path):
solcx.install_solc(version='latest')
# install_solc(version='latest')
# install_solc("0.6.0")
# print(solcx.get_compilable_solc_versions())
with open(file_path, 'r') as f:
source = f.read()
print(source)
return solcx.compile_source(source)

def deploy_contract(w3, contract_interface):
# unicorns = w3.eth.contract(address="0x589a1532Aasfgs4e38345b58C11CF4697Ea89A866", abi=contract_interface['abi'])
# nonce = w3.eth.get_transaction_count('0x589a1532AAaE84e38345b58C11CF4697Eaasasd') 
# unicorn_txn = unicorns.functions.transfer(
#  '0x589a1532AAaE84e38345b58C11CF4697asdfasd',
#  1,
# ).buildTransaction({
#  'chainId': 1,
#  'gas': 70000,
#  'maxFeePerGas': w3.toWei('2', 'gwei'),
#  'maxPriorityFeePerGas': w3.toWei('1', 'gwei'),
#  'nonce': nonce,
# })
# print(unicorn_txn)
tx_hash = w3.eth.contract(
abi=contract_interface['abi'],
bytecode=contract_interface['bin']).constructor().transact()
address = w3.eth.get_transaction_receipt(tx_hash)['contractAddress']
return address

def main():
w3 = Web3(Web3.HTTPProvider("https://rinkeby.infura.io/v3/c0easgasgas2666cd56ef4e3"))
w3.middleware_onion.inject(geth_poa_middleware, layer=0)
# print(w3.eth.get_block('latest'))
# print(w3.eth.get_balance('0x589a15asdfasdfab58C11CF4697Ea89A866'))
address = '0x589a1532AAaE8asdfasdf8C11CF4697Ea89A866'
abi = '[{"inputs":[{"internalType":"address","name":"account","type":"address"},{"internalType":"address","name":"minter_","type":"address"}'
compiled_sol = compile_source_file('contract.sol')
contract_id, contract_interface = compiled_sol.popitem()
# print(contract_id)
# print(contract_interface)
address = deploy_contract(w3, contract_interface)
print(f'Deployed {contract_id} to: {address}n')

main()

请帮我解决这个问题。

更新:

solcx.compile_source有一个可选的output_values参数。如果你不提供它,py-solc-x将解析它的solc --help输出。在solc v0.8.9和更早的版本中,solc --help的返回值是1,但在v0.8.10中变为0,这打破了py-solc-x的预期。

我在py-solc-x中有一个pull请求来修复它。同时,您可以使用solc <=v0.8.9或指定自己的output_values

原件:

我不确定问题的根源是什么,但指定了一个solc版本<0.8.9对我有用。在你的例子中:

def compile_source_file(file_path):
solcx.install_solc(version='0.8.9')
solcx.set_solc_version('0.8.9')
with open(file_path, 'r') as f:
source = f.read()
print(source)
return solcx.compile_source(source)

相关内容

最新更新