Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | 51x 51x 51x 783x 783x 783x 2x 2x 2x 2x 2x 2x 2x 2x 5x 5x 5x 2x 2x 783x 3x 3x 3x 8x 8x 8x 8x 1x 1x 5x 8x 1x 1x 1x 4x 4x 4x 4x 4x 4x 8x 3x 3x 1x 3x 783x 162x 162x 162x 162x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 162x 162x 162x 110x 162x 783x 163x 163x 163x 156x 156x 163x 163x 5x 5x 5x 5x 152x 7x 5x 5x 7x 163x 783x 5x 5x 5x 5x 5x 5x 5x 783x 783x 783x 783x 783x 783x 783x 783x 51x | 'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
module.exports = ({ core: { publicErrors: { createError, errorCodes }, createLedgerValuationRateResolver, }, numeric: { decimal: { formatDecimal, multiplyDecimals, parseDecimal, }, }, databaseValuationCommodity: { valuationCommodityFromDatabase }, }) => {
const missingValuation = (message) => createError(errorCodes.MISSING_VALUATION_DATA, message);
const LEDGER_RESOLVER_CACHE_KEY = Symbol('ledger valuation rate resolver');
function selectLatestPrices(database, throughDate) {
const prices = new Map();
const rows = database.prepare(`
SELECT p.base_commodity, p.quote_quantity, p.quote_commodity
FROM prices AS p
WHERE p.date <= COALESCE(?, '9999-12-31')
ORDER BY p.base_commodity, p.date DESC, p.entry_id DESC
`).all(throughDate ?? null);
for (const row of rows) {
if (!prices.has(row.base_commodity))
prices.set(row.base_commodity, row);
}
return prices;
}
function resolveValuationRates(prices, commodities, valuationCommodity, throughDate) {
const rates = new Map([[valuationCommodity, '1']]);
const visiting = new Set();
function resolve(commodity) {
const cachedRate = rates.get(commodity);
if (cachedRate !== undefined)
return cachedRate;
if (visiting.has(commodity)) {
throw missingValuation(`Circular price chain while converting ${commodity} to ${valuationCommodity}`);
}
const price = prices.get(commodity);
if (!price || !price.quote_commodity) {
const dateContext = throughDate ? ` on or before ${throughDate}` : '';
throw missingValuation(`No price for ${commodity}${dateContext} can convert it to ${valuationCommodity}`);
}
visiting.add(commodity);
const quoteRate = resolve(price.quote_commodity);
const rate = formatDecimal(multiplyDecimals(parseDecimal(price.quote_quantity), parseDecimal(quoteRate)));
visiting.delete(commodity);
rates.set(commodity, rate);
return rate;
}
for (const commodity of commodities)
resolve(commodity);
return rates;
}
function selectMaterializedValuationRates(database, throughDate, commodities, valuationCommodity) {
const selected = [...commodities].filter((commodity) => commodity !== valuationCommodity);
const rates = new Map([[valuationCommodity, '1']]);
if (selected.length === 0)
return rates;
const placeholders = selected.map(() => '?').join(', ');
const rows = database.prepare(`
SELECT commodity, rate
FROM valuation_prices
WHERE date = (
SELECT MAX(date)
FROM valuation_prices
WHERE date <= COALESCE(?, '9999-12-31')
)
AND commodity IN (${placeholders})
ORDER BY commodity
`).all(throughDate ?? null, ...selected);
for (const row of rows)
rates.set(row.commodity, row.rate);
return rates;
}
function queryValuationRates(database, throughDate, commodities, priceCache) {
const valuationCommodity = valuationCommodityFromDatabase(database);
const materializedRates = selectMaterializedValuationRates(database, throughDate, commodities, valuationCommodity);
if ([...commodities].every((commodity) => materializedRates.has(commodity))) {
return materializedRates;
}
let resolve = priceCache?.get(LEDGER_RESOLVER_CACHE_KEY);
if (!resolve) {
resolve = createLedgerValuationRateResolver(selectPriceHistory(database), valuationCommodity);
priceCache?.set(LEDGER_RESOLVER_CACHE_KEY, resolve);
}
const rates = new Map([[valuationCommodity, '1']]);
for (const commodity of commodities) {
if (commodity !== valuationCommodity) {
rates.set(commodity, resolve(commodity, throughDate ?? '9999-12-31'));
}
}
return rates;
}
function selectPriceHistory(database, throughDate) {
return database.prepare(`
SELECT p.date, p.base_commodity, p.quote_quantity, p.quote_commodity
FROM prices AS p
WHERE p.date <= COALESCE(?, '9999-12-31')
ORDER BY p.base_commodity, p.date, p.entry_id
`).all(throughDate ?? null);
}
return {
queryValuationRates,
$$private: {
resolveValuationRates,
selectLatestPrices,
selectMaterializedValuationRates,
},
};
};
|