131 lines
5.3 KiB
TypeScript
131 lines
5.3 KiB
TypeScript
import type { FastifyInstance, FastifyRequest } from 'fastify'
|
|
import { z } from 'zod'
|
|
import { eq } from 'drizzle-orm'
|
|
import { subscriptions } from '@trumpsignal/database'
|
|
import type { Context } from '@trumpsignal/backend/context'
|
|
import type { RawBody } from '@trumpsignal/backend/auth'
|
|
import { requireThat, utcMs } from '@trumpsignal/backend/http'
|
|
import {
|
|
subscribe,
|
|
publicUser,
|
|
privateUser,
|
|
setKey,
|
|
clearAndArm,
|
|
closedTrades,
|
|
performance,
|
|
subscription,
|
|
settingsDto,
|
|
} from '@trumpsignal/backend/accounts'
|
|
const body = (r: FastifyRequest) => z.record(z.unknown()).parse(r.body) as RawBody
|
|
const wallet = (r: FastifyRequest) =>
|
|
z
|
|
.string()
|
|
.parse((r.params as { wallet: string }).wallet)
|
|
.toLowerCase()
|
|
.trim()
|
|
const settingsSchema = z.object({
|
|
leverage: z.number().int().min(1).max(50),
|
|
position_size_usd: z.number().min(5).max(10000),
|
|
take_profit_pct: z.number().min(0.1).max(50).nullable().optional(),
|
|
stop_loss_pct: z.number().min(0.1).max(50).nullable().optional(),
|
|
min_confidence: z.number().int().min(0).max(100),
|
|
daily_budget_usd: z.number().positive().max(100000).nullable().optional(),
|
|
sys2_leverage: z.number().int().min(1).max(10).nullable().optional(),
|
|
sys2_mode: z.enum(['standard', 'aggressive', '']).nullable().optional(),
|
|
active_from: z.string().nullable().optional(),
|
|
active_until: z.string().nullable().optional(),
|
|
trump_enabled: z.boolean().nullable().optional(),
|
|
macro_enabled: z.boolean().nullable().optional(),
|
|
})
|
|
export async function registerAccounts(app: FastifyInstance, c: Context) {
|
|
app.post('/api/subscribe', async (req) => {
|
|
const raw = body(req)
|
|
const paper = z.boolean().optional().parse(raw.paper_mode) ?? false
|
|
const w = await c.auth.write('subscribe', raw, paper ? { paper_mode: true } : null)
|
|
return subscribe(c, w, paper)
|
|
})
|
|
app.get('/api/user/:wallet/public', async (req) => publicUser(c, wallet(req)))
|
|
app.get('/api/user/:wallet', async (req) => {
|
|
const w = await c.auth.read(['view_user'], wallet(req), req.headers, req.query as RawBody)
|
|
return privateUser(c, w)
|
|
})
|
|
app.put('/api/user/:wallet/hl-api-key', async (req) => {
|
|
const raw = body(req),
|
|
key = z.string().parse(raw.api_key).trim()
|
|
const w = await c.auth.write('set_hl_api_key', raw, { api_key: key }, wallet(req))
|
|
return setKey(c, w, key)
|
|
})
|
|
app.put('/api/user/:wallet/settings', async (req) => {
|
|
const raw = body(req),
|
|
s = settingsSchema.parse(raw.settings)
|
|
if (s.trump_enabled)
|
|
requireThat(
|
|
s.take_profit_pct != null && s.stop_loss_pct != null,
|
|
422,
|
|
'TP and SL required when Trump module is enabled',
|
|
)
|
|
const af = s.active_from ? utcMs(s.active_from) : null,
|
|
au = s.active_until ? utcMs(s.active_until) : null
|
|
requireThat(
|
|
(af == null) === (au == null) &&
|
|
(af == null || (Number.isFinite(af) && Number.isFinite(au) && au! > af)),
|
|
422,
|
|
'Invalid active window',
|
|
)
|
|
const w = await c.auth.write('set_settings', raw, raw.settings, wallet(req))
|
|
const sub = await subscription(c, w)
|
|
const values = {
|
|
leverage: s.leverage,
|
|
position_size_usd: s.position_size_usd,
|
|
take_profit_pct: s.take_profit_pct ?? null,
|
|
stop_loss_pct: s.stop_loss_pct ?? null,
|
|
min_confidence: s.min_confidence,
|
|
daily_budget_usd: s.daily_budget_usd ?? null,
|
|
sys2_leverage: s.sys2_leverage ?? null,
|
|
active_from: af == null ? null : new Date(af).toISOString().slice(0, -1),
|
|
active_until: au == null ? null : new Date(au).toISOString().slice(0, -1),
|
|
...(s.sys2_mode != null ? { sys2_mode: s.sys2_mode || 'standard' } : {}),
|
|
...(s.trump_enabled != null ? { trump_enabled: s.trump_enabled } : {}),
|
|
...(s.macro_enabled != null ? { macro_enabled: s.macro_enabled } : {}),
|
|
}
|
|
await c.db.update(subscriptions).set(values).where(eq(subscriptions.id, sub.id))
|
|
return {
|
|
...s,
|
|
active_from: af == null ? null : new Date(af).toISOString(),
|
|
active_until: au == null ? null : new Date(au).toISOString(),
|
|
}
|
|
})
|
|
app.post('/api/user/:wallet/manual-window', async (req) => {
|
|
const raw = body(req),
|
|
hours = z.number().int().min(0).max(168).parse(raw.hours)
|
|
const w = await c.auth.write('set_manual_window', raw, { hours }, wallet(req))
|
|
return clearAndArm(c, w, { hours })
|
|
})
|
|
app.post('/api/user/:wallet/auto-trade', async (req) => {
|
|
const raw = body(req),
|
|
enabled = z.boolean().parse(raw.enabled)
|
|
const w = await c.auth.write('set_auto_trade', raw, { enabled }, wallet(req))
|
|
return clearAndArm(c, w, { enabled })
|
|
})
|
|
app.get('/api/trades', async (req) => {
|
|
const q = z
|
|
.object({
|
|
wallet: z.string(),
|
|
limit: z.coerce.number().int().min(1).max(500).default(20),
|
|
page: z.coerce.number().int().min(1).default(1),
|
|
})
|
|
.passthrough()
|
|
.parse(req.query)
|
|
const w = await c.auth.read(['view_trades', 'view_user'], q.wallet, req.headers, q)
|
|
return closedTrades(c, w, q.limit, q.page)
|
|
})
|
|
app.get('/api/performance', async (req) => {
|
|
const q = z
|
|
.object({ wallet: z.string(), include_paper: z.enum(['true', 'false']).optional() })
|
|
.passthrough()
|
|
.parse(req.query)
|
|
const w = await c.auth.read(['view_performance', 'view_user'], q.wallet, req.headers, q)
|
|
return performance(c, w, q.include_paper === 'true')
|
|
})
|
|
}
|