Nik*_*aya 0 python python-3.x blockchain solidity web3py
我是区块链技术的新手。我正在尝试部署智能合约。但我在编译 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.
Run Code Online (Sandbox Code Playgroud)
我正在使用 web3 和 solcx
这是contract.sol文件
// SPDX-许可证-标识符:MIT pragma Solidity ^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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是主文件
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()
Run Code Online (Sandbox Code Playgroud)
请帮我解决这个问题。
更新:
solcx.compile_source有一个可选output_values参数。如果您不提供它,py-solc-x将解析solc --help它的输出。在solc v0.8.9以及更早的版本中, 的返回值为solc --help1,但使用 时变为 0 v0.8.10,这打破了py-solc-x预期。
我有一个请求来py-solc-x修复它。同时,您可以使用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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1429 次 |
| 最近记录: |