diff --git a/backend/services/trading-service/src/application/services/c2c-bot.service.ts b/backend/services/trading-service/src/application/services/c2c-bot.service.ts index 6d9dc06a..3c77bd8d 100644 --- a/backend/services/trading-service/src/application/services/c2c-bot.service.ts +++ b/backend/services/trading-service/src/application/services/c2c-bot.service.ts @@ -47,15 +47,24 @@ export class C2cBotService { } this.logger.log(`[BOT] Seller Kava address: ${kavaAddress}`); - // 2. 计算 dUSDT 支付金额(积分值 = dUSDT,1:1 兑换) + // 2. 先扣减卖家积分值余额(可逆的DB操作,放在不可逆的链上转账之前) const paymentAmount = order.totalAmount; - this.logger.log(`[BOT] Payment amount: ${paymentAmount} dUSDT`); + await this.deductSellerBalance(order.makerAccountSequence, paymentAmount); + this.logger.log(`[BOT] Deducted seller balance, proceeding with on-chain transfer`); - // 3. 调用 mining-blockchain-service 转账 dUSDT - const transferResult = await this.blockchainClient.transferDusdt(kavaAddress, paymentAmount); + // 3. 调用 mining-blockchain-service 转账 dUSDT(不可逆) + let transferResult; + try { + transferResult = await this.blockchainClient.transferDusdt(kavaAddress, paymentAmount); + } catch (error: any) { + this.logger.error(`[BOT] Transfer exception: ${error.message}, restoring seller balance`); + await this.restoreSellerBalance(order.makerAccountSequence, paymentAmount); + return false; + } if (!transferResult.success) { - this.logger.error(`[BOT] Transfer failed: ${transferResult.error}`); + this.logger.error(`[BOT] Transfer failed: ${transferResult.error}, restoring seller balance`); + await this.restoreSellerBalance(order.makerAccountSequence, paymentAmount); return false; } @@ -67,9 +76,6 @@ export class C2cBotService { paymentTxHash: transferResult.txHash!, }); - // 5. 扣减卖家的积分值余额 - await this.deductSellerBalance(order.makerAccountSequence, order.totalAmount); - this.logger.log(`[BOT] Order ${order.orderNo} completed successfully`); return true; } catch (error: any) { @@ -96,7 +102,6 @@ export class C2cBotService { throw new Error(`Insufficient cash balance for ${accountSequence}`); } - // 扣减余额 await this.tradingAccountRepository.updateCashBalance( accountSequence, amountDecimal.negated().toString(), @@ -105,6 +110,22 @@ export class C2cBotService { this.logger.log(`[BOT] Deducted ${amount} from ${accountSequence}'s cash balance`); } + /** + * 回补卖家的积分值余额(链上转账失败时的补偿操作) + */ + private async restoreSellerBalance(accountSequence: string, amount: string): Promise { + try { + await this.tradingAccountRepository.updateCashBalance( + accountSequence, + amount, // 正数,加回余额 + ); + this.logger.log(`[BOT] Restored ${amount} to ${accountSequence}'s cash balance`); + } catch (error: any) { + // 回补失败是严重问题,必须告警 + this.logger.error(`[BOT] CRITICAL: Failed to restore ${amount} to ${accountSequence}: ${error.message}`); + } + } + /** * 检查 Bot 服务是否可用 */