lushdog@outlook.com 1 mese fa
parent
commit
13414cf17f
1 ha cambiato i file con 55 aggiunte e 0 eliminazioni
  1. 55 0
      src/app/api/tick-to-price/route.ts

+ 55 - 0
src/app/api/tick-to-price/route.ts

@@ -0,0 +1,55 @@
+import { NextRequest, NextResponse } from 'next/server'
+import { TickMath } from '@/lib/byreal-clmm-sdk/src/instructions/utils/tickMath'
+
+export async function GET(request: NextRequest) {
+	try {
+		const searchParams = request.nextUrl.searchParams
+		const tickParam = searchParams.get('tick')
+		const decimalsAParam = searchParams.get('decimalsA')
+		const decimalsBParam = searchParams.get('decimalsB')
+		const baseInParam = searchParams.get('baseIn')
+
+		if (
+			tickParam === null ||
+			decimalsAParam === null ||
+			decimalsBParam === null
+		) {
+			return NextResponse.json(
+				{ error: 'Missing required parameters: tick, decimalsA, decimalsB' },
+				{ status: 400 }
+			)
+		}
+
+		const tick = parseInt(tickParam, 10)
+		const decimalsA = parseInt(decimalsAParam, 10)
+		const decimalsB = parseInt(decimalsBParam, 10)
+		const baseIn = baseInParam === 'false' ? false : true
+
+		if (isNaN(tick) || isNaN(decimalsA) || isNaN(decimalsB)) {
+			return NextResponse.json(
+				{
+					error:
+						'Invalid parameter types. tick, decimalsA, and decimalsB must be numbers.',
+				},
+				{ status: 400 }
+			)
+		}
+
+		const price = TickMath.getPriceFromTick({
+			tick,
+			decimalsA,
+			decimalsB,
+			baseIn,
+		})
+
+		return NextResponse.json({
+			price: price.toString(),
+		})
+	} catch (error) {
+		console.error('Tick to Price API Error:', error)
+		return NextResponse.json(
+			{ error: 'Internal Server Error' },
+			{ status: 500 }
+		)
+	}
+}