fix(api): prefer paper alpaca keys for market data
This commit is contained in:
parent
351412423f
commit
adfadd824b
@ -151,7 +151,7 @@ interface AlpacaCredentials {
|
|||||||
secret: string;
|
secret: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getUserAlpacaCredentials(userId: string): Promise<AlpacaCredentials> {
|
async function getUserExecutionAlpacaCredentials(userId: string): Promise<AlpacaCredentials> {
|
||||||
const profile = await getCurrentUserProfile(userId);
|
const profile = await getCurrentUserProfile(userId);
|
||||||
const key = String(config.PAPER_TRADING ? profile.ALPACA_API_KEY || '' : profile.REAL_ALPACA_API_KEY || '').trim();
|
const key = String(config.PAPER_TRADING ? profile.ALPACA_API_KEY || '' : profile.REAL_ALPACA_API_KEY || '').trim();
|
||||||
const secret = String(config.PAPER_TRADING ? profile.ALPACA_SECRET_KEY || '' : profile.REAL_ALPACA_SECRET_KEY || '').trim();
|
const secret = String(config.PAPER_TRADING ? profile.ALPACA_SECRET_KEY || '' : profile.REAL_ALPACA_SECRET_KEY || '').trim();
|
||||||
@ -167,6 +167,24 @@ async function getUserAlpacaCredentials(userId: string): Promise<AlpacaCredentia
|
|||||||
return { key, secret };
|
return { key, secret };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getUserMarketDataAlpacaCredentials(userId: string): Promise<AlpacaCredentials> {
|
||||||
|
const profile = await getCurrentUserProfile(userId);
|
||||||
|
|
||||||
|
const paperKey = String(profile.ALPACA_API_KEY || '').trim();
|
||||||
|
const paperSecret = String(profile.ALPACA_SECRET_KEY || '').trim();
|
||||||
|
if (paperKey && paperSecret) {
|
||||||
|
return { key: paperKey, secret: paperSecret };
|
||||||
|
}
|
||||||
|
|
||||||
|
const liveKey = String(profile.REAL_ALPACA_API_KEY || '').trim();
|
||||||
|
const liveSecret = String(profile.REAL_ALPACA_SECRET_KEY || '').trim();
|
||||||
|
if (liveKey && liveSecret) {
|
||||||
|
return { key: liveKey, secret: liveSecret };
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new MissingServiceConfigError('User Alpaca credentials are not configured');
|
||||||
|
}
|
||||||
|
|
||||||
interface TradeAuditEvent {
|
interface TradeAuditEvent {
|
||||||
event: string;
|
event: string;
|
||||||
userId?: string;
|
userId?: string;
|
||||||
@ -2709,7 +2727,7 @@ RULES:
|
|||||||
const period = String(req.query.period || '1Y').trim();
|
const period = String(req.query.period || '1Y').trim();
|
||||||
if (!authUserId) return res.status(401).json({ error: 'Unauthorized' });
|
if (!authUserId) return res.status(401).json({ error: 'Unauthorized' });
|
||||||
if (!symbol) return res.status(400).json({ error: 'symbol required' });
|
if (!symbol) return res.status(400).json({ error: 'symbol required' });
|
||||||
const alpaca = await getUserAlpacaCredentials(authUserId);
|
const alpaca = await getUserMarketDataAlpacaCredentials(authUserId);
|
||||||
|
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
let start = new Date(now);
|
let start = new Date(now);
|
||||||
@ -2796,7 +2814,7 @@ RULES:
|
|||||||
return res.status(401).json({ error: 'Unauthorized' });
|
return res.status(401).json({ error: 'Unauthorized' });
|
||||||
}
|
}
|
||||||
const limit = Math.max(1, Math.min(50, Number(req.query.limit) || 10));
|
const limit = Math.max(1, Math.min(50, Number(req.query.limit) || 10));
|
||||||
const alpaca = await getUserAlpacaCredentials(authUserId);
|
const alpaca = await getUserMarketDataAlpacaCredentials(authUserId);
|
||||||
const qs = new URLSearchParams({
|
const qs = new URLSearchParams({
|
||||||
...(symbols ? { symbols } : {}),
|
...(symbols ? { symbols } : {}),
|
||||||
limit: String(limit),
|
limit: String(limit),
|
||||||
@ -2827,7 +2845,7 @@ RULES:
|
|||||||
if (!authUserId) {
|
if (!authUserId) {
|
||||||
return res.status(401).json({ error: 'Unauthorized' });
|
return res.status(401).json({ error: 'Unauthorized' });
|
||||||
}
|
}
|
||||||
const alpaca = await getUserAlpacaCredentials(authUserId);
|
const alpaca = await getUserMarketDataAlpacaCredentials(authUserId);
|
||||||
const url = 'https://data.alpaca.markets/v2/stocks/snapshots?symbols=SPY,DIA,QQQ&feed=iex';
|
const url = 'https://data.alpaca.markets/v2/stocks/snapshots?symbols=SPY,DIA,QQQ&feed=iex';
|
||||||
const r = await fetch(url, {
|
const r = await fetch(url, {
|
||||||
headers: {
|
headers: {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user