Package Logo
coinranking_uniswap_v3
coinranking_uniswap_v3@v0.1.0
Total Downloads
5
Published
2 weeks ago
Network
mainnet mainnet
Publisher
User Avatar wouthoekstra

Readme

Uniswap V3 24-Hour Rolling Volume Substream

This Substreams package provides real-time 24-hour rolling volume tracking for Uniswap V3 pools on Ethereum mainnet.

Features

  • Tracks pool creation events
  • Captures all swap transactions
  • Calculates 24-hour rolling volumes using a circular buffer approach
  • Uses 5-minute aggregation periods for efficient storage
  • Prevents partial period data from affecting volume calculations

Architecture

The system uses three store modules:

  • store_completed_periods: Tracks the last completed 5-minute period for each pool
  • store_minute_volumes: Stores volume data in 5-minute buckets (288 buckets for 24 hours)
  • store_rolling_volumes: Maintains the current 24-hour rolling volume for each pool

Testing

Use the test script with various options:

# Run with default settings
./test.sh

# Test with swap activity and filtering
./test.sh -s 12400000 -e 12400010 -f

# Use UI output format
./test.sh --output ui

# Show help
./test.sh -h

Building

# Build the WASM module
cargo build --target wasm32-unknown-unknown --release

# Generate required protobuf files from imported packages
substreams protogen substreams.yaml --output-path src/generated

Packaging for Deployment

To prepare your Substreams for deployment:

# 1. Build the WASM module
cargo build --target wasm32-unknown-unknown --release

# 2. Generate required protobuf files (if not already present)
substreams protogen substreams.yaml --output-path src/generated

# 3. Create the SPKG package
substreams pack
# This creates: coinranking-uniswap-v3-v0.1.0.spkg

The SPKG file contains:

  • Compiled WASM module
  • Protobuf definitions
  • Substreams manifest
  • All dependencies

Running the Package

To test your packaged Substreams:

# Load JWT token from .env file
source ../../.env

# For testing: Run 100 blocks from a specific start
substreams run coinranking-uniswap-v3-v0.1.0.spkg map_uniswap_ticker_output \
  --substreams-api-token="$SUBSTREAMS_API_TOKEN" \
  --substreams-endpoint="mainnet.eth.streamingfast.io:443" \
  -s 22964000 \    # Start at specific block (overrides initialBlock in yaml)
  -t +100          # Stop after 100 blocks

# For production: Start from 24+ hours ago for complete rolling volumes
substreams run coinranking-uniswap-v3-v0.1.0.spkg map_uniswap_ticker_output \
  --substreams-api-token="$SUBSTREAMS_API_TOKEN" \
  --substreams-endpoint="mainnet.eth.streamingfast.io:443" \
  -s -7500 \       # ~25 hours ago (7200 blocks/day + margin)
  -t 0             # Stream indefinitely

Understanding Block Numbers

  • Ethereum block time: ~12 seconds
  • Blocks per day: ~7,200 (86,400 seconds ÷ 12)
  • Recommended start: -7500 (25 hours ago)
    • Provides margin for block time variance
    • Ensures complete 24h rolling volume data
    • Allows proper warm-up of volume calculations

The .env file should contain:

SUBSTREAMS_API_TOKEN=your-jwt-token-here

Publishing

Once tested, you can publish your package:

  1. Upload the .spkg file to GitHub releases, IPFS, or another public location
  2. Share the URL for others to use in their substreams.yaml:
    imports:
      coinranking: https://github.com/coinranking/substreams/releases/download/v0.1.0/coinranking-uniswap-v3-v0.1.0.spkg
    

Output Format

The main output includes:

  • pools_created: New Uniswap V3 pools
  • tokens: Token information for pools
  • swaps: Individual swap events
  • rolling_volumes: 24-hour rolling volumes per pool

Development

Generated Files

The src/generated/ directory contains protobuf-generated Rust code from the imported Uniswap substreams package. These files are:

  • Not tracked in git (see .gitignore)
  • Required for compilation but should not be edited
  • Generated using the substreams protogen command

To regenerate these files:

# Generate Rust bindings from the imported Uniswap package
substreams protogen substreams.yaml --output-path src/generated

# Note: Only uniswap.types.v1.rs is actually used by our code

This command extracts and generates Rust code from the imported Uniswap SPKG defined in substreams.yaml.

Code Quality

This project uses Rust's standard formatting and linting tools:

# Format code
cargo fmt

# Check for common mistakes and improvements
cargo clippy

# Run before committing
cargo fmt && cargo clippy && cargo build --target wasm32-unknown-unknown --release

Contributing

  1. Always run cargo fmt before committing
  2. Fix all clippy warnings (except those in generated code)
  3. Ensure the WASM build succeeds
  4. Update tests if modifying functionality

Documentation

Modules

Maps icon
Maps

map
map_uniswap_ticker_output

a61a1efd8eb7e1368380ffb48ce78967fb3d2219
map map_uniswap_ticker_output (
blocksf.ethereum.type.v2.Block
store_rolling_deltasGET<add,bigdecimal>
)  -> dex.common.v1.DexOutput
substreams gui coinranking-uniswap-v3@v0.1.0 map_uniswap_ticker_output

map
uniswap:uni_v0_2_9:map_pools_created

3a5e4d92aeccd801140b7a330e3a1c0407bae58d
map uniswap:uni_v0_2_9:map_pools_created (
)  -> uniswap.types.v1.Pools

This module will loop over block transactions and detect pools created events. Once the pool created events have been detected, Pools structs will be emitted out of the module.

Try with

substreams gui substreams.yaml map_pools_created -t +1000
substreams gui coinranking-uniswap-v3@v0.1.0 uniswap:uni_v0_2_9:map_pools_created

map
uniswap:uni_v0_2_9:map_extract_data_types

9daec64c4ffe5d1cb38272f14d656b87928d2e28

Chunky module which emits multiple types of Events. Looping over every transaction to filter out Events. For some events, we have to loop over StorageChanges to be able to fetch certain changes such as liquidity increases and decreases. For more information on what the Event contains, check proto/uniswap/v1/uniswap.proto.

Try with

substreams gui substreams.yaml map_extract_data_types -t +1000
substreams gui coinranking-uniswap-v3@v0.1.0 uniswap:uni_v0_2_9:map_extract_data_types
Stores icon
Stores

store
store_period_volumes

b9444b942a6581315a6d841bc0d585e3c1a76aa2
substreams gui coinranking-uniswap-v3@v0.1.0 store_period_volumes

store
store_rolling_deltas

70bc1deaae266bbb85923b6f508ad4afc4eaa39c
substreams gui coinranking-uniswap-v3@v0.1.0 store_rolling_deltas

store
uniswap:uni_v0_2_9:store_pools_created

3ec75d75eea4b8dfd8b515e09dca992a14f059ed
store <set,uniswap.types.v1.Pool> uniswap:uni_v0_2_9:store_pools_created (
)

This module stores the Pools emitted by the map_pools_created module. Dynamic data sources pattern for Uniswap v3 pools.

substreams gui coinranking-uniswap-v3@v0.1.0 uniswap:uni_v0_2_9:store_pools_created
Protobuf

Protobuf Docs Explorer

uniswap.types.v1
dex.common.v1