【问题标题】:Solcx compile source is throwing error - error occurred during executionSolcx 编译源抛出错误 - 执行期间发生错误
【发布时间】:2021-12-28 09:39:51
【问题描述】:

我是区块链技术的新手。我正在尝试部署智能合约。但是在编译 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-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-License-Identifier: MIT pragma 可靠性 ^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()

请帮我解决这个问题。

【问题讨论】:

    标签: python-3.x blockchain solidity web3


    【解决方案1】:

    更新:

    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 &lt;=v0.8.9 或指定您自己的output_values

    原文:

    我不确定问题的根源是什么,但指定一个 solc 版本

    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)
    

    【讨论】:

      猜你喜欢
      • 2014-08-07
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 2018-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-23
      相关资源
      最近更新 更多