Reuse same connection instance

This commit is contained in:
Shreerang Kale 2025-07-24 19:58:55 +05:30
parent 32b583a637
commit f385a5030b
3 changed files with 30 additions and 21 deletions

View File

@ -10,7 +10,7 @@ import { DENOM as ALNT_DENOM } from '@cerc-io/registry-sdk';
import { verifyUnusedSolanaPayment } from '@/utils/solana-verify';
import { transferLNTTokens } from '@/services/laconic-transfer';
import { getRegistry, getRegistryConfig } from '@/config';
import { getRequiredTokenInfo } from '@/services/jupiter-price';
import { getRequiredTokenInfo, RequiredTokenInfo } from '@/services/jupiter-price';
import { IS_NAT_GOR_TRANSFER_ENABLED, WRAPPED_SOL_MINT_ADDRESS } from '@/constants/payments';
import { PaymentMethod } from '@/types';
@ -139,12 +139,27 @@ export const registryTransactionWithRetry = async (
throw lastError;
};
let solanaConnection: Connection | null = null;
let gorbaganaConnection: Connection | null = null;
// Helper function to get the appropriate connection based on payment method
const getConnection = (paymentMethod: PaymentMethod): Connection => {
if (paymentMethod === PaymentMethod.NAT_GOR && GORBAGANA_RPC_URL) {
return new Connection(GORBAGANA_RPC_URL);
switch (paymentMethod) {
case PaymentMethod.NAT_GOR:
if (!gorbaganaConnection) {
gorbaganaConnection = new Connection(GORBAGANA_RPC_URL!);
}
return gorbaganaConnection;
case PaymentMethod.SPL_TOKEN:
if (!solanaConnection) {
solanaConnection = new Connection(SOLANA_RPC_URL);
}
return solanaConnection;
default:
throw new Error("Invalid payment method");
}
return new Connection(SOLANA_RPC_URL);
};
export async function POST(request: NextRequest) {
@ -205,18 +220,16 @@ export async function POST(request: NextRequest) {
// Calculate expected token amount based on current price
let requiredAmountInBaseUnits: number;
let requiredTokenInfo: RequiredTokenInfo;
const targetUsdAmount = parseFloat(process.env.NEXT_PUBLIC_SOLANA_PAYMENT_AMOUNT_USD!);
const mintAddress = process.env.NEXT_PUBLIC_SOLANA_TOKEN_MINT_ADDRESS!;
try {
if (paymentMethod === PaymentMethod.NAT_GOR) {
const requiredNativeGorInfo = await getRequiredTokenInfo(targetUsdAmount, WRAPPED_SOL_MINT_ADDRESS);
requiredAmountInBaseUnits = requiredNativeGorInfo.requiredAmountInBaseUnits;
requiredTokenInfo = await getRequiredTokenInfo(targetUsdAmount, WRAPPED_SOL_MINT_ADDRESS);
} else if (paymentMethod === PaymentMethod.SPL_TOKEN) {
const requiredTokenInfo = await getRequiredTokenInfo(targetUsdAmount, mintAddress);
requiredAmountInBaseUnits = requiredTokenInfo.requiredAmountInBaseUnits;
requiredTokenInfo = await getRequiredTokenInfo(targetUsdAmount, mintAddress);
} else {
return NextResponse.json({
status: 'error',
@ -231,6 +244,7 @@ export async function POST(request: NextRequest) {
}, { status: 500 });
}
const requiredAmountInBaseUnits = requiredTokenInfo.requiredAmountInBaseUnits;
const expectedTokenAmount = Math.round(requiredAmountInBaseUnits - ALLOWED_SLIPPAGE_FACTOR * requiredAmountInBaseUnits);
const solanaPaymentResult = await verifyUnusedSolanaPayment(

View File

@ -3,7 +3,7 @@
import { useCallback, useState, useEffect } from 'react';
import assert from 'assert';
import { Connection, LAMPORTS_PER_SOL } from '@solana/web3.js';
import { Connection } from '@solana/web3.js';
import { useConnection, useWallet } from '@solana/wallet-adapter-react';
import { sendSolanaPayment } from '@/services/solana';
@ -131,16 +131,11 @@ export default function PaymentModal({
}, [paymentMethod, tokenAmount, loadingPrice, wallet, solanaConnection, publicKey, onPaymentComplete]);
const getPaymentAmountDisplay = () => {
if (loadingPrice) return 'Loading...';
if (loadingPrice) {
return 'Loading...'
};
switch (paymentMethod) {
case PaymentMethod.NAT_GOR:
return tokenAmount > 0 ? `${(tokenAmount / LAMPORTS_PER_SOL).toFixed(6)} GOR` : '0';
case PaymentMethod.SPL_TOKEN:
return tokenAmount > 0 ? `${(tokenAmount / Math.pow(10, tokenDecimals)).toFixed(6)} ${tokenSymbol}` : '0';
default:
return '';
}
return tokenAmount > 0 ? `${(tokenAmount / Math.pow(10, tokenDecimals)).toFixed(6)}` : '0';
};
if (!isOpen || !paymentMethod) return null;

View File

@ -43,8 +43,8 @@ export default function WalletProviders({ children }: WalletProvidersProps) {
// Filter wallets based on selected payment method
const wallets = useMemo(() => {
if (!selectedPaymentMethod) {
// If no payment method selected, show all wallets
return allWallets;
// If no payment method selected, do not show any wallets
return [];
}
return allWallets.filter(wallet => {