Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiling a contract with import_remappings fails #44

Open
zlevas opened this issue Jan 15, 2018 · 6 comments
Open

Compiling a contract with import_remappings fails #44

zlevas opened this issue Jan 15, 2018 · 6 comments

Comments

@zlevas
Copy link

zlevas commented Jan 15, 2018

  • py-solc Version: 2.1.0
  • solc Version: 0.4.17
  • Python Version: 3.5.2
  • OS: Ubuntu 16.04, 4.4.0-104-generic

What was wrong?

I tried to compile a contract, say like this one:

pragma solidity 0.4.17;

contract Blah {
    uint public a;

    function Blah(uint _a) public {
        a = _a;
    }

    function getA() public view returns(uint) {
        return a;
    }
}

The python compilation code is as follows:

class Chain:
    ...
    @classmethod
    def compileContract(cls, filename):
	with open(filename,'r') as f:
            contract_source_code = f.read()
        compiled_sol = compile_source(contract_source_code, import_remappings=['='])                                                    
        return compiled_sol ['<stdin>:Blah']
    ...

Executing this function returns an error:

Traceback (most recent call last):
  File "compile.py", line 71, in <module>
    compiled = Chain.compileContract(sys.argv[1])
  File "compile.py", line 24, in compileContract
    compiled_sol = compile_source(contract_source_code, import_remappings=["="])
  File "/usr/local/lib/python3.5/dist-packages/solc/main.py", line 106, in compile_source
    stdoutdata, stderrdata, command, proc = solc_wrapper(**compiler_kwargs)
  File "/usr/local/lib/python3.5/dist-packages/solc/utils/string.py", line 85, in inner
    return force_obj_to_text(fn(*args, **kwargs))
  File "/usr/local/lib/python3.5/dist-packages/solc/wrapper.py", line 165, in solc_wrapper
    stderr_data=stderrdata,
solc.exceptions.SolcError: An error occurred during execution
> command: `solc --combined-json abi,asm,ast,bin,bin-runtime,clone-bin,devdoc,interface,opcodes,userdoc =`
> return code: `1`
> stderr:

> stdout:

Notes:

  • It fails whatever value I assign to import_remapings.
  • Executing with import_remappings removed works fine (it's not needed in this example, but I have another case where I'm importing other contracts and I need to use it).
  • Executing solc --combined-json abi,asm,ast,bin,bin-runtime,clone-bin,devdoc,interface,opcodes,userdoc = blah.sol directly from the command line works fine too (no errors, no warnings).
  • I tried also running with solc 0.4.19 in docker but problem persisted.
  • All files I have in one directory and all commands were executed inside this directory.

Cute Animal Picture

tumblr_mbyr4z1nm41rzey51o1_500

@zlevas
Copy link
Author

zlevas commented Jan 15, 2018

A simple temporary workaround:

# compile.py
class Chain:
    ...
    @classmethod
    def readCompiledFromJSON(cls, j):
        compiled = json.loads(j)

        contracts = list(compiled['contracts'].keys())
        if (len(contracts) > 1):
            print("Warning: more than one contract at once supplied. Reading the first one.")

        contract_name = contracts[0]
        print("Reading contract: ", contract_name.split(":")[1])

        compiled = compiled['contracts'][contract_name]
        compiled['abi'] = json.loads(compiled['abi']) # abi is stored as a separate json object                      

        return compiled
    ...

if __name__ == "__main__":
    ...
    compiled = Chain.readCompiledFromJSON(sys.argv[1])
    ...

Execute:

python compile.py $(solc --combined-json bin,abi blah.sol)

@Garito
Copy link

Garito commented Feb 1, 2018

Any advance on this?
I can't understand the workaround

@zlevas
Copy link
Author

zlevas commented Feb 5, 2018

My workaround is just a function which reads a json passed in a command line argument, which is a compiled contract. Compilation itself is done in memory with solc, not with py-solc. It just allows for having the contract data stored the same way as it would be using py-solc without affecting other parts of code, and does not require compiling it in advance and storing in a file.

Which part of it don't you understand exactly?

@Garito
Copy link

Garito commented Feb 6, 2018

I wasn't understanding that the issue was about compiling source code and you are loading an already compiled json
Now I got it, thanks

@saaperezru
Copy link

saaperezru commented Feb 12, 2018

For anyone else facing this issue, a simple solution is to send an extra dash at the end of the command:

compiled_sol = compile_source(contract_source_code, import_remappings=['=/', '-'])

The problem seems to be related to the way the solc compiler cli behaves when sending the source code via stdin AND sending the remapping arguments. The following command does not work if you try to send the source code via stdin (it returns immediately without waiting for input):

$ solc --combined-json abi,asm,ast,bin,bin-runtime,clone-bin,devdoc,interface,opcodes,userdoc =/

However the following DOES work:

$ solc --combined-json abi,asm,ast,bin,bin-runtime,clone-bin,devdoc,interface,opcodes,userdoc =/ -

So the simplest solution is to send an extra dash at the end of the command, and the import_remappings arguments provides an easy way to do so :)

@ethereum ethereum deleted a comment from meherett Aug 1, 2018
@karthika-glance
Copy link

How does the above change apply when I do compile_files(....)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants