bot-me / src /bots /utils /priceUtils.ts
Mohammed Foud
allh
80d4bc1
import { BotContext } from "../types/botTypes";
import { createLogger } from "../../utils/logger";
const logger = createLogger('PriceUtils');
// Currency exchange rates (you might want to fetch these dynamically)
const exchangeRates: Record<string, number> = {
'USD_RUB': 79.6229,
'RUB_USD': 1 / 79.6229, // Add this
'EUR_RUB': 86.5,
'RUB_EUR': 1 / 86.5, // Add this
};
/**
* Convert price from one currency to another
*
* @param amount The amount to convert
* @param fromCurrency Source currency code
* @param toCurrency Target currency code
* @returns Converted amount
*/
export const convertCurrency = (
amount: number,
fromCurrency: string = 'USD',
toCurrency: string = 'RUB'
): number => {
try {
if (fromCurrency === toCurrency) {
return amount;
}
const rateKey = `${fromCurrency}_${toCurrency}`;
const rate = exchangeRates[rateKey];
if (!rate) {
logger.warn(`Exchange rate not found for ${rateKey}, returning original amount`);
return amount;
}
return amount * rate;
} catch (error: any) {
logger.error(`Error converting currency: ${error.message}`);
return amount;
}
};
/**
* Calculate the final price with profit based on bot settings
*
* @param ctx The bot context containing botData
* @param originalPrice The original price to apply profit to
* @param originalCurrency The currency of the original price
* @returns The calculated price with profit applied
*/
export const calculatePriceWithProfit = (
ctx: BotContext,
originalPrice: number,
originalCurrency: string = 'USD'
): number => {
try {
// Check if botData exists
if (!ctx.botData) {
logger.warn('Bot data not available, returning original price');
return originalPrice;
}
const { profit_type, profit_value_percentage, profit_value_fix, currency } = ctx.botData;
// Convert to bot's currency if needed
const priceInBotCurrency = currency !== originalCurrency
? convertCurrency(originalPrice, originalCurrency, currency)
: originalPrice;
// Calculate price based on profit type
let finalPrice = priceInBotCurrency;
if (profit_type === 'percentage' && profit_value_percentage) {
// Calculate percentage profit
const profitAmount = priceInBotCurrency * (profit_value_percentage / 100);
finalPrice = priceInBotCurrency + profitAmount;
} else if (profit_type === 'fix' && profit_value_fix) {
// Add fixed profit amount
finalPrice = priceInBotCurrency + profit_value_fix;
} else {
// Default case - return converted price if profit settings are invalid
logger.warn('Invalid profit configuration, returning converted price');
}
return finalPrice;
} catch (error: any) {
logger.error(`Error calculating price with profit: ${error.message}`);
return originalPrice; // Return original price in case of error
}
};
/**
* Format price with currency symbol based on bot settings
*
* @param ctx The bot context containing botData
* @param price The price to format
* @returns Formatted price string with currency
*/
export const formatPrice = (ctx: BotContext, price: number): string => {
try {
// if (typeof price !== 'number' || isNaN(price)) {
// logger.error(`Invalid price type: ${typeof price} - ${JSON.stringify(price)}`);
// return 'N/A'; // or some fallback text
// }
const currency = ctx.botData?.currency || 'USD';
switch (currency) {
case 'RUB':
return `${price.toFixed(2)} ₽`;
case 'USD':
return `$${price.toFixed(2)}`;
case 'EUR':
return `€${price.toFixed(2)}`;
default:
return `${price.toFixed(2)} ${currency}`;
}
} catch (error: any) {
logger.error(`Error formatting price: ${error.message}`);
return 'N/A';
}
};