Skip to content

Add storage_variables printer #2667

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

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions slither/printers/all_printers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@
from .summary.martin import Martin
from .summary.cheatcodes import CheatcodePrinter
from .summary.entry_points import PrinterEntryPoints
from .summary.storage_variables import PrinterStorageVariables
66 changes: 66 additions & 0 deletions slither/printers/summary/storage_variables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""
Module printing all storage variables of the contracts
"""

from slither.printers.abstract_printer import AbstractPrinter
from slither.utils.colors import Colors
from slither.utils.output import Output
from slither.utils.myprettytable import MyPrettyTable


class PrinterStorageVariables(AbstractPrinter):

ARGUMENT = "storage-variables"
HELP = "Print all storage variables of the contracts"

WIKI = "https://github.com/trailofbits/slither/wiki/Printer-documentation#storage-variables"

def output(self, _filename) -> Output:
"""
_filename is not used
Args:
_filename(string)
"""
all_contracts = []

for contract in sorted(
(
c
for c in self.slither.contracts_derived
if not c.is_test and not c.is_from_dependency()
),
key=lambda x: x.name,
):
storage_vars = contract.storage_variables_ordered

if not storage_vars:
continue

table = MyPrettyTable(
["Variable", "Type", "Visibility", "Slot", "Offset", "Inherited From"]
)

contract_info = [
f"\nContract {Colors.BOLD}{Colors.YELLOW}{contract.name}{Colors.END}"
f" ({contract.source_mapping})"
]

for v in storage_vars:
slot, offset = contract.compilation_unit.storage_layout_of(contract, v)
inherited = v.contract.name if v.contract != contract else ""
table.add_row(
[
f"{Colors.BOLD}{Colors.RED}{v.name}{Colors.END}",
f"{Colors.GREEN}{v.type}{Colors.END}",
f"{Colors.BLUE}{v.visibility}{Colors.END}",
slot,
offset,
f"{Colors.MAGENTA}{inherited}{Colors.END}",
]
)

all_contracts.append("\n".join(contract_info + [str(table)]))

info = "\n".join(all_contracts) if all_contracts else ""
self.info(info)
return self.generate_output(info)