From de0b4d42c2a59a5c9444f6dcc4c0b0e2cb3725d9 Mon Sep 17 00:00:00 2001 From: Adw8 Date: Thu, 30 Jan 2025 12:07:43 +0530 Subject: [PATCH] Update variable --- server.ts | 3 +-- src/app/api/flux/route.ts | 6 +++--- src/app/page.tsx | 10 +++++----- src/components/AIServiceCard.tsx | 8 ++++---- src/utils/verifyPayment.ts | 4 ++-- 5 files changed, 15 insertions(+), 16 deletions(-) diff --git a/server.ts b/server.ts index 4d6c2a0..dcba014 100644 --- a/server.ts +++ b/server.ts @@ -21,7 +21,7 @@ declare global { } // TODO: Look for a better way to use quotesService -// Initialize the global quote service +// Initialize global quotes service (global as any).quotesService = quotesService app.prepare().then(() => { @@ -29,7 +29,6 @@ app.prepare().then(() => { const parsedUrl = parse(req.url!, true); (req as any).quotesService = quotesService; - // Default Next.js request handling handle(req, res, parsedUrl); }); diff --git a/src/app/api/flux/route.ts b/src/app/api/flux/route.ts index 2da9c52..0d90b55 100644 --- a/src/app/api/flux/route.ts +++ b/src/app/api/flux/route.ts @@ -48,17 +48,17 @@ export async function POST(req: NextRequest): Promise { } const quotes: BN[] = (global as any).quotesService.getQuotes(); - const mtmFor1USDC = quotes.reduce((min, quote) => quote.lt(min) ? quote : min, quotes[0]); // Take the least cost in the array + const priceMTMFor1USDC = quotes.reduce((min, quote) => quote.lt(min) ? quote : min, quotes[0]); // Take the minimum quote from the array const scale = new BN(100); const scaledCost = new BN(model.cost * 100); - const lowestQuote = scaledCost.mul(new BN(mtmFor1USDC)).div(scale); + const lowestQuote = scaledCost.mul(new BN(priceMTMFor1USDC)).div(scale); const isPaymentVerified = await verifyPayment(transactionSignature, lowestQuote); if (!isPaymentVerified) { return NextResponse.json( - { error: 'Payment verification failed or transaction signature has already been used', reload: true }, + { error: 'Payment verification failed or transaction signature has already been used' }, { status: 400 } ) } diff --git a/src/app/page.tsx b/src/app/page.tsx index e01cba9..0a1461c 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -17,7 +17,7 @@ const Page: React.FC = (): React.ReactElement => { type: null }) - const [mtmFor1USDC, setMtmFor1USDC] = useState(new BN(0)); + const [priceMTMFor1USDC, setPriceMTMFor1USDC] = useState(new BN(0)); useEffect(() => { const fetchPrice = async () => { @@ -27,7 +27,7 @@ const Page: React.FC = (): React.ReactElement => { // Convert the string back to BN const price = new BN(data[data.length - 1]); - setMtmFor1USDC(price); + setPriceMTMFor1USDC(price); } catch (error) { console.error('Failed to fetch price:', error); } @@ -122,7 +122,7 @@ const Page: React.FC = (): React.ReactElement => { {FLUX_MODELS.map((model) => { // Convert cost from number to BN const costBN = new BN(model.cost * 100); - const mtmPriceBN = costBN.mul(mtmFor1USDC).div(new BN(100)); + const priceMTM = costBN.mul(priceMTMFor1USDC).div(new BN(100)); return ( { onGenerate={handleFluxGeneration( model.modelId, // Calculate scaled cost directly in bn.js - mtmPriceBN + priceMTM )} - mtmPrice={mtmPriceBN} + priceMTM={priceMTM} /> ); })} diff --git a/src/components/AIServiceCard.tsx b/src/components/AIServiceCard.tsx index 27a3f59..2f73b79 100644 --- a/src/components/AIServiceCard.tsx +++ b/src/components/AIServiceCard.tsx @@ -8,7 +8,7 @@ interface AIServiceCardProps { description: string isWalletConnected: boolean onGenerate: (prompt: string) => Promise<{ imageUrl?: string, error?: string }> - mtmPrice: BN + priceMTM: BN } interface GenerationState { @@ -32,7 +32,7 @@ const AIServiceCard: React.FC = ({ description, isWalletConnected, onGenerate, - mtmPrice + priceMTM }) => { const [inputText, setInputText] = useState('') const [generationState, setGenerationState] = useState({ @@ -91,7 +91,7 @@ const AIServiceCard: React.FC = ({

{description}

- Cost: {mtmPrice ? formatBNWithDecimals(mtmPrice, 6) : '...'} MTM + Cost: {priceMTM ? formatBNWithDecimals(priceMTM, 6) : '...'} MTM
@@ -115,7 +115,7 @@ const AIServiceCard: React.FC = ({ transition-all duration-200 shadow-lg hover:shadow-green-500/25 disabled:opacity-50 disabled:cursor-not-allowed disabled:hover:shadow-none" > - {generationState.loading ? 'Processing...' : `Pay ${mtmPrice ? formatBNWithDecimals(mtmPrice, 6) : '...'} MTM & Generate`} + {generationState.loading ? 'Processing...' : `Pay ${priceMTM ? formatBNWithDecimals(priceMTM, 6) : '...'} MTM & Generate`} diff --git a/src/utils/verifyPayment.ts b/src/utils/verifyPayment.ts index c3427a5..145ab21 100644 --- a/src/utils/verifyPayment.ts +++ b/src/utils/verifyPayment.ts @@ -75,10 +75,10 @@ export async function verifyPayment( const transactionAmount = new BN(amount); if (transactionAmount.gte(lowestQuote)) { - console.log('Transaction amount is within the expected range.'); + console.log('Transaction amount is greater than minimum amount'); return true; } - console.log('Transaction amount is not within the expected range.'); + console.log('Transaction amount is less than minimum amount. Rejecting request'); return false; } catch (error) { console.error('Verification error:', error);