From 28c44d72198c5e5ee578be8b1ffdee8e64accdd9 Mon Sep 17 00:00:00 2001 From: hailin Date: Mon, 15 Dec 2025 07:37:36 -0800 Subject: [PATCH] =?UTF-8?q?fix(wallet):=20=E6=8F=90=E7=8E=B0=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E8=A7=A3=E5=86=BB=E6=97=B6=E4=BC=98=E5=85=88=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=20accountSequence=20=E6=9F=A5=E6=89=BE=E9=92=B1?= =?UTF-8?q?=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题背景: - 原实现使用 userId 查找钱包进行解冻操作 - userId 来自外部 identity-service,存在变化风险 - 如果 userId 发生变化,可能导致解冻到错误的钱包 解决方案: - 优先使用 accountSequence 查找钱包(wallet-service 内部主键,稳定可靠) - 保留 userId 作为兜底查找方式,确保向后兼容 - 增加钱包找不到时的详细错误日志 改动点: - withdrawal-status.handler.ts: handleWithdrawalFailed() 方法 - 与认种(planting)的钱包查找逻辑保持一致 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- .../event-handlers/withdrawal-status.handler.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/backend/services/wallet-service/src/application/event-handlers/withdrawal-status.handler.ts b/backend/services/wallet-service/src/application/event-handlers/withdrawal-status.handler.ts index b8fb3a98..10622bbc 100644 --- a/backend/services/wallet-service/src/application/event-handlers/withdrawal-status.handler.ts +++ b/backend/services/wallet-service/src/application/event-handlers/withdrawal-status.handler.ts @@ -103,12 +103,19 @@ export class WithdrawalStatusHandler implements OnModuleInit { // Refund frozen funds back to available balance if needed if (order.needsUnfreeze()) { - const wallet = await this.walletRepo.findByUserId(order.userId.value); + // 优先使用 accountSequence 查找钱包(更可靠,避免 userId 变化导致扣错人) + let wallet = await this.walletRepo.findByAccountSequence(order.accountSequence); + if (!wallet) { + // 兜底:使用 userId 查找 + wallet = await this.walletRepo.findByUserId(order.userId.value); + } if (wallet) { // Unfreeze the amount (add back to available balance) wallet.unfreeze(order.amount); await this.walletRepo.save(wallet); - this.logger.log(`[FAILED] Refunded ${order.amount.value} USDT to user ${order.userId}`); + this.logger.log(`[FAILED] Refunded ${order.amount.value} USDT to account ${order.accountSequence}`); + } else { + this.logger.error(`[FAILED] Wallet not found for accountSequence: ${order.accountSequence}, userId: ${order.userId}`); } }