1Branch0Tags
GL
glucryptoAdopt opentool 0.19.8
1f37f4912 days ago17Commits
typescript
import { store } from "opentool/store"; import type { ToolProfile } from "opentool"; import { HyperliquidApiError } from "opentool/adapters/hyperliquid"; import { backtestDecisionRequestSchema, buildBacktestDecisionSeriesInput, resolveBacktestMode, } from "opentool/backtest"; import { readConfig, resolveProfileAssets, resolveScheduleConfig, TWAP_BOT_TEMPLATE_CONFIG, } from "../src/config"; import { buildBacktestDecisionSeries, runTwapBot } from "../src/twap-bot"; const config = readConfig(); export const schema = backtestDecisionRequestSchema.partial(); type BacktestAwareToolProfile = ToolProfile & { backtest?: { mode: "decisions"; }; }; export const profile: BacktestAwareToolProfile = { description: "Hyperliquid TWAP bot.", category: "strategy", backtest: { mode: "decisions", }, templatePreview: { subtitle: "Native Hyperliquid TWAP execution with fixed-amount sizing.", description: `Uses the native Hyperliquid TWAP order path with a compact fixed-amount setup. Supports buy or sell execution, configurable TWAP duration, and optional randomization. Designed as a direct execution starter rather than an indicator or thesis bot.`, }, schedule: resolveScheduleConfig(config), assets: resolveProfileAssets(config), templateConfig: TWAP_BOT_TEMPLATE_CONFIG, }; function buildFailureMetadata(error: unknown) { const message = error instanceof Error ? error.message : "unknown"; return { ok: false, error: message, errorType: error instanceof Error ? error.name : typeof error, ...(error instanceof HyperliquidApiError ? { errorResponse: error.response } : {}), }; } async function executeLive() { const snapshot = readConfig(); try { return Response.json(await runTwapBot(snapshot)); } catch (error) { const metadata = buildFailureMetadata(error); await store({ source: "twap-bot", ref: `twap-bot-${Date.now()}`, status: "failed", action: "signal", metadata, }); return new Response(JSON.stringify(metadata), { status: 400, headers: { "content-type": "application/json" }, }); } } export async function POST(req: Request) { const snapshot = readConfig(); const payload = await req.json().catch(() => null); const mode = payload && typeof payload === "object" && !Array.isArray(payload) ? (payload as { mode?: unknown }).mode : null; const normalizedMode = resolveBacktestMode(mode); if (normalizedMode === "backtest_decisions") { try { const parsed = backtestDecisionRequestSchema.safeParse(payload); if (!parsed.success) { return new Response( JSON.stringify({ ok: false, error: parsed.error.issues[0]?.message ?? "invalid backtest request payload", }), { status: 400, headers: { "content-type": "application/json" }, }, ); } const backtest = await buildBacktestDecisionSeries({ config: snapshot, ...buildBacktestDecisionSeriesInput(parsed.data), }); return Response.json({ ok: true, mode: normalizedMode, backtest, }); } catch (error) { const message = error instanceof Error ? error.message : "unknown"; return new Response(JSON.stringify({ ok: false, error: message }), { status: 400, headers: { "content-type": "application/json" }, }); } } return executeLive(); }