Skip to content

Commit

Permalink
Create conversion.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Dec 3, 2024
1 parent 89f77e0 commit 96a8482
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/wallet/conversion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// wallet/conversion.js
const axios = require('axios');

class CurrencyConverter {
constructor(apiKey) {
this.apiKey = apiKey;
this.apiUrl = 'https://api.exchangerate-api.com/v4/latest/';
}

async convert(amount, fromCurrency, toCurrency) {
try {
const response = await axios.get(`${this.apiUrl}${fromCurrency}`);
const rates = response.data.rates;
if (!rates[toCurrency]) throw new Error('Invalid currency code');
const convertedAmount = (amount * rates[toCurrency]) / rates[fromCurrency];
return convertedAmount;
} catch (error) {
throw new Error(`Currency conversion failed: ${error.message}`);
}
}
}

module.exports = CurrencyConverter;

0 comments on commit 96a8482

Please sign in to comment.