diff --git a/quotes-service.ts b/quotes-service.ts index bb3d72a..780823b 100644 --- a/quotes-service.ts +++ b/quotes-service.ts @@ -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 { 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; } } diff --git a/server.ts b/server.ts index 86253c6..4789578 100644 --- a/server.ts +++ b/server.ts @@ -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'; diff --git a/src/app/api/flux/route.ts b/src/app/api/flux/route.ts index 8b7601d..4773307 100644 --- a/src/app/api/flux/route.ts +++ b/src/app/api/flux/route.ts @@ -47,14 +47,14 @@ export async function POST(req: NextRequest): Promise { ) } - 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( diff --git a/src/app/api/quotes/route.ts b/src/app/api/quotes/route.ts index 8ef3744..24a5b07 100644 --- a/src/app/api/quotes/route.ts +++ b/src/app/api/quotes/route.ts @@ -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 }); } diff --git a/src/components/AIServiceCard.tsx b/src/components/AIServiceCard.tsx index 547b8bd..57b8e00 100644 --- a/src/components/AIServiceCard.tsx +++ b/src/components/AIServiceCard.tsx @@ -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); diff --git a/tsconfig.server.json b/tsconfig.server.json index 6a8dc0a..7e9180b 100644 --- a/tsconfig.server.json +++ b/tsconfig.server.json @@ -1,3 +1,4 @@ +// Reference: https://github.com/vercel/next.js/blob/canary/examples/custom-server/tsconfig.server.json { "extends": "./tsconfig.json", "compilerOptions": {