46 lines
1.5 KiB
TypeScript
46 lines
1.5 KiB
TypeScript
// Fonction de conversion des crédits en euros
|
|
export function convertCreditsToEuros(credits: number): {
|
|
usd: number;
|
|
eur: number;
|
|
formatted: {
|
|
usd: string;
|
|
eur: string;
|
|
};
|
|
} {
|
|
// Votre règle : 5 millions de crédits = 5 USD
|
|
// Donc : 1 million de crédits = 1 USD
|
|
const usdAmount = credits / 1_000_000;
|
|
|
|
// Taux de change USD → EUR (vous pouvez ajuster ce taux)
|
|
// Taux approximatif actuel : 1 USD ≈ 0.92 EUR
|
|
const USD_TO_EUR_RATE = 0.92;
|
|
const eurAmount = usdAmount * USD_TO_EUR_RATE;
|
|
|
|
return {
|
|
usd: usdAmount,
|
|
eur: eurAmount,
|
|
formatted: {
|
|
usd: `$${usdAmount.toFixed(2)}`,
|
|
eur: `€${eurAmount.toFixed(2)}`
|
|
}
|
|
};
|
|
}
|
|
|
|
// Fonction pour formater les crédits avec conversion
|
|
export function formatCreditsWithCurrency(credits: number): string {
|
|
const conversion = convertCreditsToEuros(credits);
|
|
return `${credits.toLocaleString()} crédits (${conversion.formatted.eur})`;
|
|
}
|
|
|
|
// Fonction pour obtenir le taux de change en temps réel (optionnel)
|
|
export async function getCurrentExchangeRate(): Promise<number> {
|
|
try {
|
|
// Vous pouvez utiliser une API gratuite comme exchangerate-api.com
|
|
const response = await fetch('https://api.exchangerate-api.com/v4/latest/USD');
|
|
const data = await response.json();
|
|
return data.rates.EUR || 0.92; // Fallback au taux fixe
|
|
} catch (error) {
|
|
console.warn('Impossible de récupérer le taux de change, utilisation du taux fixe');
|
|
return 0.92; // Taux fixe de fallback
|
|
}
|
|
} |