feat: add Solana USDT/USDC support and refine admin payouts UI
This commit is contained in:
72
app/api/admin/treasury/balances/route.ts
Normal file
72
app/api/admin/treasury/balances/route.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { db } from '@/lib/db';
|
||||
import { CryptoEngine } from '@/lib/crypto-engine';
|
||||
import { ethers } from 'ethers';
|
||||
import { PublicKey } from '@solana/web3.js';
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
// 1. Fetch platform addresses from settings
|
||||
const settingsRes = await db.query("SELECT key, value FROM system_settings WHERE key IN ('sol_platform_address', 'evm_platform_address', 'tron_platform_address', 'btc_platform_address')");
|
||||
const settings: Record<string, string> = {};
|
||||
settingsRes.rows.forEach(r => settings[r.key] = r.value);
|
||||
|
||||
const addresses = {
|
||||
EVM: settings.evm_platform_address || "0x70997970C51812dc3A010C7d01b50e0d17dc79C8",
|
||||
SOLANA: settings.sol_platform_address || "Ajr4nKieZJVu9q2d1eVF9pQPRCLoZ6v4tapB3iQn2SyQ",
|
||||
TRON: settings.tron_platform_address || "TY795B6FmDNV4Xm5U6G1rP9yvX7S9rK6G1P", // Valid TRON address format
|
||||
BITCOIN: settings.btc_platform_address || "17V95B6FmDNV4Xm5U6G1rP9yvX7S9rK6G1P" // Valid BTC address format
|
||||
};
|
||||
|
||||
// 2. Fetch balances for each network from config
|
||||
const cryptoConfig = require('@/lib/crypto-config.json');
|
||||
const balances: any = {};
|
||||
|
||||
for (const netConfig of cryptoConfig.networks) {
|
||||
const net = netConfig.id;
|
||||
console.log(`[Treasury] Fetching balances for ${net}...`);
|
||||
const engine = new CryptoEngine(net);
|
||||
const addr = (addresses as any)[['POLYGON', 'BSC', 'ETH'].includes(net) ? 'EVM' : net];
|
||||
|
||||
if (!addr) {
|
||||
console.warn(`[Treasury] No address found for network ${net}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
const tokenBalances: Record<string, string> = {};
|
||||
let nativeBalance = "0.00";
|
||||
let nativeSymbol = net;
|
||||
|
||||
for (const token of netConfig.tokens) {
|
||||
const balance = await engine.getBalance(addr, token.symbol);
|
||||
if (token.address === 'NATIVE') {
|
||||
nativeBalance = balance;
|
||||
nativeSymbol = token.symbol;
|
||||
} else {
|
||||
tokenBalances[token.symbol] = balance;
|
||||
}
|
||||
}
|
||||
|
||||
balances[net] = {
|
||||
address: addr,
|
||||
native: nativeBalance,
|
||||
nativeSymbol: nativeSymbol,
|
||||
tokens: tokenBalances
|
||||
};
|
||||
} catch (err) {
|
||||
console.error(`[Treasury Balance Error] ${net}:`, err);
|
||||
balances[net] = { address: addr, native: "Error", tokens: {} };
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
balances
|
||||
});
|
||||
|
||||
} catch (error: any) {
|
||||
return NextResponse.json({ error: error.message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user