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

Uploaded Source

Built Distribution

py_solc_ast-0.1.4-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: py-solc-ast-0.1.4.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.4.tar.gz
Algorithm Hash digest
SHA256 1c358a46597106c6f4b1a5ec0f4f7e289343c7eb35bfa576ac4178df62c3db75
MD5 341403a14ddf5ed3d4b544e41c760408
BLAKE2b-256 f0605e00fe1b0a93db89bfaa363c69a6882acf2fcbc8a2a1c7b666be5b7f4e10

See more details on using hashes here.

File details

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

File metadata

  • Download URL: py_solc_ast-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 11.1 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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 4af97e8a328255ea5b2d7e353220b77ebe73ed3866ac9c0df6615b5e4c095b64
MD5 d9d22081f11d089614cb5a56f63a6341
BLAKE2b-256 8a84f6f34fd81fb3c95f19493a5ec7f0032ee377574fdf132c79b456e1baee27

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