Data can also be queried based on specific sort of a field path. This can be helpful if you want get data such as the top performing pools based on revenue, etc.
Click for Interactive Documentation
Clicking this button will enable editing and execution of the code-blocks on this page. Learn more here.
from subgrounds import Subgrounds sg = Subgrounds() curve = sg.load_subgraph( "https://api.thegraph.com/subgraphs/name/messari/curve-finance-ethereum") pool = curve.LiquidtyPool # shorthand for examples snapshot = curve.DailySnapshot
To sort, we define the
orderBy argument on a field path:
sg.query_df( curve.Query.liquidityPool(orderBy=pool.cumulativeTotalRevenueUSD) )
By default, the sorting method is ascending. We can change it to descending (providing us with the highest performing pools) via
sg.query_df( curve.Query.liquidityPool(orderBy=pool.cumulativeTotalRevenueUSD, orderDirection="desc") )
Since nested fields can also have arguments, we can layer multiple sortings on top of each other:
top_pools = curve.Query.liquidityPool( first=4, orderBy=pool.cumulativeTotalRevenueUSD, orderDirection="desc", ) sg.query_df( top_pools.dailySnapshots( first=3, orderBy=snapshot.dailyVolumeUSD, orderDirection="desc", ) )
Adding more complexity to your query will lead to longer query times as the indexer has to perform multiple internal database queries to construct the data as you request. If you hit the natural timeout of 30s, you can try running the query again as the indexer will continue to process your query in the background, caching the value for future queries.
Sorting by Nested Fields#
We aren't just limited by sorting only on the top-level fields — we can sort by nested fields. This is different than layering since we are ordering the main list of rows based upon a nested value (usually within an object).
Nested filtering usually only works at a maximum depth of 2 and may not work across older subgraphs.
sg.query_df( curve.Query.liquidityPool( orderBy=pool.outputToken.lastPriceUSD, orderDirection="desc", ) )