Update variable names in quotes service

This commit is contained in:
Adw8 2025-01-30 17:42:55 +05:30
parent b886aca067
commit 2806fd344a
6 changed files with 19 additions and 20 deletions

View File

@ -10,7 +10,8 @@ const MTM_TOKEN_MINT = process.env.NEXT_PUBLIC_MTM_TOKEN_MINT;
const USDC_MINT = process.env.NEXT_PUBLIC_USDC_MINT; const USDC_MINT = process.env.NEXT_PUBLIC_USDC_MINT;
class QuotesService { class QuotesService {
private cachedQuotes: BN[] = []; // Stores the MTM amount for 1 USDC
private cachedMTMAmounts: BN[] = [];
async fetchAndCacheQuotes(): Promise<void> { async fetchAndCacheQuotes(): Promise<void> {
try { try {
@ -25,19 +26,19 @@ class QuotesService {
// Handle price with Big.js, then convert to BN // Handle price with Big.js, then convert to BN
const priceMTMFor1USDC = new Big(quoteResponse['data'][USDC_MINT]['price']).toFixed(6); const priceMTMFor1USDC = new Big(quoteResponse['data'][USDC_MINT]['price']).toFixed(6);
const priceInBaseUnits = new BN(new Big(priceMTMFor1USDC).times(new Big(10).pow(6)).toString()); const priceMTMFor1USDCInBaseUnits = new BN(new Big(priceMTMFor1USDC).times(new Big(10).pow(6)).toString());
this.cachedQuotes.push(priceInBaseUnits); this.cachedMTMAmounts.push(priceMTMFor1USDCInBaseUnits);
if (this.cachedQuotes.length > 3) { if (this.cachedMTMAmounts.length > 3) {
this.cachedQuotes.shift(); this.cachedMTMAmounts.shift();
} }
} catch (error) { } catch (error) {
console.error('Error fetching quotes:', error); console.error('Error fetching quotes:', error);
} }
} }
getQuotes(): BN[] { getMTMAmountsFor1USDC(): BN[] {
return this.cachedQuotes; return this.cachedMTMAmounts;
} }
} }

View File

@ -2,9 +2,8 @@ import { createServer } from 'http';
import { parse } from 'url'; import { parse } from 'url';
import next from 'next'; import next from 'next';
// Reference: https://www.dotenv.org/docs/quickstart#initial-setup // Reference: https://github.com/motdotla/dotenv?tab=readme-ov-file#how-do-i-use-dotenv-with-import
import dotenv from 'dotenv'; import 'dotenv/config'
dotenv.config();
import { QuotesService } from './quotes-service'; import { QuotesService } from './quotes-service';

View File

@ -47,14 +47,14 @@ export async function POST(req: NextRequest): Promise<NextResponse> {
) )
} }
const quotes: BN[] = (global as any).quotesService.getQuotes(); const amountOfMTM: BN[] = (global as any).quotesService.getMTMAmountsFor1USDC();
const lowestQuote = quotes.reduce((minQuote, currentQuote) => BN.min(minQuote, currentQuote), quotes[0]); const lowestAmountOfMTM = amountOfMTM.reduce((minQuote, currentQuote) => BN.min(minQuote, currentQuote), amountOfMTM[0]);
const scale = new BN(100); const scale = new BN(100);
const scaledCost = new BN(model.cost * 100); const scaledModelCost = new BN(model.cost).mul(scale);
const tokenAmount = scaledCost.mul(new BN(lowestQuote)).div(scale); const lowestTokenAmount = scaledModelCost.mul(new BN(lowestAmountOfMTM)).div(scale);
const isPaymentVerified = await verifyPayment(transactionSignature, tokenAmount); const isPaymentVerified = await verifyPayment(transactionSignature, lowestTokenAmount);
if (!isPaymentVerified) { if (!isPaymentVerified) {
return NextResponse.json( return NextResponse.json(

View File

@ -3,9 +3,9 @@ import { NextRequest, NextResponse } from 'next/server';
export async function GET(req: NextRequest) { export async function GET(req: NextRequest) {
try { try {
const quotes: BN[] = (global as any).quotesService.getQuotes(); const amountOfMTM: BN[] = (global as any).quotesService.getMTMAmountsFor1USDC();
const quotesAsString = quotes.map(quote => quote.toString()); const amountOfMTMAsString = amountOfMTM.map(amount => amount.toString());
return NextResponse.json(quotesAsString); return NextResponse.json(amountOfMTMAsString);
} catch (error) { } catch (error) {
return NextResponse.json({ error: 'Failed to fetch quotes' }, { status: 500 }); return NextResponse.json({ error: 'Failed to fetch quotes' }, { status: 500 });
} }

View File

@ -20,8 +20,6 @@ interface GenerationState {
} }
const formatBNWithDecimals = (value: BN, decimals: number): string => { const formatBNWithDecimals = (value: BN, decimals: number): string => {
if (value.isZero()) return '0.' + '0'.repeat(decimals); // Handle zero case
const bigValue = new Big(value.toString()); const bigValue = new Big(value.toString());
const factor = new Big(10).pow(decimals); const factor = new Big(10).pow(decimals);

View File

@ -1,3 +1,4 @@
// Reference: https://github.com/vercel/next.js/blob/canary/examples/custom-server/tsconfig.server.json
{ {
"extends": "./tsconfig.json", "extends": "./tsconfig.json",
"compilerOptions": { "compilerOptions": {