Files
AtreidesAssetsFree/shared/wealth-metrics.js
T

53 lines
2.2 KiB
JavaScript
Raw Permalink Normal View History

2026-10-29 11:57:29 +01:00
// Generated, do not edit. sha256:d8050520cd6294fe603e71def64a9f6a61433bedfd9e055fc63f536c99905b1a
export function assetMetrics(positie, price) {
const div = Math.pow(10, positie.decimals);
const units = positie.net / div;
const value = price != null ? units * price : null;
const avgPrice = positie.buy_amount ? positie.buy_spot_raw / positie.buy_amount : null;
const avgCost = avgCostOf(positie);
const paidUnits = paidUnitsOf(positie);
const costAtAvg = avgCost != null ? paidUnits * avgCost : (paidUnits > 0 ? null : 0);
return {
units,
value,
avgPrice,
avgCost,
unrealized: (value != null && costAtAvg != null) ? value - costAtAvg : null,
avgPremium: positie.buy_events ? positie.buy_fee_total / positie.buy_events : null,
invested: (positie.buy_spot_raw / div) + positie.buy_fee_total,
};
}
export function saleResult(positie, sale) {
const div = Math.pow(10, positie.decimals);
const verkocht = (Number(sale.amount) || 0) / div;
const opbrengst = verkocht * (Number(sale.fiatPrice) || 0) - (Number(sale.fiatFee) || 0);
const weg = verkocht + (Number(sale.unitFee) || 0) / div;
const kostEenheden = Math.min(weg, paidUnitsOf(positie));
const avgCost = avgCostOf(positie);
if (avgCost == null) return kostEenheden > 0 ? null : opbrengst;
return opbrengst - kostEenheden * avgCost;
}
export function feeResult(positie, unitFee) {
return saleResult(positie, { amount: 0, fiatPrice: 0, fiatFee: 0, unitFee });
}
export function combinePositions(rows) {
const som = { decimals: rows[0]?.decimals ?? 0, net: 0, buy_amount: 0, buy_spot_raw: 0, buy_fee_total: 0, buy_events: 0, gift_amount: 0 };
for (const r of rows) {
for (const k of ['net', 'buy_amount', 'buy_spot_raw', 'buy_fee_total', 'buy_events', 'gift_amount']) som[k] += Number(r[k]) || 0;
}
return som;
}
function avgCostOf(positie) {
const div = Math.pow(10, positie.decimals);
return positie.buy_amount
? (positie.buy_spot_raw + (positie.buy_fee_total || 0) * div) / positie.buy_amount : null;
}
function paidUnitsOf(positie) {
const div = Math.pow(10, positie.decimals);
return Math.max(0, positie.net / div - (positie.gift_amount || 0) / div);
}