首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Solcx编译源正在抛出错误--在执行过程中发生错误。

Solcx编译源正在抛出错误--在执行过程中发生错误。
EN

Stack Overflow用户
提问于 2021-12-28 09:39:51
回答 1查看 749关注 0票数 0

我对区块链技术很陌生。我正在尝试部署智能合同。但是,在编译sol时,我总是遇到错误。它在工作,但突然停止工作。我没有做任何改变。

这就是我要犯的错误

代码语言:javascript
复制
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-32\lib\site-packages\solcx\main.py", line 130, in compile_source
    allow_empty=allow_empty,
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\solcx\main.py", line 277, in _compile_combined_json
    combined_json = _get_combined_json_outputs(solc_binary)
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\solcx\main.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-32\lib\site-packages\solcx\wrapper.py", line 163, in solc_wrapper
    stderr_data=stderrdata,
solcx.exceptions.SolcError: An error occurred during execution
> command: `C:\Users\Nikesh\.solcx\solc-v0.8.11\solc.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;

代码语言:javascript
复制
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;
    }

}

这是主要文件

代码语言:javascript
复制
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()

请帮我解决这个问题。

EN

回答 1

Stack Overflow用户

发布于 2022-01-05 23:12:24

更新:

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的拉请求来修复它。同时,您可以使用solc <=v0.8.9或指定您自己的output_values

原件:

我不知道问题的根源是什么,但指定solc版本的<=0.8.9对我有效。在你的例子中:

代码语言:javascript
复制
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)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70505253

复制
相关文章

相似问题

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