MCPcopy Create free account
hub / github.com/AmazingAng/PolyWorld / processEvents

Function processEvents

src/lib/polymarket.ts:101–202  ·  view source on GitHub ↗
(events: PolymarketEvent[])

Source from the content-addressed store, hash-verified

99const previousPrices = new Map<string, number>();
100
101export function processEvents(events: PolymarketEvent[]): {
102 mapped: ProcessedMarket[];
103 unmapped: ProcessedMarket[];
104} {
105 const mapped: ProcessedMarket[] = [];
106 const unmapped: ProcessedMarket[] = [];
107
108 for (const event of events) {
109 const vol = parseFloat(String(event.volume || event.volume_num || 0));
110 if (vol < 500) continue;
111
112 const category = detectCategory(event);
113
114 const markets = event.markets || [];
115
116 // Find the highest-probability sub-market (not just the first one)
117 let primary = markets[0];
118 let prob: number | null = null;
119 for (const mk of markets) {
120 try {
121 const raw = mk.outcomePrices;
122 if (!raw) continue;
123 const p = Array.isArray(raw) ? raw : JSON.parse(raw as string);
124 const val = parseFloat(p[0]);
125 if (!isNaN(val) && (prob === null || val > prob)) {
126 prob = val;
127 primary = mk;
128 }
129 } catch {
130 // Skip malformed outcomePrices
131 }
132 }
133
134 // Fallback to event-level prices if no sub-market had valid prices
135 if (prob === null) {
136 try {
137 const prices = (event as unknown as Record<string, unknown>).outcomePrices;
138 if (prices) {
139 const p = Array.isArray(prices) ? prices : JSON.parse(prices as string);
140 prob = parseFloat(p[0]);
141 }
142 } catch {
143 // Skip malformed outcomePrices
144 }
145 }
146
147 const marketId = primary?.id || event.id;
148 let change: number | null = null;
149 if (primary?.oneDayPriceChange !== undefined) {
150 change = parseFloat(String(primary.oneDayPriceChange));
151 } else if (event.oneDayPriceChange !== undefined) {
152 change = parseFloat(String(event.oneDayPriceChange));
153 }
154
155 let recentChange: number | null = null;
156 if (prob !== null && previousPrices.has(marketId)) {
157 recentChange = prob - previousPrices.get(marketId)!;
158 }

Callers 2

runSyncFunction · 0.90
HomeFunction · 0.90

Calls 3

detectCategoryFunction · 0.90
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected