Skip to main content

A tool for exploring the abstrax syntrax tree generated by solc.

Project description

solc-ast

Pypi Status

A tool for exploring the Solidity abstrax syntrax tree as generated by the solc compiler.

Installation

You can install the latest release via pip:

$ pip install py-solc-ast

Or clone the repo and use setuptools:

$ python setup.py install

Usage

First, use py-solc-x to compile your contracts to the standard JSON output format.

>>> import json
>>> import solcx
>>> input_json = json.load(open('input.json'))
>>> output_json = solcx.compile_standard(input_json)

Next, import solcast and initialize using from_standard_output_json or from_standard_output. This returns a list of SourceUnit objects, which each represent the base node in a Solidity AST.

>>> import solcast
>>> nodes = solcast.from_standard_output(output_json)

From the initial objects, you can explore the AST:

>>> nodes
[<SourceUnit iterable object 'contracts/Token.sol'>]
>>> s = nodes[0]
>>> s
<SourceUnit iterable object 'contracts/Token.sol'>

>>> s.keys()
['children', 'contract_id', 'contracts', 'depth', 'keys', 'name', 'node_type', 'offset', 'parent', 'path', 'value']

>>> s.contracts
[<ContractDefinition iterable 'Token'>]

>>> s[0]
<ContractDefinition iterable 'Token'>

>>> s['Token']
<ContractDefinition iterable 'Token'>

>>> s['Token'].keys()
['children', 'contract_id', 'depth', 'functions', 'keys', 'name', 'node_class', 'node_type', 'offset', 'parent', 'value']

>>> s['Token'].functions
[<FunctionDefinition iterable '<constructor>'>, <FunctionDefinition iterable '<fallback>'>, <FunctionDefinition iterable 'balanceOf'>, <FunctionDefinition iterable 'allowance'>, <FunctionDefinition iterable 'approve'>, <FunctionDefinition iterable 'transfer'>, <FunctionDefinition iterable 'transferFrom'>]

>>> s['Token']['transfer']
<FunctionDefinition iterable 'transfer'>

>>> s['Token']['transfer'].statements
[<ExpressionStatement.FunctionCall 'require(balances[msg.sender] >= _value, Insufficient Balance)'>, <ExpressionStatement.Assignment iterable uint256 'balances[msg.sender] = balances[msg.sender].sub(_value)'>, <ExpressionStatement.Assignment iterable uint256 'balances[_to] = balances[_to].add(_value)'>, <EmitStatement.FunctionCall 'Transfer'>, <Return.Literal bool 'true'>]

Use the Node.children and Node.parents methods to access and filter related nodes:

>>> node = s['Token']['transfer']

>>> node.children(depth=1)
[<ExpressionStatement.FunctionCall 'require(balances[msg.sender] >= _value, Insufficient Balance)'>, <ExpressionStatement.Assignment iterable uint256 'balances[msg.sender] = balances[msg.sender].sub(_value)'>, <ExpressionStatement.Assignment iterable uint256 'balances[_to] = balances[_to].add(_value)'>, <EmitStatement.FunctionCall 'Transfer'>, <Return.Literal bool 'true'>]

>>> node.children(include_children=False, filters={'node_type': "FunctionCall", 'name': "require"})
[<ExpressionStatement.FunctionCall 'require(balances[msg.sender] >= _value, Insufficient Balance)'>]

>>> node.parents()
[<ContractDefinition iterable 'Token'>, <SourceUnit iterable object 'contracts/Token.sol'>]

Calling help on either of these methods provides a more detailed explanation of their functionality.

Development

This project is still in development and should be considered an early alpha. All feedback and contributions are welcomed!

Not all nodes have been implemented yet. From any object, you can use the Node._unimplemented method to get a list of keys that contain AST nodes that have not yet been included. The raw json data is stored at Node._node.

>>> s['Token']['transfer']._unimplemented()
['parameters', 'returnParameters']

>>> s['Token']['transfer']._node['returnParameters']
{'id': 328, 'nodeType': 'ParameterList', 'parameters': [{'constant': False, 'id': 327, 'name': '', 'nodeType': 'VariableDeclaration', 'scope': 373, 'src': '1573:4:2', 'stateVariable': False, 'storageLocation': 'default', 'typeDescriptions': {'typeIdentifier': 't_bool', 'typeString': 'bool'}, 'typeName': {'id': 326, 'name': 'bool', 'nodeType': 'ElementaryTypeName', 'src': '1573:4:2', 'typeDescriptions': {'typeIdentifier': 't_bool', 'typeString': 'bool'}}, 'value': None, 'visibility': 'internal'}], 'src': '1572:6:2'}

See the Solidity documentation for information about the AST grammar.

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-ast-0.1.3.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

py_solc_ast-0.1.3-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file py-solc-ast-0.1.3.tar.gz.

File metadata

  • Download URL: py-solc-ast-0.1.3.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for py-solc-ast-0.1.3.tar.gz
Algorithm Hash digest
SHA256 bd984eac68240000a5506e5047cbc46b5f203df2bf81e46c61b69fff8c069b74
MD5 e805790e7df28ef10758521fa4751212
BLAKE2b-256 a3db34f0de85bf2f7d83038fe0f130a457c3bb403bfd060375c519ca23a97388

See more details on using hashes here.

File details

Details for the file py_solc_ast-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: py_solc_ast-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for py_solc_ast-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2abe822446d8d3ae626124fc4cd61d3592f18e004dfae3f042aa61640b257c18
MD5 eb1335e97928243c269f0ed7f05f7598
BLAKE2b-256 993e69ce29c50946adc7bcf05244348236e2d913c5e150ee1f48b7d9e35edbfa

See more details on using hashes here.

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