From f0bccea4794aeb8a22ee7a08aee510cb0398e79d Mon Sep 17 00:00:00 2001 From: antazoey Date: Wed, 19 Feb 2025 15:14:03 -0600 Subject: [PATCH] feat: config showing full calldata --- src/ape/api/transactions.py | 50 +++++++++++++++++++++++++----------- src/ape_accounts/__init__.py | 9 ++++++- src/ape_accounts/config.py | 14 ++++++++++ 3 files changed, 57 insertions(+), 16 deletions(-) create mode 100644 src/ape_accounts/config.py diff --git a/src/ape/api/transactions.py b/src/ape/api/transactions.py index fc7f908cc2..7f6df3acd8 100644 --- a/src/ape/api/transactions.py +++ b/src/ape/api/transactions.py @@ -183,26 +183,46 @@ def __repr__(self) -> str: return f"<{cls_name} {params}>" def __str__(self) -> str: - # NOTE: Using JSON mode for style. - data = self.model_dump(mode="json") - if len(data["data"]) > 9: - # only want to specify encoding if data["data"] is a string - if isinstance(data["data"], str): - data["data"] = ( + return self.to_string() + + def to_string(self, show_full_calldata: Optional[bool] = None) -> str: + """ + Get the stringified representation of the transaction. + + Args: + show_full_calldata (bool | None): Specify whether to abridge of show full calldata + when it is long. Defaults to the value from the config + (``accounts.show_full_calldata``). + + Returns: + str + """ + data = self.model_dump(mode="json") # JSON mode used for style purposes. + + if show_full_calldata is None: + # If was not specified, use the default value from the config. + show_full_calldata = self.config_manager.accounts.show_full_calldata + + if show_full_calldata or len(data["data"]) <= 9: + data["data"] = ( + to_hex(bytes(data["data"], encoding="utf8")) + if isinstance(data["data"], str) + else to_hex(bytes(data["data"])) + ) + + else: + # Only want to specify encoding if data["data"] is a string + data["data"] = ( + ( "0x" + bytes(data["data"][:4], encoding="utf8").hex() + "..." + bytes(data["data"][-4:], encoding="utf8").hex() ) - else: - data["data"] = ( - to_hex(bytes(data["data"][:4])) + "..." + to_hex(bytes(data["data"][-4:])) - ) - else: - if isinstance(data["data"], str): - data["data"] = to_hex(bytes(data["data"], encoding="utf8")) - else: - data["data"] = to_hex(bytes(data["data"])) + if isinstance(data["data"], str) + else to_hex(bytes(data["data"][:4])) + "..." + to_hex(bytes(data["data"][-4:])) + ) + params = "\n ".join(f"{k}: {v}" for k, v in data.items()) cls_name = getattr(type(self), "__name__", TransactionAPI.__name__) return f"{cls_name}:\n {params}" diff --git a/src/ape_accounts/__init__.py b/src/ape_accounts/__init__.py index 2c9148cf2a..8dddb6bfbf 100644 --- a/src/ape_accounts/__init__.py +++ b/src/ape_accounts/__init__.py @@ -1,6 +1,13 @@ from importlib import import_module -from ape.plugins import AccountPlugin, register +from ape.plugins import AccountPlugin, Config, register + + +@register(Config) +def config_class(): + from ape_accounts.config import AccountsConfig + + return AccountsConfig @register(AccountPlugin) diff --git a/src/ape_accounts/config.py b/src/ape_accounts/config.py new file mode 100644 index 0000000000..a595c236fb --- /dev/null +++ b/src/ape_accounts/config.py @@ -0,0 +1,14 @@ +from ape.api.config import PluginConfig + + +class AccountsConfig(PluginConfig): + """ + Config accounts generally. + """ + + show_full_calldata: bool = False + """ + When signing transactions, ``True`` will always show the full + calldata where ``False`` shows an abridged version of the data + (enough to see the method ID). Defaults to ``False``. + """