Skip to content

Commit

Permalink
refactor: apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: helloibis <108302637+helloibis@users.noreply.github.com>
  • Loading branch information
fubuloubu and helloibis committed Mar 21, 2023
1 parent b97947e commit 3f60320
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 33 deletions.
36 changes: 8 additions & 28 deletions docs/userguides/data.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`
Expand Down Expand Up @@ -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` |
8 changes: 4 additions & 4 deletions src/ape/api/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/ape/managers/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)])`)
Expand Down

0 comments on commit 3f60320

Please sign in to comment.