unrip/test/portfolio-metrics.test.mjs
philipp e0dfd24a8b
All checks were successful
deploy / deploy (push) Successful in 32s
Link quote outcomes to settled inventory
Proof: Adds a durable quote outcome attribution model, refreshes it from submitted execution results plus inventory snapshots, and updates dashboard lifecycle rows so submitted, blocked, rejected, not-filled, and completed states are separated by durable evidence. Lowers the approved live strategy edge threshold to 1.49%.

Assumptions: Exact asset-unit inventory deltas inside the attribution window are acceptable as heuristic settlement evidence for the active BTC/EURe NEAR Intents path when the uncertainty is stored and shown. Deadline-plus-inventory non-fill is inferred until venue terminal events are persisted.

Still fake: No venue-native terminal fill event or per-trade fee/cost ledger is stored yet; heuristic completed and not-filled records remain explicitly labeled as inferred where applicable, and realized net PnL is still not claimed.
2026-04-10 11:24:22 +02:00

175 lines
5.5 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { buildPortfolioMetricId, computePortfolioMetric } from '../src/core/portfolio-metrics.mjs';
const btcAsset = {
assetId: 'nep141:btc.omft.near',
symbol: 'BTC',
decimals: 8,
};
const eureAsset = {
assetId: 'nep141:eure.omft.near',
symbol: 'EURe',
decimals: 18,
};
test('portfolio metrics compute portfolio comparison and mark-to-market pnl from baseline funding inventory', () => {
const metric = computePortfolioMetric({
baseline: {
anchor: 'latest_inventory_before_first_command',
command_at: '2026-04-02T18:10:43.569Z',
inventory: {
inventory_id: 'baseline-1',
synced_at: '2026-04-02T18:10:33.381Z',
spendable: {
'nep141:btc.omft.near': '100000',
'nep141:eure.omft.near': '60000000000000000000',
},
},
price: {
price_id: 'price-baseline-1',
observed_at: '2026-04-02T18:10:30.109Z',
eure_per_btc: '57792.20000000',
},
},
currentInventory: {
inventory_id: 'current-1',
synced_at: '2026-04-02T20:46:48.492Z',
spendable: {
'nep141:btc.omft.near': '137014',
'nep141:eure.omft.near': '38999978799978799978',
},
},
currentPrice: {
price_id: 'price-current-1',
observed_at: '2026-04-02T20:46:55.305Z',
eure_per_btc: '58079.20000000',
},
btcAsset,
eureAsset,
commandCount: 7,
resultCount: 7,
});
assert.equal(metric.baseline_status, 'active');
assert.equal(metric.current_portfolio_value_eure, '118.576613887978799978');
assert.equal(metric.portfolio_vs_simple_hold_eure, '0.497413887978799978');
assert.equal(metric.trade_pnl_eure, null);
assert.equal(metric.mark_to_market_pnl_eure, '0.784413887978799978');
assert.equal(metric.price_move_pnl_eure, '0.287');
assert.deepEqual(metric.inventory_delta, {
btc_units: '37014',
btc: '0.00037014',
eure_units: '-21000021200021200022',
eure: '-21.000021200021200022',
});
});
test('portfolio metrics stay available before the first live execution', () => {
const metric = computePortfolioMetric({
baseline: null,
currentInventory: {
inventory_id: 'current-2',
synced_at: '2026-04-02T20:46:48.492Z',
spendable: {
'nep141:btc.omft.near': '100000',
'nep141:eure.omft.near': '60000000000000000000',
},
},
currentPrice: {
price_id: 'price-current-2',
observed_at: '2026-04-02T20:46:55.305Z',
eure_per_btc: '58079.20000000',
},
btcAsset,
eureAsset,
commandCount: 0,
resultCount: 0,
});
assert.equal(metric.baseline_status, 'awaiting_first_execution');
assert.equal(metric.current_portfolio_value_eure, '118.0792');
assert.equal(metric.portfolio_vs_simple_hold_eure, null);
assert.equal(metric.trade_pnl_eure, null);
assert.equal(metric.mark_to_market_pnl_eure, null);
assert.equal(metric.price_move_pnl_eure, null);
});
test('portfolio metrics treat later deposits and withdrawals as external cash flows instead of PnL', () => {
const metric = computePortfolioMetric({
baseline: {
anchor: 'latest_inventory_before_first_command',
command_at: '2026-04-02T18:10:43.569Z',
inventory: {
inventory_id: 'baseline-2',
synced_at: '2026-04-02T18:10:33.381Z',
spendable: {
'nep141:btc.omft.near': '100000',
'nep141:eure.omft.near': '60000000000000000000',
},
},
price: {
price_id: 'price-baseline-2',
observed_at: '2026-04-02T18:10:30.109Z',
eure_per_btc: '57792.20000000',
},
},
currentInventory: {
inventory_id: 'current-3',
synced_at: '2026-04-07T15:43:30.463Z',
spendable: {
'nep141:btc.omft.near': '137014',
'nep141:eure.omft.near': '63999978599978799978',
},
},
currentPrice: {
price_id: 'price-current-3',
observed_at: '2026-04-07T15:43:29.885Z',
eure_per_btc: '58845.90000000',
},
externalFlows: [
{
flow_id: 'withdrawal-1',
kind: 'withdrawal',
asset_id: eureAsset.assetId,
effective_at: '2026-04-02T10:52:27.863Z',
signed_units: '-1000000000000000000',
reference_price_eure_per_btc_at_flow_time: null,
},
{
flow_id: 'deposit-1',
kind: 'deposit',
asset_id: eureAsset.assetId,
effective_at: '2026-04-07T15:20:54.757Z',
signed_units: '24999999800000000000',
reference_price_eure_per_btc_at_flow_time: null,
},
],
btcAsset,
eureAsset,
commandCount: 7,
resultCount: 7,
});
assert.equal(metric.external_cash_flows.flow_count, 2);
assert.equal(metric.external_cash_flows.net_eure, '23.9999998');
assert.equal(metric.external_cash_flows.net_value_eure_at_flow_time, '23.9999998');
assert.equal(metric.baseline_portfolio_value_eure_at_baseline_price, '141.7921998');
assert.equal(metric.baseline_portfolio_value_eure_at_current_price, '142.8458998');
assert.equal(metric.mark_to_market_pnl_eure, '2.834900225978799978');
assert.equal(metric.price_move_pnl_eure, '1.0537');
assert.equal(metric.portfolio_vs_simple_hold_eure, '1.781200225978799978');
assert.equal(metric.trade_pnl_eure, null);
});
test('portfolio metric id keys off the baseline and current snapshots', () => {
const metricId = buildPortfolioMetricId({
baselineInventoryId: 'baseline-1',
currentInventoryId: 'current-1',
currentPriceId: 'price-current-1',
});
assert.equal(metricId, 'portfolio-metric:baseline-1:current-1:price-current-1');
});