Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions tests/unit/handlers/finance.handlers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,19 @@ test('processEbitdaPrices - processes valid data', (t) => {
t.pass()
})

test('processEbitdaPrices - flat per-ork items with priceUSD (production shape)', (t) => {
const results = [
[
{ ts: 1700006400000, priceUSD: 40000 },
{ ts: 1700092800000, priceUSD: 41500 }
]
]
const daily = processEbitdaPrices(results)
t.is(daily[1700006400000], 40000, 'should extract priceUSD for first day')
t.is(daily[1700092800000], 41500, 'should extract priceUSD for second day')
t.pass()
})

test('calculateEbitdaSummary - calculates from log entries', (t) => {
const log = [
{ revenueBTC: 0.5, revenueUSD: 20000, totalCostsUSD: 5000, ebitdaSelling: 15000, ebitdaHodl: 15000 },
Expand Down
15 changes: 9 additions & 6 deletions workers/lib/server/handlers/finance.handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,19 +449,22 @@ function processEbitdaPrices (results) {
if (!Array.isArray(data)) continue
for (const entry of data) {
if (!entry) continue
const items = entry.data || entry.prices || entry
// mempool returns a flat array of price records; entry IS the item.
const rawTs = entry.ts || entry.timestamp || entry.time
const items = rawTs ? [entry] : (entry.data || entry.prices || entry)
if (Array.isArray(items)) {
for (const item of items) {
const ts = getStartOfDay(item.ts || item.timestamp || item.time)
if (ts && item.price) {
daily[ts] = item.price
const price = item.priceUSD || item.price
if (ts && price) {
daily[ts] = price
}
}
} else if (typeof items === 'object' && !Array.isArray(items)) {
} else if (typeof items === 'object') {
for (const [key, val] of Object.entries(items)) {
const ts = getStartOfDay(Number(key))
if (ts) {
daily[ts] = typeof val === 'object' ? (val.USD || val.price || 0) : Number(val) || 0
daily[ts] = typeof val === 'object' ? (val.USD || val.priceUSD || val.price || 0) : Number(val) || 0
}
}
}
Expand Down Expand Up @@ -518,7 +521,7 @@ async function getCostSummary (ctx, req) {

(cb) => ctx.dataProxy.requestData(RPC_METHODS.GET_WRK_EXT_DATA, {
type: WORKER_TYPES.MEMPOOL,
query: { key: 'prices', start, end }
query: { key: 'HISTORICAL_PRICES', start, end }
}).then(r => cb(null, r)).catch(cb),

(cb) => ctx.dataProxy.requestData(RPC_METHODS.TAIL_LOG_RANGE_AGGR, {
Expand Down
Loading