From 3f603200409c138c53efcd1f6deec829560413fe Mon Sep 17 00:00:00 2001 From: El De-dog-lo <3859395+fubuloubu@users.noreply.github.com> Date: Mon, 20 Mar 2023 21:06:17 -0400 Subject: [PATCH] refactor: apply suggestions from code review Co-authored-by: helloibis <108302637+helloibis@users.noreply.github.com> --- docs/userguides/data.md | 36 ++++++++---------------------------- src/ape/api/query.py | 8 ++++---- src/ape/managers/chain.py | 2 +- 3 files changed, 13 insertions(+), 33 deletions(-) diff --git a/docs/userguides/data.md b/docs/userguides/data.md index a73aae34f0..48d80a4619 100644 --- a/docs/userguides/data.md +++ b/docs/userguides/data.md @@ -33,13 +33,13 @@ In [3]: acct.history.query("total_fees_paid").sum() # Sum of ether paid for fee ## Getting Contract Event Data -On a deployed contract, you can query event history: +On a deployed contract, you can query event history. For example, we have a contract with a `FooHappened` event that you want to query from. This is how you would query the args from an event: ```python -contract_instance.FooHappened.query("*", start_block=-1) +In [1]: df = contract_instance.FooHappened.query("*", start_block=-1) ``` where `contract_instance` is the return value of `owner.deploy(MyContract)` @@ -69,30 +69,10 @@ ape cache init --network ethereum:mainnet This creates a SQLite database file in ape's data folder inside your home directory. You can query the cache database directly, for debugging purposes. -For example, to get block data, you would query the `blocks` table: +The cache database has the following tables: -```bash -ape cache query --network ethereum:mainnet:infura "SELECT * FROM blocks" -``` - -Which will return something like this: - -```bash - hash num_transactions number parent_hash size timestamp gas_limit gas_used base_fee difficulty total_difficulty -0 b'\xd4\xe5g@\xf8v\xae\xf8\xc0\x10\xb8j@\xd5\xf... 0 0 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00... 540 0 5000 0 None 17179869184 17179869184 -1 b'\x88\xe9mE7\xbe\xa4\xd9\xc0]\x12T\x99\x07\xb... 0 1 b'\xd4\xe5g@\xf8v\xae\xf8\xc0\x10\xb8j@\xd5\xf... 537 1438269988 5000 0 None 17171480576 34351349760 -2 b'\xb4\x95\xa1\xd7\xe6f1R\xae\x92p\x8d\xa4\x84... 0 2 b'\x88\xe9mE7\xbe\xa4\xd9\xc0]\x12T\x99\x07\xb... 544 1438270017 5000 0 None 17163096064 51514445824 -... -``` - -Similarly, to get transaction data, you would query the `transactions` table: - -```bash -ape cache query --network ethereum:mainnet:infura "SELECT * FROM transactions" -``` - -Finally, to query cached contract events you would query the `contract_events` table: - -```bash -ape cache query --network ethereum:mainnet:infura "SELECT * FROM contract_events" -``` +| Table Name | Dataclass base | +| ----------------- | -------------- | +| `blocks` | `BlockAPI` | +| `transactions` | `ReceiptAPI` | +| `contract_events` | `ContractLog` | diff --git a/src/ape/api/query.py b/src/ape/api/query.py index 47a6136022..c154c599a2 100644 --- a/src/ape/api/query.py +++ b/src/ape/api/query.py @@ -38,11 +38,11 @@ def _all_columns(Model: Type[BaseInterfaceModel]) -> Set[str]: # NOTE: Iterate down the series of subclasses of `Model` (e.g. Block and BlockAPI) # and get all of the public property methods of each class (which are valid columns) columns |= { - field + field_name for cls in Model.__mro__ - if issubclass(cls, BaseInterfaceModel) and cls != BaseInterfaceModel - for field in vars(cls) - if not field.startswith("_") and isinstance(vars(cls)[field], (property, cached_property)) + if issubclass(cls, BaseInterfaceModel) and cls is not BaseInterfaceModel + for field_name, field in vars(cls).items() + if not field_name.startswith("_") and isinstance(field, (property, cached_property)) } # TODO: Remove once `ReceiptAPI` fields cleaned up for better processing diff --git a/src/ape/managers/chain.py b/src/ape/managers/chain.py index b17ac5bff0..acd8e93833 100644 --- a/src/ape/managers/chain.py +++ b/src/ape/managers/chain.py @@ -416,7 +416,7 @@ def outgoing(self) -> Iterator[ReceiptAPI]: """ start_nonce = 0 - stop_nonce = self.__len__() - 1 # just to cache this value + stop_nonce = len(self) - 1 # just to cache this value # TODO: Add ephemeral network sessional history to `ape-cache` instead, # and remove this (replace with `yield from iter(self[:len(self)])`)