Skip to content

Instantly share code, notes, and snippets.

@raineorshine
Last active August 18, 2020 06:19
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raineorshine/e74ee7c447548afb12989ab2610ee020 to your computer and use it in GitHub Desktop.
Save raineorshine/e74ee7c447548afb12989ab2610ee020 to your computer and use it in GitHub Desktop.
Generate an AST for an Ethereum Solidity contract with the solidity-parser module.
echo {} > package.json
npm install --save solidity-parser
FILE=MyContract.sol
node -e "console.log(JSON.stringify(require('solidity-parser').parseFile('./${FILE}'), null, 2))"
{
"type": "Program",
"body": [
{
"type": "ContractStatement",
"name": "MyContract",
"is": [],
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "AssignmentExpression",
"operator": "=",
"left": {
"type": "DeclarativeExpression",
"name": "counter",
"literal": {
"type": "Type",
"literal": "uint",
"members": [],
"array_parts": []
},
"is_constant": false,
"is_public": false,
"is_memory": false
},
"right": {
"type": "Literal",
"value": 0
}
}
},
{
"type": "FunctionDeclaration",
"name": "Count",
"params": null,
"modifiers": null,
"body": {
"type": "BlockStatement",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "UpdateExpression",
"operator": "++",
"argument": {
"type": "Identifier",
"name": "counter"
},
"prefix": false
}
}
]
},
"is_abstract": false
},
{
"type": "FunctionDeclaration",
"name": "CallCount",
"params": null,
"modifiers": null,
"body": {
"type": "BlockStatement",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "CallExpression",
"callee": {
"type": "Identifier",
"name": "Count"
},
"arguments": []
}
}
]
},
"is_abstract": false
},
{
"type": "FunctionDeclaration",
"name": "Send",
"params": null,
"modifiers": null,
"body": {
"type": "BlockStatement",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "CallExpression",
"callee": {
"type": "MemberExpression",
"object": {
"type": "MemberExpression",
"object": {
"type": "Identifier",
"name": "msg"
},
"property": {
"type": "Identifier",
"name": "sender"
},
"computed": false
},
"property": {
"type": "Identifier",
"name": "send"
},
"computed": false
},
"arguments": [
{
"type": "Literal",
"value": 1
}
]
}
}
]
},
"is_abstract": false
},
{
"type": "FunctionDeclaration",
"name": "Call",
"params": null,
"modifiers": null,
"body": {
"type": "BlockStatement",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "CallExpression",
"arguments": [],
"callee": {
"type": "MemberExpression",
"property": {
"type": "Identifier",
"name": "call"
},
"computed": false,
"object": {
"type": "CallExpression",
"callee": {
"type": "MemberExpression",
"object": {
"type": "MemberExpression",
"object": {
"type": "Identifier",
"name": "msg"
},
"property": {
"type": "Identifier",
"name": "sender"
},
"computed": false
},
"property": {
"type": "Identifier",
"name": "value"
},
"computed": false
},
"arguments": [
{
"type": "Literal",
"value": 1
}
]
}
}
}
}
]
},
"is_abstract": false
}
]
}
]
}
contract MyContract {
uint counter = 0;
function Count() {
counter++;
}
function CallCount() {
Count();
}
function Send() {
msg.sender.send(1);
}
function Call() {
msg.sender.value(1).call();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment