fix(api): 修复 portfolio 计算中漏写 new Big() 包装导致的 TypeError

This commit is contained in:
kennethcheng 2026-04-28 01:18:19 +08:00
parent b9186d4699
commit 746be06840

View File

@ -54,7 +54,7 @@ function calculateCnyValueFromPrice(
const directRate = getRate(rateMap, baseCurrency, 'CNY'); const directRate = getRate(rateMap, baseCurrency, 'CNY');
if (directRate) { if (directRate) {
return quantity.times(price).times(directRate); return quantity.times(price).times(new Big(directRate));
} }
const usdToCny = getRate(rateMap, 'USD', 'CNY'); const usdToCny = getRate(rateMap, 'USD', 'CNY');
@ -64,7 +64,7 @@ function calculateCnyValueFromPrice(
const usdRate = getRate(rateMap, baseCurrency, 'USD'); const usdRate = getRate(rateMap, baseCurrency, 'USD');
if (usdRate) { if (usdRate) {
return quantity.times(price).times(usdRate).times(usdToCny); return quantity.times(price).times(new Big(usdRate)).times(new Big(usdToCny));
} }
return new Big('0'); return new Big('0');
@ -116,16 +116,16 @@ export async function getPortfolioPositions(): Promise<Position[]> {
const holding = holdings.get(tx.assetId)!; const holding = holdings.get(tx.assetId)!;
if (tx.txType === 'BUY') { if (tx.txType === 'BUY') {
holding.quantity = holding.quantity.plus(tx.quantity); holding.quantity = holding.quantity.plus(new Big(tx.quantity));
const costPerUnit = tx.quantity.times(tx.price); const costPerUnit = new Big(tx.quantity).times(new Big(tx.price));
const costCny = costPerUnit.times(tx.exchangeRate || '1'); const costCny = costPerUnit.times(new Big(tx.exchangeRate || '1'));
holding.totalCostCny = holding.totalCostCny.plus(costCny); holding.totalCostCny = holding.totalCostCny.plus(costCny);
} else if (tx.txType === 'SELL') { } else if (tx.txType === 'SELL') {
holding.quantity = holding.quantity.minus(tx.quantity); holding.quantity = holding.quantity.minus(new Big(tx.quantity));
} else if (tx.txType === 'AIRDROP') { } else if (tx.txType === 'AIRDROP') {
holding.quantity = holding.quantity.plus(tx.quantity); holding.quantity = holding.quantity.plus(new Big(tx.quantity));
} else if (tx.txType === 'DIVIDEND') { } else if (tx.txType === 'DIVIDEND') {
holding.quantity = holding.quantity.plus(tx.quantity); holding.quantity = holding.quantity.plus(new Big(tx.quantity));
} }
if (tx.assetLatestPrice) { if (tx.assetLatestPrice) {