|
@@ -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 }
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+}
|