Skip to content

Commit

Permalink
feat: config showing full calldata
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Feb 19, 2025
1 parent a32e621 commit f0bccea
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 16 deletions.
50 changes: 35 additions & 15 deletions src/ape/api/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
9 changes: 8 additions & 1 deletion src/ape_accounts/__init__.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
14 changes: 14 additions & 0 deletions src/ape_accounts/config.py
Original file line number Diff line number Diff line change
@@ -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``.
"""

0 comments on commit f0bccea

Please sign in to comment.