Bladeren bron

close position

lushdog@outlook.com 1 maand geleden
bovenliggende
commit
b869f64f84
3 gewijzigde bestanden met toevoegingen van 87 en 5 verwijderingen
  1. 53 0
      src/app/api/lp-index/lp-close/route.ts
  2. 6 4
      src/app/components/DataTable.tsx
  3. 28 1
      src/app/my-lp/page.tsx

+ 53 - 0
src/app/api/lp-index/lp-close/route.ts

@@ -0,0 +1,53 @@
+import { NextRequest, NextResponse } from 'next/server'
+import { chain } from '@/lib/config'
+import { Keypair, PublicKey, VersionedTransaction } from '@solana/web3.js'
+import bs58 from 'bs58'
+
+export async function POST(request: NextRequest) {
+	try {
+		const body = await request.json()
+		const { nftMintAddress } = body
+		const nftMint = new PublicKey(nftMintAddress)
+
+		const secretKey = process.env.SOL_SECRET_KEY
+		if (!secretKey) {
+			return NextResponse.json(
+				{ error: 'SOL_SECRET_KEY not configured' },
+				{ status: 500 }
+			)
+		}
+
+		const userKeypair = Keypair.fromSecretKey(bs58.decode(secretKey))
+		const userAddress = userKeypair.publicKey
+
+		console.log('User Address:', userAddress.toBase58())
+		console.log('\n--- Executing Transaction ---')
+		console.log('Closing position on-chain...')
+
+		// 执行实际上链操作
+		const signerCallback = async (tx: VersionedTransaction) => {
+			tx.sign([userKeypair])
+			return tx
+		}
+
+		const txid = await chain.decreaseFullLiquidity({
+			userAddress,
+			nftMint,
+			signerCallback,
+		})
+		console.log('Transaction ID:', txid)
+		console.log('Position closed successfully!')
+		console.log('==========================================\n')
+
+		return NextResponse.json({
+			success: true,
+			txid,
+		})
+	} catch (error) {
+		console.error('API Route Error:', error)
+		return NextResponse.json(
+			{ retCode: 500, retMsg: '服务器内部错误' },
+			{ status: 500 }
+		)
+	}
+}

+ 6 - 4
src/app/components/DataTable.tsx

@@ -195,7 +195,11 @@ function DataTableContent() {
 	}
 
 	function handleQuickCopy(record: TableData) {
-		message.loading('快速复制中...')
+		message.loading({
+			key: 'quickCopy',
+			content: '复制中...',
+			duration: 0,
+		})
 		fetch('/api/lp-copy', {
 			method: 'POST',
 			headers: {
@@ -209,15 +213,13 @@ function DataTableContent() {
 		})
 			.then((res) => res.json())
 			.then((data) => {
+				message.destroy('quickCopy')
 				if (data.success) {
 					message.success('快速复制成功')
 				} else {
 					message.error(data.error || '快速复制失败')
 				}
 			})
-			.finally(() => {
-				message.destroy()
-			})
 			.catch((err) => {
 				console.error('Error quick copying:', err)
 				message.error('快速复制失败')

+ 28 - 1
src/app/my-lp/page.tsx

@@ -143,7 +143,34 @@ function MyLPPageContent() {
 	}
 
 	const handleClosePosition = (record: RecordInfo) => {
-		console.log(record, 'record')
+		message.loading({
+			key: 'closePosition',
+			content: '正在关仓...',
+			duration: 0,
+		})
+		fetch('/api/lp-index/lp-close', {
+			method: 'POST',
+			body: JSON.stringify({
+				nftMintAddress: record.nftMintAddress,
+			}),
+		})
+			.then((res) => res.json())
+			.then((data) => {
+				message.destroy('closePosition')
+				if (data.success) {
+					message.success('关仓成功')
+					const newLpList = lpList.filter(
+						(item) => item.nftMintAddress !== record.nftMintAddress
+					)
+					setLpList(newLpList)
+				} else {
+					message.error('关仓失败')
+				}
+			})
+			.catch((err) => {
+				console.error('Error closing position:', err)
+				message.error('关仓失败')
+			})
 	}
 
 	const columns: ColumnsType<RecordInfo> = [