153 lines
5.6 KiB
TypeScript
153 lines
5.6 KiB
TypeScript
![]() |
import { NextResponse } from 'next/server';
|
||
|
import { ethers } from 'ethers';
|
||
|
import { USER_TRUST_CONTRACT_ADDRESS, USER_TRUST_CONTRACT_ABI } from '@/lib/constants';
|
||
|
|
||
|
// Mock database for development purposes
|
||
|
// In production, this would be a real database
|
||
|
const mockUserTrustDB = new Map<string, { trustScore: number, dailyLimit: number, lastUpdated: string }>();
|
||
|
|
||
|
export async function GET(request: Request) {
|
||
|
const { searchParams } = new URL(request.url);
|
||
|
const walletAddress = searchParams.get('address');
|
||
|
|
||
|
if (!walletAddress) {
|
||
|
return NextResponse.json({ error: 'Missing wallet address' }, { status: 400 });
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
// In production, this would fetch from the blockchain
|
||
|
// For development, we'll use our mock DB
|
||
|
if (process.env.NODE_ENV === 'development') {
|
||
|
if (!mockUserTrustDB.has(walletAddress)) {
|
||
|
// Default values for new users
|
||
|
mockUserTrustDB.set(walletAddress, {
|
||
|
trustScore: 20,
|
||
|
dailyLimit: 5,
|
||
|
lastUpdated: new Date().toISOString()
|
||
|
});
|
||
|
}
|
||
|
|
||
|
const userData = mockUserTrustDB.get(walletAddress);
|
||
|
return NextResponse.json({
|
||
|
address: walletAddress,
|
||
|
...userData
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// In production, this would be the actual blockchain call
|
||
|
const provider = new ethers.providers.JsonRpcProvider(process.env.ETHEREUM_RPC_URL);
|
||
|
const contract = new ethers.Contract(
|
||
|
USER_TRUST_CONTRACT_ADDRESS,
|
||
|
USER_TRUST_CONTRACT_ABI,
|
||
|
provider
|
||
|
);
|
||
|
|
||
|
const userData = await contract.getUserTrustData(walletAddress);
|
||
|
|
||
|
return NextResponse.json({
|
||
|
address: walletAddress,
|
||
|
trustScore: userData.trustScore.toNumber(),
|
||
|
dailyLimit: userData.dailyLimit.toNumber(),
|
||
|
lastUpdated: new Date().toISOString()
|
||
|
});
|
||
|
} catch (error) {
|
||
|
console.error('Error fetching trust data:', error);
|
||
|
return NextResponse.json({ error: 'Failed to fetch trust data' }, { status: 500 });
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export async function POST(request: Request) {
|
||
|
try {
|
||
|
const body = await request.json();
|
||
|
const { walletAddress, action, amount = 1 } = body;
|
||
|
|
||
|
if (!walletAddress) {
|
||
|
return NextResponse.json({ error: 'Missing wallet address' }, { status: 400 });
|
||
|
}
|
||
|
|
||
|
if (!action) {
|
||
|
return NextResponse.json({ error: 'Missing action' }, { status: 400 });
|
||
|
}
|
||
|
|
||
|
// In production, this would update the blockchain
|
||
|
// For development, we'll use our mock DB
|
||
|
if (process.env.NODE_ENV === 'development') {
|
||
|
if (!mockUserTrustDB.has(walletAddress)) {
|
||
|
mockUserTrustDB.set(walletAddress, {
|
||
|
trustScore: 20,
|
||
|
dailyLimit: 5,
|
||
|
lastUpdated: new Date().toISOString()
|
||
|
});
|
||
|
}
|
||
|
|
||
|
const userData = mockUserTrustDB.get(walletAddress)!;
|
||
|
|
||
|
// Different actions have different point values
|
||
|
let pointsToAdd = amount;
|
||
|
switch (action) {
|
||
|
case 'submit_perspective':
|
||
|
pointsToAdd = 1 * amount;
|
||
|
break;
|
||
|
case 'receive_upvote':
|
||
|
pointsToAdd = 5 * amount;
|
||
|
break;
|
||
|
case 'included_in_consensus':
|
||
|
pointsToAdd = 10 * amount;
|
||
|
break;
|
||
|
case 'consistent_participation':
|
||
|
pointsToAdd = 15 * amount;
|
||
|
break;
|
||
|
case 'zero_flags':
|
||
|
pointsToAdd = 5 * amount;
|
||
|
break;
|
||
|
default:
|
||
|
pointsToAdd = 1 * amount;
|
||
|
}
|
||
|
|
||
|
// Update trust score (capped at 100)
|
||
|
userData.trustScore = Math.min(100, userData.trustScore + pointsToAdd);
|
||
|
|
||
|
// Update daily limit based on trust score for unverified users
|
||
|
// This is a simple linear scale: dailyLimit = 5 + (trustScore / 20)
|
||
|
userData.dailyLimit = Math.floor(5 + (userData.trustScore / 20));
|
||
|
userData.lastUpdated = new Date().toISOString();
|
||
|
|
||
|
mockUserTrustDB.set(walletAddress, userData);
|
||
|
|
||
|
return NextResponse.json({
|
||
|
address: walletAddress,
|
||
|
...userData,
|
||
|
pointsAdded: pointsToAdd
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// In production, this would be the actual blockchain call
|
||
|
// We'd need a server-side wallet to sign transactions
|
||
|
const provider = new ethers.providers.JsonRpcProvider(process.env.ETHEREUM_RPC_URL);
|
||
|
const privateKey = process.env.SERVER_PRIVATE_KEY!;
|
||
|
const wallet = new ethers.Wallet(privateKey, provider);
|
||
|
|
||
|
const contract = new ethers.Contract(
|
||
|
USER_TRUST_CONTRACT_ADDRESS,
|
||
|
USER_TRUST_CONTRACT_ABI,
|
||
|
wallet
|
||
|
);
|
||
|
|
||
|
const tx = await contract.incrementTrustScore(walletAddress, amount);
|
||
|
await tx.wait();
|
||
|
|
||
|
// Fetch the updated data
|
||
|
const userData = await contract.getUserTrustData(walletAddress);
|
||
|
|
||
|
return NextResponse.json({
|
||
|
address: walletAddress,
|
||
|
trustScore: userData.trustScore.toNumber(),
|
||
|
dailyLimit: userData.dailyLimit.toNumber(),
|
||
|
lastUpdated: new Date().toISOString(),
|
||
|
pointsAdded: amount
|
||
|
});
|
||
|
} catch (error) {
|
||
|
console.error('Error updating trust score:', error);
|
||
|
return NextResponse.json({ error: 'Failed to update trust score' }, { status: 500 });
|
||
|
}
|
||
|
}
|