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';
import { useEffect, useState } from 'react';
import React from "react";
import {
@ -17,6 +18,7 @@ import {
getAssociatedTokenAddress,
createTransferInstruction,
TOKEN_PROGRAM_ID,
ASSOCIATED_TOKEN_PROGRAM_ID,
createAssociatedTokenAccountInstruction,
} 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 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 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.");
}
// 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 toPubKey = new PublicKey(BUSINESS_WALLET);
const transaction = new Transaction();
@ -441,6 +447,7 @@ export const GpuPaymentModal = ({ isOpen, onClose, gpu }: GpuPaymentModalProps)
let amountDisplay = "";
if (selectedToken === "SOL") {
// SOL Payment Logic (unchanged)
const { data } = await axios.get(
"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 {
// For both USDC and VERTEX tokens
// Token Payment Logic
const selectedMint = selectedToken === "USDC"
? USDC_MINT
: new PublicKey(customToken?.address || "");
@ -471,28 +478,41 @@ export const GpuPaymentModal = ({ isOpen, onClose, gpu }: GpuPaymentModalProps)
const tokenAmount = (gpu.price_usd / tokenPrice).toFixed(tokenDecimals);
amountDisplay = `${tokenAmount} ${selectedToken}`;
const fromTokenAccount = await getAssociatedTokenAddress(selectedMint, fromPubKey);
const toTokenAccount = await getAssociatedTokenAddress(selectedMint, toPubKey);
// Get or create token accounts using the correct method
const fromTokenAccount = await getAssociatedTokenAddress(
selectedMint,
fromPubKey,
false,
TOKEN_PROGRAM_ID,
ASSOCIATED_TOKEN_PROGRAM_ID
);
const toTokenAccount = await getAssociatedTokenAddress(
selectedMint,
toPubKey,
false,
TOKEN_PROGRAM_ID,
ASSOCIATED_TOKEN_PROGRAM_ID
);
// Check if destination token account exists, if not create it
// Check if destination token account exists
const toTokenAccountInfo = await connection.getAccountInfo(toTokenAccount);
if (!toTokenAccountInfo) {
// Import the necessary function
const { createAssociatedTokenAccountInstruction } = await import("@solana/spl-token");
// Add instruction to create the associated token account for the business wallet
// Create associated token account with explicit program IDs
transaction.add(
createAssociatedTokenAccountInstruction(
fromPubKey, // payer
toTokenAccount, // associated token account address
toPubKey, // owner of the new account
selectedMint // token mint
fromPubKey,
toTokenAccount,
toPubKey,
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(
createTransferInstruction(
fromTokenAccount,
@ -505,14 +525,17 @@ export const GpuPaymentModal = ({ isOpen, onClose, gpu }: GpuPaymentModalProps)
);
}
// Set transaction properties (unchanged)
transaction.feePayer = fromPubKey;
const { blockhash } = await connection.getLatestBlockhash();
transaction.recentBlockhash = blockhash;
// Sign and send (unchanged)
const signedTx = await solWallet.signTransaction(transaction);
txId = await connection.sendRawTransaction(signedTx.serialize());
await connection.confirmTransaction(txId, "confirmed");
// Process order completion (unchanged)
await axios.post(EMAIL_API_URL, {
email: userEmail,
product: gpu.title,
@ -520,7 +543,6 @@ export const GpuPaymentModal = ({ isOpen, onClose, gpu }: GpuPaymentModalProps)
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}`;
setOrderId(generatedOrderId);