fix: implement fiat-to-crypto conversion for sweep verification

This commit is contained in:
mstfyldz
2026-03-13 01:48:53 +03:00
parent 3f2e21c3d3
commit a82771f9c3

View File

@@ -81,16 +81,45 @@ export async function POST(request: Request) {
// 5. Initialize Engine and Verify Payment first // 5. Initialize Engine and Verify Payment first
const cryptoEngine = new CryptoEngine(selectedNetwork); const cryptoEngine = new CryptoEngine(selectedNetwork);
// 5.1 Convert Fiat amount to Crypto amount for verification
let expectedCryptoAmount = transaction.amount.toString();
// Always try to fetch price to ensure we are comparing crypto to crypto
try {
const coinIdMap: Record<string, string> = {
'SOL': 'solana',
'USDC': 'usd-coin',
'USDT': 'tether',
'TRX': 'tron',
'BTC': 'bitcoin'
};
const coinId = coinIdMap[selectedToken] || 'solana';
const priceUrl = `https://api.coingecko.com/api/v3/simple/price?ids=${coinId}&vs_currencies=usd,try`;
const priceRes = await fetch(priceUrl);
const priceData = await priceRes.json();
const currencyKey = (transaction.currency || 'TRY').toLowerCase();
const price = priceData[coinId][currencyKey] || priceData[coinId]['usd'];
if (price) {
// Apply a small tolerance (e.g., 2% for price fluctuations)
const rawExpected = parseFloat(transaction.amount) / price;
expectedCryptoAmount = (rawExpected * 0.98).toFixed(6);
console.log(`[Sweep] Verified Amount: ${transaction.amount} ${transaction.currency} => Expected ~${rawExpected.toFixed(6)} ${selectedToken} (Threshold: ${expectedCryptoAmount})`);
}
} catch (priceErr) {
console.warn("[Sweep] Could not fetch real-time price, using raw amount as fallback:", priceErr);
}
const verification = await cryptoEngine.verifyPayment( const verification = await cryptoEngine.verifyPayment(
tempWalletConfig.address, tempWalletConfig.address,
transaction.amount.toString(), expectedCryptoAmount,
selectedToken selectedToken
); );
if (!verification.success) { if (!verification.success) {
return NextResponse.json({ return NextResponse.json({
success: false, success: false,
error: "Henüz ödeme algılanmadı (On-chain bakiye yetersiz)", error: `Henüz ödeme algılanmadı. Beklenen: ~${expectedCryptoAmount} ${selectedToken}`,
status: 'waiting' status: 'waiting'
}); });
} }