From 375f33973e22b3a346f3a3a4eac0a0c2243b7e89 Mon Sep 17 00:00:00 2001 From: Shreerang Kale Date: Wed, 23 Jul 2025 18:01:22 +0530 Subject: [PATCH] Pull in required amount of native GOR from jupiter API --- src/app/page.tsx | 8 +---- src/components/PaymentModal.tsx | 53 +++++++++++++++++---------------- src/constants/payments.ts | 2 +- src/services/jupiter-price.ts | 24 +++++++++++++++ 4 files changed, 53 insertions(+), 34 deletions(-) diff --git a/src/app/page.tsx b/src/app/page.tsx index 04898ca..b778826 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -121,10 +121,7 @@ export default function Home() { }} >
-

GOR (Native)

-

- Pay 0.01 GOR (~$2-5 USD) -

+

GOR (native)

Compatible with: Backpack

@@ -142,9 +139,6 @@ export default function Home() { >

{process.env.NEXT_PUBLIC_SOLANA_TOKEN_SYMBOL} Token

-

- Pay ${process.env.NEXT_PUBLIC_SOLANA_PAYMENT_AMOUNT_USD} worth -

Compatible with: Phantom, Solflare

diff --git a/src/components/PaymentModal.tsx b/src/components/PaymentModal.tsx index 1a06bd2..d576c8d 100644 --- a/src/components/PaymentModal.tsx +++ b/src/components/PaymentModal.tsx @@ -7,9 +7,9 @@ import { Connection, LAMPORTS_PER_SOL } from '@solana/web3.js'; import { useWallet } from '@solana/wallet-adapter-react'; import { sendSolanaPayment, getRecipientAddress } from '@/services/solana'; -import { getRequiredTokenInfo } from '@/services/jupiter-price'; +import { getRequiredTokenInfo, getRequiredNativeGorInfo } from '@/services/jupiter-price'; import { PaymentModalProps, PaymentRequest } from '@/types'; -import { PAYMENT_METHOD_LABELS, SOL_PAYMENT_AMOUNT_LAMPORTS } from '@/constants/payments'; +import { PAYMENT_METHOD_LABELS } from '@/constants/payments'; import { usePaymentMethod } from '@/contexts/PaymentMethodContext'; assert(process.env.NEXT_PUBLIC_SOLANA_RPC_URL, 'SOLANA_RPC_URL is required'); @@ -43,54 +43,54 @@ export default function PaymentModal({ const mintAddress = process.env.NEXT_PUBLIC_SOLANA_TOKEN_MINT_ADDRESS!; const tokenSymbol = process.env.NEXT_PUBLIC_SOLANA_TOKEN_SYMBOL || 'TOKEN'; - // Fetch token amount based on USD price when SPL token method is selected + // Fetch payment amount based on USD price for both payment methods useEffect(() => { - if (!isOpen || paymentMethod !== 'spl-token') { + if (!isOpen || !paymentMethod) { setLoadingPrice(false); return; } - const fetchTokenAmount = async () => { + const fetchPaymentAmount = async () => { setLoadingPrice(true); setError(''); try { - const {requiredAmountInBaseUnits, decimals} = await getRequiredTokenInfo(targetUsdAmount, mintAddress); - setTokenAmount(requiredAmountInBaseUnits); - setTokenDecimals(decimals); + if (paymentMethod === 'nat-gor') { + // Fetch native GOR amount using wrapped SOL price + const {requiredAmountInBaseUnits, decimals} = await getRequiredNativeGorInfo(targetUsdAmount); + setTokenAmount(requiredAmountInBaseUnits); + setTokenDecimals(decimals); + } else if (paymentMethod === 'spl-token') { + // Fetch SPL token amount using token mint price + const {requiredAmountInBaseUnits, decimals} = await getRequiredTokenInfo(targetUsdAmount, mintAddress); + setTokenAmount(requiredAmountInBaseUnits); + setTokenDecimals(decimals); + } } catch (error) { - console.error('Error fetching token price:', error); - setError('Unable to fetch current token price. Please try again.'); + console.error('Error fetching payment amount:', error); + setError('Unable to fetch current payment amount. Please try again.'); } finally { setLoadingPrice(false); } }; - fetchTokenAmount(); + fetchPaymentAmount(); }, [isOpen, paymentMethod, targetUsdAmount, mintAddress]); - // Initialize payment method when modal opens + // Initialize state when modal opens useEffect(() => { if (isOpen) { setError(''); - setLoadingPrice(false); - - // Set tokenAmount for native GOR payments to maintain consistency - if (paymentMethod === 'nat-gor') { - setTokenAmount(SOL_PAYMENT_AMOUNT_LAMPORTS); - } else { - setTokenAmount(0); - } + setTokenAmount(0); } - }, [isOpen, paymentMethod]); - + }, [isOpen]); const handlePayment = useCallback(async () => { if (!paymentMethod) { return; } - if (tokenAmount === 0 || (paymentMethod === 'spl-token' && loadingPrice)) { + if (tokenAmount === 0 || loadingPrice) { setError('Payment amount not ready. Please wait.'); return; } @@ -133,11 +133,12 @@ export default function PaymentModal({ }, [paymentMethod, tokenAmount, loadingPrice, wallet, directConnection, solConnection, publicKey, onPaymentComplete]); const getPaymentAmountDisplay = () => { + if (loadingPrice) return 'Loading...'; + switch (paymentMethod) { case 'nat-gor': - return `${SOL_PAYMENT_AMOUNT_LAMPORTS / LAMPORTS_PER_SOL} GOR`; + return tokenAmount > 0 ? `${(tokenAmount / LAMPORTS_PER_SOL).toFixed(6)} GOR` : '0'; case 'spl-token': - if (loadingPrice) return 'Loading...'; return tokenAmount > 0 ? `${(tokenAmount / Math.pow(10, tokenDecimals)).toFixed(6)} ${tokenSymbol}` : '0'; default: return ''; @@ -209,7 +210,7 @@ export default function PaymentModal({
- {(paymentMethod === 'spl-token' && loadingPrice) ? ( + {loadingPrice ? (
= { - 'nat-gor': 'GOR (Native)', + 'nat-gor': 'GOR (native)', 'spl-token': process.env.NEXT_PUBLIC_SOLANA_TOKEN_SYMBOL || 'SPL Token' }; diff --git a/src/services/jupiter-price.ts b/src/services/jupiter-price.ts index 5a20f7a..0ba8dad 100644 --- a/src/services/jupiter-price.ts +++ b/src/services/jupiter-price.ts @@ -65,3 +65,27 @@ export async function getRequiredTokenInfo(targetUsdAmount: number, mintAddress: return {requiredAmountInBaseUnits: amountInSmallestUnits, decimals: priceInfo.decimals}; } + +/** + * Calculates the native GOR amount needed for a given USD price + * Uses wrapped SOL price since native GOR = SOL + * @param targetUsdAmount The target amount in USD + * @returns The GOR amount in lamports needed and decimals (always 9 for SOL/GOR) + */ +export async function getRequiredNativeGorInfo(targetUsdAmount: number): Promise { + // Wrapped SOL mint address + const wrappedSolMint = 'So11111111111111111111111111111111111111112'; + + const priceInfo = await getTokenInfo(wrappedSolMint); + + // Calculate GOR amount needed (same as SOL) + const gorAmount = targetUsdAmount / priceInfo.usdPrice; + + // Convert to lamports (SOL/GOR has 9 decimals) + const amountInLamports = Math.round(gorAmount * Math.pow(10, 9)); + + return { + requiredAmountInBaseUnits: amountInLamports, + decimals: 9 // SOL/GOR always has 9 decimals + }; +}