From a016c664792583295d26ea2c38ff5577e0c1bd72 Mon Sep 17 00:00:00 2001 From: moose-code Date: Mon, 13 Jul 2026 15:45:31 +0100 Subject: [PATCH] fix: always link V2 fills to their market by tokenId MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit V2Market.id IS the tokenId, so set market_id unconditionally instead of leaving fills permanently unattributed when the Gamma metadata fetch fails or returns null — the join resolves once metadata lands via a later fill of the same token. Mirrors enviodev/polymarket-v2-indexer. Co-Authored-By: Claude Fable 5 --- src/handlers/v2/CTFExchangeV2.ts | 49 +++++++++++++++++--------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/src/handlers/v2/CTFExchangeV2.ts b/src/handlers/v2/CTFExchangeV2.ts index cfa72c4..8fa4a04 100644 --- a/src/handlers/v2/CTFExchangeV2.ts +++ b/src/handlers/v2/CTFExchangeV2.ts @@ -41,32 +41,35 @@ const getOrInitStats = async (context: any, id: string) => const ensureMarket = async (context: any, tokenId: bigint) => { const tokenIdStr = tokenId.toString(); const existing = await context.V2Market.get(tokenIdStr); - if (existing) return tokenIdStr; - - try { - const meta = await context.effect(getMarketMetadata, tokenIdStr); - if (meta) { - context.V2Market.set({ - id: tokenIdStr, - question: meta.question, - slug: meta.slug, - outcomes: meta.outcomes, - outcomePrices: meta.outcomePrices, - description: meta.description, - image: meta.image, - startDate: meta.startDate, - endDate: meta.endDate, - conditionId: meta.conditionId, - }); - return tokenIdStr; + if (!existing) { + try { + const meta = await context.effect(getMarketMetadata, tokenIdStr); + if (meta) { + context.V2Market.set({ + id: tokenIdStr, + question: meta.question, + slug: meta.slug, + outcomes: meta.outcomes, + outcomePrices: meta.outcomePrices, + description: meta.description, + image: meta.image, + startDate: meta.startDate, + endDate: meta.endDate, + conditionId: meta.conditionId, + }); + } + } catch (e) { + context.log.warn( + `Failed to fetch V2 market metadata for tokenId ${tokenIdStr}: ${e}`, + ); } - } catch (e) { - context.log.warn( - `Failed to fetch V2 market metadata for tokenId ${tokenIdStr}: ${e}`, - ); } - return undefined; + // V2Market.id IS the tokenId, so always link fills by tokenId: the join + // resolves as soon as metadata lands (a later fill of the same token + // retries the fetch). Returning undefined on fetch failure would leave + // the fill permanently unattributed under Gamma rate-limit pressure. + return tokenIdStr; }; // ── Trading ────────────────────────────────────────────────────────