fix: implement fiat-to-crypto conversion for sweep verification
This commit is contained in:
@@ -81,16 +81,45 @@ export async function POST(request: Request) {
|
||||
// 5. Initialize Engine and Verify Payment first
|
||||
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(
|
||||
tempWalletConfig.address,
|
||||
transaction.amount.toString(),
|
||||
expectedCryptoAmount,
|
||||
selectedToken
|
||||
);
|
||||
|
||||
if (!verification.success) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: "Henüz ödeme algılanmadı (On-chain bakiye yetersiz)",
|
||||
error: `Henüz ödeme algılanmadı. Beklenen: ~${expectedCryptoAmount} ${selectedToken}`,
|
||||
status: 'waiting'
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user