Update variable

This commit is contained in:
Adw8 2025-01-30 12:07:43 +05:30
parent ddda589da9
commit de0b4d42c2
5 changed files with 15 additions and 16 deletions

View File

@ -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);
});

View File

@ -48,17 +48,17 @@ export async function POST(req: NextRequest): Promise<NextResponse> {
}
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 }
)
}

View File

@ -17,7 +17,7 @@ const Page: React.FC = (): React.ReactElement => {
type: null
})
const [mtmFor1USDC, setMtmFor1USDC] = useState<BN>(new BN(0));
const [priceMTMFor1USDC, setPriceMTMFor1USDC] = useState<BN>(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 (
<AIServiceCard
@ -133,9 +133,9 @@ const Page: React.FC = (): React.ReactElement => {
onGenerate={handleFluxGeneration(
model.modelId,
// Calculate scaled cost directly in bn.js
mtmPriceBN
priceMTM
)}
mtmPrice={mtmPriceBN}
priceMTM={priceMTM}
/>
);
})}

View File

@ -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<AIServiceCardProps> = ({
description,
isWalletConnected,
onGenerate,
mtmPrice
priceMTM
}) => {
const [inputText, setInputText] = useState<string>('')
const [generationState, setGenerationState] = useState<GenerationState>({
@ -91,7 +91,7 @@ const AIServiceCard: React.FC<AIServiceCardProps> = ({
</h2>
<p className="text-gray-400 mt-2">{description}</p>
<div className="mt-2 inline-block px-3 py-1 bg-green-500/20 rounded-full text-green-300 text-sm">
Cost: {mtmPrice ? formatBNWithDecimals(mtmPrice, 6) : '...'} MTM
Cost: {priceMTM ? formatBNWithDecimals(priceMTM, 6) : '...'} MTM
</div>
</div>
@ -115,7 +115,7 @@ const AIServiceCard: React.FC<AIServiceCardProps> = ({
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`}
</button>
</div>

View File

@ -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);