route.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { NextRequest, NextResponse } from 'next/server'
  2. import { TickMath } from '@/lib/byreal-clmm-sdk/src/instructions/utils/tickMath'
  3. export async function GET(request: NextRequest) {
  4. try {
  5. const searchParams = request.nextUrl.searchParams
  6. const tickParam = searchParams.get('tick')
  7. const decimalsAParam = searchParams.get('decimalsA')
  8. const decimalsBParam = searchParams.get('decimalsB')
  9. const baseInParam = searchParams.get('baseIn')
  10. if (
  11. tickParam === null ||
  12. decimalsAParam === null ||
  13. decimalsBParam === null
  14. ) {
  15. return NextResponse.json(
  16. { error: 'Missing required parameters: tick, decimalsA, decimalsB' },
  17. { status: 400 }
  18. )
  19. }
  20. const tick = parseInt(tickParam, 10)
  21. const decimalsA = parseInt(decimalsAParam, 10)
  22. const decimalsB = parseInt(decimalsBParam, 10)
  23. const baseIn = baseInParam === 'false' ? false : true
  24. if (isNaN(tick) || isNaN(decimalsA) || isNaN(decimalsB)) {
  25. return NextResponse.json(
  26. {
  27. error:
  28. 'Invalid parameter types. tick, decimalsA, and decimalsB must be numbers.',
  29. },
  30. { status: 400 }
  31. )
  32. }
  33. const price = TickMath.getPriceFromTick({
  34. tick,
  35. decimalsA,
  36. decimalsB,
  37. baseIn,
  38. })
  39. return NextResponse.json({
  40. price: price.toString(),
  41. })
  42. } catch (error) {
  43. console.error('Tick to Price API Error:', error)
  44. return NextResponse.json(
  45. { error: 'Internal Server Error' },
  46. { status: 500 }
  47. )
  48. }
  49. }