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;
class QuotesService {
private cachedQuotes: BN[] = [];
// Stores the MTM amount for 1 USDC
private cachedMTMAmounts: BN[] = [];
async fetchAndCacheQuotes(): Promise<void> {
try {
@ -25,19 +26,19 @@ class QuotesService {
// Handle price with Big.js, then convert to BN
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);
if (this.cachedQuotes.length > 3) {
this.cachedQuotes.shift();
this.cachedMTMAmounts.push(priceMTMFor1USDCInBaseUnits);
if (this.cachedMTMAmounts.length > 3) {
this.cachedMTMAmounts.shift();
}
} catch (error) {
console.error('Error fetching quotes:', error);
}
}
getQuotes(): BN[] {
return this.cachedQuotes;
getMTMAmountsFor1USDC(): BN[] {
return this.cachedMTMAmounts;
}
}

View File

@ -2,9 +2,8 @@ import { createServer } from 'http';
import { parse } from 'url';
import next from 'next';
// Reference: https://www.dotenv.org/docs/quickstart#initial-setup
import dotenv from 'dotenv';
dotenv.config();
// Reference: https://github.com/motdotla/dotenv?tab=readme-ov-file#how-do-i-use-dotenv-with-import
import 'dotenv/config'
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 lowestQuote = quotes.reduce((minQuote, currentQuote) => BN.min(minQuote, currentQuote), quotes[0]);
const amountOfMTM: BN[] = (global as any).quotesService.getMTMAmountsFor1USDC();
const lowestAmountOfMTM = amountOfMTM.reduce((minQuote, currentQuote) => BN.min(minQuote, currentQuote), amountOfMTM[0]);
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 isPaymentVerified = await verifyPayment(transactionSignature, tokenAmount);
const lowestTokenAmount = scaledModelCost.mul(new BN(lowestAmountOfMTM)).div(scale);
const isPaymentVerified = await verifyPayment(transactionSignature, lowestTokenAmount);
if (!isPaymentVerified) {
return NextResponse.json(

View File

@ -3,9 +3,9 @@ import { NextRequest, NextResponse } from 'next/server';
export async function GET(req: NextRequest) {
try {
const quotes: BN[] = (global as any).quotesService.getQuotes();
const quotesAsString = quotes.map(quote => quote.toString());
return NextResponse.json(quotesAsString);
const amountOfMTM: BN[] = (global as any).quotesService.getMTMAmountsFor1USDC();
const amountOfMTMAsString = amountOfMTM.map(amount => amount.toString());
return NextResponse.json(amountOfMTMAsString);
} catch (error) {
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 => {
if (value.isZero()) return '0.' + '0'.repeat(decimals); // Handle zero case
const bigValue = new Big(value.toString());
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",
"compilerOptions": {