-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add example code, update README and add docs
- Loading branch information
Showing
11 changed files
with
282 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.0; | ||
|
||
library Adder { | ||
function add(uint256 a, uint256 b) public pure returns (uint256) { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.0; | ||
|
||
import "AdderLib.sol"; | ||
|
||
contract MyContract { | ||
Adder private adder; | ||
uint256 public myVariable; | ||
|
||
function addToVariable(uint256 value) public { | ||
myVariable = adder.add(myVariable, value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.0; | ||
|
||
import "@openzeppelin/access/Ownable.sol"; | ||
|
||
contract MyContract is Ownable { | ||
uint256 public myVariable; | ||
|
||
// Function to set the value of myVariable | ||
function setMyVariable(uint256 newValue) public onlyOwner { | ||
myVariable = newValue; | ||
} | ||
|
||
function getMyVariable() public view returns (uint256) { | ||
return myVariable; | ||
} | ||
|
||
function addToVariable(uint256 value) public onlyOwner { | ||
myVariable += value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.0; | ||
|
||
contract Ownable { | ||
address public owner; | ||
|
||
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); | ||
|
||
function Ownable() public { | ||
owner = msg.sender; | ||
} | ||
|
||
modifier onlyOwner() { | ||
require(msg.sender == owner); | ||
_; | ||
} | ||
|
||
function transferOwnership(address newOwner) public onlyOwner { | ||
require(newOwner != address(0)); | ||
emit OwnershipTransferred(owner, newOwner); | ||
owner = newOwner; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@openzeppelin/=lib/oz/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# makes this file easily runnable in Pycharm | ||
if __name__ == '__main__': | ||
pass | ||
|
||
|
||
from pathlib import Path | ||
|
||
from solidity_parser import filesys | ||
from solidity_parser.ast import symtab, ast2builder, solnodes2 | ||
|
||
|
||
project_dir = Path('./project') | ||
source_dir = project_dir / 'contracts' | ||
library_dir = project_dir / 'lib' | ||
remappings_file = project_dir / 'remappings.txt' | ||
|
||
|
||
# setup our VFS with all of the source directories and remappings if required, this is where AST1 parse trees come from | ||
|
||
vfs = filesys.VirtualFileSystem( | ||
# base_path is the project path | ||
project_dir, | ||
# don't pass in CWD, VFS will get it | ||
None, | ||
# pass in source and library directories as "include_paths", i.e. source paths | ||
[source_dir, library_dir], | ||
# no forced compiler version | ||
None | ||
) | ||
|
||
if remappings_file.exists(): | ||
vfs.parse_import_remappings(remappings_file) | ||
|
||
# symbol table builder is required to get symbol info from AST1 for AST2 | ||
sym_builder = symtab.Builder2(vfs) | ||
|
||
file_to_analyse = Path('TestContract.sol') | ||
# searches for the file to analysis and gets us back a FileScope(the input to AST2builder) | ||
# pass in the str representation of the Path | ||
file_sym_info: symtab.FileScope = sym_builder.process_or_find_from_base_dir(file_to_analyse) | ||
|
||
# setup the AST2 builder | ||
ast2_builder = ast2builder.Builder() | ||
ast2_builder.enqueue_files([file_sym_info]) | ||
|
||
# run the builder, this will create AST2 parse trees for the file_to_analyse and any files that are referenced from | ||
# there and need to be analysed in the process(all lazily) | ||
ast2_builder.process_all() | ||
|
||
# AST2 creates "top level units" for contracts, interfaces, enums, libraries and also "synthetic top level units" for | ||
# functions, errors, events and constants that are "free", i.e. not defined in a top level node, in our example code | ||
# we only have a contract defined at the top level, so get that | ||
all_units: list[solnodes2.TopLevelUnit] = ast2_builder.get_top_level_units() | ||
|
||
# u.name is a solnodes2.Ident, str(u.name) makes it comparable to strings | ||
# hint for ContractDefinition since we know the type | ||
my_contract: solnodes2.ContractDefinition = [u for u in all_units if str(u.name) == 'MyContract'][0] | ||
|
||
for p in my_contract.parts: | ||
if isinstance(p, solnodes2.FunctionDefinition): | ||
print(f'Found a function: {p.descriptor()}') | ||
|
||
# Should print: | ||
# Found a function: TestContract.sol.MyContract::myVariable() returns (uint256) | ||
# Found a function: TestContract.sol.MyContract::setMyVariable(uint256) returns () | ||
# Found a function: TestContract.sol.MyContract::getMyVariable() returns (uint256) | ||
# Found a function: TestContract.sol.MyContract::addToVariable(uint256) returns () | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.