Skip to content

Conversation

@mentatbot
Copy link
Contributor

@mentatbot mentatbot bot commented May 31, 2025

This implementation calculates TWAMM volume using the sale_rate * time_elapsed
formula for periods with constant sale rates, as requested in issue #7.

Key features:

  • Tracks sale rate changes from twamm_sale_rate_deltas_materialized view
  • Calculates volume for periods between consecutive rate changes
  • Groups results by hour and token for hourly_volume_by_token table
  • Subtracts TWAMM swap volume to avoid double counting as specified
  • Handles both token0 and token1 sale rates properly

The calculation uses existing TWAMM infrastructure including virtual order
executions and sale rate delta tracking to provide accurate volume metrics.

Closes #7


🤖 See my steps and cost here

  • Wake on any new activity.

This implementation calculates TWAMM volume using the sale_rate * time_elapsed
formula for periods with constant sale rates, as requested in issue # 7.

Key features:
- Tracks sale rate changes from twamm_sale_rate_deltas_materialized view
- Calculates volume for periods between consecutive rate changes
- Groups results by hour and token for hourly_volume_by_token table
- Subtracts TWAMM swap volume to avoid double counting as specified
- Handles both token0 and token1 sale rates properly

The calculation uses existing TWAMM infrastructure including virtual order
executions and sale rate delta tracking to provide accurate volume metrics.

Closes # 7

Co-authored-by: moodysalem <7897876+moodysalem@users.noreply.github.com>
@mentatbot
Copy link
Contributor Author

mentatbot bot commented May 31, 2025

I've implemented the TWAMM volume calculation as requested in issue #7. Here's what the implementation does:

Overview

  • Calculates TWAMM volume using sale_rate * time_elapsed for periods with constant sale rates
  • Adds this volume to the existing hourly_volume_by_token table
  • Subtracts TWAMM swap volume to avoid double counting

Algorithm

  1. Track sale rate changes: Uses twamm_sale_rate_deltas_materialized to identify when sale rates change (order starts/ends)
  2. Include virtual executions: Incorporates twamm_virtual_order_executions as baseline reset points
  3. Calculate cumulative rates: Builds up cumulative sale rates for each time period
  4. Hourly volume calculation: For each hour, calculates sale_rate * time_elapsed for active periods
  5. Subtract TWAMM swaps: Removes direct swap volume where locker = TWAMM_ADDRESS to prevent double counting
  6. Update volume table: Adds net TWAMM volume to hourly_volume_by_token

Key Features

  • Handles both token0 and token1 sale rates properly
  • Works with existing TWAMM infrastructure and materialized views
  • Respects hourly boundaries for volume aggregation
  • Prevents negative volumes with GREATEST(0, calculated - actual_swaps)

The implementation integrates into the existing refreshAnalyticalTables method and will run automatically as part of the regular analytical table refresh process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

index twamm volume into hourly_volume table

1 participant