| import { BotContext } from "../types/botTypes"; |
| import { createLogger } from "../../utils/logger"; |
|
|
| const logger = createLogger('PriceUtils'); |
|
|
| |
| const exchangeRates: Record<string, number> = { |
| 'USD_RUB': 79.6229, |
| 'RUB_USD': 1 / 79.6229, |
| 'EUR_RUB': 86.5, |
| 'RUB_EUR': 1 / 86.5, |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| 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; |
| } |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export const calculatePriceWithProfit = ( |
| ctx: BotContext, |
| originalPrice: number, |
| originalCurrency: string = 'USD' |
| ): number => { |
| try { |
| |
| |
| 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; |
| |
| |
| const priceInBotCurrency = currency !== originalCurrency |
| ? convertCurrency(originalPrice, originalCurrency, currency) |
| : originalPrice; |
| |
| |
| let finalPrice = priceInBotCurrency; |
| |
| if (profit_type === 'percentage' && profit_value_percentage) { |
| |
| const profitAmount = priceInBotCurrency * (profit_value_percentage / 100); |
| finalPrice = priceInBotCurrency + profitAmount; |
| } else if (profit_type === 'fix' && profit_value_fix) { |
| |
| finalPrice = priceInBotCurrency + profit_value_fix; |
| } else { |
| |
| logger.warn('Invalid profit configuration, returning converted price'); |
| } |
| |
| return finalPrice; |
| } catch (error: any) { |
| logger.error(`Error calculating price with profit: ${error.message}`); |
| return originalPrice; |
| } |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| export const formatPrice = (ctx: BotContext, price: number): string => { |
| try { |
| |
| |
| |
| |
| |
| 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'; |
| } |
| }; |
| |