|
|
@@ -12,7 +12,6 @@ import { chain } from '@/lib/config'
|
|
|
import { TickMath } from '@/lib/byreal-clmm-sdk/src/instructions/utils/tickMath'
|
|
|
import {
|
|
|
ensureSufficientBalances,
|
|
|
- getTokenBalance,
|
|
|
getTokenPricesFromJupiter,
|
|
|
} from '@/lib/jupiter'
|
|
|
|
|
|
@@ -129,10 +128,6 @@ async function copyLPPosition(
|
|
|
tokenAPriceUsd = parseFloat(poolData.mintA.price)
|
|
|
tokenBPriceUsd = parseFloat(poolData.mintB.price)
|
|
|
priceFromApi = true
|
|
|
- console.log('Using prices from ByReal API:', {
|
|
|
- tokenAPriceUsd,
|
|
|
- tokenBPriceUsd,
|
|
|
- })
|
|
|
}
|
|
|
} catch (error) {
|
|
|
console.warn('Failed to fetch prices from API:', error)
|
|
|
@@ -148,10 +143,6 @@ async function copyLPPosition(
|
|
|
tokenAPriceUsd = jupiterPrices.tokenAPriceUsd
|
|
|
tokenBPriceUsd = jupiterPrices.tokenBPriceUsd
|
|
|
priceFromApi = true
|
|
|
- console.log('Using prices from Jupiter Price API v3:', {
|
|
|
- tokenAPriceUsd,
|
|
|
- tokenBPriceUsd,
|
|
|
- })
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -181,9 +172,6 @@ async function copyLPPosition(
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- console.log('tokenAPriceUsd', tokenAPriceUsd)
|
|
|
- console.log('tokenBPriceUsd', tokenBPriceUsd)
|
|
|
-
|
|
|
// 计算比例,使得总价值不超过 maxUsdValue
|
|
|
// tickLower/tickUpper 默认使用父仓位的范围;当传入 priceLower/priceUpper(百分比) 且都不为 0 时覆盖
|
|
|
let tickLower = positionInfo.rawPositionInfo.tickLower
|
|
|
@@ -487,47 +475,6 @@ async function copyLPPosition(
|
|
|
finalUiAmountA.toNumber() * tokenAPriceUsd +
|
|
|
finalUiAmountB.toNumber() * tokenBPriceUsd
|
|
|
|
|
|
- // 打印所有信息
|
|
|
- console.log('\n========== LP Copy Information ==========')
|
|
|
- console.log('Original Position Address:', positionAddress)
|
|
|
- console.log('Referer Position (NFT Mint):', nftMint.toBase58())
|
|
|
- console.log('\n--- Pool Information ---')
|
|
|
- console.log('Pool Address:', poolInfo.poolId.toBase58())
|
|
|
- console.log('Token A Address:', poolInfo.mintA.toBase58())
|
|
|
- console.log('Token B Address:', poolInfo.mintB.toBase58())
|
|
|
- console.log('Token A Decimals:', poolInfo.mintDecimalsA)
|
|
|
- console.log('Token B Decimals:', poolInfo.mintDecimalsB)
|
|
|
- console.log('Current Price (A/B):', currentPrice)
|
|
|
- console.log('Token A Price (USD):', tokenAPriceUsd)
|
|
|
- console.log('Token B Price (USD):', tokenBPriceUsd)
|
|
|
- console.log('\n--- Position Range ---')
|
|
|
- console.log('Tick Lower:', tickLower)
|
|
|
- console.log('Tick Upper:', tickUpper)
|
|
|
- console.log('Price Lower:', priceLower.toString())
|
|
|
- console.log('Price Upper:', priceUpper.toString())
|
|
|
- if (shouldOverrideTicks) {
|
|
|
- console.log('\n--- Range Override (Percent) ---')
|
|
|
- console.log('Input priceLower(% of current):', priceLowerPct)
|
|
|
- console.log('Input priceUpper(% of current):', priceUpperPct)
|
|
|
- }
|
|
|
- console.log('\n--- Investment Details ---')
|
|
|
- console.log('Max USD Value:', maxUsdValue)
|
|
|
- console.log('Base Token:', base)
|
|
|
- console.log('Base Amount (raw):', baseAmount.toString())
|
|
|
- console.log('Other Amount Max (raw):', otherAmountMax.toString())
|
|
|
- console.log('Token A Amount (UI):', finalUiAmountA.toString())
|
|
|
- console.log('Token B Amount (UI):', finalUiAmountB.toString())
|
|
|
- console.log(
|
|
|
- 'Token A Value (USD):',
|
|
|
- (finalUiAmountA.toNumber() * tokenAPriceUsd).toFixed(2)
|
|
|
- )
|
|
|
- console.log(
|
|
|
- 'Token B Value (USD):',
|
|
|
- (finalUiAmountB.toNumber() * tokenBPriceUsd).toFixed(2)
|
|
|
- )
|
|
|
- console.log('Total Value (USD):', totalValue.toFixed(2))
|
|
|
- console.log('==========================================\n')
|
|
|
-
|
|
|
// 检查代币数量是否为0
|
|
|
if (baseAmount.eq(new BN(0)) || otherAmountMax.eq(new BN(0))) {
|
|
|
console.error('Error: One of the token amounts is zero')
|
|
|
@@ -579,11 +526,6 @@ async function copyLPPosition(
|
|
|
const connection = new Connection(rpcUrl, 'confirmed')
|
|
|
|
|
|
if (needSwap) {
|
|
|
- // ========== 按 valueUsd 换币(ExactIn)==========
|
|
|
- console.log('\n--- Swapping by valueUsd (ExactIn) ---')
|
|
|
- console.log(`Token A (${tokenAInfo.mint.slice(0, 8)}...): swap $${tokenAValueUsd.toFixed(2)} USDC`)
|
|
|
- console.log(`Token B (${tokenBInfo.mint.slice(0, 8)}...): swap $${tokenBValueUsd.toFixed(2)} USDC`)
|
|
|
-
|
|
|
const balanceCheck = await ensureSufficientBalances(
|
|
|
connection,
|
|
|
userKeypair,
|
|
|
@@ -602,43 +544,8 @@ async function copyLPPosition(
|
|
|
{ status: 400 }
|
|
|
)
|
|
|
}
|
|
|
-
|
|
|
- if (balanceCheck.swapTxids.length > 0) {
|
|
|
- console.log('\n--- Swap Transactions ---')
|
|
|
- balanceCheck.swapTxids.forEach((txid, index) => {
|
|
|
- console.log(`Swap ${index + 1}: https://solscan.io/tx/${txid}`)
|
|
|
- })
|
|
|
- }
|
|
|
- } else {
|
|
|
- // ========== 不换币:仅检查余额是否足够 ==========
|
|
|
- console.log('\n--- Checking balances (no swap) ---')
|
|
|
- const requiredA = finalUiAmountA.toNumber()
|
|
|
- const requiredB = finalUiAmountB.toNumber()
|
|
|
- const [balanceA, balanceB] = await Promise.all([
|
|
|
- getTokenBalance(connection, userAddress, tokenAInfo.mint),
|
|
|
- getTokenBalance(connection, userAddress, tokenBInfo.mint),
|
|
|
- ])
|
|
|
- console.log(`Token A: balance=${balanceA.toFixed(6)}, required=${requiredA.toFixed(6)}`)
|
|
|
- console.log(`Token B: balance=${balanceB.toFixed(6)}, required=${requiredB.toFixed(6)}`)
|
|
|
- if (balanceA < requiredA || balanceB < requiredB) {
|
|
|
- return NextResponse.json(
|
|
|
- {
|
|
|
- error: '余额不足',
|
|
|
- details: {
|
|
|
- tokenA: { balance: balanceA, required: requiredA, mint: tokenAInfo.mint },
|
|
|
- tokenB: { balance: balanceB, required: requiredB, mint: tokenBInfo.mint },
|
|
|
- },
|
|
|
- },
|
|
|
- { status: 400 }
|
|
|
- )
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
- console.log('\n--- Balances Sufficient, Proceeding ---')
|
|
|
- console.log('User Address:', userAddress.toBase58())
|
|
|
- console.log('\n--- Executing Transaction ---')
|
|
|
- console.log('Creating position on-chain...')
|
|
|
-
|
|
|
// 执行实际上链操作
|
|
|
const signerCallback = async (tx: VersionedTransaction) => {
|
|
|
tx.sign([userKeypair])
|
|
|
@@ -657,10 +564,6 @@ async function copyLPPosition(
|
|
|
signerCallback,
|
|
|
})
|
|
|
|
|
|
- console.log('Transaction ID:', txid)
|
|
|
- console.log('Position created successfully!')
|
|
|
- console.log('==========================================\n')
|
|
|
-
|
|
|
return NextResponse.json({
|
|
|
success: true,
|
|
|
txid,
|