From 96a8482a68ce680375e0f44e4b7e9a253e2df6f8 Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Tue, 3 Dec 2024 15:14:00 +0700 Subject: [PATCH] Create conversion.js --- src/wallet/conversion.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/wallet/conversion.js 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;