ApeWorX / ape-tokens

Tokens converter plugin for the Ape Framework
https://www.apeworx.io/
Apache License 2.0
10 stars 7 forks source link

Add query subclasses for querying token information [APE-1243] #33

Open fubuloubu opened 1 year ago

fubuloubu commented 1 year ago

Overview

There are lots of indexes out there that index specific token information such as totalSupply and balanceOf over ranges of block history, such as Etherscan. Define several subclasses of the query definition in https://github.com/ApeWorX/ape/issues/380 to define an easier way to source this information from these indexes.

Specification

Might look something like this:

from ape.api.query import ContractCallQuery
# ...or whatever the linked issue calls the base class for querying view calls

class TokenBalanceQuery(ContractCallQuery):
    token: Address
    account: Address
    from_block: int
    to_block: int

# Other queries such as `TokenSupplyQuery`, etc.

Also, to be most useful it would need to have the ability to use ORDER BY and LIMIT semantics to do queries like "get me the top 20 accounts over the last 1000 blocks for token X by % of total supply", something like:

from ape_tokens import tokens

usdc = tokens["USDC"]

top_20_accounts = usdc.balanceOf.query("*", order_by_result=True, limit=20, starting_block=-1000)
print(top_20_accounts / usdc.totalSupply.query(starting_block=-1000))

NOTE: Above is just a sketch, needs work

Dependencies

https://github.com/ApeWorX/ape/issues/380

fubuloubu commented 1 year ago

Thinking further on this, not sure how necessary the query subclasses are. I was thinking they can be used to upcast the more-general view call query into something that many indexers are already indexing, so they can more easily determine if they have it indexed. And if not, just convert it to it's base query so that it can be done the harder way if necessary