UNPKG

medusa-payment-solana

Version:

Solana crypto currency payment provider for MedusaJS 2.0

52 lines 1.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CoinGeckoConverter = void 0; class CoinGeckoConverter { constructor(apiKey) { this.cache = null; this.CACHE_DURATION = 5 * 60 * 1000; // 5 minutes if (!apiKey) { throw new Error('CoinGecko API key is required'); } this.apiKey = apiKey; } async convertToSol(amount, currencyCode) { const rate = await this.getRate(currencyCode); return parseFloat((amount / rate).toFixed(9)); } async getRate(currencyCode) { // Use cached rate if available and not expired if (this.cache && Date.now() - this.cache.timestamp < this.CACHE_DURATION) { return this.cache.rate; } try { const response = await fetch(`https://api.coingecko.com/api/v3/simple/price?ids=solana&vs_currencies=${currencyCode.toLowerCase()}`, { headers: { 'x-cg-demo-api-key': this.apiKey } }); if (!response.ok) { throw new Error(`CoinGecko API error: ${response.statusText}`); } const data = await response.json(); const rate = data.solana?.[currencyCode.toLowerCase()]; if (!rate) { throw new Error(`Invalid rate data for ${currencyCode}`); } // Update cache this.cache = { rate, timestamp: Date.now() }; return rate; } catch (error) { if (error instanceof Error) { throw new Error(`Failed to fetch SOL/${currencyCode} rate: ${error.message}`); } throw new Error(`Failed to fetch SOL/${currencyCode} rate`); } } } exports.CoinGeckoConverter = CoinGeckoConverter; //# sourceMappingURL=coingecko-converter.js.map