From 1ee1090179a970df2f6177af8042abab852eafce Mon Sep 17 00:00:00 2001 From: Cho Young-Hwi Date: Mon, 30 Mar 2026 21:17:31 +0100 Subject: [PATCH] [#637] Align real-time trade indexer price with cron indexer Real-time indexer used priceForNextMint (marginal bonding curve price) while cron indexer used batch average (reserveAmount / tokenAmount). Switched real-time to batch average to match cron: - Reflects actual cost paid, not marginal price - Removes extra priceForNextMint RPC call - Deterministic and consistent regardless of which path runs first Fixes realproject7/plotlink#637 Co-Authored-By: Claude Opus 4.6 (1M context) --- src/app/api/index/trade/route.ts | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/src/app/api/index/trade/route.ts b/src/app/api/index/trade/route.ts index 5930bbf2..93c7b3cc 100644 --- a/src/app/api/index/trade/route.ts +++ b/src/app/api/index/trade/route.ts @@ -2,7 +2,7 @@ import { NextResponse } from "next/server"; import { type Hex, decodeEventLog, formatUnits } from "viem"; import { publicClient, getReceiptWithRetry } from "../../../../../lib/rpc"; import { createServerClient } from "../../../../../lib/supabase"; -import { mcv2BondEventAbi, priceForNextMintFunction } from "../../../../../lib/contracts/abi"; +import { mcv2BondEventAbi } from "../../../../../lib/contracts/abi"; import { MCV2_BOND, ZAP_PLOTLINK } from "../../../../../lib/contracts/constants"; import { erc20Abi } from "../../../../../lib/price"; import { validateRecentTx } from "../../../../../lib/index-auth"; @@ -87,24 +87,11 @@ export async function POST(req: Request) { const reserveAmount = isMint ? args.reserveAmount! : args.refundAmount!; const tokenAmount = isMint ? args.amountMinted! : args.amountBurned!; - // Marginal price from bonding curve (not batch average) - let pricePerToken = 0; - try { - const price = await publicClient.readContract({ - address: MCV2_BOND as `0x${string}`, - abi: [priceForNextMintFunction], - functionName: "priceForNextMint", - args: [args.token], - blockNumber: receipt.blockNumber, - }); - pricePerToken = Number(formatUnits(price, 18)); - } catch { - // Fallback to batch average if RPC call fails - pricePerToken = tokenAmount > BigInt(0) - ? Number(formatUnits(reserveAmount, 18)) / - Number(formatUnits(tokenAmount, 18)) - : 0; - } + // Batch average price (reserveAmount / tokenAmount) — consistent with cron indexer + const pricePerToken = tokenAmount > BigInt(0) + ? Number(formatUnits(reserveAmount, 18)) / + Number(formatUnits(tokenAmount, 18)) + : 0; let totalSupply = BigInt(0); try {