diff --git a/src/angel/market.ts b/src/angel/market.ts index 6873f1d..3842c30 100644 --- a/src/angel/market.ts +++ b/src/angel/market.ts @@ -51,33 +51,34 @@ function loadFromCache(): MarketQuote[] { async function fetchFromAngel(): Promise { const headers = await getAuthHeaders(); - // All tokens hardcoded - build token map directly + // Build exchange->tokens map and a tradingSymbol->key lookup const tokenMap: Record = {}; - const tokenIndex: Record = {}; + // We match by tradingSymbol because exchangeType is sometimes undefined in response + const symToKey: Record = { + BSX: SENSEX, // BSE token 1 + USDINR: USDINR, // CDS token 1 + NIFTY: NIFTY50, + BANKNIFTY: BANKNIFTY, + INDIA VIX: INDIAVIX, + }; + for (const idx of INDEX_TOKENS) { if (!tokenMap[idx.exchange]) tokenMap[idx.exchange] = []; tokenMap[idx.exchange].push(idx.token); - tokenIndex[idx.token] = idx.key; } - const r = await axios.post(BASE + '/rest/secure/angelbroking/market/v1/quote/', - { mode: 'FULL', exchangeTokens: tokenMap }, { headers, timeout: 8000 }); + const r = await axios.post( + BASE + /rest/secure/angelbroking/market/v1/quote/, + { mode: FULL, exchangeTokens: tokenMap }, + { headers, timeout: 8000 } + ); const fetched: any[] = r.data?.data?.fetched ?? []; if (!fetched.length) return []; - const LABELS: Record = { - SENSEX: { label: 'SENSEX', unit: 'pts' }, - NIFTY50: { label: 'NIFTY 50', unit: 'pts' }, - BANKNIFTY: { label: 'BANK NIFTY', unit: 'pts' }, - INDIAVIX: { label: 'INDIA VIX', unit: '%' }, - CRUDEOIL: { label: 'Crude Oil', unit: '\u20b9/bbl' }, - USDINR: { label: 'INR/USD', unit: '\u20b9' }, - }; - return fetched.map((q: any) => { - const key = tokenIndex[q.symbolToken] ?? q.tradingSymbol; - const meta = LABELS[key] ?? { label: q.tradingSymbol, unit: '' }; + const key = symToKey[q.tradingSymbol] ?? q.tradingSymbol; + const meta = LABELS[key] ?? { label: q.tradingSymbol, unit: }; const ltp = parseFloat(q.ltp) || 0; const close = parseFloat(q.close) || 0; const change = parseFloat(q.netChange) || (ltp - close); @@ -85,7 +86,6 @@ async function fetchFromAngel(): Promise { return { key, label: meta.label, price: ltp, change, changePct, unit: meta.unit, stale: false }; }); } - export async function fetchMarketData(): Promise { try { const live = await fetchFromAngel();