From 3d603cac32dc2e040fe2ca59718a0cb23363651f Mon Sep 17 00:00:00 2001 From: WrBug Date: Wed, 7 Jan 2026 10:55:11 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20RPC=20=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E6=97=B6=E8=AF=AF=E5=88=9B=E5=BB=BA=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8D=96=E5=87=BA=E8=AE=B0=E5=BD=95=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 当链上查询市场条件出现 RPC 错误(execution reverted)时,不再降级到其他数据源 - 抛出异常让调用方决定如何处理,避免在市场不存在或尚未创建时误判为已卖出 - 修改 getPriceFromChainCondition 返回 Pair,第二个值表示是否发生 RPC 错误 - 在 getCurrentMarketPrice 中检测到 RPC 错误时抛出异常,PositionCheckService 会捕获并跳过该市场的处理 --- .../service/common/MarketPriceService.kt | 41 ++++++++++++++----- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/backend/src/main/kotlin/com/wrbug/polymarketbot/service/common/MarketPriceService.kt b/backend/src/main/kotlin/com/wrbug/polymarketbot/service/common/MarketPriceService.kt index 7a49a97..c9df632 100644 --- a/backend/src/main/kotlin/com/wrbug/polymarketbot/service/common/MarketPriceService.kt +++ b/backend/src/main/kotlin/com/wrbug/polymarketbot/service/common/MarketPriceService.kt @@ -43,12 +43,20 @@ class MarketPriceService( */ suspend fun getCurrentMarketPrice(marketId: String, outcomeIndex: Int): BigDecimal { // 1. 优先从链上查询市场结算结果 - val chainPrice = getPriceFromChainCondition(marketId, outcomeIndex) + val (chainPrice, hasRpcError) = getPriceFromChainCondition(marketId, outcomeIndex) if (chainPrice != null) { // 截位到 4 位小数(向下截断,不四舍五入) return chainPrice.setScale(4, java.math.RoundingMode.DOWN) } + // 如果链上查询出现 RPC 错误(execution reverted),说明市场可能不存在或尚未创建 + // 在这种情况下,不应该继续处理,应该抛出异常,让调用方决定如何处理 + if (hasRpcError) { + val errorMsg = "链上查询市场条件出现 RPC 错误(execution reverted),市场可能不存在或尚未创建: marketId=$marketId, outcomeIndex=$outcomeIndex" + logger.warn(errorMsg) + throw IllegalStateException(errorMsg) + } + // 2. 从 CLOB API 查询订单簿价格(最准确,优先使用) val orderbookPrice = getPriceFromClobOrderbook(marketId, outcomeIndex) if (orderbookPrice != null) { @@ -75,8 +83,10 @@ class MarketPriceService( * - payout > 0(赢了)→ 返回 1.0 * - payout == 0(输了)→ 返回 0.0 * 如果市场未结算或查询失败,返回 null + * + * @return Pair 第一个值是价格(如果已结算),第二个值表示是否发生了 RPC 错误(execution reverted) */ - private suspend fun getPriceFromChainCondition(marketId: String, outcomeIndex: Int): BigDecimal? { + private suspend fun getPriceFromChainCondition(marketId: String, outcomeIndex: Int): Pair { return try { val chainResult = blockchainService.getCondition(marketId) chainResult.fold( @@ -87,30 +97,41 @@ class MarketPriceService( when { payout > BigInteger.ZERO -> { logger.info("从链上查询到市场已结算,该 outcome 赢了: marketId=$marketId, outcomeIndex=$outcomeIndex, payout=$payout") - return BigDecimal.ONE + return Pair(BigDecimal.ONE, false) } payout == BigInteger.ZERO -> { logger.info("从链上查询到市场已结算,该 outcome 输了: marketId=$marketId, outcomeIndex=$outcomeIndex, payout=$payout") - return BigDecimal.ZERO + return Pair(BigDecimal.ZERO, false) } else -> { logger.warn("从链上查询到异常的 payout 值: marketId=$marketId, outcomeIndex=$outcomeIndex, payout=$payout") - null + Pair(null, false) } } } else { logger.debug("从链上查询到市场尚未结算: marketId=$marketId, payouts=${payouts.size}") - null + Pair(null, false) } }, onFailure = { e -> - logger.debug("链上查询市场条件失败,降级到 API 查询: marketId=$marketId, error=${e.message}") - null + // 检查是否是 execution reverted 错误 + val isRpcError = e.message?.contains("execution reverted", ignoreCase = true) == true + if (isRpcError) { + logger.warn("链上查询市场条件出现 RPC 错误(execution reverted),可能市场不存在或尚未创建: marketId=$marketId, error=${e.message}") + } else { + logger.debug("链上查询市场条件失败,降级到 API 查询: marketId=$marketId, error=${e.message}") + } + Pair(null, isRpcError) } ) } catch (e: Exception) { - logger.debug("链上查询市场条件异常: marketId=$marketId, outcomeIndex=$outcomeIndex, error=${e.message}") - null + val isRpcError = e.message?.contains("execution reverted", ignoreCase = true) == true + if (isRpcError) { + logger.warn("链上查询市场条件异常(execution reverted): marketId=$marketId, outcomeIndex=$outcomeIndex, error=${e.message}") + } else { + logger.debug("链上查询市场条件异常: marketId=$marketId, outcomeIndex=$outcomeIndex, error=${e.message}") + } + Pair(null, isRpcError) } }