forked from mito-systems/sol-mem-gen
Update variable
This commit is contained in:
parent
ddda589da9
commit
de0b4d42c2
@ -21,7 +21,7 @@ declare global {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Look for a better way to use quotesService
|
// TODO: Look for a better way to use quotesService
|
||||||
// Initialize the global quote service
|
// Initialize global quotes service
|
||||||
(global as any).quotesService = quotesService
|
(global as any).quotesService = quotesService
|
||||||
|
|
||||||
app.prepare().then(() => {
|
app.prepare().then(() => {
|
||||||
@ -29,7 +29,6 @@ app.prepare().then(() => {
|
|||||||
const parsedUrl = parse(req.url!, true);
|
const parsedUrl = parse(req.url!, true);
|
||||||
(req as any).quotesService = quotesService;
|
(req as any).quotesService = quotesService;
|
||||||
|
|
||||||
// Default Next.js request handling
|
|
||||||
handle(req, res, parsedUrl);
|
handle(req, res, parsedUrl);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -48,17 +48,17 @@ export async function POST(req: NextRequest): Promise<NextResponse> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const quotes: BN[] = (global as any).quotesService.getQuotes();
|
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 scale = new BN(100);
|
||||||
const scaledCost = new BN(model.cost * 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);
|
const isPaymentVerified = await verifyPayment(transactionSignature, lowestQuote);
|
||||||
|
|
||||||
if (!isPaymentVerified) {
|
if (!isPaymentVerified) {
|
||||||
return NextResponse.json(
|
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 }
|
{ status: 400 }
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ const Page: React.FC = (): React.ReactElement => {
|
|||||||
type: null
|
type: null
|
||||||
})
|
})
|
||||||
|
|
||||||
const [mtmFor1USDC, setMtmFor1USDC] = useState<BN>(new BN(0));
|
const [priceMTMFor1USDC, setPriceMTMFor1USDC] = useState<BN>(new BN(0));
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchPrice = async () => {
|
const fetchPrice = async () => {
|
||||||
@ -27,7 +27,7 @@ const Page: React.FC = (): React.ReactElement => {
|
|||||||
|
|
||||||
// Convert the string back to BN
|
// Convert the string back to BN
|
||||||
const price = new BN(data[data.length - 1]);
|
const price = new BN(data[data.length - 1]);
|
||||||
setMtmFor1USDC(price);
|
setPriceMTMFor1USDC(price);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to fetch price:', error);
|
console.error('Failed to fetch price:', error);
|
||||||
}
|
}
|
||||||
@ -122,7 +122,7 @@ const Page: React.FC = (): React.ReactElement => {
|
|||||||
{FLUX_MODELS.map((model) => {
|
{FLUX_MODELS.map((model) => {
|
||||||
// Convert cost from number to BN
|
// Convert cost from number to BN
|
||||||
const costBN = new BN(model.cost * 100);
|
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 (
|
return (
|
||||||
<AIServiceCard
|
<AIServiceCard
|
||||||
@ -133,9 +133,9 @@ const Page: React.FC = (): React.ReactElement => {
|
|||||||
onGenerate={handleFluxGeneration(
|
onGenerate={handleFluxGeneration(
|
||||||
model.modelId,
|
model.modelId,
|
||||||
// Calculate scaled cost directly in bn.js
|
// Calculate scaled cost directly in bn.js
|
||||||
mtmPriceBN
|
priceMTM
|
||||||
)}
|
)}
|
||||||
mtmPrice={mtmPriceBN}
|
priceMTM={priceMTM}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
|
@ -8,7 +8,7 @@ interface AIServiceCardProps {
|
|||||||
description: string
|
description: string
|
||||||
isWalletConnected: boolean
|
isWalletConnected: boolean
|
||||||
onGenerate: (prompt: string) => Promise<{ imageUrl?: string, error?: string }>
|
onGenerate: (prompt: string) => Promise<{ imageUrl?: string, error?: string }>
|
||||||
mtmPrice: BN
|
priceMTM: BN
|
||||||
}
|
}
|
||||||
|
|
||||||
interface GenerationState {
|
interface GenerationState {
|
||||||
@ -32,7 +32,7 @@ const AIServiceCard: React.FC<AIServiceCardProps> = ({
|
|||||||
description,
|
description,
|
||||||
isWalletConnected,
|
isWalletConnected,
|
||||||
onGenerate,
|
onGenerate,
|
||||||
mtmPrice
|
priceMTM
|
||||||
}) => {
|
}) => {
|
||||||
const [inputText, setInputText] = useState<string>('')
|
const [inputText, setInputText] = useState<string>('')
|
||||||
const [generationState, setGenerationState] = useState<GenerationState>({
|
const [generationState, setGenerationState] = useState<GenerationState>({
|
||||||
@ -91,7 +91,7 @@ const AIServiceCard: React.FC<AIServiceCardProps> = ({
|
|||||||
</h2>
|
</h2>
|
||||||
<p className="text-gray-400 mt-2">{description}</p>
|
<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">
|
<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>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@ -115,7 +115,7 @@ const AIServiceCard: React.FC<AIServiceCardProps> = ({
|
|||||||
transition-all duration-200 shadow-lg hover:shadow-green-500/25
|
transition-all duration-200 shadow-lg hover:shadow-green-500/25
|
||||||
disabled:opacity-50 disabled:cursor-not-allowed disabled:hover:shadow-none"
|
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>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@ -75,10 +75,10 @@ export async function verifyPayment(
|
|||||||
const transactionAmount = new BN(amount);
|
const transactionAmount = new BN(amount);
|
||||||
|
|
||||||
if (transactionAmount.gte(lowestQuote)) {
|
if (transactionAmount.gte(lowestQuote)) {
|
||||||
console.log('Transaction amount is within the expected range.');
|
console.log('Transaction amount is greater than minimum amount');
|
||||||
return true;
|
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;
|
return false;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Verification error:', error);
|
console.error('Verification error:', error);
|
||||||
|
Loading…
Reference in New Issue
Block a user