import assert from "assert"; import BN from "bn.js"; import fetch from 'node-fetch'; import Big from 'big.js'; assert(process.env.NEXT_PUBLIC_USDC_MINT, 'USDC_MINT is required'); assert(process.env.NEXT_PUBLIC_MTM_TOKEN_MINT, 'MTM_TOKEN_MINT is required'); const MTM_TOKEN_MINT = process.env.NEXT_PUBLIC_MTM_TOKEN_MINT; const USDC_MINT = process.env.NEXT_PUBLIC_USDC_MINT; class QuotesService { // Stores the MTM amount for 1 USDC private cachedMTMAmounts: BN[] = []; async fetchAndCacheQuotes(): Promise { try { const url = `https://api.jup.ag/price/v2?ids=${USDC_MINT}&vsToken=${MTM_TOKEN_MINT}`; const response = await fetch(url); if (!response.ok) { throw new Error(`Failed to fetch quote: ${response.statusText}`); } const quoteResponse = await response.json(); // Handle price with big.js, then convert to bn.js instance const priceMTMFor1USDC = new Big(quoteResponse['data'][USDC_MINT]['price']).toFixed(6); const priceMTMFor1USDCInBaseUnits = new BN(new Big(priceMTMFor1USDC).times(new Big(10).pow(6)).toString()); this.cachedMTMAmounts.push(priceMTMFor1USDCInBaseUnits); if (this.cachedMTMAmounts.length > 3) { this.cachedMTMAmounts.shift(); } } catch (error) { console.error('Error fetching quotes:', error); } } getMTMAmountsFor1USDC(): BN[] { return this.cachedMTMAmounts; } } export { QuotesService };