68 lines
1.8 KiB
TypeScript
68 lines
1.8 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { prisma } from "@/lib/prisma";
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const body = await request.json();
|
|
|
|
if (body.jsonrpc !== "2.0" || body.method !== "A2A_STAKE_DEPOSIT" || !body.params) {
|
|
return NextResponse.json({
|
|
jsonrpc: "2.0",
|
|
error: { code: -32600, message: "Invalid Request Structure" }
|
|
}, { status: 400 });
|
|
}
|
|
|
|
const { agent_id, amount_cents } = body.params;
|
|
|
|
if (!agent_id || !amount_cents || amount_cents <= 0) {
|
|
return NextResponse.json({
|
|
jsonrpc: "2.0",
|
|
error: { code: -32602, message: "Missing or invalid parameters" }
|
|
}, { status: 400 });
|
|
}
|
|
|
|
// In a real Web3 environment, we would verify a transaction hash here.
|
|
// For this protocol, we mock the successful deposit.
|
|
|
|
const agent = await prisma.agentProfile.findUnique({
|
|
where: { agent_id }
|
|
});
|
|
|
|
if (!agent) {
|
|
return NextResponse.json({
|
|
jsonrpc: "2.0",
|
|
error: { code: -32001, message: "Agent not found" }
|
|
}, { status: 404 });
|
|
}
|
|
|
|
const newAmount = agent.staked_amount + amount_cents;
|
|
const newTier = newAmount >= 50000 ? "PREMIUM" : agent.tier; // 500 USDC = Premium
|
|
|
|
await prisma.agentProfile.update({
|
|
where: { agent_id },
|
|
data: {
|
|
staked_amount: newAmount,
|
|
tier: newTier
|
|
}
|
|
});
|
|
|
|
return NextResponse.json({
|
|
jsonrpc: "2.0",
|
|
result: {
|
|
success: true,
|
|
message: "Stake deposited successfully",
|
|
staked_amount: newAmount,
|
|
tier: newTier
|
|
},
|
|
id: body.id || null
|
|
});
|
|
|
|
} catch (error: any) {
|
|
console.error("[Staking Error]", error);
|
|
return NextResponse.json({
|
|
jsonrpc: "2.0",
|
|
error: { code: -32000, message: "Server error", data: error.message }
|
|
}, { status: 500 });
|
|
}
|
|
}
|