@odatano/core 2.0.0-rc.17 → 2.0.0-rc.19

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.
Files changed (35) hide show
  1. package/@cds-models/CardanoIndexerService/index.d.ts +52 -0
  2. package/@cds-models/CardanoIndexerService/index.js +7 -0
  3. package/@cds-models/CardanoODataService/index.d.ts +170 -0
  4. package/@cds-models/CardanoODataService/index.js +21 -0
  5. package/@cds-models/odatano/cardano/index.d.ts +176 -0
  6. package/@cds-models/odatano/cardano/index.js +21 -0
  7. package/README.md +3 -3
  8. package/db/schema.cds +277 -0
  9. package/package.json +179 -179
  10. package/srv/blockchain/backends/cardano-backend.d.ts +16 -0
  11. package/srv/blockchain/backends/cardano-backend.js +4 -0
  12. package/srv/blockchain/backends/koios-backend.d.ts +80 -1
  13. package/srv/blockchain/backends/koios-backend.js +128 -48
  14. package/srv/blockchain/backends/ogmios-backend.d.ts +21 -2
  15. package/srv/blockchain/backends/ogmios-backend.js +120 -16
  16. package/srv/blockchain/cardano-client.d.ts +3 -1
  17. package/srv/blockchain/cardano-client.js +9 -0
  18. package/srv/blockchain/cardano-indexer.d.ts +56 -3
  19. package/srv/blockchain/cardano-indexer.js +124 -3
  20. package/srv/blockchain/crawler/crawler.d.ts +11 -1
  21. package/srv/blockchain/crawler/crawler.js +70 -1
  22. package/srv/blockchain/crawler/sync-state.d.ts +27 -1
  23. package/srv/blockchain/crawler/sync-state.js +83 -1
  24. package/srv/blockchain/crawler/utxo-set-import.d.ts +86 -0
  25. package/srv/blockchain/crawler/utxo-set-import.js +289 -0
  26. package/srv/blockchain/ledger-state.d.ts +89 -0
  27. package/srv/blockchain/ledger-state.js +383 -0
  28. package/srv/cardano-indexer-service.cds +26 -0
  29. package/srv/cardano-indexer-service.js +71 -0
  30. package/srv/cardano-service.cds +35 -0
  31. package/srv/server.js +20 -12
  32. package/srv/utils/db-indexes.js +12 -0
  33. package/srv/utils/mappers.d.ts +43 -3
  34. package/srv/utils/mappers.js +115 -1
  35. package/srv/utils/types.d.ts +38 -0
