97 lines
3.5 KiB
TypeScript
97 lines
3.5 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { getDb } from '@/lib/db';
|
|
import { polls, pollOptions } from '@/lib/db/schema';
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
import { createPublicClient, http, parseEther, formatEther } from 'viem';
|
|
import { celoAlfajores } from 'viem/chains';
|
|
|
|
const PROTOCOL_WALLET = '0x742d35Cc6634C0532925a3b844Bc454e4438f44e'; // Must match frontend
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const db = getDb(process.env.DATABASE_URL!);
|
|
const body = await request.json();
|
|
const { question, options, creatorFid, creatorAddress, txHash, verificationType, description, emoji, verificationConfig } = body as {
|
|
question: string
|
|
options: string[]
|
|
creatorFid: number
|
|
creatorAddress: string
|
|
txHash: string
|
|
verificationType: string
|
|
description?: string
|
|
emoji?: string
|
|
verificationConfig?: any
|
|
};
|
|
|
|
// Verify Transaction
|
|
if (!txHash) {
|
|
return NextResponse.json({ error: 'Transaction hash is required' }, { status: 400 });
|
|
}
|
|
|
|
const client = createPublicClient({
|
|
chain: celoAlfajores,
|
|
transport: http(),
|
|
});
|
|
|
|
try {
|
|
const tx = await client.getTransaction({ hash: txHash as `0x${string}` });
|
|
const receipt = await client.getTransactionReceipt({ hash: txHash as `0x${string}` });
|
|
|
|
if (receipt.status !== 'success') {
|
|
return NextResponse.json({ error: 'Transaction failed' }, { status: 400 });
|
|
}
|
|
|
|
if (tx.to?.toLowerCase() !== PROTOCOL_WALLET.toLowerCase()) {
|
|
return NextResponse.json({ error: 'Invalid transaction recipient' }, { status: 400 });
|
|
}
|
|
|
|
// Check value (Assuming 0.01 CELO for MVP as per frontend)
|
|
// In production, this should be 5 cUSD (ERC20 transfer check)
|
|
const expectedValue = parseEther('0.01');
|
|
if (tx.value < expectedValue) {
|
|
return NextResponse.json({ error: `Insufficient payment. Expected 0.01 CELO, got ${formatEther(tx.value)}` }, { status: 400 });
|
|
}
|
|
|
|
} catch (e) {
|
|
console.error('Transaction verification failed:', e);
|
|
return NextResponse.json({ error: 'Invalid transaction' }, { status: 400 });
|
|
}
|
|
|
|
const pollId = uuidv4();
|
|
const startTime = Date.now();
|
|
const endTime = startTime + 24 * 60 * 60 * 1000; // 24 hours
|
|
|
|
await db.insert(polls).values({
|
|
id: pollId,
|
|
creatorFid,
|
|
creatorAddress,
|
|
question,
|
|
startTime,
|
|
endTime,
|
|
status: 'active',
|
|
verificationType: (verificationType as 'chainlink' | 'api' | 'manual') || 'manual',
|
|
verificationConfig: verificationConfig || {},
|
|
description: description || null,
|
|
emoji: emoji || null,
|
|
createdAt: Date.now(),
|
|
totalPot: '3', // 3 cUSD seed
|
|
creationFee: '5',
|
|
protocolFee: '1.5',
|
|
});
|
|
|
|
for (let i = 0; i < options.length; i++) {
|
|
await db.insert(pollOptions).values({
|
|
id: uuidv4(),
|
|
pollId,
|
|
text: options[i],
|
|
position: i,
|
|
});
|
|
}
|
|
|
|
return NextResponse.json({ success: true, pollId });
|
|
} catch (e) {
|
|
console.error('Error creating poll:', e);
|
|
return NextResponse.json({ error: String(e) }, { status: 500 });
|
|
}
|
|
}
|