fixed usdc payments

This commit is contained in:
shialoth 2025-05-08 23:27:30 +05:30
parent bd1677ae0d
commit 0dfff9c4d8

View File

@ -1,5 +1,6 @@
'use client'; 'use client';
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import React from "react"; import React from "react";
import { import {
@ -17,6 +18,7 @@ import {
getAssociatedTokenAddress, getAssociatedTokenAddress,
createTransferInstruction, createTransferInstruction,
TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID,
ASSOCIATED_TOKEN_PROGRAM_ID,
createAssociatedTokenAccountInstruction, createAssociatedTokenAccountInstruction,
} from "@solana/spl-token"; } from "@solana/spl-token";
@ -42,7 +44,7 @@ const BUSINESS_WALLET = process.env.NEXT_PUBLIC_BUSINESS_WALLET!;
const EMAIL_API_URL = process.env.NEXT_PUBLIC_EMAIL_API_URL!; const EMAIL_API_URL = process.env.NEXT_PUBLIC_EMAIL_API_URL!;
const CUSTOM_TOKEN_API_URL = "https://catools.dev3vds1.link/get/vertex"; const CUSTOM_TOKEN_API_URL = "https://catools.dev3vds1.link/get/vertex";
const USDC_MINT = new PublicKey("Es9vMFrzaCERJ8gLhEvX5yQceQ2uKcXfUrx2Wcikgqay"); const USDC_MINT = new PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");
const CornerEdge = ({ position }: { position: 'left-top' | 'right-top' | 'left-bottom' | 'right-bottom' }) => { const CornerEdge = ({ position }: { position: 'left-top' | 'right-top' | 'left-bottom' | 'right-bottom' }) => {
const paths = { const paths = {
@ -433,6 +435,10 @@ export const GpuPaymentModal = ({ isOpen, onClose, gpu }: GpuPaymentModalProps)
throw new Error("No connected Solana wallet found or wallet can't sign."); throw new Error("No connected Solana wallet found or wallet can't sign.");
} }
// Define the correct program IDs
const TOKEN_PROGRAM_ID = new PublicKey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
const ASSOCIATED_TOKEN_PROGRAM_ID = new PublicKey("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL");
const fromPubKey = new PublicKey(solWallet.address); const fromPubKey = new PublicKey(solWallet.address);
const toPubKey = new PublicKey(BUSINESS_WALLET); const toPubKey = new PublicKey(BUSINESS_WALLET);
const transaction = new Transaction(); const transaction = new Transaction();
@ -441,6 +447,7 @@ export const GpuPaymentModal = ({ isOpen, onClose, gpu }: GpuPaymentModalProps)
let amountDisplay = ""; let amountDisplay = "";
if (selectedToken === "SOL") { if (selectedToken === "SOL") {
// SOL Payment Logic (unchanged)
const { data } = await axios.get( const { data } = await axios.get(
"https://api.coingecko.com/api/v3/simple/price?ids=solana&vs_currencies=usd" "https://api.coingecko.com/api/v3/simple/price?ids=solana&vs_currencies=usd"
); );
@ -456,7 +463,7 @@ export const GpuPaymentModal = ({ isOpen, onClose, gpu }: GpuPaymentModalProps)
}) })
); );
} else { } else {
// For both USDC and VERTEX tokens // Token Payment Logic
const selectedMint = selectedToken === "USDC" const selectedMint = selectedToken === "USDC"
? USDC_MINT ? USDC_MINT
: new PublicKey(customToken?.address || ""); : new PublicKey(customToken?.address || "");
@ -471,28 +478,41 @@ export const GpuPaymentModal = ({ isOpen, onClose, gpu }: GpuPaymentModalProps)
const tokenAmount = (gpu.price_usd / tokenPrice).toFixed(tokenDecimals); const tokenAmount = (gpu.price_usd / tokenPrice).toFixed(tokenDecimals);
amountDisplay = `${tokenAmount} ${selectedToken}`; amountDisplay = `${tokenAmount} ${selectedToken}`;
const fromTokenAccount = await getAssociatedTokenAddress(selectedMint, fromPubKey); // Get or create token accounts using the correct method
const toTokenAccount = await getAssociatedTokenAddress(selectedMint, toPubKey); const fromTokenAccount = await getAssociatedTokenAddress(
selectedMint,
fromPubKey,
false,
TOKEN_PROGRAM_ID,
ASSOCIATED_TOKEN_PROGRAM_ID
);
// Check if destination token account exists, if not create it const toTokenAccount = await getAssociatedTokenAddress(
selectedMint,
toPubKey,
false,
TOKEN_PROGRAM_ID,
ASSOCIATED_TOKEN_PROGRAM_ID
);
// Check if destination token account exists
const toTokenAccountInfo = await connection.getAccountInfo(toTokenAccount); const toTokenAccountInfo = await connection.getAccountInfo(toTokenAccount);
if (!toTokenAccountInfo) { if (!toTokenAccountInfo) {
// Import the necessary function // Create associated token account with explicit program IDs
const { createAssociatedTokenAccountInstruction } = await import("@solana/spl-token");
// Add instruction to create the associated token account for the business wallet
transaction.add( transaction.add(
createAssociatedTokenAccountInstruction( createAssociatedTokenAccountInstruction(
fromPubKey, // payer fromPubKey,
toTokenAccount, // associated token account address toTokenAccount,
toPubKey, // owner of the new account toPubKey,
selectedMint // token mint selectedMint,
TOKEN_PROGRAM_ID,
ASSOCIATED_TOKEN_PROGRAM_ID
) )
); );
} }
// Add the transfer instruction after ensuring the account exists // Add token transfer instruction with explicit program ID
transaction.add( transaction.add(
createTransferInstruction( createTransferInstruction(
fromTokenAccount, fromTokenAccount,
@ -505,14 +525,17 @@ export const GpuPaymentModal = ({ isOpen, onClose, gpu }: GpuPaymentModalProps)
); );
} }
// Set transaction properties (unchanged)
transaction.feePayer = fromPubKey; transaction.feePayer = fromPubKey;
const { blockhash } = await connection.getLatestBlockhash(); const { blockhash } = await connection.getLatestBlockhash();
transaction.recentBlockhash = blockhash; transaction.recentBlockhash = blockhash;
// Sign and send (unchanged)
const signedTx = await solWallet.signTransaction(transaction); const signedTx = await solWallet.signTransaction(transaction);
txId = await connection.sendRawTransaction(signedTx.serialize()); txId = await connection.sendRawTransaction(signedTx.serialize());
await connection.confirmTransaction(txId, "confirmed"); await connection.confirmTransaction(txId, "confirmed");
// Process order completion (unchanged)
await axios.post(EMAIL_API_URL, { await axios.post(EMAIL_API_URL, {
email: userEmail, email: userEmail,
product: gpu.title, product: gpu.title,
@ -520,7 +543,6 @@ export const GpuPaymentModal = ({ isOpen, onClose, gpu }: GpuPaymentModalProps)
price: gpu.price_usd.toFixed(2) price: gpu.price_usd.toFixed(2)
}); });
// Generate a random order ID (in production this would come from the backend)
const generatedOrderId = `${Math.floor(Math.random() * 900) + 100}-${Math.floor(Math.random() * 900) + 100}-${Math.floor(Math.random() * 900) + 100}`; const generatedOrderId = `${Math.floor(Math.random() * 900) + 100}-${Math.floor(Math.random() * 900) + 100}-${Math.floor(Math.random() * 900) + 100}`;
setOrderId(generatedOrderId); setOrderId(generatedOrderId);