Files
autofirmer-expanded/app/api/accounts/[id]/claim/route.ts
T
Brandon LiandClaude Opus 5.5 71606ffc9e Claim now: a button on the account page claims a payout account's profit
For a payout account on a firm that takes claims by API (Vest), the
account page shows what it could claim right now — its whole profit above
the account size — or why not (not flat, history loading, below its
start). Pressing it asks first, then claims to the login's main account
under the lane's mutex, with the same checks and bookkeeping as the
bot's own claim at a stage's target: the account trades its next stage.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 19:44:10 -05:00

35 lines
1.7 KiB
TypeScript

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 });
}
}