Skip to content

Commit 4294200

Browse files
committed
fix(address-apy): parenthesize DISTINCT ON blocks in portfolio UNION
Postgres rejects UNION between two SELECT DISTINCT ON ... ORDER BY blocks unless each is parenthesized. Wrap both weight-CTE branches so addressApy stops throwing 'syntax error at or near UNION'.
1 parent 6c751d1 commit 4294200

1 file changed

Lines changed: 20 additions & 16 deletions

File tree

src/server-extension/address-apy.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -152,23 +152,27 @@ export class AddressApyResolver {
152152
-- A fully-exited position's latest value is ~0, so it contributes ~0 weight.
153153
SELECT chain_id, product, usd_value
154154
FROM (
155-
SELECT DISTINCT ON (ay.chain_id, ay.arm)
156-
ay.chain_id, ay.arm AS product,
157-
(ay.value::numeric * ads.rate_usd / ${E18})::float8 AS usd_value
158-
FROM arm_address_yield ay
159-
JOIN arm_daily_stat ads
160-
ON ads.chain_id = ay.chain_id AND ads.address = ay.arm AND ads.date = ay.date
161-
WHERE ay.address = $1 AND ${chainFilter}
162-
ORDER BY ay.chain_id, ay.arm, ay.date DESC
155+
(
156+
SELECT DISTINCT ON (ay.chain_id, ay.arm)
157+
ay.chain_id, ay.arm AS product,
158+
(ay.value::numeric * ads.rate_usd / ${E18})::float8 AS usd_value
159+
FROM arm_address_yield ay
160+
JOIN arm_daily_stat ads
161+
ON ads.chain_id = ay.chain_id AND ads.address = ay.arm AND ads.date = ay.date
162+
WHERE ay.address = $1 AND ${chainFilter}
163+
ORDER BY ay.chain_id, ay.arm, ay.date DESC
164+
)
163165
UNION ALL
164-
SELECT DISTINCT ON (oy.chain_id, oy.otoken)
165-
oy.chain_id, oy.otoken,
166-
(oy.balance::numeric * ods.rate_usd::numeric / ${E36})::float8
167-
FROM o_token_address_yield oy
168-
JOIN o_token_daily_stat ods
169-
ON ods.chain_id = oy.chain_id AND ods.otoken = oy.otoken AND ods.date = oy.date
170-
WHERE oy.address = $1 AND ${chainFilter}
171-
ORDER BY oy.chain_id, oy.otoken, oy.date DESC
166+
(
167+
SELECT DISTINCT ON (oy.chain_id, oy.otoken)
168+
oy.chain_id, oy.otoken,
169+
(oy.balance::numeric * ods.rate_usd::numeric / ${E36})::float8
170+
FROM o_token_address_yield oy
171+
JOIN o_token_daily_stat ods
172+
ON ods.chain_id = oy.chain_id AND ods.otoken = oy.otoken AND ods.date = oy.date
173+
WHERE oy.address = $1 AND ${chainFilter}
174+
ORDER BY oy.chain_id, oy.otoken, oy.date DESC
175+
)
172176
) w
173177
)
174178
SELECT

0 commit comments

Comments
 (0)