Skip to content

Commit

Permalink
perf: account alias lookup (#2191)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Jul 29, 2024
1 parent 320f30e commit 34bee0a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/ape/managers/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ class AccountManager(BaseManager):
my_accounts = accounts.load("dev")
"""

_alias_to_account_cache: dict[str, AccountAPI] = {}

@property
def default_sender(self) -> Optional[AccountAPI]:
return _DEFAULT_SENDERS[-1] if _DEFAULT_SENDERS else None
Expand Down Expand Up @@ -257,12 +259,15 @@ def load(self, alias: str) -> AccountAPI:
Returns:
:class:`~ape.api.accounts.AccountAPI`
"""

if alias == "":
raise ValueError("Cannot use empty string as alias!")

elif alias in self._alias_to_account_cache:
return self._alias_to_account_cache[alias]

for account in self:
if account.alias and account.alias == alias:
self._alias_to_account_cache[alias] = account
return account

raise KeyError(f"No account with alias '{alias}'.")
Expand Down
5 changes: 5 additions & 0 deletions tests/functional/test_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,3 +881,8 @@ def test_import_account_from_private_key_insecure_passphrase(delete_account_afte
with delete_account_after(simple_alias):
with pytest.warns(UserWarning, match="simple"):
import_account_from_private_key(simple_alias, "simple", PRIVATE_KEY)


def test_load(accounts, keyfile_account):
account = accounts.load(keyfile_account.alias)
assert account == keyfile_account

0 comments on commit 34bee0a

Please sign in to comment.