Pull in required amount of native GOR from jupiter API
This commit is contained in:
parent
75c0e146c8
commit
375f33973e
@ -121,10 +121,7 @@ export default function Home() {
|
||||
}}
|
||||
>
|
||||
<div className="text-left">
|
||||
<h3 className="font-semibold text-lg mb-2">GOR (Native)</h3>
|
||||
<p className="text-sm" style={{ color: 'var(--muted-foreground)' }}>
|
||||
Pay 0.01 GOR (~$2-5 USD)
|
||||
</p>
|
||||
<h3 className="font-semibold text-lg mb-2">GOR (native)</h3>
|
||||
<p className="text-xs mt-1" style={{ color: 'var(--muted-foreground)' }}>
|
||||
Compatible with: Backpack
|
||||
</p>
|
||||
@ -142,9 +139,6 @@ export default function Home() {
|
||||
>
|
||||
<div className="text-left">
|
||||
<h3 className="font-semibold text-lg mb-2">{process.env.NEXT_PUBLIC_SOLANA_TOKEN_SYMBOL} Token</h3>
|
||||
<p className="text-sm" style={{ color: 'var(--muted-foreground)' }}>
|
||||
Pay ${process.env.NEXT_PUBLIC_SOLANA_PAYMENT_AMOUNT_USD} worth
|
||||
</p>
|
||||
<p className="text-xs mt-1" style={{ color: 'var(--muted-foreground)' }}>
|
||||
Compatible with: Phantom, Solflare
|
||||
</p>
|
||||
|
||||
@ -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({
|
||||
</div>
|
||||
|
||||
<div className="relative">
|
||||
{(paymentMethod === 'spl-token' && loadingPrice) ? (
|
||||
{loadingPrice ? (
|
||||
<div className="w-full p-3 rounded-md flex items-center" style={{
|
||||
background: 'var(--muted-light)',
|
||||
border: '1px solid var(--input-border)',
|
||||
|
||||
@ -8,7 +8,7 @@ export type PaymentMethod = 'nat-gor' | 'spl-token';
|
||||
|
||||
// Payment method labels for UI
|
||||
export const PAYMENT_METHOD_LABELS: Record<PaymentMethod, string> = {
|
||||
'nat-gor': 'GOR (Native)',
|
||||
'nat-gor': 'GOR (native)',
|
||||
'spl-token': process.env.NEXT_PUBLIC_SOLANA_TOKEN_SYMBOL || 'SPL Token'
|
||||
};
|
||||
|
||||
|
||||
@ -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<RequiredTokenInfo> {
|
||||
// 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
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user