Skip to content

Read owner/cex-dex exchange data from precomputed daily tables - #5477

Open
apozdeyev wants to merge 1 commit into
masterfrom
agent/use-precomputed-exchange-tables
Open

apozdeyev wants to merge 1 commit into
masterfrom
agent/use-precomputed-exchange-tables

Conversation

@apozdeyev

@apozdeyev apozdeyev commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Sanbase.Clickhouse.Exchanges.top_exchanges_by_balance and Sanbase.Clickhouse.ExchangeAddress.exchange_addresses/.exchange_addresses_for_exchange each re-aggregated current_label_addresses live (twice per call) to intersect owner labels with the centralized_exchange/decentralized_exchange marker label - same expensive join duplicated in 3 places.
  • Switches all 3 to read from two tables precomputed once a day by the labeled-entities-names Airflow DAG:
    • labeled_entities_cex_or_dex (santiment/clickhouse-tables#2465, santiment/db-schema#64)
    • exchange_addresses_daily
  • Removed exchange_type_filter/1 in exchange_address.ex, now dead code.
  • Existing tests mock Sanbase.ClickhouseRepo.query/3 / ExchangeAddress at the function level (not SQL text), so no test changes needed.

Behavior change

  • Data freshness goes from effectively live (only bounded by the 10-min GraphQL cache_resolve TTL) to at most ~24h stale (daily DAG run at 6AM).
  • Known pre-existing bug, not fixed here: exchange_address.ex passes blockchain "xrp" (lowercased) to these queries, but the actual ClickHouse blockchain column value is "ripple" - so xrp-related calls return empty both before and after this change.

Dependencies / deploy order

  1. santiment/db-schema#64 (creates the 2 tables)
  2. santiment/clickhouse-tables#2465 (populates them) - should run at least once
  3. this PR

Test plan

  • Confirm both precomputed tables have rows on stage before deploying
  • topExchangesByBalance(slug: "bitcoin"), allExchanges(slug: "ethereum") return the same shape as before

Summary by CodeRabbit

  • Updates
    • Exchange address listings now use daily exchange address data and continue to support blockchain and owner filters.
    • Top-exchange balance results now use exchange classifications associated with the selected blockchain.

top_exchanges_by_balance and the two ExchangeAddress address-listing
queries each re-aggregated current_label_addresses live (twice per
call) to intersect owner labels with the centralized_exchange/
decentralized_exchange marker label - the same expensive join
duplicated in 3 places. Switches all 3 to read from
labeled_entities_cex_or_dex / exchange_addresses_daily, two tables
precomputed once a day by the labeled-entities-names Airflow DAG
(santiment/clickhouse-tables#2465, santiment/db-schema#64).

Data is now at most ~24h stale instead of live (bounded before only by
the 10-minute GraphQL cache). Deploy only after the DAG has populated
both tables at least once, or these resolvers will return empty.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Exchange address queries now read from exchange_addresses_daily. The top-exchange balance query now uses labeled_entities_cex_or_dex for label IDs and CEX/DEX classifications.

Changes: Exchange queries

Layer / File(s) Summary
Exchange address queries
lib/sanbase/clickhouse/exchange_address.ex
Both queries read from exchange_addresses_daily. The general query selects address, label, and owner by blockchain. The owner-specific query selects distinct addresses by blockchain and owner. The queries retain the limit and lowercase blockchain parameter. The exchange_type_filter/1 helper was removed.
Top-exchange classification
lib/sanbase/clickhouse/exchanges/exchanges.ex
The query gets exchange label IDs and CEX/DEX classifications from labeled_entities_cex_or_dex. It uses those IDs to filter metrics and joins the CTE to return classifications.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Merge Risk: Minimal · up to 8a032

Exchange queries now read from precomputed daily tables, so results can be up to about a day stale. Deploy only after the schema change and the first DAG run have populated the tables. No concrete defect was found in the code.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: the owner and CEX/DEX exchange reads now come from precomputed daily tables. The raw summary shows both exchange-address queries reading from exchange_addresses_daily …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: the owner and CEX/DEX exchange reads now come from precomputed daily tables. The raw summary shows both exchange-address queries reading from exchange_addresses_daily and the top-exchanges query reading from labeled_entities_cex_or_dex. The title is specific and matches the changeset and the PR objectives.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4d6439d1-0b55-4ffb-869a-1be021023521
Commits

Reviewing files that changed from the base of the PR and between 47f0235 and 8a03271.

Files selected for processing: 2

  • lib/sanbase/clickhouse/exchange_address.ex
  • lib/sanbase/clickhouse/exchanges/exchanges.ex

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

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.

1 participant