UNPKG

somali-exchange-rates

Version:

πŸ‡ΈπŸ‡΄ Comprehensive Somali Exchange Rates platform with real-time rates, transfer fees, alerts, multi-language support, and advanced financial tools

1,569 lines (1,550 loc) β€’ 75.5 kB
#!/usr/bin/env node "use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __esm = (fn, res) => function __init() { return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res; }; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/utils.ts async function tryReadJSON(p) { try { const raw = await import_promises.default.readFile(p, "utf8"); return JSON.parse(raw); } catch { return null; } } async function tryWriteJSON(p, data) { try { await import_promises.default.mkdir(import_node_path.default.dirname(p), { recursive: true }); await import_promises.default.writeFile(p, JSON.stringify(data, null, 2), "utf8"); } catch { } } function invert(baseToOthers, sosPerBase) { const out = {}; for (const [k, v] of Object.entries(baseToOthers)) { out[k] = v / sosPerBase; } return out; } function nice(n) { return Number(n.toFixed(n < 1 ? 4 : 2)); } var import_promises, import_node_path; var init_utils = __esm({ "src/utils.ts"() { "use strict"; import_promises = __toESM(require("fs/promises")); import_node_path = __toESM(require("path")); } }); // src/providers/exchangeratehost.ts var API, ExchangerateHostProvider; var init_exchangeratehost = __esm({ "src/providers/exchangeratehost.ts"() { "use strict"; init_utils(); API = "https://api.exchangerate.host/latest"; ExchangerateHostProvider = class { name = "exchangerate.host"; async fetchRatesSOS() { const symbols = ["SOS", "EUR", "GBP", "KES", "ETB", "AED", "SAR", "TRY", "CNY", "USD"].join(","); const url = `${API}?base=USD&symbols=${symbols}`; const res = await fetch(url); if (!res.ok) throw new Error(`Provider error ${res.status}`); const data = await res.json(); const usdTo = data.rates; const sosPerUsd = usdTo["SOS"]; if (!sosPerUsd) throw new Error("Provider returned no SOS rate"); return invert(usdTo, sosPerUsd); } }; } }); // src/providers/manager.ts var ProviderManager; var init_manager = __esm({ "src/providers/manager.ts"() { "use strict"; init_exchangeratehost(); ProviderManager = class { config; constructor(config) { this.config = { primary: new ExchangerateHostProvider(), fallbacks: [], timeout: 1e4, maxRetries: 3, ...config }; } async fetchRates() { const providers = [this.config.primary, ...this.config.fallbacks]; let lastError = null; for (const provider of providers) { for (let attempt = 1; attempt <= this.config.maxRetries; attempt++) { try { console.log(`Attempting to fetch rates from ${provider.name} (attempt ${attempt})`); const rates = await this.fetchWithTimeout(provider, this.config.timeout); console.log(`Successfully fetched rates from ${provider.name}`); return rates; } catch (error) { lastError = error; console.warn(`Failed to fetch from ${provider.name} (attempt ${attempt}):`, error); if (attempt < this.config.maxRetries) { const delay = Math.pow(2, attempt - 1) * 1e3; await new Promise((resolve) => setTimeout(resolve, delay)); } } } } throw new Error(`All providers failed. Last error: ${lastError?.message}`); } async fetchHistoricalRates(date) { const providers = [this.config.primary, ...this.config.fallbacks].filter((p) => p.fetchHistoricalRates); let lastError = null; for (const provider of providers) { try { if (provider.fetchHistoricalRates) { console.log(`Fetching historical rates from ${provider.name} for ${date}`); const rates = await provider.fetchHistoricalRates(date); console.log(`Successfully fetched historical rates from ${provider.name}`); return rates; } } catch (error) { lastError = error; console.warn(`Failed to fetch historical rates from ${provider.name}:`, error); } } throw new Error(`All providers failed for historical data. Last error: ${lastError?.message}`); } async fetchWithTimeout(provider, timeout) { return new Promise((resolve, reject) => { const timer = setTimeout(() => { reject(new Error(`Provider ${provider.name} timed out after ${timeout}ms`)); }, timeout); provider.fetchRatesSOS().then((rates) => { clearTimeout(timer); resolve(rates); }).catch((error) => { clearTimeout(timer); reject(error); }); }); } addFallbackProvider(provider) { this.config.fallbacks.push(provider); this.config.fallbacks.sort((a, b) => (a.priority || 999) - (b.priority || 999)); } setPrimaryProvider(provider) { this.config.primary = provider; } getProviderStatus() { const providers = [this.config.primary, ...this.config.fallbacks]; return providers.map((provider) => ({ name: provider.name, priority: provider.priority || 999, available: true // Could implement health checks here })); } }; } }); // src/historical.ts async function getHistoricalRates(date, provider) { if (!historicalService) { const providerManager = provider ? new ProviderManager({ primary: provider }) : void 0; historicalService = new HistoricalRateService(providerManager); } return historicalService.getHistoricalRates(date); } async function getRateHistory(currency, from, to) { if (!historicalService) { historicalService = new HistoricalRateService(); } return historicalService.getRateHistory({ currency, from, to }); } async function getVolatility(currency, period = "30d") { if (!historicalService) { historicalService = new HistoricalRateService(); } return historicalService.getVolatility(currency, period); } var import_node_path2, import_node_os, HistoricalRateService, historicalService; var init_historical = __esm({ "src/historical.ts"() { "use strict"; init_manager(); init_utils(); import_node_path2 = __toESM(require("path")); import_node_os = __toESM(require("os")); HistoricalRateService = class { providerManager; cachePath; constructor(providerManager) { this.providerManager = providerManager || new ProviderManager(); this.cachePath = import_node_path2.default.join(import_node_os.default.homedir(), ".sosx", "historical-cache.json"); } async getHistoricalRates(date) { const cached = await this.getCachedRates(); if (cached[date]) { console.log(`Using cached historical rates for ${date}`); return cached[date]; } try { const rates = await this.providerManager.fetchHistoricalRates(date); await this.cacheRates(date, rates); return rates; } catch (error) { console.warn(`Failed to fetch historical rates for ${date}:`, error); throw error; } } async getRateHistory(options) { const { from, to, currency, baseCurrency = "SOS" } = options; const startDate = new Date(from); const endDate = new Date(to); const results = []; const currentDate = new Date(startDate); while (currentDate <= endDate) { const dateStr = currentDate.toISOString().split("T")[0]; try { const rates = await this.getHistoricalRates(dateStr); const rate = baseCurrency === "SOS" ? rates[currency] : 1 / rates[currency]; results.push({ date: dateStr, rate }); } catch (error) { console.warn(`Skipping ${dateStr} due to error:`, error); } currentDate.setDate(currentDate.getDate() + 1); } return results; } async getVolatility(currency, period = "30d") { const days = parseInt(period.replace("d", "")); const endDate = /* @__PURE__ */ new Date(); const startDate = /* @__PURE__ */ new Date(); startDate.setDate(startDate.getDate() - days); const history = await this.getRateHistory({ from: startDate.toISOString().split("T")[0], to: endDate.toISOString().split("T")[0], currency }); if (history.length < 2) return 0; const returns = []; for (let i = 1; i < history.length; i++) { const dailyReturn = (history[i].rate - history[i - 1].rate) / history[i - 1].rate; returns.push(dailyReturn); } const mean = returns.reduce((sum, ret) => sum + ret, 0) / returns.length; const variance = returns.reduce((sum, ret) => sum + Math.pow(ret - mean, 2), 0) / returns.length; return Math.sqrt(variance) * Math.sqrt(365); } async getCachedRates() { const cached = await tryReadJSON(this.cachePath); return cached || {}; } async cacheRates(date, rates) { const cached = await this.getCachedRates(); cached[date] = rates; const cutoffDate = /* @__PURE__ */ new Date(); cutoffDate.setDate(cutoffDate.getDate() - 90); const cutoffStr = cutoffDate.toISOString().split("T")[0]; Object.keys(cached).forEach((date2) => { if (date2 < cutoffStr) { delete cached[date2]; } }); await tryWriteJSON(this.cachePath, cached); } }; } }); // src/analysis.ts var analysis_exports = {}; __export(analysis_exports, { MarketAnalyzer: () => MarketAnalyzer, SomaliaMarketService: () => SomaliaMarketService, analyzeMarket: () => analyzeMarket, detectAnomalies: () => detectAnomalies, getSomaliaMarketData: () => getSomaliaMarketData }); async function analyzeMarket(from, to, period) { if (!marketAnalyzer) { marketAnalyzer = new MarketAnalyzer(); } return marketAnalyzer.analyzeMarket(from, to, period); } async function getSomaliaMarketData() { if (!somaliaMarketService) { somaliaMarketService = new SomaliaMarketService(); } return somaliaMarketService.getSomaliaMarketData(); } async function detectAnomalies(from, to, options) { const { threshold, timeWindow } = options; const days = parseInt(timeWindow.replace(/[^\d]/g, "")); const endDate = /* @__PURE__ */ new Date(); const startDate = /* @__PURE__ */ new Date(); startDate.setDate(startDate.getDate() - days); const history = await getRateHistory( to, startDate.toISOString().split("T")[0], endDate.toISOString().split("T")[0] ); if (history.length < 2) { return { anomaly: false, deviation: 0, message: "Insufficient data" }; } const currentRate = history[history.length - 1].rate; const previousRate = history[history.length - 2].rate; const change = Math.abs((currentRate - previousRate) / previousRate); const anomaly = change > threshold; const message = anomaly ? `Anomaly detected: ${(change * 100).toFixed(2)}% change in ${from}/${to} rate` : "No anomaly detected"; return { anomaly, deviation: change, message }; } var MarketAnalyzer, SomaliaMarketService, marketAnalyzer, somaliaMarketService; var init_analysis = __esm({ "src/analysis.ts"() { "use strict"; init_historical(); MarketAnalyzer = class { async analyzeMarket(from, to, period = "30d") { const days = parseInt(period.replace("d", "")); const endDate = /* @__PURE__ */ new Date(); const startDate = /* @__PURE__ */ new Date(); startDate.setDate(startDate.getDate() - days); const history = await getRateHistory( to, startDate.toISOString().split("T")[0], endDate.toISOString().split("T")[0] ); if (history.length < 14) { throw new Error("Insufficient data for analysis (minimum 14 days required)"); } const rates = history.map((h) => h.rate); const volatility = await getVolatility(to, period); return { volatility, trend: this.calculateTrend(rates), support: this.calculateSupport(rates), resistance: this.calculateResistance(rates), rsi: this.calculateRSI(rates), sma: this.calculateSMA(rates, [7, 14, 30]), ema: this.calculateEMA(rates, [7, 14, 30]) }; } calculateTrend(rates) { if (rates.length < 2) return "neutral"; const recentRates = rates.slice(-7); const olderRates = rates.slice(-14, -7); const recentAvg = recentRates.reduce((sum, rate) => sum + rate, 0) / recentRates.length; const olderAvg = olderRates.reduce((sum, rate) => sum + rate, 0) / olderRates.length; const change = (recentAvg - olderAvg) / olderAvg; if (change > 0.02) return "bullish"; if (change < -0.02) return "bearish"; return "neutral"; } calculateSupport(rates) { const recentRates = rates.slice(-30); return Math.min(...recentRates); } calculateResistance(rates) { const recentRates = rates.slice(-30); return Math.max(...recentRates); } calculateRSI(rates, period = 14) { if (rates.length < period + 1) return 50; const changes = []; for (let i = 1; i < rates.length; i++) { changes.push(rates[i] - rates[i - 1]); } const recentChanges = changes.slice(-period); const gains = recentChanges.filter((change) => change > 0); const losses = recentChanges.filter((change) => change < 0).map((loss) => Math.abs(loss)); const avgGain = gains.length > 0 ? gains.reduce((sum, gain) => sum + gain, 0) / gains.length : 0; const avgLoss = losses.length > 0 ? losses.reduce((sum, loss) => sum + loss, 0) / losses.length : 0; if (avgLoss === 0) return 100; const rs = avgGain / avgLoss; return 100 - 100 / (1 + rs); } calculateSMA(rates, periods) { return periods.map((period) => { if (rates.length < period) return rates[rates.length - 1] || 0; const recentRates = rates.slice(-period); return recentRates.reduce((sum, rate) => sum + rate, 0) / recentRates.length; }); } calculateEMA(rates, periods) { return periods.map((period) => { if (rates.length < period) return rates[rates.length - 1] || 0; const multiplier = 2 / (period + 1); let ema = rates[0]; for (let i = 1; i < rates.length; i++) { ema = rates[i] * multiplier + ema * (1 - multiplier); } return ema; }); } }; SomaliaMarketService = class { async getSomaliaMarketData() { const baseRate = 570; return { regions: { mogadishu: { officialRate: baseRate, blackMarketRate: baseRate * 1.05, // 5% premium spread: 0.05, volume: 1e6, // Daily volume in USD lastUpdated: /* @__PURE__ */ new Date() }, hargeisa: { officialRate: baseRate, blackMarketRate: baseRate * 1.08, // 8% premium spread: 0.08, volume: 5e5, lastUpdated: /* @__PURE__ */ new Date() }, bosaso: { officialRate: baseRate, blackMarketRate: baseRate * 1.12, // 12% premium spread: 0.12, volume: 2e5, lastUpdated: /* @__PURE__ */ new Date() }, kismayo: { officialRate: baseRate, blackMarketRate: baseRate * 1.15, // 15% premium spread: 0.15, volume: 15e4, lastUpdated: /* @__PURE__ */ new Date() }, garowe: { officialRate: baseRate, blackMarketRate: baseRate * 1.1, // 10% premium spread: 0.1, volume: 1e5, lastUpdated: /* @__PURE__ */ new Date() } } }; } async getRegionalSpread(region) { const marketData = await this.getSomaliaMarketData(); const regionData = marketData.regions[region.toLowerCase()]; if (!regionData) { throw new Error(`Unknown region: ${region}`); } return regionData.spread; } async getBestRegionalRate(amount) { const marketData = await this.getSomaliaMarketData(); let bestRegion = ""; let bestRate = 0; let bestAmount = 0; for (const [region, data] of Object.entries(marketData.regions)) { const rate = data.blackMarketRate || data.officialRate; const convertedAmount = amount * rate; if (convertedAmount > bestAmount) { bestRegion = region; bestRate = rate; bestAmount = convertedAmount; } } const worstAmount = Math.min(...Object.values(marketData.regions).map( (data) => amount * (data.blackMarketRate || data.officialRate) )); const savings = bestAmount - worstAmount; return { region: bestRegion, rate: bestRate, savings }; } }; } }); // src/cli.ts var cli_exports = {}; __export(cli_exports, { help: () => help, main: () => main }); module.exports = __toCommonJS(cli_exports); // src/index.ts var import_node_os4 = __toESM(require("os")); var import_node_path5 = __toESM(require("path")); init_exchangeratehost(); // src/data/seed.json var seed_default = { USD: 175e-5, EUR: 16e-4, GBP: 135e-5, KES: 0.225, ETB: 0.102, AED: 64e-4, SAR: 66e-4, TRY: 0.056, CNY: 0.012 }; // src/index.ts init_utils(); // src/cache.ts var memoryCache = null; function getMemoryCache() { return memoryCache; } function setMemoryCache(c) { memoryCache = c; } // src/index.ts init_historical(); // src/alerts.ts init_utils(); var cron = __toESM(require("node-cron")); var nodemailer = __toESM(require("nodemailer")); var import_node_path3 = __toESM(require("path")); var import_node_os2 = __toESM(require("os")); var AlertManager = class { alertsPath; webhookConfigs = []; emailTransporter; monitoringTask; constructor() { this.alertsPath = import_node_path3.default.join(import_node_os2.default.homedir(), ".sosx", "alerts.json"); } async createAlert(alert) { const newAlert = { ...alert, id: this.generateId(), createdAt: /* @__PURE__ */ new Date(), active: true }; const alerts = await this.getAlerts(); alerts.push(newAlert); await this.saveAlerts(alerts); console.log(`Created alert: ${newAlert.from}/${newAlert.to} ${newAlert.direction} ${newAlert.threshold}`); this.startMonitoring(); return newAlert.id; } async updateAlert(id, updates) { const alerts = await this.getAlerts(); const index = alerts.findIndex((alert) => alert.id === id); if (index === -1) { throw new Error(`Alert with id ${id} not found`); } alerts[index] = { ...alerts[index], ...updates }; await this.saveAlerts(alerts); } async deleteAlert(id) { const alerts = await this.getAlerts(); const filtered = alerts.filter((alert) => alert.id !== id); if (filtered.length === alerts.length) { throw new Error(`Alert with id ${id} not found`); } await this.saveAlerts(filtered); console.log(`Deleted alert ${id}`); } async getAlerts() { const alerts = await tryReadJSON(this.alertsPath); return alerts || []; } async checkAlerts() { const alerts = await this.getAlerts(); const activeAlerts = alerts.filter((alert) => alert.active); if (activeAlerts.length === 0) return; try { const rates = await getRates(); for (const alert of activeAlerts) { const currentRate = rates[alert.to] / rates[alert.from]; const shouldTrigger = this.shouldTriggerAlert(alert, currentRate); if (shouldTrigger) { await this.triggerAlert(alert, currentRate); } } } catch (error) { console.error("Error checking alerts:", error); } } shouldTriggerAlert(alert, currentRate) { if (alert.direction === "above") { return currentRate > alert.threshold; } else { return currentRate < alert.threshold; } } async triggerAlert(alert, currentRate) { const message = `Alert triggered: ${alert.from}/${alert.to} is ${currentRate.toFixed(6)} (${alert.direction} ${alert.threshold})`; console.log(message); if (alert.webhook) { await this.sendWebhook(alert.webhook, { type: "alert-triggered", alert, currentRate, message, timestamp: (/* @__PURE__ */ new Date()).toISOString() }); } if (alert.email && this.emailTransporter) { await this.sendEmail(alert.email, "Rate Alert Triggered", message); } for (const config of this.webhookConfigs) { if (config.events.includes("alert-triggered") && config.currencies.includes(alert.from) && config.currencies.includes(alert.to)) { await this.sendWebhook(config.url, { type: "alert-triggered", alert, currentRate, message, timestamp: (/* @__PURE__ */ new Date()).toISOString() }); } } } async sendWebhook(url, payload) { try { const response = await fetch(url, { method: "POST", headers: { "Content-Type": "application/json", "User-Agent": "Somali-Exchange-Rates/1.0" }, body: JSON.stringify(payload) }); if (!response.ok) { console.warn(`Webhook failed: ${response.status} ${response.statusText}`); } } catch (error) { console.error("Webhook error:", error); } } async sendEmail(to, subject, text2) { if (!this.emailTransporter) return; try { await this.emailTransporter.sendMail({ from: process.env.SMTP_FROM || "alerts@sosx.com", to, subject, text: text2 }); } catch (error) { console.error("Email error:", error); } } setupEmailTransporter(config) { this.emailTransporter = nodemailer.createTransporter(config); } addWebhookConfig(config) { this.webhookConfigs.push(config); } startMonitoring(interval = "*/5 * * * *") { if (this.monitoringTask) { this.monitoringTask.stop(); } this.monitoringTask = cron.schedule(interval, async () => { await this.checkAlerts(); }, { scheduled: false }); this.monitoringTask.start(); console.log(`Started alert monitoring with interval: ${interval}`); } stopMonitoring() { if (this.monitoringTask) { this.monitoringTask.stop(); this.monitoringTask = void 0; console.log("Stopped alert monitoring"); } } async saveAlerts(alerts) { await tryWriteJSON(this.alertsPath, alerts); } generateId() { return Math.random().toString(36).substr(2, 9); } }; var alertManager; function getAlertManager() { if (!alertManager) { alertManager = new AlertManager(); } return alertManager; } async function setRateAlert(from, to, threshold, direction, options = {}) { const manager = getAlertManager(); return manager.createAlert({ from, to, threshold, direction, webhook: options.webhook, email: options.email }); } async function removeRateAlert(id) { const manager = getAlertManager(); return manager.deleteAlert(id); } async function listRateAlerts() { const manager = getAlertManager(); return manager.getAlerts(); } function startAlertMonitoring(interval) { const manager = getAlertManager(); manager.startMonitoring(interval); } // src/transfer-fees.ts var PROVIDER_FEES = { "western-union": { "bank-transfer": { fixedFee: 5, percentageFee: 0.015, // 1.5% exchangeRateMargin: 0.02, // 2% margin on exchange rate estimatedTime: "1-3 business days", minimumFee: 5, maximumFee: 50 }, "cash-pickup": { fixedFee: 8, percentageFee: 0.02, // 2% exchangeRateMargin: 0.025, // 2.5% margin estimatedTime: "Within minutes", minimumFee: 8, maximumFee: 75 }, "mobile-money": { fixedFee: 3, percentageFee: 0.01, // 1% exchangeRateMargin: 0.015, // 1.5% margin estimatedTime: "Within minutes", minimumFee: 3, maximumFee: 25 } }, "remitly": { "bank-transfer": { fixedFee: 3.99, percentageFee: 0.01, // 1% exchangeRateMargin: 0.015, // 1.5% margin estimatedTime: "1-2 business days", minimumFee: 3.99, maximumFee: 30 }, "cash-pickup": { fixedFee: 4.99, percentageFee: 0.015, // 1.5% exchangeRateMargin: 0.02, // 2% margin estimatedTime: "Within minutes", minimumFee: 4.99, maximumFee: 40 }, "mobile-money": { fixedFee: 1.99, percentageFee: 5e-3, // 0.5% exchangeRateMargin: 0.01, // 1% margin estimatedTime: "Within minutes", minimumFee: 1.99, maximumFee: 15 } }, "worldremit": { "bank-transfer": { fixedFee: 2.99, percentageFee: 0.012, // 1.2% exchangeRateMargin: 0.018, // 1.8% margin estimatedTime: "1-2 business days", minimumFee: 2.99, maximumFee: 35 }, "cash-pickup": { fixedFee: 5.99, percentageFee: 0.018, // 1.8% exchangeRateMargin: 0.022, // 2.2% margin estimatedTime: "Within minutes", minimumFee: 5.99, maximumFee: 45 }, "mobile-money": { fixedFee: 2.49, percentageFee: 8e-3, // 0.8% exchangeRateMargin: 0.012, // 1.2% margin estimatedTime: "Within minutes", minimumFee: 2.49, maximumFee: 20 } }, "wise": { "bank-transfer": { fixedFee: 1.5, percentageFee: 5e-3, // 0.5% exchangeRateMargin: 5e-3, // 0.5% margin (Wise uses mid-market rate) estimatedTime: "1-2 business days", minimumFee: 1.5, maximumFee: 15 }, "cash-pickup": { fixedFee: 0, // Not available percentageFee: 0, exchangeRateMargin: 0, estimatedTime: "Not available", minimumFee: 0, maximumFee: 0 }, "mobile-money": { fixedFee: 2, percentageFee: 7e-3, // 0.7% exchangeRateMargin: 8e-3, // 0.8% margin estimatedTime: "Within hours", minimumFee: 2, maximumFee: 12 } } }; async function calculateTransferFee(options) { const { amount, from, to, provider, method } = options; const providerFees = PROVIDER_FEES[provider]; if (!providerFees) { throw new Error(`Unsupported provider: ${provider}`); } const feeStructure = providerFees[method]; if (!feeStructure) { throw new Error(`Method ${method} not available for ${provider}`); } if (feeStructure.fixedFee === 0 && feeStructure.percentageFee === 0) { throw new Error(`${method} is not available for ${provider}`); } const midMarketRate = await convert(1, from, to); const providerRate = midMarketRate * (1 - feeStructure.exchangeRateMargin); const percentageFee = amount * feeStructure.percentageFee; let totalFee = feeStructure.fixedFee + percentageFee; if (feeStructure.minimumFee && totalFee < feeStructure.minimumFee) { totalFee = feeStructure.minimumFee; } if (feeStructure.maximumFee && totalFee > feeStructure.maximumFee) { totalFee = feeStructure.maximumFee; } const totalCost = amount + totalFee; const recipientAmount = amount * providerRate; return { fee: totalFee, exchangeRate: providerRate, totalCost, recipientAmount, provider, estimatedTime: feeStructure.estimatedTime }; } async function compareTransferOptions(amount, from, to, method) { const providers = ["western-union", "remitly", "worldremit", "wise"]; const results = []; for (const provider of providers) { try { const result = await calculateTransferFee({ amount, from, to, provider, method }); results.push(result); } catch (error) { console.warn(`Failed to calculate fees for ${provider}:`, error); } } return results.sort((a, b) => a.totalCost - b.totalCost); } async function getBestTransferOption(amount, from, to) { const methods = ["bank-transfer", "cash-pickup", "mobile-money"]; let bestOption = null; for (const method of methods) { try { const options = await compareTransferOptions(amount, from, to, method); if (options.length > 0) { const best = options[0]; if (!bestOption || best.totalCost < bestOption.result.totalCost) { bestOption = { method, result: best }; } } } catch (error) { console.warn(`Failed to compare options for ${method}:`, error); } } if (!bestOption) { throw new Error("No transfer options available"); } return bestOption; } // src/index.ts init_analysis(); // src/export.ts init_historical(); init_analysis(); var XLSX = __toESM(require("xlsx")); var import_csv_writer = require("csv-writer"); var import_node_fs = require("fs"); var ExportService = class { async exportRates(options) { const { format, period, currencies, output } = options; const days = parseInt(period.replace(/[^\d]/g, "")); const endDate = /* @__PURE__ */ new Date(); const startDate = /* @__PURE__ */ new Date(); startDate.setDate(startDate.getDate() - days); const data = []; for (const currency of currencies) { if (currency === "SOS") continue; try { const history = await getRateHistory( currency, startDate.toISOString().split("T")[0], endDate.toISOString().split("T")[0] ); history.forEach((record) => { data.push({ date: record.date, currency, rate: record.rate, baseCurrency: "SOS" }); }); } catch (error) { console.warn(`Failed to get history for ${currency}:`, error); } } data.sort((a, b) => new Date(a.date).getTime() - new Date(b.date).getTime()); const filename = output || this.generateFilename(format, period, currencies); switch (format) { case "csv": return this.exportToCSV(data, filename); case "xlsx": return this.exportToXLSX(data, filename); case "json": return this.exportToJSON(data, filename); case "pdf": return this.exportToPDF(data, filename); default: throw new Error(`Unsupported export format: ${format}`); } } async exportAnalysisReport(currencies, period, output) { const analysisData = []; for (const currency of currencies) { if (currency === "SOS") continue; try { const analysis = await analyzeMarket("SOS", currency, period); analysisData.push({ currency, baseCurrency: "SOS", period, volatility: analysis.volatility, trend: analysis.trend, support: analysis.support, resistance: analysis.resistance, rsi: analysis.rsi, sma7: analysis.sma[0], sma14: analysis.sma[1], sma30: analysis.sma[2], ema7: analysis.ema[0], ema14: analysis.ema[1], ema30: analysis.ema[2], generatedAt: (/* @__PURE__ */ new Date()).toISOString() }); } catch (error) { console.warn(`Failed to analyze ${currency}:`, error); } } const filename = output || `analysis-report-${period}-${Date.now()}.xlsx`; return this.exportToXLSX(analysisData, filename); } async exportToCSV(data, filename) { if (data.length === 0) { throw new Error("No data to export"); } const csvWriter = (0, import_csv_writer.createObjectCsvWriter)({ path: filename, header: Object.keys(data[0]).map((key) => ({ id: key, title: key })) }); await csvWriter.writeRecords(data); console.log(`Exported ${data.length} records to ${filename}`); return filename; } async exportToXLSX(data, filename) { if (data.length === 0) { throw new Error("No data to export"); } const worksheet = XLSX.utils.json_to_sheet(data); const workbook = XLSX.utils.book_new(); XLSX.utils.book_append_sheet(workbook, worksheet, "Exchange Rates"); const range = XLSX.utils.decode_range(worksheet["!ref"] || "A1"); const colWidths = []; for (let col = range.s.c; col <= range.e.c; col++) { let maxWidth = 10; for (let row = range.s.r; row <= range.e.r; row++) { const cellAddress = XLSX.utils.encode_cell({ r: row, c: col }); const cell = worksheet[cellAddress]; if (cell && cell.v) { const cellLength = cell.v.toString().length; maxWidth = Math.max(maxWidth, cellLength); } } colWidths.push({ wch: Math.min(maxWidth + 2, 50) }); } worksheet["!cols"] = colWidths; XLSX.writeFile(workbook, filename); console.log(`Exported ${data.length} records to ${filename}`); return filename; } async exportToJSON(data, filename) { const jsonData = { exportedAt: (/* @__PURE__ */ new Date()).toISOString(), recordCount: data.length, data }; await import_node_fs.promises.writeFile(filename, JSON.stringify(jsonData, null, 2)); console.log(`Exported ${data.length} records to ${filename}`); return filename; } async exportToPDF(data, filename) { const html = this.generateHTMLReport(data); const htmlFilename = filename.replace(".pdf", ".html"); await import_node_fs.promises.writeFile(htmlFilename, html); console.log(`Exported HTML report to ${htmlFilename} (PDF conversion requires additional library)`); return htmlFilename; } generateHTMLReport(data) { const headers = data.length > 0 ? Object.keys(data[0]) : []; return ` <!DOCTYPE html> <html> <head> <title>Somali Exchange Rates Report</title> <style> body { font-family: Arial, sans-serif; margin: 20px; } table { border-collapse: collapse; width: 100%; } th, td { border: 1px solid #ddd; padding: 8px; text-align: left; } th { background-color: #f2f2f2; } .header { margin-bottom: 20px; } .summary { margin-bottom: 20px; padding: 10px; background-color: #f9f9f9; } </style> </head> <body> <div class="header"> <h1>Somali Exchange Rates Report</h1> <p>Generated on: ${(/* @__PURE__ */ new Date()).toLocaleString()}</p> </div> <div class="summary"> <h2>Summary</h2> <p>Total Records: ${data.length}</p> <p>Currencies: ${[...new Set(data.map((d) => d.currency))].join(", ")}</p> <p>Date Range: ${data.length > 0 ? `${data[0].date} to ${data[data.length - 1].date}` : "N/A"}</p> </div> <table> <thead> <tr> ${headers.map((header) => `<th>${header}</th>`).join("")} </tr> </thead> <tbody> ${data.map((row) => ` <tr> ${headers.map((header) => `<td>${row[header] || ""}</td>`).join("")} </tr> `).join("")} </tbody> </table> </body> </html>`; } generateFilename(format, period, currencies) { const timestamp = (/* @__PURE__ */ new Date()).toISOString().split("T")[0]; const currencyList = currencies.slice(0, 3).join("-"); return `exchange-rates-${currencyList}-${period}-${timestamp}.${format}`; } }; var exportService; async function exportRates(options) { if (!exportService) { exportService = new ExportService(); } return exportService.exportRates(options); } async function exportAnalysisReport(currencies, period, output) { if (!exportService) { exportService = new ExportService(); } return exportService.exportAnalysisReport(currencies, period, output); } // src/config.ts init_utils(); var import_node_path4 = __toESM(require("path")); var import_node_os3 = __toESM(require("os")); var DEFAULT_CONFIG = { defaultCurrencies: ["USD", "EUR", "GBP", "KES", "ETB"], language: "en", locale: "en-US", notifications: {}, providers: { primary: "exchangerate-host", fallbacks: ["fixer", "currencyapi"] } }; var ConfigManager = class { configPath; config; constructor() { this.configPath = import_node_path4.default.join(import_node_os3.default.homedir(), ".sosx", "config.json"); this.config = { ...DEFAULT_CONFIG }; } async loadConfig() { try { const savedConfig = await tryReadJSON(this.configPath); if (savedConfig) { this.config = { ...DEFAULT_CONFIG, ...savedConfig }; } } catch (error) { console.warn("Failed to load config, using defaults:", error); } return this.config; } async saveConfig() { await tryWriteJSON(this.configPath, this.config); console.log(`Configuration saved to ${this.configPath}`); } getConfig() { return { ...this.config }; } // Language and Locale setLanguage(language) { this.config.language = language; switch (language) { case "so": this.config.locale = "so-SO"; break; case "ar": this.config.locale = "ar-SA"; break; default: this.config.locale = "en-US"; } } setLocale(locale) { this.config.locale = locale; } getLanguage() { return this.config.language; } getLocale() { return this.config.locale; } // Default Currencies setDefaultCurrencies(currencies) { this.config.defaultCurrencies = currencies; } addDefaultCurrency(currency) { if (!this.config.defaultCurrencies.includes(currency)) { this.config.defaultCurrencies.push(currency); } } removeDefaultCurrency(currency) { this.config.defaultCurrencies = this.config.defaultCurrencies.filter((c) => c !== currency); } getDefaultCurrencies() { return [...this.config.defaultCurrencies]; } // Notifications setEmailNotification(email) { this.config.notifications.email = email; } setWebhookNotification(webhook) { this.config.notifications.webhook = webhook; } removeEmailNotification() { delete this.config.notifications.email; } removeWebhookNotification() { delete this.config.notifications.webhook; } getNotificationSettings() { return { ...this.config.notifications }; } // Providers setPrimaryProvider(provider) { this.config.providers.primary = provider; } setFallbackProviders(providers) { this.config.providers.fallbacks = providers; } addFallbackProvider(provider) { if (!this.config.providers.fallbacks.includes(provider)) { this.config.providers.fallbacks.push(provider); } } removeFallbackProvider(provider) { this.config.providers.fallbacks = this.config.providers.fallbacks.filter((p) => p !== provider); } getProviderSettings() { return { ...this.config.providers }; } // Database setDatabaseConfig(config) { this.config.database = config; } removeDatabaseConfig() { delete this.config.database; } getDatabaseConfig() { return this.config.database ? { ...this.config.database } : void 0; } // Validation validateConfig() { const errors = []; const validCurrencies = ["SOS", "USD", "EUR", "GBP", "KES", "ETB", "AED", "SAR", "TRY", "CNY"]; for (const currency of this.config.defaultCurrencies) { if (!validCurrencies.includes(currency)) { errors.push(`Invalid currency: ${currency}`); } } const validLanguages = ["en", "so", "ar"]; if (!validLanguages.includes(this.config.language)) { errors.push(`Invalid language: ${this.config.language}`); } const validLocales = ["en-US", "so-SO", "ar-SA"]; if (!validLocales.includes(this.config.locale)) { errors.push(`Invalid locale: ${this.config.locale}`); } if (this.config.notifications.email) { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!emailRegex.test(this.config.notifications.email)) { errors.push(`Invalid email format: ${this.config.notifications.email}`); } } if (this.config.notifications.webhook) { try { new URL(this.config.notifications.webhook); } catch { errors.push(`Invalid webhook URL: ${this.config.notifications.webhook}`); } } return { valid: errors.length === 0, errors }; } // Reset to defaults resetToDefaults() { this.config = { ...DEFAULT_CONFIG }; } // Export/Import exportConfig() { return JSON.stringify(this.config, null, 2); } importConfig(configJson) { try { const importedConfig = JSON.parse(configJson); this.config = { ...DEFAULT_CONFIG, ...importedConfig }; const validation = this.validateConfig(); if (!validation.valid) { throw new Error(`Invalid configuration: ${validation.errors.join(", ")}`); } } catch (error) { throw new Error(`Failed to import configuration: ${error}`); } } }; var configManager; function getConfigManager() { if (!configManager) { configManager = new ConfigManager(); } return configManager; } async function loadUserConfig() { return getConfigManager().loadConfig(); } function getUserConfig() { return getConfigManager().getConfig(); } async function runConfigWizard() { const manager = getConfigManager(); console.log("\u{1F527} Somali Exchange Rates Configuration Wizard"); console.log("This will help you set up your preferences.\n"); manager.setLanguage("en"); manager.setDefaultCurrencies(["USD", "EUR", "GBP", "KES", "ETB"]); manager.setPrimaryProvider("exchangerate-host"); manager.setFallbackProviders(["fixer", "currencyapi"]); await manager.saveConfig(); console.log("\u2705 Configuration saved successfully!"); console.log(`Configuration file: ${manager["configPath"]}`); } // src/realtime.ts var import_ws = require("ws"); var import_events = require("events"); var cron2 = __toESM(require("node-cron")); var RateStreamServer = class extends import_events.EventEmitter { server; clients = /* @__PURE__ */ new Set(); updateTask; lastRates = {}; constructor(port = 8080) { super(); this.server = new import_ws.WebSocketServer({ port }); this.setupServer(); } setupServer() { this.server.on("connection", (ws) => { console.log("New WebSocket client connected"); this.clients.add(ws); this.sendCurrentRates(ws); ws.on("message", (message) => { try { const data = JSON.parse(message); this.handleClientMessage(ws, data); } catch (error) { console.error("Invalid message from client:", error); } }); ws.on("close", () => { console.log("WebSocket client disconnected"); this.clients.delete(ws); }); ws.on("error", (error) => { console.error("WebSocket error:", error); this.clients.delete(ws); }); }); console.log(`Rate stream server started on port ${this.server.options.port}`); } handleClientMessage(ws, data) { switch (data.type) { case "subscribe": if (data.currencies && Array.isArray(data.currencies)) { ws.send(JSON.stringify({ type: "subscription_confirmed", currencies: data.currencies, timestamp: (/* @__PURE__ */ new Date()).toISOString() })); } break; case "ping": ws.send(JSON.stringify({ type: "pong", timestamp: (/* @__PURE__ */ new Date()).toISOString() })); break; } } async sendCurrentRates(ws) { try { const rates = await getRates(); ws.send(JSON.stringify({ type: "current_rates", rates, timestamp: (/* @__PURE__ */ new Date()).toISOString() })); } catch (error) { console.error("Failed to send current rates:", error); } } startUpdates(interval = "*/1 * * * *") { if (this.updateTask) { this.updateTask.stop(); } this.updateTask = cron2.schedule(interval, async () => { await this.checkForUpdates(); }, { scheduled: false }); this.updateTask.start(); console.log(`Started rate updates with interval: ${interval}`); } stopUpdates() { if (this.updateTask) { this.updateTask.stop(); this.updateTask = void 0; console.log("Stopped rate updates"); } } async checkForUpdates() { try { const newRates = await getRates(); const updates = []; for (const [currency, rate] of Object.entries(newRates)) { const previousRate = this.lastRates[currency]; if (previousRate && previousRate !== rate) { const change = rate - previousRate; const changePercent = change / previousRate * 100; updates.push({ from: "SOS", to: currency, rate, previousRate, change, changePercent, timestamp: (/* @__PURE__ */ new Date()).toISOString() }); } } if (updates.length > 0) { this.broadcastUpdates(updates); this.emit("rate-updates", updates); } this.lastRates = newRates; } catch (error) { console.error("Failed to check for rate updates:", error); } } broadcastUpdates(updates) { const message = JSON.stringify({ type: "rate_updates", updates, timestamp: (/* @__PURE__ */ new Date()).toISOString() }); this.clients.forEach((client) => { if (client.readyState === import_ws.WebSocket.OPEN) { client.send(message); } }); console.log(`Broadcasted ${updates.length} rate updates to ${this.clients.size} clients`); } getConnectedClients() { return this.clients.size; } close() { this.stopUpdates(); this.clients.forEach((client) => client.close()); this.server.close(); console.log("Rate stream server closed"); } }; var RateStreamClient = class extends import_events.EventEmitter { ws; url; reconnectAttempts = 0; maxReconnectAttempts = 5; reconnectDelay = 1e3; constructor(url = "ws://localhost:8080") { super(); this.url = url; } connect() { try { this.ws = new import_ws.WebSocket(this.url); this.ws.on("open", () => { console.log("Connected to rate stream server"); this.reconnectAttempts = 0; this.emit("connected"); }); this.ws.on("message", (data) => { try { const message = JSON.parse(data); this.handleMessage(message); } catch (error) { console.error("Failed to parse message:", error); } }); this.ws.on("close", () => { console.log("Disconnected from rate stream server"); this.emit("disconnected"); this.attemptReconnect(); }); this.ws.on("error", (error) => { console.error("WebSocket error:", error); this.emit("error", error); }); } catch (error) { console.error("Failed to connect to rate stream server:", error); this.attemptReconnect(); } } handleMessage(message) { switch (message.type) { case "current_rates": this.emit("current-rates", message.rates); break; case "rate_updates": this.emit("rate-updates", message.updates); break; case "subscription_confirmed": this.emit("subscription-confirmed", message.currencies); break; case "pong": this.emit("pong"); break; } } subscribe(currencies) { if (this.ws && this.ws.readyState === import_ws.WebSocket.OPEN) { this.ws.send(JSON.stringify({ type: "subscribe", currencies })); } } ping() { if (this.ws && this.ws.readyState === import_ws.WebSocket.OPEN) { this.ws.send(JSON.stringify({ type: "ping" })); } } attemptReconnect() { if (this.reconnectAttempts < this.maxReconnectAttempts) { this.reconnectAttempts++; const delay = this.reconnectDelay * Math