@@ -1,5 +1,5 @@
1
- import { Transaction as TransactionProviderData, Address as AddressProviderData, UTxO as UtxosProviderData, TxInputLine as TxInputProviderData, TxOutputLine as TxOutputProviderData, Amount as AmountProviderData, NetworkInformation as NetworkInfoProviderData, BlockData as BlockProviderData, EpochData as EpochProviderData, MetadataLabelTx as MetadataLabelTxProviderData, PoolData as PoolProviderData, DrepData as DrepProviderData, AccountData as AccountProviderData, AssetInfo as AssetInfoProviderData, AssetHistoryEntry as AssetHistoryEntryProviderData, TxBuildResult as TransactionBuildResult, LedgerProtocolParameters as ProtocolParameters } from './types';
2
- import { Address as AddressRow, AddressAsset as AddressAssetRow, AddressUTxO as AddressUTxORow, UTxOAsset as UTxOAssetRow, Transaction as TransactionRow, TransactionInput as TransactionInputRow, TransactionInputAsset as TransactionInputAssetRow, TransactionOutput as TransactionOutputRow, TransactionOutputAsset as TransactionOutputAssetRow, NetworkInformation as NetworkInfoRow, TransactionMetadata as TransactionMetadataRow, Block as BlockRow, Epoch as EpochRow, Pool as PoolRow, Drep as DrepRow, PoolEpochSnapshot as PoolEpochSnapshotRow, DrepEpochSnapshot as DrepEpochSnapshotRow, Asset as AssetRow, AssetHistory as AssetHistoryRow, Account as AccountRow, LedgerProtocolParameter as ProtocolParameterRow, AddressTransaction as AddressTransactionRow } from '#cds-models/CardanoODataService';
1
+ import { Transaction as TransactionProviderData, Address as AddressProviderData, UTxO as UtxosProviderData, TxInputLine as TxInputProviderData, TxOutputLine as TxOutputProviderData, TxCertificate as TxCertificateProviderData, TxWithdrawal as TxWithdrawalProviderData, Amount as AmountProviderData, NetworkInformation as NetworkInfoProviderData, BlockData as BlockProviderData, EpochData as EpochProviderData, MetadataLabelTx as MetadataLabelTxProviderData, PoolData as PoolProviderData, DrepData as DrepProviderData, AccountData as AccountProviderData, AssetInfo as AssetInfoProviderData, AssetHistoryEntry as AssetHistoryEntryProviderData, TxBuildResult as TransactionBuildResult, LedgerProtocolParameters as ProtocolParameters } from './types';
2
+ import { Address as AddressRow, AddressAsset as AddressAssetRow, AddressUTxO as AddressUTxORow, UTxOAsset as UTxOAssetRow, Transaction as TransactionRow, TransactionInput as TransactionInputRow, TransactionInputAsset as TransactionInputAssetRow, TransactionOutput as TransactionOutputRow, TransactionOutputAsset as TransactionOutputAssetRow, TransactionCertificate as TransactionCertificateRow, TransactionWithdrawal as TransactionWithdrawalRow, NetworkInformation as NetworkInfoRow, TransactionMetadata as TransactionMetadataRow, Block as BlockRow, Epoch as EpochRow, Pool as PoolRow, Drep as DrepRow, PoolEpochSnapshot as PoolEpochSnapshotRow, DrepEpochSnapshot as DrepEpochSnapshotRow, Asset as AssetRow, AssetHistory as AssetHistoryRow, Account as AccountRow, LedgerProtocolParameter as ProtocolParameterRow, AddressTransaction as AddressTransactionRow } from '#cds-models/CardanoODataService';
3
3
  import type { TransactionBuild as TransactionBuildRow, TransactionBuildInput as TransactionBuildInputRow, TransactionBuildOutput as TransactionBuildOutputRow, TransactionSubmission as TransactionSubmissionRow, AddressTransactionBuild as AddressTransactionBuildRow } from '#cds-models/CardanoTransactionService';
4
4
  import type { AddressSigningRequest as AddressSigningRequestRow } from '#cds-models/CardanoSignService';
5
5
  import type { Request } from '@sap/cds';
