-
Notifications
You must be signed in to change notification settings - Fork 997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add tool slither-interface #1898
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
92c06c4
add tool slither-interface
0xGusMcCrae 3861ea3
add interface to ci.yml
0xGusMcCrae 279f1cb
resolve pragma version causing tests to fail
0xGusMcCrae d4b041a
fix incorrect files in test error message
0xGusMcCrae 6fbb366
add --strip-trailing-cr to diff
0xGusMcCrae File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ jobs: | |
"etherscan", | ||
"find_paths", | ||
"flat", | ||
"interface", | ||
"kspec", | ||
"printers", | ||
# "prop" | ||
|
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,95 @@ | ||
#!/usr/bin/env bash | ||
|
||
### Test slither-interface | ||
|
||
DIR_TESTS="tests/tools/interface" | ||
|
||
solc-select use 0.8.19 --always-install | ||
|
||
#Test 1 - Etherscan target | ||
slither-interface WETH9 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 | ||
DIFF=$(diff crytic-export/interfaces/IWETH9.sol "$DIR_TESTS/test_1.sol") | ||
if [ "$DIFF" != "" ] | ||
then | ||
echo "slither-interface test 1 failed" | ||
cat test_1.sol | ||
echo "" | ||
cat "$DIR_TESTS/test_1.sol" | ||
exit 255 | ||
fi | ||
|
||
|
||
#Test 2 - Local file target | ||
slither-interface Mock tests/tools/interface/ContractMock.sol | ||
DIFF=$(diff crytic-export/interfaces/IMock.sol "$DIR_TESTS/test_2.sol") | ||
if [ "$DIFF" != "" ] | ||
then | ||
echo "slither-interface test 2 failed" | ||
cat test_2.sol | ||
echo "" | ||
cat "$DIR_TESTS/test_2.sol" | ||
exit 255 | ||
fi | ||
|
||
|
||
#Test 3 - unroll structs | ||
slither-interface Mock tests/tools/interface/ContractMock.sol --unroll-structs | ||
DIFF=$(diff crytic-export/interfaces/IMock.sol "$DIR_TESTS/test_3.sol") | ||
if [ "$DIFF" != "" ] | ||
then | ||
echo "slither-interface test 3 failed" | ||
cat test_3.sol | ||
echo "" | ||
cat "$DIR_TESTS/test_3.sol" | ||
exit 255 | ||
fi | ||
|
||
#Test 4 - exclude structs | ||
slither-interface Mock tests/tools/interface/ContractMock.sol --exclude-structs | ||
DIFF=$(diff crytic-export/interfaces/IMock.sol "$DIR_TESTS/test_4.sol") | ||
if [ "$DIFF" != "" ] | ||
then | ||
echo "slither-interface test 4 failed" | ||
cat test_4.sol | ||
echo "" | ||
cat "$DIR_TESTS/test_4.sol" | ||
exit 255 | ||
fi | ||
|
||
#Test 5 - exclude errors | ||
slither-interface Mock tests/tools/interface/ContractMock.sol --exclude-errors | ||
DIFF=$(diff crytic-export/interfaces/IMock.sol "$DIR_TESTS/test_5.sol") | ||
if [ "$DIFF" != "" ] | ||
then | ||
echo "slither-interface test 5 failed" | ||
cat test_5.sol | ||
echo "" | ||
cat "$DIR_TESTS/test_5.sol" | ||
exit 255 | ||
fi | ||
|
||
#Test 6 - exclude enums | ||
slither-interface Mock tests/tools/interface/ContractMock.sol --exclude-enums | ||
DIFF=$(diff crytic-export/interfaces/IMock.sol "$DIR_TESTS/test_6.sol") | ||
if [ "$DIFF" != "" ] | ||
then | ||
echo "slither-interface test 6 failed" | ||
cat test_6.sol | ||
echo "" | ||
cat "$DIR_TESTS/test_6.sol" | ||
exit 255 | ||
fi | ||
|
||
#Test 7 - exclude events | ||
slither-interface Mock tests/tools/interface/ContractMock.sol --exclude-events | ||
DIFF=$(diff crytic-export/interfaces/IMock.sol "$DIR_TESTS/test_7.sol") | ||
if [ "$DIFF" != "" ] | ||
then | ||
echo "slither-interface test 7 failed" | ||
cat test_7.sol | ||
echo "" | ||
cat "$DIR_TESTS/test_7.sol" | ||
exit 255 | ||
fi | ||
|
||
rm -r crytic-export |
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
Empty file.
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,105 @@ | ||
import argparse | ||
import logging | ||
from pathlib import Path | ||
|
||
from crytic_compile import cryticparser | ||
|
||
from slither import Slither | ||
from slither.utils.code_generation import generate_interface | ||
|
||
logging.basicConfig() | ||
logger = logging.getLogger("Slither-Interface") | ||
logger.setLevel(logging.INFO) | ||
|
||
|
||
def parse_args() -> argparse.Namespace: | ||
""" | ||
Parse the underlying arguments for the program. | ||
:return: Returns the arguments for the program. | ||
""" | ||
parser = argparse.ArgumentParser( | ||
description="Generates code for a Solidity interface from contract", | ||
usage=("slither-interface <ContractName> <source file or deployment address>"), | ||
) | ||
|
||
parser.add_argument( | ||
"contract_source", | ||
help="The name of the contract (case sensitive) followed by the deployed contract address if verified on etherscan or project directory/filename for local contracts.", | ||
nargs="+", | ||
) | ||
|
||
parser.add_argument( | ||
"--unroll-structs", | ||
help="Whether to use structures' underlying types instead of the user-defined type", | ||
default=False, | ||
action="store_true", | ||
) | ||
|
||
parser.add_argument( | ||
"--exclude-events", | ||
help="Excludes event signatures in the interface", | ||
default=False, | ||
action="store_true", | ||
) | ||
|
||
parser.add_argument( | ||
"--exclude-errors", | ||
help="Excludes custom error signatures in the interface", | ||
default=False, | ||
action="store_true", | ||
) | ||
|
||
parser.add_argument( | ||
"--exclude-enums", | ||
help="Excludes enum definitions in the interface", | ||
default=False, | ||
action="store_true", | ||
) | ||
|
||
parser.add_argument( | ||
"--exclude-structs", | ||
help="Exclude struct definitions in the interface", | ||
default=False, | ||
action="store_true", | ||
) | ||
|
||
cryticparser.init(parser) | ||
|
||
return parser.parse_args() | ||
|
||
|
||
def main() -> None: | ||
args = parse_args() | ||
|
||
contract_name, target = args.contract_source | ||
slither = Slither(target, **vars(args)) | ||
|
||
_contract = slither.get_contract_from_name(contract_name)[0] | ||
|
||
interface = generate_interface( | ||
contract=_contract, | ||
unroll_structs=args.unroll_structs, | ||
include_events=not args.exclude_events, | ||
include_errors=not args.exclude_errors, | ||
include_enums=not args.exclude_enums, | ||
include_structs=not args.exclude_structs, | ||
) | ||
|
||
# add version pragma | ||
interface = ( | ||
f"pragma solidity {_contract.compilation_unit.pragma_directives[0].version};\n\n" | ||
+ interface | ||
) | ||
|
||
# write interface to file | ||
export = Path("crytic-export", "interfaces") | ||
export.mkdir(parents=True, exist_ok=True) | ||
filename = f"I{contract_name}.sol" | ||
path = Path(export, filename) | ||
logger.info(f" Interface exported to {path}") | ||
with open(path, "w", encoding="utf8") as f: | ||
f.write(interface) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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,33 @@ | ||
pragma solidity ^0.8.19; | ||
|
||
contract Mock { | ||
|
||
error Error1(); | ||
error Error2(); | ||
error Error3(); | ||
|
||
event Event1(); | ||
event Event2(address param); | ||
event Event3(uint256 num1, uint72 num2); | ||
|
||
struct Foo { | ||
uint256 bar; | ||
address baz; | ||
} | ||
|
||
enum Status { | ||
Active, | ||
Pending, | ||
Canceled | ||
} | ||
|
||
Foo public foo; | ||
|
||
Status public status; | ||
|
||
function function1() public pure returns (address){ | ||
return address(0); | ||
} | ||
|
||
|
||
} |
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,20 @@ | ||
pragma solidity ^0.4.18; | ||
|
||
interface IWETH9 { | ||
event Approval(address, address, uint256); | ||
event Transfer(address, address, uint256); | ||
event Deposit(address, uint256); | ||
event Withdrawal(address, uint256); | ||
function name() external returns (string memory); | ||
function symbol() external returns (string memory); | ||
function decimals() external returns (uint8); | ||
function balanceOf(address) external returns (uint256); | ||
function allowance(address,address) external returns (uint256); | ||
function deposit() external payable; | ||
function withdraw(uint256) external; | ||
function totalSupply() external view returns (uint256); | ||
function approve(address,uint256) external returns (bool); | ||
function transfer(address,uint256) external returns (bool); | ||
function transferFrom(address,address,uint256) external returns (bool); | ||
} | ||
|
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,19 @@ | ||
pragma solidity ^0.8.19; | ||
|
||
interface IMock { | ||
event Event1(); | ||
event Event2(address); | ||
event Event3(uint256, uint72); | ||
error Error1(); | ||
error Error2(); | ||
error Error3(); | ||
enum Status { Active, Pending, Canceled } | ||
struct Foo { | ||
uint256 bar; | ||
address baz; | ||
} | ||
function foo() external returns (Foo memory); | ||
function status() external returns (Status); | ||
function function1() external pure returns (address); | ||
} | ||
|
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,19 @@ | ||
pragma solidity ^0.8.19; | ||
|
||
interface IMock { | ||
event Event1(); | ||
event Event2(address); | ||
event Event3(uint256, uint72); | ||
error Error1(); | ||
error Error2(); | ||
error Error3(); | ||
enum Status { Active, Pending, Canceled } | ||
struct Foo { | ||
uint256 bar; | ||
address baz; | ||
} | ||
function foo() external returns (uint256, address); | ||
function status() external returns (uint8); | ||
function function1() external pure returns (address); | ||
} | ||
|
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,15 @@ | ||
pragma solidity ^0.8.19; | ||
|
||
interface IMock { | ||
event Event1(); | ||
event Event2(address); | ||
event Event3(uint256, uint72); | ||
error Error1(); | ||
error Error2(); | ||
error Error3(); | ||
enum Status { Active, Pending, Canceled } | ||
function foo() external returns (Foo memory); | ||
function status() external returns (Status); | ||
function function1() external pure returns (address); | ||
} | ||
|
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,16 @@ | ||
pragma solidity ^0.8.19; | ||
|
||
interface IMock { | ||
event Event1(); | ||
event Event2(address); | ||
event Event3(uint256, uint72); | ||
enum Status { Active, Pending, Canceled } | ||
struct Foo { | ||
uint256 bar; | ||
address baz; | ||
} | ||
function foo() external returns (Foo memory); | ||
function status() external returns (Status); | ||
function function1() external pure returns (address); | ||
} | ||
|
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,18 @@ | ||
pragma solidity ^0.8.19; | ||
|
||
interface IMock { | ||
event Event1(); | ||
event Event2(address); | ||
event Event3(uint256, uint72); | ||
error Error1(); | ||
error Error2(); | ||
error Error3(); | ||
struct Foo { | ||
uint256 bar; | ||
address baz; | ||
} | ||
function foo() external returns (Foo memory); | ||
function status() external returns (Status); | ||
function function1() external pure returns (address); | ||
} | ||
|
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,16 @@ | ||
pragma solidity ^0.8.19; | ||
|
||
interface IMock { | ||
error Error1(); | ||
error Error2(); | ||
error Error3(); | ||
enum Status { Active, Pending, Canceled } | ||
struct Foo { | ||
uint256 bar; | ||
address baz; | ||
} | ||
function foo() external returns (Foo memory); | ||
function status() external returns (Status); | ||
function function1() external pure returns (address); | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be
crytic-export/interfaces/IWETH9.sol
? ditto for the other occurrences below.