sol-mem-gen/quotes-service.ts
adwait 7300d0133d Create WSOL lock for every fourth verified tweet (#12)
Part of https://www.notion.so/Option-to-post-paid-for-memes-to-twitter-x-18ca6b22d4728051804ef4f55065d5ba

Co-authored-by: AdityaSalunkhe21 <adityasalunkhe2204@gmail.com>
Co-authored-by: Adw8 <adwaitgharpure@gmail.com>
Co-authored-by: IshaVenikar <ishavenikar7@gmail.com>
Reviewed-on: #12
Co-authored-by: adwait <adwait@noreply.git.vdb.to>
Co-committed-by: adwait <adwait@noreply.git.vdb.to>
2025-02-06 12:48:52 +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_ADDRESS, 'USDC_MINT_ADDRESS is required');
assert(process.env.NEXT_PUBLIC_MTM_MINT_ADDRESS, 'MTM_MINT_ADDRESS is required');
const MTM_TOKEN_MINT = process.env.NEXT_PUBLIC_MTM_MINT_ADDRESS;
const USDC_MINT = process.env.NEXT_PUBLIC_USDC_MINT_ADDRESS;
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 };