-
Notifications
You must be signed in to change notification settings - Fork 160
fix: cache getAcceptsAddresses #431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,28 +13,24 @@ const getOverallSellerStatisticsUncached = async ( | |
| input: z.infer<typeof sellerStatisticsInputSchema> | ||
| ) => { | ||
| const { startDate, endDate } = getTimeRangeFromTimeframe(input.timeframe); | ||
|
|
||
| // Use the recipient_first_transaction materialized view for fast lookups | ||
| // This avoids scanning all hypertable chunks to compute MIN(block_timestamp) | ||
| const sql = Prisma.sql` | ||
| WITH seller_first_transactions AS ( | ||
| SELECT | ||
| recipient, | ||
| MIN(block_timestamp) AS first_transaction_date | ||
| FROM "TransferEvent" | ||
| GROUP BY recipient | ||
| ), | ||
| filtered_transfers AS ( | ||
| WITH filtered_transfers AS ( | ||
| SELECT DISTINCT t.recipient | ||
| FROM "TransferEvent" t | ||
| ${transfersWhereClause(input)} | ||
| ) | ||
| SELECT | ||
| COUNT(DISTINCT ft.recipient)::int AS total_sellers, | ||
| COUNT(DISTINCT CASE | ||
| WHEN sft.first_transaction_date >= ${startDate ?? Prisma.sql`'1970-01-01'::timestamp`} | ||
| AND sft.first_transaction_date <= ${endDate ?? Prisma.sql`NOW()`} | ||
| WHEN rft.first_transaction_date >= ${startDate ?? Prisma.sql`'1970-01-01'::timestamp`} | ||
| AND rft.first_transaction_date <= ${endDate ?? Prisma.sql`NOW()`} | ||
| THEN ft.recipient | ||
| END)::int AS new_sellers | ||
| FROM filtered_transfers ft | ||
| LEFT JOIN seller_first_transactions sft ON ft.recipient = sft.recipient | ||
| LEFT JOIN recipient_first_transaction rft ON ft.recipient = rft.recipient | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code references a materialized view View Details📝 Patch Detailsdiff --git a/packages/internal/databases/transfers/prisma/migrations/20260105000000_recipient_first_transaction/migration.sql b/packages/internal/databases/transfers/prisma/migrations/20260105000000_recipient_first_transaction/migration.sql
new file mode 100644
index 00000000..45829f39
--- /dev/null
+++ b/packages/internal/databases/transfers/prisma/migrations/20260105000000_recipient_first_transaction/migration.sql
@@ -0,0 +1,11 @@
+-- CreateMaterializedView
+CREATE MATERIALIZED VIEW recipient_first_transaction AS
+SELECT
+ recipient,
+ MIN(block_timestamp) AS first_transaction_date
+FROM "TransferEvent"
+GROUP BY recipient;
+
+-- CreateIndex
+CREATE UNIQUE INDEX recipient_first_transaction_idx
+ON recipient_first_transaction (recipient);
AnalysisMissing materialized view
|
||
| `; | ||
|
|
||
| const result = await queryRaw( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code references a materialized view
recipient_first_transactionthat doesn't exist in the database, causing this query to fail at runtime.View Details
📝 Patch Details
Analysis
Missing materialized view
recipient_first_transactionreferenced in seller statistics queriesWhat fails: The seller statistics functions
getBucketedSellerStatistics()inapps/scan/src/services/transfers/sellers/stats/bucketed.ts(line 66) andgetOverallSellerStatistics()inapps/scan/src/services/transfers/sellers/stats/overall.ts(line 33) reference a materialized viewrecipient_first_transactionthat does not exist in the database, causing runtime SQL errors.How to reproduce:
Result: Runtime error - PostgreSQL cannot execute the query due to missing materialized view
Expected: The materialized view should exist and contain recipient addresses with their first transaction timestamps
Root cause: Commit
2eb749c5(use MV for first txs) refactored seller statistics from using inline CTEseller_first_transactionsto a materialized viewrecipient_first_transaction, but the corresponding database migration was never created.Fix: Created migration
packages/internal/databases/transfers/prisma/migrations/20260105182134_add_recipient_first_transaction_mv/migration.sqlthat creates the missing materialized view with proper indexing for query performance.