import { NextResponse } from 'next/server'; import { claimableNow, claimAccountNow, ClaimRefusedError } from '@/lib/auto-trade'; import { errorMessage } from '@/lib/platform-client'; /** * Claim now (the account page's button). GET: how much the account could * claim right now — its whole profit above the account size — or why not. * POST { amount }: claim it, to the login's main account (Vest's Claim * Profit). Only a person pressing the button calls this; the bot claims on * its own at each stage's target (runClaims). */ export async function GET(_req: Request, { params }: { params: Promise<{ id: string }> }) { const { id } = await params; return NextResponse.json(claimableNow(Number(id))); } export async function POST(req: Request, { params }: { params: Promise<{ id: string }> }) { // JSON only: a cross-site form or no-cors fetch can't send it without a // preflight this route never answers. if (!(req.headers.get('content-type') ?? '').includes('application/json')) { return NextResponse.json({ error: 'JSON body required' }, { status: 415 }); } const { id } = await params; const body = await req.json().catch(() => ({})) as { amount?: unknown }; const amount = Number(body.amount); if (!Number.isFinite(amount)) return NextResponse.json({ error: 'amount required' }, { status: 400 }); try { return NextResponse.json({ ok: true, ...await claimAccountNow(Number(id), amount) }); } catch (err) { if (err instanceof ClaimRefusedError) return NextResponse.json({ error: err.message }, { status: 400 }); console.error(`[claim-now] account ${id}: ${errorMessage(err)}`); return NextResponse.json({ error: `the claim failed: ${errorMessage(err)}` }, { status: 502 }); } }