@@ -29,6 +29,20 @@ export declare function mapTransactionInputs(txHash: string, txInputs: TxInputPr
29
29
  * @returns {TransactionInputAssetRow[]} mapped transaction input asset rows
30
30
  */
31
31
  export declare function mapTransactionInputAssets(txHash: string, inputs: TxInputProviderData[]): TransactionInputAssetRow[];
32
+ /**
33
+ * Map Transaction Certificates (crawler coverage, `crawler.certificates`).
34
+ * @param txHash transaction hash
35
+ * @param certificates normalized certificates from the source
36
+ * @returns {TransactionCertificateRow[]} one row per (certIndex, kind)
37
+ */
38
+ export declare function mapTransactionCertificates(txHash: string, certificates: TxCertificateProviderData[]): TransactionCertificateRow[];
39
+ /**
40
+ * Map Transaction Withdrawals (crawler coverage, `crawler.certificates`).
41
+ * @param txHash transaction hash
42
+ * @param withdrawals normalized withdrawals from the source
43
+ * @returns {TransactionWithdrawalRow[]} one row per reward account
44
+ */
45
+ export declare function mapTransactionWithdrawals(txHash: string, withdrawals: TxWithdrawalProviderData[]): TransactionWithdrawalRow[];
32
46
  /**
33
47
  * Map Transaction Outputs
34
48
  * Converts provider transaction output data into TransactionOutputRow format
@@ -183,7 +197,7 @@ export declare const BARE_ASSET_STAMP = "1970-01-01T00:00:00.000Z";
183
197
  * Map a bare asset row from an asset unit alone — everything derivable without a provider
184
198
  * call: policyId, assetNameHex, the decoded name and the CIP-14 fingerprint. Used by the
185
199
  * crawler to keep the `Assets` catalogue complete for units it meets in a block, at zero
186
- * network cost (FR "crawler coverage for analytics").
200
+ * network cost (analytics coverage).
187
201
  *
188
202
  * The row is stamped as ALREADY EXPIRED (`validTo === validFrom`). That is deliberate: CAP's
189
203
  * temporal filter hides it from OData reads, so the first keyed read still counts as a miss
@@ -337,4 +351,30 @@ export declare function computeCip14Fingerprint(policyIdHex: string, assetNameHe
337
351
  * Header: 0x71 (mainnet, type 7 network 1) or 0x70 (testnet, type 7 network 0).
338
352
  */
339
353
  export declare function scriptHashToEnterpriseAddress(scriptHashHex: string, network: 'mainnet' | 'preprod' | 'preview'): string;
354
+ /**
355
+ * Encode a stake credential as a bech32 reward account (stake address).
356
+ * Header: type nibble 0xe (key) / 0xf (script), network nibble 1 (mainnet) / 0 (testnets).
357
+ */
358
+ export declare function credentialToStakeAddress(credentialHex: string, isScript: boolean, network: 'mainnet' | 'preprod' | 'preview'): string;
359
+ export interface DecodedAddress {
360
+ type: 'base' | 'pointer' | 'enterprise' | 'reward' | 'byron' | 'unknown';
361
+ /** Payment credential is a script hash. */
362
+ isScript: boolean;
363
+ /** Reward account (bech32) of a base address; null for every other type. */
364
+ stakeAddress: string | null;
365
+ /** Network nibble of the header (1 = mainnet, 0 = testnets); null when not a Shelley address. */
366
+ networkId: number | null;
367
+ }
368
+ /**
369
+ * Decode the Shelley address header (CIP-19) without a provider: type, script flag and,
370
+ * for base addresses, the embedded stake credential re-encoded as a reward account.
371
+ * Byron (base58) and anything unparseable come back as `byron` / `unknown` with no stake.
372
+ */
373
+ export declare function decodeShelleyAddress(address: string): DecodedAddress;
374
+ /**
375
+ * Encode a DRep credential as a CIP-129 DRep ID (`drep1…`, 29 bytes).
376
+ * Header byte: high nibble 0x2 = DRep, low nibble 0x2 = key hash / 0x3 = script hash —
377
+ * the inverse of `decodeDrepId` in the Ogmios backend.
378
+ */
379
+ export declare function credentialToDrepId(credentialHex: string, isScript: boolean): string;
340
380
  //# sourceMappingURL=mappers.d.ts.map
@@ -7,6 +7,8 @@ exports.BARE_ASSET_STAMP = void 0;
7
7
  exports.mapTransaction = mapTransaction;
8
8
  exports.mapTransactionInputs = mapTransactionInputs;
9
9
  exports.mapTransactionInputAssets = mapTransactionInputAssets;
10
+ exports.mapTransactionCertificates = mapTransactionCertificates;
11
+ exports.mapTransactionWithdrawals = mapTransactionWithdrawals;
10
12
  exports.mapTransactionOutputs = mapTransactionOutputs;
11
13
  exports.mapTransactionOutputAssets = mapTransactionOutputAssets;
12
14
  exports.mapAddress = mapAddress;
@@ -39,6 +41,9 @@ exports.normalizeCostModels = normalizeCostModels;
39
41
  exports.decodeAssetName = decodeAssetName;
40
42
  exports.computeCip14Fingerprint = computeCip14Fingerprint;
41
43
  exports.scriptHashToEnterpriseAddress = scriptHashToEnterpriseAddress;
44
+ exports.credentialToStakeAddress = credentialToStakeAddress;
45
+ exports.decodeShelleyAddress = decodeShelleyAddress;
46
+ exports.credentialToDrepId = credentialToDrepId;
42
47
  const cds_1 = __importDefault(require("@sap/cds"));
43
48
  const node_util_1 = require("node:util");
44
49
  const blake2b_1 = __importDefault(require("blake2b"));
@@ -103,6 +108,10 @@ function mapTransactionInputs(txHash, txInputs) {
103
108
  utxoData_dataHash: input.dataHash || null,
104
109
  utxoData_inlineDatum: input.inlineDatum || null,
105
110
  utxoData_referenceScriptHash: input.referenceScriptHash || null,
111
+ // Outpoint of the consumed UTxO — every source carries it in memory (the chain-sync
112
+ // path needs it for resolveInputs), the row just never kept it before.
113
+ spentTxHash: input.txHash || null,
114
+ spentOutputIndex: Number.isInteger(input.outputIndex) ? input.outputIndex : null,
106
115
  isCollateral: Boolean(input.isCollateral),
107
116
  isReference: Boolean(input.isReference),
108
117
  hasAddresses: hasAddress,
@@ -136,6 +145,39 @@ function mapTransactionInputAssets(txHash, inputs) {
136
145
  });
137
146
  });
