sol-mem-gen/quotes-service.ts
adwait 36f298ff96 Use MTM price based on USDC for meme generation (#7)
Part of https://www.notion.so/Use-MTM-price-based-on-pool-189a6b22d47280ff9373c89f40b378d1

Co-authored-by: Adw8 <adwaitgharpure@gmail.com>
Co-authored-by: Nabarun <nabarun@deepstacksoft.com>
Co-authored-by: Adwait Gharpure <adwaitgharpure@gmail.com>
Reviewed-on: #7
Co-authored-by: adwait <adwait@noreply.git.vdb.to>
Co-committed-by: adwait <adwait@noreply.git.vdb.to>
2025-01-30 15:03:09 +00:00

46 lines
1.4 KiB
TypeScript

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<void> {
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 };