From 521fc97e0d7c36f2add3efeb3f99df102d0f8c66 Mon Sep 17 00:00:00 2001 From: fiqriBTI Date: Sat, 15 Nov 2025 11:41:49 +0700 Subject: [PATCH] fix: create pool service --- .../liquidity-pools/create-pool/page.tsx | 14 ++++++-- .../liquidity-pools/deposit/[poolId]/page.tsx | 7 ++++ .../liquidity-pools/detail/[poolId]/page.tsx | 36 +++++++------------ src/features/liquidityPool/services.ts | 31 +++++++++++++--- 4 files changed, 57 insertions(+), 31 deletions(-) diff --git a/src/app/(walletConnected)/liquidity-pools/create-pool/page.tsx b/src/app/(walletConnected)/liquidity-pools/create-pool/page.tsx index e487700..2327834 100644 --- a/src/app/(walletConnected)/liquidity-pools/create-pool/page.tsx +++ b/src/app/(walletConnected)/liquidity-pools/create-pool/page.tsx @@ -44,7 +44,6 @@ import { TCreatePoolPayload, MintInfo } from '@/features/liquidityPool/types' import { isBBAPool, getBBAPositionInPool, - requiresBBAWrapping } from '@/features/liquidityPool/utils' import { createPoolValidation } from '@/features/liquidityPool/validation' import { LoadingDialog } from '@/features/nfts/components/StatusDialog' @@ -393,6 +392,11 @@ export default function CreatePool() { setIsTokenDialogOpen(true) }, []) + const resetTokenAmounts = useCallback(() => { + form.setValue('baseTokenAmount', '') + form.setValue('quoteTokenAmount', '') + }, [form]) + // Auto-calculate quote amount based on price ratio // Initial Price format: "X BaseToken per QuoteToken" // Example: "1000 SHIB per USDT" means 1 USDT = 1000 SHIB, so 1 SHIB = 1/1000 USDT @@ -400,6 +404,8 @@ export default function CreatePool() { (value: string) => { form.setValue('baseTokenAmount', value, { shouldValidate: true }) + if(value === '') return resetTokenAmounts() + // Auto-calculate quote amount if both tokens and initial price are set const initialPrice = parseFloat(form.getValues('initialPrice')) if (initialPrice && value && selectedBaseToken && selectedQuoteToken) { @@ -409,13 +415,15 @@ export default function CreatePool() { form.setValue('quoteTokenAmount', quoteAmount.toString(), { shouldValidate: true }) } }, - [form, selectedBaseToken, selectedQuoteToken] + [form, resetTokenAmounts, selectedBaseToken, selectedQuoteToken] ) const handleQuoteAmountChange = useCallback( (value: string) => { form.setValue('quoteTokenAmount', value, { shouldValidate: true }) + if (value === '') return resetTokenAmounts() + // Auto-calculate base amount if both tokens and initial price are set const initialPrice = parseFloat(form.getValues('initialPrice')) if (initialPrice && value && selectedBaseToken && selectedQuoteToken) { @@ -425,7 +433,7 @@ export default function CreatePool() { form.setValue('baseTokenAmount', baseAmount.toString(), { shouldValidate: true }) } }, - [form, selectedBaseToken, selectedQuoteToken] + [form, resetTokenAmounts, selectedBaseToken, selectedQuoteToken] ) // Show loading state for tokens diff --git a/src/app/(walletConnected)/liquidity-pools/deposit/[poolId]/page.tsx b/src/app/(walletConnected)/liquidity-pools/deposit/[poolId]/page.tsx index 8a0a6ef..cd9a4f6 100644 --- a/src/app/(walletConnected)/liquidity-pools/deposit/[poolId]/page.tsx +++ b/src/app/(walletConnected)/liquidity-pools/deposit/[poolId]/page.tsx @@ -148,13 +148,20 @@ export default function LiquidityPoolDeposit({ params }: { params: { poolId: str !hasInsufficientBalanceB && !depositMutation.isPending + const resetValue = () => { + setFromAmount('') + setToAmount('') + } + const handleFromAmountChange = (value: string) => { setFromAmount(value) + if (value === '') return resetValue() setLastChangedField('from') } const handleToAmountChange = (value: string) => { setToAmount(value) + if (value === '') return resetValue() setLastChangedField('to') } diff --git a/src/app/(walletConnected)/liquidity-pools/detail/[poolId]/page.tsx b/src/app/(walletConnected)/liquidity-pools/detail/[poolId]/page.tsx index 0a60a92..b2e6002 100644 --- a/src/app/(walletConnected)/liquidity-pools/detail/[poolId]/page.tsx +++ b/src/app/(walletConnected)/liquidity-pools/detail/[poolId]/page.tsx @@ -137,7 +137,6 @@ export default function PoolDetail({ params }: { params: { poolId: string } }) { const router = useRouter() const poolId = params.poolId - const [isInitialLoad, setIsInitialLoad] = useState(true) const [isOwnerTab, setIsOwnerTab] = useState(false) const isMobile = useIsMobile() @@ -182,16 +181,7 @@ export default function PoolDetail({ params }: { params: { poolId: string } }) { } }, [getTransactionsByPoolId.error, getTransactionsByPoolId.isError]) - useEffect(() => { - if (!(getPoolById.isLoading || getTransactionsByPoolId.isLoading)) { - setIsInitialLoad(false) - } - }, [getPoolById.isLoading, getTransactionsByPoolId.isLoading]) - - if ( - isInitialLoad && - (getPoolById.isLoading || getTransactionsByPoolId.isLoading || getUserStats.isLoading) - ) + if (getPoolById.isLoading || getTransactionsByPoolId.isLoading || getUserStats.isLoading) return (
- {getPoolById.isLoading ? ( + {getPoolById.isRefetching ? (
@@ -340,33 +330,33 @@ export default function PoolDetail({ params }: { params: { poolId: string } }) { mintBAmount={mintBAmount} mintASymbol={pool?.mintA?.symbol ?? ''} mintBSymbol={pool?.mintB?.symbol ?? ''} - isLoading={getPoolById.isLoading} + isLoading={getPoolById.isRefetching} />






Transactions

- {getTransactionsByPoolId.isLoading ? ( + {getTransactionsByPoolId.isRefetching ? ( ) : ( { const getAllTokenPrices = useGetAllTokenPrices() const allTokenPrices = getAllTokenPrices.data const getPoolsWithPrices = useQuery({ - queryKey: [SERVICES_KEY.POOL.GET_POOLS, allTokenPrices], + queryKey: [SERVICES_KEY.POOL.GET_POOLS], queryFn: async () => { const poolAccounts = await getPoolAccounts(connection) const batchSize = 10 @@ -122,7 +122,7 @@ export const useGetPoolById = ({ poolId }: { poolId: string }) => { const allTokenPrices = getAllTokenPrices.data const getPoolWithPricesQuery = useQuery({ - queryKey: [SERVICES_KEY.POOL.GET_POOL_BY_ID, poolId, allTokenPrices], + queryKey: [SERVICES_KEY.POOL.GET_POOL_BY_ID, poolId], queryFn: async () => { const pubKey = new PublicKey(poolId) const accountInfo = await connection.getAccountInfo(pubKey) @@ -805,6 +805,14 @@ export const useCreatePool = () => { baseAmountDaltons ) + // transaction approval + const approveIx = createApproveInstruction( + userBaseTokenAccount, + ownerAddress, + ownerAddress, + baseAmountDaltons + ) + // Transfer BBA to pool (using special BBA handling) console.log('🔄 Transferring BBA to pool account...') const transferBBAIx = SystemProgram.transfer({ @@ -817,7 +825,12 @@ export const useCreatePool = () => { const syncBBAIx = createSyncNativeInstruction(swapTokenBAccount) // Combine all transfers - const liquidityTx = new Transaction().add(transferBaseIx, transferBBAIx, syncBBAIx) + const liquidityTx = new Transaction().add( + approveIx, + transferBaseIx, + transferBBAIx, + syncBBAIx + ) const liquiditySig = await sendTransactionWithRetry( liquidityTx, connection, @@ -891,8 +904,16 @@ export const useCreatePool = () => { quoteAmountDaltons ) + // approval instruction + const approveIx = createApproveInstruction( + userQuoteTokenAccount, + ownerAddress, + ownerAddress, + baseAmountDaltons + ) + // Send initial liquidity transfer - const liquidityTx = new Transaction().add(transferBaseIx, transferQuoteIx) + const liquidityTx = new Transaction().add(approveIx, transferBaseIx, transferQuoteIx) const liquiditySig = await sendTransactionWithRetry( liquidityTx, connection, @@ -1454,7 +1475,7 @@ export const useDepositToPool = () => { try { const signature = await sendTransaction(transaction, connection, { skipPreflight: false, - preflightCommitment: 'confirmed', + preflightCommitment: 'confirmed' }) console.log('✅ Transaction sent successfully:', signature)