Skip to main content

Python wrapper around the solc binary with 0.5.x and 0.6.x support

Project description

Pypi Status Build Status Coverage Status

Python wrapper around the solc Solidity compiler with 0.5.x and 0.6.x support.

Forked from py-solc.

Dependencies

Py-solc-x allows the use of multiple versions of solc and installs them as needed. You must have all required solc dependencies installed for it to work properly.

Supported Versions

Py-solc-x can install the following solc versions:

  • Linux and Windows: >=0.4.11

  • OSX: >=0.5.0

0.4.x versions are available on OSX if they have been installed via brew, but cannot be installed directly by py-solc-x.

Quickstart

Installation

pip install py-solc-x

Installing the solc Executable

The first time py-solc-x is imported it will automatically check for an installed version of solc on your system. If none is found, you must manually install via solcx.install_solc:

>>> from solcx import install_solc
>>> install_solc('v0.4.25')

Or via the command line:

python -m solcx.install v0.4.25

By default, solc versions are installed at ~/.solcx/. If you wish to use a different directory you can specify it with the SOLCX_BINARY_PATH environment variable.

Setting the solc Version

Py-solc-x defaults to the most recent installed version set as the active one. To check or modify the active version:

>>> from solcx import get_solc_version, set_solc_version
>>> get_solc_version()
Version('0.5.7+commit.6da8b019.Linux.gpp')
>>> set_solc_version('v0.4.25')
>>>

You can also set the version based on the pragma version string. The highest compatible version will be used:

>>> from solcx import set_solc_version_pragma
>>> set_solc_version_pragma('^0.4.20 || >0.5.5 <0.7.0')
Using solc version 0.5.8
>>> set_solc_version_pragma('^0.4.20 || >0.5.5 <0.7.0', check_new=True)
Using solc version 0.5.8
Newer compatible solc version exists: 0.6.0

To view available and installed versions:

>>> from solcx import get_installed_solc_versions, get_available_solc_versions
>>> get_installed_solc_versions()
['v0.4.25', 'v0.5.3', 'v0.6.0']
>>> get_available_solc_versions()
['v0.6.0', 'v0.5.15', 'v0.5.14', 'v0.5.13', 'v0.5.12', 'v0.5.11', 'v0.5.10', 'v0.5.9', 'v0.5.8', 'v0.5.7', 'v0.5.6', 'v0.5.5', 'v0.5.4', 'v0.5.3', 'v0.5.2', 'v0.5.1', 'v0.5.0', 'v0.4.25', 'v0.4.24', 'v0.4.23', 'v0.4.22', 'v0.4.21', 'v0.4.20', 'v0.4.19', 'v0.4.18', 'v0.4.17', 'v0.4.16', 'v0.4.15', 'v0.4.14', 'v0.4.13', 'v0.4.12', 'v0.4.11']

To install the highest compatible version based on the pragma version string:

>>> from solcx import install_solc_pragma
>>> install_solc_pragma('^0.4.20 || >0.5.5 <0.7.0')

Standard JSON Compilation

Use the solcx.compile_standard function to make use of the standard-json compilation feature.

>>> from solcx import compile_standard
>>> compile_standard({
...     'language': 'Solidity',
...     'sources': {'Foo.sol': 'content': "...."},
... })
{
    'contracts': {...},
    'sources': {...},
    'errors': {...},
}
>>> compile_standard({
...     'language': 'Solidity',
...     'sources': {'Foo.sol': {'urls': ["/path/to/my/sources/Foo.sol"]}},
... }, allow_paths="/path/to/my/sources")
{
    'contracts': {...},
    'sources': {...},
    'errors': {...},
}

Legacy Combined JSON compilation

>>> from solcx import compile_source, compile_files
>>> compile_source("contract Foo { function Foo() {} }")
{
    'Foo': {
        'abi': [{'inputs': [], 'type': 'constructor'}],
        'code': '0x60606040525b5b600a8060126000396000f360606040526008565b00',
        'code_runtime': '0x60606040526008565b00',
        'source': None,
        'meta': {
            'compilerVersion': '0.3.5-9da08ac3',
            'language': 'Solidity',
            'languageVersion': '0',
        },
    },
}
>>> compile_files(["/path/to/Foo.sol", "/path/to/Bar.sol"])
{
    'Foo': {
        'abi': [{'inputs': [], 'type': 'constructor'}],
        'code': '0x60606040525b5b600a8060126000396000f360606040526008565b00',
        'code_runtime': '0x60606040526008565b00',
        'source': None,
        'meta': {
            'compilerVersion': '0.3.5-9da08ac3',
            'language': 'Solidity',
            'languageVersion': '0',
        },
    },
    'Bar': {
        'abi': [{'inputs': [], 'type': 'constructor'}],
        'code': '0x60606040525b5b600a8060126000396000f360606040526008565b00',
        'code_runtime': '0x60606040526008565b00',
        'source': None,
        'meta': {
            'compilerVersion': '0.3.5-9da08ac3',
            'language': 'Solidity',
            'languageVersion': '0',
        },
    },
}

Unlinked Libraries

>>> from solcx import link_code
>>> unlinked_bytecode = "606060405260768060106000396000f3606060405260e060020a6000350463e7f09e058114601a575b005b60187f0c55699c00000000000000000000000000000000000000000000000000000000606090815273__TestA_________________________________90630c55699c906064906000906004818660325a03f41560025750505056"
>>> link_code(unlinked_bytecode, {'TestA': '0xd3cda913deb6f67967b99d67acdfa1712c293601'})
... "606060405260768060106000396000f3606060405260e060020a6000350463e7f09e058114601a575b005b60187f0c55699c00000000000000000000000000000000000000000000000000000000606090815273d3cda913deb6f67967b99d67acdfa1712c29360190630c55699c906064906000906004818660325a03f41560025750505056"

Import Path Remappings

solc provides path aliasing allow you to have more reusable project configurations.

You can use this like:

>>> from solcx import compile_files

>>> compile_files([source_file_path], import_remappings=["zeppeling=/my-zeppelin-checkout-folder"])

More information about solc import aliasing

Development

This project was forked from py-solc and should be considered a beta. Comments, questions, criticisms and pull requests are welcomed.

Tests

Py-solc-x is tested on Linux and Windows with solc versions >=0.4.11.

To run the test suite:

pytest tests/

By default, the test suite installs all available solc versions for your OS. If you only wish to test against already installed versions, include the --no-install flag.

License

This project is licensed under the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py-solc-x-0.8.1.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

py_solc_x-0.8.1-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file py-solc-x-0.8.1.tar.gz.

File metadata

  • Download URL: py-solc-x-0.8.1.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.5

File hashes

Hashes for py-solc-x-0.8.1.tar.gz
Algorithm Hash digest
SHA256 9c7406d5702bee6e3af61f2a269bbf1eb2583671f0401a161b596c84c8563ec5
MD5 012eacb0dd80167632ccdc5ded93c4cd
BLAKE2b-256 ca5020b8375869bc6bb3e6aed5a340c7c420f0bdba43ce42d3b2d88e214d1643

See more details on using hashes here.

Provenance

File details

Details for the file py_solc_x-0.8.1-py3-none-any.whl.

File metadata

  • Download URL: py_solc_x-0.8.1-py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.5

File hashes

Hashes for py_solc_x-0.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 263176b37c718289df21f665d97123b10535e11bb6ece3806fd3b366adc3d439
MD5 9628392ed36a4c9c02941c10627ea1e5
BLAKE2b-256 7dd48a5fd1a528f1044b3e7b3a9550c441815b827a7bc748452d6164ced189d3

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page