fix: update checkout sweep api to use binance price fetching for consistency with sync worker
This commit is contained in:
@@ -87,26 +87,39 @@ export async function POST(request: Request) {
|
|||||||
let expectedCryptoAmount = transaction.amount.toString();
|
let expectedCryptoAmount = transaction.amount.toString();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const coinIdMap: Record<string, string> = {
|
const symbolMap: Record<string, string> = {
|
||||||
'SOL': 'solana', 'USDC': 'usd-coin', 'USDT': 'tether', 'TRX': 'tron', 'BTC': 'bitcoin'
|
'SOL': 'SOLUSDT', 'MATIC': 'MATICUSDT', 'POLYGON': 'MATICUSDT',
|
||||||
|
'USDC': 'USDCUSDT', 'USDT': 'USDTUSDT', 'TRX': 'TRXUSDT'
|
||||||
};
|
};
|
||||||
const coinId = coinIdMap[selectedToken] || 'solana';
|
const pair = symbolMap[selectedToken] || 'SOLUSDT';
|
||||||
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 priceInCurrency = priceData[coinId][currencyKey] || priceData[coinId]['usd'];
|
// 1. Get USD/TRY rate
|
||||||
|
const tryRes = await fetch(`https://api.binance.com/api/v3/ticker/price?symbol=USDTTRY`);
|
||||||
|
const tryData = await tryRes.json();
|
||||||
|
const usdTryPrice = parseFloat(tryData.price) || 32.5;
|
||||||
|
|
||||||
if (priceInCurrency) {
|
// 2. Get Crypto/USD rate
|
||||||
// Apply a small tolerance (2%) and cross-convert if needed (simple assumption for demo)
|
let cryptoUsdPrice = 1.0;
|
||||||
const price = currencyKey === 'try' ? priceInCurrency : (priceInCurrency * 32.5); // Fallback conversion
|
if (pair !== 'USDTUSDT' && pair !== 'USDCUSDT') {
|
||||||
const rawExpected = parseFloat(transaction.amount) / priceInCurrency;
|
const cryptoRes = await fetch(`https://api.binance.com/api/v3/ticker/price?symbol=${pair}`);
|
||||||
|
const cryptoData = await cryptoRes.json();
|
||||||
|
cryptoUsdPrice = parseFloat(cryptoData.price) || 1.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
const priceInTry = cryptoUsdPrice * usdTryPrice;
|
||||||
|
const currency = (transaction.currency || 'TRY').toUpperCase();
|
||||||
|
const priceInTarget = currency === 'USD' ? cryptoUsdPrice : priceInTry;
|
||||||
|
|
||||||
|
if (priceInTarget > 0) {
|
||||||
|
const rawExpected = parseFloat(transaction.amount) / priceInTarget;
|
||||||
expectedCryptoAmount = (rawExpected * 0.98).toFixed(6);
|
expectedCryptoAmount = (rawExpected * 0.98).toFixed(6);
|
||||||
console.log(`[Sweep] ${transaction.amount} ${transaction.currency} => ~${rawExpected.toFixed(6)} ${selectedToken}`);
|
console.log(`[Sweep API] Price for ${selectedToken}: ${priceInTarget.toFixed(2)} ${currency} | Expected: ${expectedCryptoAmount}`);
|
||||||
}
|
}
|
||||||
} catch (priceErr) {
|
} catch (priceErr) {
|
||||||
console.warn("[Sweep] Price fetch failed, using order amount as fallback");
|
console.warn("[Sweep API] Price fetch failed, using fallback rates.");
|
||||||
|
const staticRates: Record<string, number> = { 'SOL': 4000, 'USDT': 33, 'USDC': 33, 'TRX': 5, 'MATIC': 25 };
|
||||||
|
const rate = staticRates[selectedToken] || 1;
|
||||||
|
expectedCryptoAmount = (parseFloat(transaction.amount) / rate * 0.95).toFixed(6);
|
||||||
}
|
}
|
||||||
|
|
||||||
const verification = await cryptoEngine.verifyPayment(
|
const verification = await cryptoEngine.verifyPayment(
|
||||||
|
|||||||
Reference in New Issue
Block a user