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

Function parseTraderPosition

src/lib/smartMoney.ts:317–353  ·  view source on GitHub ↗
(p: TraderPositionApiEntry, redeemed: boolean)

Source from the content-addressed store, hash-verified

315}
316
317function parseTraderPosition(p: TraderPositionApiEntry, redeemed: boolean): TraderPosition {
318 if (redeemed) {
319 // Closed positions: API provides totalBought, realizedPnl, avgPrice, curPrice — no size/currentValue
320 const totalBought = parseFloat(String(p.totalBought || 0));
321 const avgPrice = parseFloat(String(p.avgPrice || p.averagePrice || 0));
322 const realizedPnl = parseFloat(String(p.realizedPnl || 0));
323 const cost = avgPrice > 0 ? totalBought * avgPrice : totalBought;
324 return {
325 conditionId: String(p.conditionId || p.market || ""),
326 title: String(p.title || p.marketTitle || p.question || ""),
327 outcome: String(p.outcome || ""),
328 size: totalBought,
329 avgPrice,
330 currentValue: cost + realizedPnl,
331 cashPnl: realizedPnl,
332 percentPnl: cost > 0 ? (realizedPnl / cost) * 100 : 0,
333 redeemed: true,
334 };
335 }
336 // Open positions: API provides cashPnl, percentPnl directly
337 const size = parseFloat(String(p.size || p.shares || 0));
338 const avgPrice = parseFloat(String(p.avgPrice || p.averagePrice || 0));
339 const currentValue = parseFloat(String(p.currentValue || p.value || 0));
340 const cashPnl = typeof p.cashPnl === "number" ? p.cashPnl : parseFloat(String(p.cashPnl || 0));
341 const percentPnl = typeof p.percentPnl === "number" ? p.percentPnl : parseFloat(String(p.percentPnl || 0));
342 return {
343 conditionId: String(p.conditionId || p.market || ""),
344 title: String(p.title || p.marketTitle || p.question || ""),
345 outcome: String(p.outcome || ""),
346 size,
347 avgPrice,
348 currentValue,
349 cashPnl,
350 percentPnl,
351 redeemed: false,
352 };
353}
354
355export async function fetchTraderPositions(wallet: string): Promise<TraderPosition[]> {
356 try {

Callers 1

fetchTraderPositionsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected