diff --git a/backend/services/trading-service/src/infrastructure/kafka/market-maker-deposit-consumer.service.ts b/backend/services/trading-service/src/infrastructure/kafka/market-maker-deposit-consumer.service.ts index 9531571f..1efd341b 100644 --- a/backend/services/trading-service/src/infrastructure/kafka/market-maker-deposit-consumer.service.ts +++ b/backend/services/trading-service/src/infrastructure/kafka/market-maker-deposit-consumer.service.ts @@ -204,25 +204,23 @@ export class MarketMakerDepositConsumerService implements OnModuleInit, OnModule } // 2. 根据资产类型更新余额 + // 与中心化充值保持一致:只操作 trading_accounts,balanceBefore 从 trading_accounts 读取 const assetField = payload.assetType === 'EUSDT' ? 'shareBalance' : 'cashBalance'; - const currentBalance = payload.assetType === 'EUSDT' - ? marketMaker.shareBalance - : marketMaker.cashBalance; - const newBalance = currentBalance.add(new Prisma.Decimal(payload.amountFormatted)); + const depositAmount = new Prisma.Decimal(payload.amountFormatted); - // 更新做市商配置余额 - await tx.marketMakerConfig.update({ - where: { id: marketMaker.id }, - data: { - [assetField]: newBalance, - }, + const tradingAccountBefore = await tx.tradingAccount.findUnique({ + where: { accountSequence: marketMaker.accountSequence }, }); + const currentBalance = tradingAccountBefore + ? (payload.assetType === 'EUSDT' ? tradingAccountBefore.shareBalance : tradingAccountBefore.cashBalance) + : new Prisma.Decimal(0); + const newBalance = currentBalance.add(depositAmount); - // 同步更新交易账户余额(getConfig 从 trading_accounts 读取余额) + // 更新交易账户余额(权威数据源,与中心化充值路径一致) await tx.tradingAccount.update({ where: { accountSequence: marketMaker.accountSequence }, data: { - [assetField]: { increment: new Prisma.Decimal(payload.amountFormatted) }, + [assetField]: { increment: depositAmount }, }, }); @@ -232,7 +230,7 @@ export class MarketMakerDepositConsumerService implements OnModuleInit, OnModule marketMakerId: marketMaker.id, type: 'DEPOSIT', assetType: payload.assetType === 'EUSDT' ? 'SHARE' : 'CASH', - amount: new Prisma.Decimal(payload.amountFormatted), + amount: depositAmount, balanceBefore: currentBalance, balanceAfter: newBalance, memo: `区块链充值: ${payload.txHash.slice(0, 10)}... (${payload.fromAddress.slice(0, 10)}...)`,