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 | 71x 71x 811x 811x 811x 811x 811x 811x 346x 346x 93371x 93371x 93371x 93371x 346x 346x 323038x 323038x 304179x 304179x 304179x 323038x 323001x 3x 3x 323038x 323038x 323038x 323038x 323038x 299819x 299819x 299819x 4290x 323001x 109x 109x 109x 109x 109x 109x 109x 109x 109x 42x 42x 42x 42x 109x 4217x 4217x 323038x 346x 322931x 322931x 2x 2x 322929x 346x 346x 811x 811x 811x 71x | 'use strict';
module.exports = ({ numeric: { decimal: { formatDecimal, multiplyDecimals, parseDecimal }, }, publicErrors: { createError, errorCodes }, external: { zod: { z } }, }) => {
const missingValuation = (message) => createError(errorCodes.MISSING_VALUATION_DATA, message);
const resolverArgumentsSchema = z.tuple([
z.string().min(1),
z.string().optional(),
]);
function createLedgerValuationRateResolver(priceRows, valuationCommodity) {
const histories = new Map();
for (const price of priceRows) {
const history = histories.get(price.base_commodity) ?? [];
history.push(price);
histories.set(price.base_commodity, history);
}
const cache = new Map();
function resolve(commodity, throughDate, visiting) {
if (commodity === valuationCommodity)
return '1';
const key = `${commodity}\0${throughDate}`;
const cached = cache.get(key);
if (cached !== undefined)
return cached;
if (visiting.has(commodity)) {
throw missingValuation(`Circular price chain while converting ${commodity} to ${valuationCommodity}`);
}
const eligible = (histories.get(commodity) ?? [])
.filter((price) => throughDate !== undefined && price.date <= throughDate);
// Ledger prefers a direct quote even when a newer indirect quote exists.
const direct = eligible.slice().reverse().find((price) => price.quote_commodity === valuationCommodity);
if (direct) {
cache.set(key, direct.quote_quantity);
return direct.quote_quantity;
}
visiting.add(commodity);
for (let index = eligible.length - 1; index >= 0; index -= 1) {
const price = eligible[index];
try {
const quoteRate = resolve(price.quote_commodity, throughDate, visiting);
const rate = formatDecimal(multiplyDecimals(parseDecimal(price.quote_quantity), parseDecimal(quoteRate)));
cache.set(key, rate);
visiting.delete(commodity);
return rate;
}
catch (error) {
if (!(error instanceof Error) || !('code' in error) || error.code !== errorCodes.MISSING_VALUATION_DATA ||
!error.message.startsWith('No price for '))
throw error;
}
}
visiting.delete(commodity);
throw missingValuation(`No price for ${commodity} on or before ${throughDate} can convert it to ${valuationCommodity}`);
}
return (commodity, throughDate) => {
const result = resolverArgumentsSchema.safeParse([commodity, throughDate]);
if (!result.success) {
throw createError(errorCodes.INVALID_API_INPUT, `Invalid valuation-rate arguments: ${result.error.issues[0].message}`, TypeError);
}
return resolve(result.data[0], result.data[1], new Set());
};
}
return {
createLedgerValuationRateResolver,
};
};
|