|
|
@@ -1,15 +1,23 @@
|
|
|
import { NextRequest, NextResponse } from 'next/server'
|
|
|
-import { Keypair, PublicKey, VersionedTransaction } from '@solana/web3.js'
|
|
|
+import {
|
|
|
+ Keypair,
|
|
|
+ PublicKey,
|
|
|
+ VersionedTransaction,
|
|
|
+ Connection,
|
|
|
+} from '@solana/web3.js'
|
|
|
import BN from 'bn.js'
|
|
|
import { Decimal } from 'decimal.js'
|
|
|
import bs58 from 'bs58'
|
|
|
import { chain } from '@/lib/config'
|
|
|
-import { getTokenPricesFromJupiter } from '@/lib/jupiter'
|
|
|
+import {
|
|
|
+ getTokenPricesFromJupiter,
|
|
|
+ ensureSufficientBalances,
|
|
|
+} from '@/lib/jupiter'
|
|
|
|
|
|
export async function POST(request: NextRequest) {
|
|
|
try {
|
|
|
const body = await request.json()
|
|
|
- const { nftMintAddress, addUsdValue } = body
|
|
|
+ const { nftMintAddress, addUsdValue, needSwap = false } = body
|
|
|
|
|
|
// 验证输入
|
|
|
if (!nftMintAddress) {
|
|
|
@@ -300,6 +308,55 @@ export async function POST(request: NextRequest) {
|
|
|
10 ** poolInfo.mintDecimalsB
|
|
|
)
|
|
|
|
|
|
+ // 从环境变量读取私钥(余额检查/换币需要用到)
|
|
|
+ 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
|
|
|
+
|
|
|
+ const tokenAValueUsd = uiAmountA.toNumber() * tokenAPriceUsd
|
|
|
+ const tokenBValueUsd = uiAmountB.toNumber() * tokenBPriceUsd
|
|
|
+ const tokenAInfo = {
|
|
|
+ mint: tokenAAddress,
|
|
|
+ valueUsd: tokenAValueUsd,
|
|
|
+ }
|
|
|
+ const tokenBInfo = {
|
|
|
+ mint: tokenBAddress,
|
|
|
+ valueUsd: tokenBValueUsd,
|
|
|
+ }
|
|
|
+
|
|
|
+ if (needSwap) {
|
|
|
+ const rpcUrl =
|
|
|
+ process.env.SOL_RPC_URL || 'https://api.mainnet-beta.solana.com'
|
|
|
+ const connection = new Connection(rpcUrl, 'confirmed')
|
|
|
+ const balanceCheck = await ensureSufficientBalances(
|
|
|
+ connection,
|
|
|
+ userKeypair,
|
|
|
+ tokenAInfo,
|
|
|
+ tokenBInfo
|
|
|
+ )
|
|
|
+ if (!balanceCheck.success) {
|
|
|
+ console.error(
|
|
|
+ 'Failed to ensure sufficient balances:',
|
|
|
+ balanceCheck.error
|
|
|
+ )
|
|
|
+ return NextResponse.json(
|
|
|
+ {
|
|
|
+ error: '余额不足且自动换币失败',
|
|
|
+ details: balanceCheck.error,
|
|
|
+ swapTxids: balanceCheck.swapTxids,
|
|
|
+ },
|
|
|
+ { status: 400 }
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// 打印所有信息
|
|
|
console.log('\n========== Add Liquidity Information ==========')
|
|
|
console.log('Position NFT Address:', nftMint.toBase58())
|
|
|
@@ -332,18 +389,6 @@ export async function POST(request: NextRequest) {
|
|
|
)
|
|
|
console.log('==========================================\n')
|
|
|
|
|
|
- // 从环境变量读取私钥
|
|
|
- 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('Adding liquidity on-chain...')
|