fix: improve price fetching reliability in sync worker with Binance API and fallbacks
This commit is contained in:
@@ -87,23 +87,40 @@ export async function syncPendingPayments() {
|
||||
|
||||
let expectedCryptoAmount = tx.amount.toString();
|
||||
try {
|
||||
const coinIdMap: Record<string, string> = {
|
||||
'SOL': 'solana', 'MATIC': 'matic-network', 'POLYGON': 'matic-network',
|
||||
'USDC': 'usd-coin', 'USDT': 'tether', 'TRX': 'tron', 'BTC': 'bitcoin'
|
||||
const symbolMap: Record<string, string> = {
|
||||
'SOL': 'SOLUSDT', 'MATIC': 'MATICUSDT', 'POLYGON': 'MATICUSDT',
|
||||
'USDC': 'USDCUSDT', 'USDT': 'USDTUSDT', 'TRX': 'TRXUSDT'
|
||||
};
|
||||
const coinId = coinIdMap[tokenSymbol] || '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 = (tx.currency || 'TRY').toLowerCase();
|
||||
const priceInCurrency = priceData[coinId][currencyKey] || priceData[coinId]['usd'];
|
||||
const pair = symbolMap[tokenSymbol] || 'SOLUSDT';
|
||||
|
||||
// 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) {
|
||||
const rawExpected = parseFloat(tx.amount) / priceInCurrency;
|
||||
// 2. Get Crypto/USD rate
|
||||
let cryptoUsdPrice = 1.0;
|
||||
if (pair !== 'USDTUSDT' && pair !== 'USDCUSDT') {
|
||||
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 = (tx.currency || 'TRY').toUpperCase();
|
||||
const priceInTarget = currency === 'USD' ? cryptoUsdPrice : priceInTry;
|
||||
|
||||
if (priceInTarget > 0) {
|
||||
const rawExpected = parseFloat(tx.amount) / priceInTarget;
|
||||
expectedCryptoAmount = (rawExpected * 0.98).toFixed(6);
|
||||
console.log(`[SyncWorker] Price for ${tokenSymbol}: ${priceInTarget.toFixed(2)} ${currency} | Expected: ${expectedCryptoAmount}`);
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn(`[SyncWorker] Price fetch failed for ${tokenSymbol}, using raw amount.`);
|
||||
console.warn(`[SyncWorker] Price fetch failed for ${tokenSymbol}, using backup static rates.`);
|
||||
// Absolute fallback for safety
|
||||
const staticRates: Record<string, number> = { 'SOL': 4000, 'USDT': 33, 'USDC': 33, 'TRX': 5, 'MATIC': 25 };
|
||||
const rate = staticRates[tokenSymbol] || 1;
|
||||
expectedCryptoAmount = (parseFloat(tx.amount) / rate * 0.95).toFixed(6);
|
||||
}
|
||||
|
||||
const verification = await cryptoEngine.verifyPayment(depositAddress, expectedCryptoAmount, tokenSymbol);
|
||||
|
||||
Reference in New Issue
Block a user