fix(trading-service): 区块链充值 balanceBefore 从 trading_accounts 读取

修复 MarketMakerDepositConsumerService 读取 market_maker_configs.cash_balance
作为 balanceBefore 的 bug。该字段由中心化充值路径不维护,导致区块链充值
流水中 balanceBefore 始终为 0 或脱轨值。

改为与 deposit()/withdraw() 一致:
- balanceBefore 从 trading_accounts 读取(权威数据源)
- 删除对 market_maker_configs.cash_balance 的写操作

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-03-11 11:12:40 -07:00
parent 7aa7a54d9c
commit e306f346d3
1 changed files with 11 additions and 13 deletions

View File

@ -204,25 +204,23 @@ export class MarketMakerDepositConsumerService implements OnModuleInit, OnModule
}
// 2. 根据资产类型更新余额
// 与中心化充值保持一致:只操作 trading_accountsbalanceBefore 从 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)}...)`,