@le-space/eigenbeleg 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +662 -0
- package/README.md +18 -2
- package/package.json +49 -3
- package/src/booking-pdf.js +172 -0
- package/src/booking.js +88 -0
- package/src/csv.js +106 -0
- package/src/document.js +306 -0
- package/src/index.js +32 -0
- package/src/layout.js +229 -0
- package/src/pdf.js +277 -0
- package/src/statement-layout.js +164 -0
- package/src/statement-pdf.js +235 -0
- package/src/statement.js +155 -0
- package/src/value.js +74 -0
package/src/statement.js
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
//
|
|
3
|
+
// The Kontoauszug: every booking of one own blockchain account in a period,
|
|
4
|
+
// laid out the way a bank lays out an account statement – opening balance,
|
|
5
|
+
// each booking with debit (Soll) or credit (Haben) and the running balance,
|
|
6
|
+
// closing balance, sums. One section per asset the account holds.
|
|
7
|
+
//
|
|
8
|
+
// It is built from public chain data by the account holder. It is no
|
|
9
|
+
// document of a bank or of anyone else, and says so.
|
|
10
|
+
//
|
|
11
|
+
// The closing balance is checked: opening balance plus bookings must give
|
|
12
|
+
// the balance the chain reports after the period (NEAR, from the archival
|
|
13
|
+
// node). A difference is shown, not hidden. Where a chain keeps no old
|
|
14
|
+
// balances (Akash, Filecoin), the balances are derived from today's balance
|
|
15
|
+
// less the movements since, and the statement says that instead.
|
|
16
|
+
|
|
17
|
+
import { normalize, sum } from '@le-space/ledger';
|
|
18
|
+
import { CHAIN_NAMES, KIND_TEXT } from './document.js';
|
|
19
|
+
|
|
20
|
+
/** Aleph Cloud credits: the same layout, as a monthly Verbrauchsauszug. */
|
|
21
|
+
export const USAGE_NOTE =
|
|
22
|
+
'Verbrauchsauszug eines Aleph-Cloud-Kontos (Credits), erstellt vom Kontoinhaber aus der öffentlichen Aleph-API. ' +
|
|
23
|
+
'Kein Dokument eines Kreditinstituts und keine Rechnung von Aleph. Verbrauch je Tag und Ressource; Euro-Wert: USD-Preis je Credit ' +
|
|
24
|
+
'(letzter Kauf am oder vor dem Tag, sonst Alephs Listenpreis von 1 USD je 1.000.000 Credits) zum EZB-Referenzkurs des Tages.';
|
|
25
|
+
|
|
26
|
+
export const STATEMENT_NOTE =
|
|
27
|
+
'Kontoauszug eines Blockchain-Kontos, erstellt vom Kontoinhaber aus öffentlichen Chain-Daten. ' +
|
|
28
|
+
'Kein Dokument eines Kreditinstituts. Jede Buchung ist über ihren Transaktions-Hash in einem öffentlichen Block-Explorer nachprüfbar.';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @typedef {object} StatementRow
|
|
32
|
+
* @property {string} date YYYY-MM-DD
|
|
33
|
+
* @property {string} time HH:MM:SS UTC
|
|
34
|
+
* @property {string} text what happened
|
|
35
|
+
* @property {string} counterparty
|
|
36
|
+
* @property {string} hash
|
|
37
|
+
* @property {string} explorerUrl
|
|
38
|
+
* @property {string} debit amount out, positive, or ''
|
|
39
|
+
* @property {string} credit amount in, positive, or ''
|
|
40
|
+
* @property {string} balance running balance after it
|
|
41
|
+
* @property {string} eur signed EUR value at the day's rate, or ''
|
|
42
|
+
* @property {string} beleg the Eigenbeleg's number, when the booking has one
|
|
43
|
+
* @property {boolean} fee
|
|
44
|
+
* @property {boolean} usage consumption billed from the balance (Aleph credits)
|
|
45
|
+
* @property {string} hashLabel what the hash is: `Tx`, or `Ressource` for usage
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* @param {object} p
|
|
50
|
+
* @param {{ name: string, address: string }} p.payer the account holder
|
|
51
|
+
* @param {string} p.account the own address
|
|
52
|
+
* @param {string} p.chain
|
|
53
|
+
* @param {string} p.network
|
|
54
|
+
* @param {{ from: string, to: string }} p.period
|
|
55
|
+
* @param {import('./value.js').ValuedEntry[]} p.entries this account's entries in the period
|
|
56
|
+
* @param {Record<string, string>} p.opening asset → balance before the period
|
|
57
|
+
* @param {Record<string, string>} p.closing asset → balance after it, as the chain or the derivation says
|
|
58
|
+
* @param {'archive' | 'derived'} p.method
|
|
59
|
+
* @param {{ opening: { height: number, time: string }, closing: { height: number, time: string } } | null} [p.at]
|
|
60
|
+
* @param {Map<string, string>} [p.belege] hash → Eigenbeleg number
|
|
61
|
+
* @param {string} [p.number]
|
|
62
|
+
* @param {string} [p.issuedAt]
|
|
63
|
+
*/
|
|
64
|
+
export function buildStatement({
|
|
65
|
+
payer,
|
|
66
|
+
account,
|
|
67
|
+
chain,
|
|
68
|
+
network,
|
|
69
|
+
period,
|
|
70
|
+
entries,
|
|
71
|
+
opening,
|
|
72
|
+
closing,
|
|
73
|
+
method,
|
|
74
|
+
at = null,
|
|
75
|
+
belege = new Map(),
|
|
76
|
+
number,
|
|
77
|
+
issuedAt
|
|
78
|
+
}) {
|
|
79
|
+
const own = entries.filter((e) => e.account === account && e.success !== false);
|
|
80
|
+
const assets = [
|
|
81
|
+
...new Set([...Object.keys(opening), ...own.filter((e) => e.priceAs).map((e) => e.asset)])
|
|
82
|
+
];
|
|
83
|
+
const sections = assets.map((asset) => {
|
|
84
|
+
let balance = normalize(opening[asset] ?? '0');
|
|
85
|
+
/** @type {StatementRow[]} */
|
|
86
|
+
const rows = [];
|
|
87
|
+
for (const e of own.filter((x) => x.asset === asset)) {
|
|
88
|
+
balance = sum([balance, e.amount]);
|
|
89
|
+
const out = e.amount.startsWith('-');
|
|
90
|
+
rows.push({
|
|
91
|
+
date: e.date,
|
|
92
|
+
time: e.time.slice(11, 19),
|
|
93
|
+
text: `${KIND_TEXT[/** @type {keyof typeof KIND_TEXT} */ (e.kind)] ?? e.kind}${e.type === 'received' ? ' (Eingang)' : ''}`,
|
|
94
|
+
counterparty: e.counterpartyLabel
|
|
95
|
+
? `${e.counterpartyLabel} · ${e.counterparty}`
|
|
96
|
+
: e.counterparty,
|
|
97
|
+
hash: e.hash,
|
|
98
|
+
explorerUrl: e.explorerUrl,
|
|
99
|
+
debit: out ? e.amount.slice(1) : '',
|
|
100
|
+
credit: out ? '' : e.amount,
|
|
101
|
+
balance,
|
|
102
|
+
eur: e.eur?.value ?? '',
|
|
103
|
+
beleg: belege.get(e.hash) ?? '',
|
|
104
|
+
fee: e.type === 'fee',
|
|
105
|
+
usage: e.kind === 'usage',
|
|
106
|
+
hashLabel: e.kind === 'usage' ? 'Ressource' : 'Tx'
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
const debit = sum(rows.map((r) => r.debit || '0'));
|
|
110
|
+
const credit = sum(rows.map((r) => r.credit || '0'));
|
|
111
|
+
const reported = closing[asset];
|
|
112
|
+
return {
|
|
113
|
+
asset,
|
|
114
|
+
opening: normalize(opening[asset] ?? '0'),
|
|
115
|
+
closing: balance,
|
|
116
|
+
reported: reported === undefined ? null : normalize(reported),
|
|
117
|
+
difference:
|
|
118
|
+
reported === undefined
|
|
119
|
+
? null
|
|
120
|
+
: sum([reported, balance.startsWith('-') ? balance.slice(1) : `-${balance}`]),
|
|
121
|
+
debit,
|
|
122
|
+
credit,
|
|
123
|
+
rows,
|
|
124
|
+
eurOut: sum(rows.filter((r) => r.debit && r.eur).map((r) => r.eur.replace(/^-/, ''))),
|
|
125
|
+
eurIn: sum(rows.filter((r) => r.credit && r.eur).map((r) => r.eur)),
|
|
126
|
+
eurFees: sum(rows.filter((r) => r.fee && r.eur).map((r) => r.eur.replace(/^-/, ''))),
|
|
127
|
+
eurUsage: sum(rows.filter((r) => r.usage && r.eur).map((r) => r.eur.replace(/^-/, ''))),
|
|
128
|
+
unvalued: rows.filter((r) => !r.eur).length
|
|
129
|
+
};
|
|
130
|
+
});
|
|
131
|
+
// Aleph credits: a monthly usage statement, laid out as the Kontoauszug.
|
|
132
|
+
const usage = chain === 'aleph';
|
|
133
|
+
return {
|
|
134
|
+
kind: 'kontoauszug',
|
|
135
|
+
title: usage ? 'Verbrauchsauszug' : 'Kontoauszug',
|
|
136
|
+
number: number || `KA-${period.from}`,
|
|
137
|
+
issuedAt: issuedAt ?? new Date().toISOString(),
|
|
138
|
+
holder: { name: payer.name.trim(), address: payer.address.trim() },
|
|
139
|
+
account,
|
|
140
|
+
chain,
|
|
141
|
+
chainName: CHAIN_NAMES[/** @type {keyof typeof CHAIN_NAMES} */ (chain)] ?? chain,
|
|
142
|
+
network,
|
|
143
|
+
testnet: network !== 'mainnet',
|
|
144
|
+
period,
|
|
145
|
+
method,
|
|
146
|
+
at,
|
|
147
|
+
sections,
|
|
148
|
+
unlisted: entries.filter(
|
|
149
|
+
(e) => e.account === account && !e.priceAs && !assets.includes(e.asset)
|
|
150
|
+
).length,
|
|
151
|
+
note: usage ? USAGE_NOTE : STATEMENT_NOTE
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** @typedef {ReturnType<typeof buildStatement>} Statement */
|
package/src/value.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// Every entry with its euro value on the day it was made. Where the rate
|
|
3
|
+
// comes from is the caller's (the pipeline asks @le-space/rates); this file
|
|
4
|
+
// only applies it, and names a fixed rate for tests and offline demos.
|
|
5
|
+
//
|
|
6
|
+
// Aleph Cloud credits have no rate of the day: they are valued at the USD
|
|
7
|
+
// price per credit the reader hands with each entry (`unitPrice`: the last
|
|
8
|
+
// purchase on or before the day, else Aleph's list price) at that day's ECB
|
|
9
|
+
// rate for USD (@le-space/rates/aleph-credits).
|
|
10
|
+
|
|
11
|
+
import { multiply } from '@le-space/ledger';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @typedef {object} Rate
|
|
15
|
+
* @property {string} rate EUR per unit, decimal string
|
|
16
|
+
* @property {string} source human-readable: where and which moment, printed on the document
|
|
17
|
+
* @property {string} date YYYY-MM-DD the rate is for
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** @typedef {{ rateFor: (asset: string, date: string) => Promise<Rate | null> }} Rates */
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @typedef {import('@le-space/ledger').LedgerEntry & { eur: { value: string, rate: string, source: string } | null }} ValuedEntry
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Every entry with its euro value (8 decimals; printed with 4, the total rounded to cents
|
|
28
|
+
* where it is printed). An entry without a price gets `eur: null`. One with a
|
|
29
|
+
* `unitPrice` in USD (Aleph credits) is valued at that price and the day's rate for USD.
|
|
30
|
+
*
|
|
31
|
+
* @param {import('@le-space/ledger').LedgerEntry[]} entries
|
|
32
|
+
* @param {Rates} rates
|
|
33
|
+
* @returns {Promise<ValuedEntry[]>}
|
|
34
|
+
*/
|
|
35
|
+
export async function valueEntries(entries, rates) {
|
|
36
|
+
const out = [];
|
|
37
|
+
for (const e of entries) {
|
|
38
|
+
const unit =
|
|
39
|
+
/** @type {{ currency?: string, amount?: string, source?: string } | undefined} */ (
|
|
40
|
+
/** @type {any} */ (e).unitPrice
|
|
41
|
+
);
|
|
42
|
+
if (!e.priceAs && e.success && unit?.currency === 'USD' && unit.amount) {
|
|
43
|
+
const usd = await rates.rateFor('USD', e.date);
|
|
44
|
+
const how = unit.source === 'purchase' ? 'Kaufpreis' : 'Listenpreis';
|
|
45
|
+
out.push({
|
|
46
|
+
...e,
|
|
47
|
+
eur: usd
|
|
48
|
+
? {
|
|
49
|
+
value: multiply(multiply(e.amount, unit.amount, 12), usd.rate, 8),
|
|
50
|
+
rate: multiply(unit.amount, usd.rate, 14),
|
|
51
|
+
source: `Aleph ${how} ${unit.amount} USD je Credit × ${usd.source}`
|
|
52
|
+
}
|
|
53
|
+
: null
|
|
54
|
+
});
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
const r = e.priceAs && e.success ? await rates.rateFor(e.priceAs, e.date) : null;
|
|
58
|
+
out.push({
|
|
59
|
+
...e,
|
|
60
|
+
eur: r ? { value: multiply(e.amount, r.rate, 8), rate: r.rate, source: r.source } : null
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
return out;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Fixed rates, for tests and offline demos. @param {Record<string, string>} table `NEAR:2026-10-05` → `3.8796` @returns {Rates} */
|
|
67
|
+
export function fixedRates(table) {
|
|
68
|
+
return {
|
|
69
|
+
async rateFor(asset, date) {
|
|
70
|
+
const rate = table[`${asset}:${date}`] ?? table[asset];
|
|
71
|
+
return rate ? { rate, source: `fester Testkurs (${asset})`, date } : null;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
}
|