Skip to content

Commit

Permalink
Create multiCurrencySupport.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Dec 3, 2024
1 parent 4d89159 commit 5bf4491
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/wallet/multiCurrencySupport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// multiCurrencySupport.js

class MultiCurrencySupport {
constructor() {
this.currencies = {
USD: 1, // Base currency
EUR: 0.85,
GBP: 0.75,
BTC: 0.000025, // Example conversion rate for Bitcoin
ETH: 0.0004, // Example conversion rate for Ethereum
};
}

// Convert an amount from one currency to another
convert(amount, fromCurrency, toCurrency) {
if (!this.currencies[fromCurrency] || !this.currencies[toCurrency]) {
throw new Error("Unsupported currency.");
}
const baseAmount = amount / this.currencies[fromCurrency];
return baseAmount * this.currencies[toCurrency];
}

// Get available currencies
getAvailableCurrencies() {
return Object.keys(this.currencies);
}

// Get conversion rate between two currencies
getConversionRate(fromCurrency, toCurrency) {
if (!this.currencies[fromCurrency] || !this.currencies[toCurrency]) {
throw new Error("Unsupported currency.");
}
return this.currencies[toCurrency] / this.currencies[fromCurrency];
}
}

// Example usage
const currencySupport = new MultiCurrencySupport();
const amountInUSD = 100;
const amountInEUR = currencySupport.convert(amountInUSD, 'USD', 'EUR');
console.log(`$${amountInUSD} is equivalent to €${amountInEUR.toFixed(2)}`);

const availableCurrencies = currencySupport.getAvailableCurrencies();
console.log("Available currencies:", availableCurrencies.join(', '));

const conversionRate = currencySupport.getConversionRate('USD', 'BTC');
console.log(`Conversion rate from USD to BTC: ${conversionRate}`);

export default MultiCurrencySupport;

0 comments on commit 5bf4491

Please sign in to comment.