Keys in stores
Using keys in stores
We use store modules to aggregate the data in the underlying key-value storage. It is important to have a system for organizing your keys to be able to efficiently retrieve, filter and free them when needed.
In most cases, you will encode data into your keys into segmented parts, adding a prefix as namespace for example user
and <address>
joined together using a separator. Segments in a key are conventionally joined with :
as a separator.
Here are some examples,
Pool:{pool_address}:volumeUSD
-{pool_address}
pool total traded USD volumeToken:{token_addr}:volume
- total{token_addr}
token volume tradedUniswapDayData:{day_id}:volumeUSD
-{day_id}
daily USD trade volumePoolDayData:{day_id}:{pool_address}:{token_addr}:volumeToken1
- total{day_id}
daily volume of{token_addr}
token that went through a{pool_address}
pool in token1 equivalent
In the example of a counter store below, we increment transaction counters for different metrics that we could use in the downstream modules:
In the downstream modules consuming this store, you can query the store by key in get
mode. Or, an even more powerful approach would be to filter needed store deltas by segments. key
module of the substreams
crates offers several helper functions. Using these functions you can extract the first/last/nth segment from a key:
key
module also provides corresponding try_
methods that don't panic:
first_segment
&try_first_segment
last_segment
&try_last_segment
segment_at
&try_segment_at
For a full example see Uniswap V3 Substreams
Links
Last updated