Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nouns-nft-price-bigint.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@anticapture/api": patch
---

Fix `/token/historical-data` returning 500 for NFT-priced DAOs (Nouns, Lil Nouns): the rolling-average SQL emitted decimal strings that crashed the wei-to-USD conversion, which in turn tripped the gateway circuit breaker and made the whole DAO unavailable.
10 changes: 7 additions & 3 deletions apps/api/src/repositories/token/nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ export class NFTPriceRepository {
): Promise<TokenHistoricalPriceResponse> {
return await this.db
.select({
// FLOOR keeps the value an integer wei string: AVG over NUMERIC emits
// decimal text (e.g. "200.5000000000000000"), which BigInt() rejects.
price: sql<string>`
CAST(
AVG(CAST(${tokenPrice.price} AS NUMERIC)) OVER (
ORDER BY ${tokenPrice.timestamp}
RANGE BETWEEN 2505600 PRECEDING AND CURRENT ROW
FLOOR(
AVG(CAST(${tokenPrice.price} AS NUMERIC)) OVER (
ORDER BY ${tokenPrice.timestamp}
RANGE BETWEEN 2505600 PRECEDING AND CURRENT ROW
)
) AS TEXT
)
`,
Expand Down
20 changes: 20 additions & 0 deletions apps/api/src/repositories/token/nft.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@ describe("NFTPriceRepository", () => {

expect(result).toEqual([]);
});

it("should return integer wei strings convertible to BigInt", async () => {
// AVG() over NUMERIC yields decimal text like "200.50000000000000000000";
// consumers convert this straight to BigInt, which rejects decimals.
await db
.insert(tokenPrice)
.values([
createTokenPrice({ timestamp: 1700000000n, price: 100n }),
createTokenPrice({ timestamp: 1700000100n, price: 301n }),
]);

const result = await repository.getHistoricalNFTPrice(10, 0);

expect(result).toHaveLength(2);
for (const { price } of result) {
expect(() => BigInt(price)).not.toThrow();
}
expect(result[0]!.price).toBe("200");
expect(result[1]!.price).toBe("100");
});
});

describe("getTokenPrice", () => {
Expand Down
Loading