fix(twap): alias executed-totals aggregates so additionalData stores all three fields - #122
Merged
Merged
Conversation
…res all three fields Ponder's context.db.sql maps raw-SQL result rows positionally via Object.values(row). The three unaliased coalesce(sum(...)) columns all came back named "coalesce", collapsed into one key in the row object, and the values shifted: the executedFee sum was stored under executedSellAmount and the other two fields were dropped from the JSON entirely (9,830 of 9,834 TWAP parents on the deployed DB show the one-key shape, and the stored value matches the children's fee sum exactly). Unique .as() aliases keep the column names distinct so the positional mapping is stable. Unit tests can't catch this (they mock the DB and vanilla drizzle drivers use array row mode, which is immune), hence the warning comment.
This was referenced Jul 23, 2026
Contributor
Author
Smoke test — indexer run from a 1-day-old start blockRan Every TWAP parent's stored
All 9 parents carry the full three-key JSON (before the fix, production had 9,830 one-key rows), fee totals are non-zero wherever children were fulfilled, and Sample GraphQL output (same query shape as the original bug report): {
"executedFee": "3458674081352794806",
"executedBuyAmount": "334666289225195989846",
"executedSellAmount": "1652961917640625000000",
"status": "fulfilled",
"conditionalOrderGenerator": {
"orderType": "TWAP",
"additionalData": {
"executedSellAmount": "105789562729000000000000",
"executedBuyAmount": "21397996081206960401538",
"executedFee": "354479378307462150268"
}
}
}The start-block changes were test-only and are not part of any PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
TWAP parents'
additionalDataon the deployed indexer is wrong: 9,830 of 9,834 TWAP generators store a one-key JSON like{"executedSellAmount":"0"}—executedBuyAmountandexecutedFeeare missing, and the value stored underexecutedSellAmountis actually the children's fee sum (verified against the production DB: the stored value matchesSUM(executed_fee)exactly on every non-zero row).Root cause
The aggregate select in
refreshTwapExecutedTotalsused unaliased raw sql fragments:drizzle does not auto-alias raw fragments, so Postgres names all three result columns
coalesce. Ponder'scontext.db.sqlproxy (ponder 0.16,indexing-store/index.js) executes raw queries in object row mode and feeds drizzle's positional mapper viarows.map((row) => Object.values(row))— the duplicate keys collapse into one, leaving[generatorId, feeSum]. Positionally that assigns the fee sum toexecutedSellAmountand leaves the other twoundefined, whichJSON.stringifythen drops.This is invisible to the unit tests (they mock the DB) and to vanilla drizzle drivers (node-postgres/pglite use array row mode) — I reproduced it only through Ponder's wrapper, which is why a warning comment now sits on the query.
Fix
Give each aggregate a unique
.as()alias so the column names stay distinct and the positional mapping is stable.No data repair is needed: the deploy flow drops the app schema and reindexes, and every reindex recomputes
additionalDatafrom the discrete orders.Verification
pnpm typecheck,pnpm lint,pnpm testgreen.