diff --git a/src/wallet/conversion.js b/src/wallet/conversion.js new file mode 100644 index 000000000..8c85b4f40 --- /dev/null +++ b/src/wallet/conversion.js @@ -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;