mirror of
https://github.com/multipleof4/KalBot.git
synced 2026-03-16 21:41:02 +00:00
Feat: Add live trades history API endpoint
This commit is contained in:
43
app/api/live-trades/route.js
Normal file
43
app/api/live-trades/route.js
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
import { NextResponse } from 'next/server';
|
||||||
|
import Surreal from 'surrealdb';
|
||||||
|
|
||||||
|
export const dynamic = 'force-dynamic';
|
||||||
|
|
||||||
|
function normalizeRows(result) {
|
||||||
|
if (!Array.isArray(result) || !result.length) return [];
|
||||||
|
const first = result[0];
|
||||||
|
if (Array.isArray(first)) return first;
|
||||||
|
if (first && typeof first === 'object' && Array.isArray(first.result)) return first.result;
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function GET(req) {
|
||||||
|
const url = process.env.SURREAL_URL;
|
||||||
|
if (!url) return NextResponse.json({ trades: [], error: 'No DB configured' });
|
||||||
|
|
||||||
|
const { searchParams } = new URL(req.url);
|
||||||
|
const strategyFilter = searchParams.get('strategy');
|
||||||
|
|
||||||
|
let client = null;
|
||||||
|
try {
|
||||||
|
client = new Surreal();
|
||||||
|
await client.connect(url);
|
||||||
|
await client.signin({ username: process.env.SURREAL_USER, password: process.env.SURREAL_PASS });
|
||||||
|
await client.use({ namespace: 'kalbot', database: 'kalbot' });
|
||||||
|
|
||||||
|
let query = 'SELECT * FROM live_orders WHERE settled = true';
|
||||||
|
const vars = {};
|
||||||
|
if (strategyFilter) {
|
||||||
|
query += ' AND strategy = $strategy';
|
||||||
|
vars.strategy = strategyFilter;
|
||||||
|
}
|
||||||
|
query += ' ORDER BY settleTime DESC LIMIT 50';
|
||||||
|
|
||||||
|
const result = await client.query(query, vars);
|
||||||
|
return NextResponse.json({ trades: normalizeRows(result) });
|
||||||
|
} catch (e) {
|
||||||
|
return NextResponse.json({ trades: [], error: e.message });
|
||||||
|
} finally {
|
||||||
|
try { await client?.close?.(); } catch {}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user