138
147
  }
148
+ /**
149
+ * Map Transaction Certificates (crawler coverage, `crawler.certificates`).
150
+ * @param txHash transaction hash
151
+ * @param certificates normalized certificates from the source
152
+ * @returns {TransactionCertificateRow[]} one row per (certIndex, kind)
153
+ */
154
+ function mapTransactionCertificates(txHash, certificates) {
155
+ return certificates.map((c) => ({
156
+ tx_hash: txHash,
157
+ certIndex: c.certIndex,
158
+ kind: c.kind,
159
+ stakeAddress: c.stakeAddress || null,
160
+ poolId: c.poolId || null,
161
+ drepId: c.drepId || null,
162
+ deposit: c.deposit != null ? String(c.deposit) : null,
163
+ epoch: c.epoch ?? null,
164
+ }));
165
+ }
166
+ /**
167
+ * Map Transaction Withdrawals (crawler coverage, `crawler.certificates`).
168
+ * @param txHash transaction hash
169
+ * @param withdrawals normalized withdrawals from the source
170
+ * @returns {TransactionWithdrawalRow[]} one row per reward account
171
+ */
172
+ function mapTransactionWithdrawals(txHash, withdrawals) {
173
+ return withdrawals
174
+ .filter((w) => !!w.stakeAddress)
175
+ .map((w) => ({
176
+ tx_hash: txHash,
177
+ stakeAddress: w.stakeAddress,
178
+ lovelace: String(w.amount ?? '0'),
179
+ }));
180
+ }
139
181
  /**
140
182
  * Map Transaction Outputs
141
183
  * Converts provider transaction output data into TransactionOutputRow format
@@ -648,7 +690,7 @@ exports.BARE_ASSET_STAMP = '1970-01-01T00:00:00.000Z';
648
690
  * Map a bare asset row from an asset unit alone — everything derivable without a provider
649
691
  * call: policyId, assetNameHex, the decoded name and the CIP-14 fingerprint. Used by the
650
692
  * crawler to keep the `Assets` catalogue complete for units it meets in a block, at zero
651
- * network cost (FR "crawler coverage for analytics").
693
+ * network cost (analytics coverage).
652
694
  *
653
695
  * The row is stamped as ALREADY EXPIRED (`validTo === validFrom`). That is deliberate: CAP's
654
696
  * temporal filter hides it from OData reads, so the first keyed read still counts as a miss
@@ -1070,6 +1112,78 @@ function scriptHashToEnterpriseAddress(scriptHashHex, network) {
1070
1112
  const hrp = network === 'mainnet' ? 'addr' : 'addr_test';
1071
1113
  return bech32_1.bech32.encode(hrp, words, 120);
1072
1114
  }
1115
+ /**
1116
+ * Encode a stake credential as a bech32 reward account (stake address).
1117
+ * Header: type nibble 0xe (key) / 0xf (script), network nibble 1 (mainnet) / 0 (testnets).
1118
+ */
1119
+ function credentialToStakeAddress(credentialHex, isScript, network) {
1120
+ const header = (isScript ? 0xf0 : 0xe0) | (network === 'mainnet' ? 0x01 : 0x00);
1121
+ const payload = Buffer.alloc(29);
1122
+ payload[0] = header;
1123
+ Buffer.from(credentialHex, 'hex').copy(payload, 1);
1124
+ const hrp = network === 'mainnet' ? 'stake' : 'stake_test';
1125
+ return bech32_1.bech32.encode(hrp, bech32_1.bech32.toWords(payload), 120);
1126
+ }
1127
+ /**
1128
+ * Decode the Shelley address header (CIP-19) without a provider: type, script flag and,
1129
+ * for base addresses, the embedded stake credential re-encoded as a reward account.
1130
+ * Byron (base58) and anything unparseable come back as `byron` / `unknown` with no stake.
1131
+ */
1132
+ function decodeShelleyAddress(address) {
1133
+ const none = { type: 'unknown', isScript: false, stakeAddress: null, networkId: null };
1134
+ if (typeof address !== 'string' || !address.length)
1135
+ return none;
1136
+ let bytes;
1137
+ try {
1138
+ const decoded = bech32_1.bech32.decode(address, 120);
1139
+ if (decoded.prefix !== 'addr' && decoded.prefix !== 'addr_test' && decoded.prefix !== 'stake' && decoded.prefix !== 'stake_test')
1140
+ return none;
1141
+ bytes = Buffer.from(bech32_1.bech32.fromWords(decoded.words));
1142
+ }
1143
+ catch {
1144
+ // Byron addresses are base58 and start with Ae2 / DdzFF (mainnet) or 2cWKM… (testnets)
1145
+ return /^(Ae2|DdzFF|2cWKM|37btj|KjgoiX)/.test(address) ? { ...none, type: 'byron' } : none;
1146
+ }
1147
+ if (!bytes.length)
1148
+ return none;
1149
+ const header = bytes[0];
1150
+ const type = header >> 4;
1151
+ const networkId = header & 0x0f;
1152
+ const net = networkId === 1 ? 'mainnet' : 'preview';
1153
+ switch (type) {
1154
+ case 0:
1155
+ case 1:
1156
+ case 2:
1157
+ case 3: {
1158
+ const stakeAddress = bytes.length >= 57
1159
+ ? credentialToStakeAddress(bytes.subarray(29, 57).toString('hex'), type >= 2, net)
1160
+ : null;
1161
+ return { type: 'base', isScript: type === 1 || type === 3, stakeAddress, networkId };
1162
+ }
1163
+ case 4:
1164
+ case 5:
1165
+ return { type: 'pointer', isScript: type === 5, stakeAddress: null, networkId };
1166
+ case 6:
1167
+ case 7:
1168
+ return { type: 'enterprise', isScript: type === 7, stakeAddress: null, networkId };
1169
+ case 14:
1170
+ case 15:
1171
+ return { type: 'reward', isScript: type === 15, stakeAddress: address, networkId };
1172
+ default:
1173
+ return { ...none, networkId };
1174
+ }
1175
+ }
1176
+ /**
1177
+ * Encode a DRep credential as a CIP-129 DRep ID (`drep1…`, 29 bytes).
1178
+ * Header byte: high nibble 0x2 = DRep, low nibble 0x2 = key hash / 0x3 = script hash —
1179
+ * the inverse of `decodeDrepId` in the Ogmios backend.
1180
+ */
1181
+ function credentialToDrepId(credentialHex, isScript) {
1182
+ const payload = Buffer.alloc(29);
1183
+ payload[0] = isScript ? 0x23 : 0x22;
1184
+ Buffer.from(credentialHex, 'hex').copy(payload, 1);
1185
+ return bech32_1.bech32.encode('drep', bech32_1.bech32.toWords(payload), 120);
1186
+ }
1073
1187
  /**
1074
1188
  * Format error message
1075
1189
  * @param code error code
@@ -49,6 +49,36 @@ export interface TxOutputLine {
49
49
  isCollateral: boolean;
50
50
  referenceScriptHash?: Hex | null;
51
51
  }
52
+ /**
53
+ * Normalized certificate kinds. Both crawler sources map onto this vocabulary; a type
54
+ * neither knows is passed through as the raw source string so nothing is dropped.
55
+ * A Conway stake+vote delegation is SPLIT into a `pool_delegation` and a `vote_delegation`
56
+ * entry sharing one `certIndex` — Koios (db-sync) reports it that way natively, the
57
+ * Ogmios mapper splits to match, so both sources yield identical rows.
58
+ */
59
+ export type CertificateKind = 'stake_registration' | 'stake_deregistration' | 'pool_delegation' | 'vote_delegation' | 'pool_registration' | 'pool_retirement' | 'drep_registration' | 'drep_update' | 'drep_retirement' | 'committee_hot_auth' | 'committee_resign' | 'genesis_delegation' | 'treasury_mir' | 'reserve_mir' | 'pot_transfer' | 'param_proposal' | (string & {});
60
+ /** One certificate of a transaction (crawler ledger-state coverage). */
61
+ export interface TxCertificate {
62
+ /** Position in the transaction's certificate list as the source reports it. */
63
+ certIndex: number;
64
+ kind: CertificateKind;
65
+ /** Reward account (bech32) the certificate concerns, when it has one. */
66
+ stakeAddress?: string | null;
67
+ /** Stake pool (bech32) for delegation / registration / retirement. */
68
+ poolId?: string | null;
69
+ /** DRep (CIP-129 bech32), or `drep_always_abstain` / `drep_always_no_confidence`. */
70
+ drepId?: string | null;
71
+ /** Deposit paid or refunded, lovelace as string. */
72
+ deposit?: Lovelace | string | null;
73
+ /** Retirement epoch of a pool retirement. */
74
+ epoch?: number | null;
75
+ }
76
+ /** One reward-account withdrawal of a transaction. */
77
+ export interface TxWithdrawal {
78
+ stakeAddress: string;
79
+ /** Lovelace as string. */
80
+ amount: Lovelace | string;
81
+ }
52
82
  /**
53
83
  * Transaction Data Structure Type - Normalized transaction structure
54
84
  */
@@ -94,6 +124,14 @@ export interface Transaction {
94
124
  inputs: TxInputLine[];
95
125
  outputs: TxOutputLine[];
96
126
  metadata?: MetadataLabelTx[];
127
+ /**
128
+ * Certificates as the source reports them; `[]` when the transaction carries none,
129
+ * undefined when the source does not report the field at all (Blockfrost — a per-tx
130
+ * enumeration would cost six extra calls per transaction, so it stays unreported there).
131
+ */
132
+ certificates?: TxCertificate[];
133
+ /** Reward-account withdrawals; same `[]` vs undefined convention as `certificates`. */
134
+ withdrawals?: TxWithdrawal[];
97
135
  }
98
136
  /**
99
137
  * Address Data Structure Type - Address view, including current value and known UTxOs