@odatano/core 0.3.1
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 +201 -0
- package/README.md +212 -0
- package/cds-plugin.js +5 -0
- package/config/preview/cardano-node/alonzo-genesis.json +196 -0
- package/config/preview/cardano-node/byron-genesis.json +117 -0
- package/config/preview/cardano-node/config.json +118 -0
- package/config/preview/cardano-node/conway-genesis.json +297 -0
- package/config/preview/cardano-node/shelley-genesis.json +68 -0
- package/config/preview/cardano-node/topology.json +19 -0
- package/db/schema.cds +1318 -0
- package/package.json +125 -0
- package/src/index.d.ts.map +1 -0
- package/src/index.js +96 -0
- package/src/index.js.map +1 -0
- package/src/plugin.d.ts.map +1 -0
- package/src/plugin.js +92 -0
- package/src/plugin.js.map +1 -0
- package/srv/blockchain/backends/blockfrost-backend.d.ts.map +1 -0
- package/srv/blockchain/backends/blockfrost-backend.js +398 -0
- package/srv/blockchain/backends/blockfrost-backend.js.map +1 -0
- package/srv/blockchain/backends/cardano-backend.d.ts.map +1 -0
- package/srv/blockchain/backends/cardano-backend.js +12 -0
- package/srv/blockchain/backends/cardano-backend.js.map +1 -0
- package/srv/blockchain/backends/koios-backend.d.ts.map +1 -0
- package/srv/blockchain/backends/koios-backend.js +537 -0
- package/srv/blockchain/backends/koios-backend.js.map +1 -0
- package/srv/blockchain/backends/ogmios-backend.d.ts.map +1 -0
- package/srv/blockchain/backends/ogmios-backend.js +516 -0
- package/srv/blockchain/backends/ogmios-backend.js.map +1 -0
- package/srv/blockchain/cardano-client.d.ts.map +1 -0
- package/srv/blockchain/cardano-client.js +377 -0
- package/srv/blockchain/cardano-client.js.map +1 -0
- package/srv/blockchain/cardano-indexer.d.ts.map +1 -0
- package/srv/blockchain/cardano-indexer.js +542 -0
- package/srv/blockchain/cardano-indexer.js.map +1 -0
- package/srv/blockchain/cardano-tx-builder.d.ts.map +1 -0
- package/srv/blockchain/cardano-tx-builder.js +232 -0
- package/srv/blockchain/cardano-tx-builder.js.map +1 -0
- package/srv/blockchain/circuit-breaker.d.ts.map +1 -0
- package/srv/blockchain/circuit-breaker.js +110 -0
- package/srv/blockchain/circuit-breaker.js.map +1 -0
- package/srv/blockchain/signing/external-signer.d.ts.map +1 -0
- package/srv/blockchain/signing/external-signer.js +302 -0
- package/srv/blockchain/signing/external-signer.js.map +1 -0
- package/srv/blockchain/signing/signature-verifier.d.ts.map +1 -0
- package/srv/blockchain/signing/signature-verifier.js +249 -0
- package/srv/blockchain/signing/signature-verifier.js.map +1 -0
- package/srv/blockchain/transaction-building/buildooor-tx.d.ts.map +1 -0
- package/srv/blockchain/transaction-building/buildooor-tx.js +636 -0
- package/srv/blockchain/transaction-building/buildooor-tx.js.map +1 -0
- package/srv/blockchain/transaction-building/cardano-tx.d.ts.map +1 -0
- package/srv/blockchain/transaction-building/cardano-tx.js +3 -0
- package/srv/blockchain/transaction-building/cardano-tx.js.map +1 -0
- package/srv/blockchain/transaction-building/csl-tx.d.ts.map +1 -0
- package/srv/blockchain/transaction-building/csl-tx.js +766 -0
- package/srv/blockchain/transaction-building/csl-tx.js.map +1 -0
- package/srv/blockchain/transaction-building/tx-builder-registry.d.ts.map +1 -0
- package/srv/blockchain/transaction-building/tx-builder-registry.js +67 -0
- package/srv/blockchain/transaction-building/tx-builder-registry.js.map +1 -0
- package/srv/cardano-service.cds +179 -0
- package/srv/cardano-service.d.ts.map +1 -0
- package/srv/cardano-service.js +227 -0
- package/srv/cardano-service.js.map +1 -0
- package/srv/cardano-tx-service.cds +298 -0
- package/srv/cardano-tx-service.d.ts.map +1 -0
- package/srv/cardano-tx-service.js +646 -0
- package/srv/cardano-tx-service.js.map +1 -0
- package/srv/cardano-ui.cds +2949 -0
- package/srv/server.d.ts.map +1 -0
- package/srv/server.js +212 -0
- package/srv/server.js.map +1 -0
- package/srv/utils/backend-request-handler.d.ts.map +1 -0
- package/srv/utils/backend-request-handler.js +47 -0
- package/srv/utils/backend-request-handler.js.map +1 -0
- package/srv/utils/const.d.ts.map +1 -0
- package/srv/utils/const.js +86 -0
- package/srv/utils/const.js.map +1 -0
- package/srv/utils/error-codes.d.ts.map +1 -0
- package/srv/utils/error-codes.js +49 -0
- package/srv/utils/error-codes.js.map +1 -0
- package/srv/utils/errors.d.ts.map +1 -0
- package/srv/utils/errors.js +389 -0
- package/srv/utils/errors.js.map +1 -0
- package/srv/utils/mappers.d.ts.map +1 -0
- package/srv/utils/mappers.js +723 -0
- package/srv/utils/mappers.js.map +1 -0
- package/srv/utils/signing-helper.d.ts.map +1 -0
- package/srv/utils/signing-helper.js +128 -0
- package/srv/utils/signing-helper.js.map +1 -0
- package/srv/utils/tx-build-helper.d.ts.map +1 -0
- package/srv/utils/tx-build-helper.js +135 -0
- package/srv/utils/tx-build-helper.js.map +1 -0
- package/srv/utils/types.d.ts.map +1 -0
- package/srv/utils/types.js +36 -0
- package/srv/utils/types.js.map +1 -0
- package/srv/utils/validators.d.ts.map +1 -0
- package/srv/utils/validators.js +382 -0
- package/srv/utils/validators.js.map +1 -0
|
@@ -0,0 +1,723 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.mapTransaction = mapTransaction;
|
|
7
|
+
exports.mapTransactionInputs = mapTransactionInputs;
|
|
8
|
+
exports.mapTransactionInputAssets = mapTransactionInputAssets;
|
|
9
|
+
exports.mapTransactionOutputs = mapTransactionOutputs;
|
|
10
|
+
exports.mapTransactionOutputAssets = mapTransactionOutputAssets;
|
|
11
|
+
exports.mapAddress = mapAddress;
|
|
12
|
+
exports.mapAddressTransactions = mapAddressTransactions;
|
|
13
|
+
exports.mapAddressUtxos = mapAddressUtxos;
|
|
14
|
+
exports.mapAddressAssets = mapAddressAssets;
|
|
15
|
+
exports.mapAddressUtxoAssets = mapAddressUtxoAssets;
|
|
16
|
+
exports.mapNetworkInfo = mapNetworkInfo;
|
|
17
|
+
exports.mapBlock = mapBlock;
|
|
18
|
+
exports.mapEpoch = mapEpoch;
|
|
19
|
+
exports.mapTransactionMetadata = mapTransactionMetadata;
|
|
20
|
+
exports.mapPool = mapPool;
|
|
21
|
+
exports.mapDrep = mapDrep;
|
|
22
|
+
exports.mapAccount = mapAccount;
|
|
23
|
+
exports.mapError = mapError;
|
|
24
|
+
exports.mapBuildResult = mapBuildResult;
|
|
25
|
+
exports.mapBuildInputs = mapBuildInputs;
|
|
26
|
+
exports.mapBuildOutputs = mapBuildOutputs;
|
|
27
|
+
exports.mapProtocolParameters = mapProtocolParameters;
|
|
28
|
+
exports.mapTransactionSubmission = mapTransactionSubmission;
|
|
29
|
+
exports.mapAddressSigningRequest = mapAddressSigningRequest;
|
|
30
|
+
exports.mapAddressTransactionBuild = mapAddressTransactionBuild;
|
|
31
|
+
const errors_1 = require("./errors");
|
|
32
|
+
const cds_1 = __importDefault(require("@sap/cds"));
|
|
33
|
+
/**
|
|
34
|
+
* Maximum age for cached/indexed data in milliseconds
|
|
35
|
+
*/
|
|
36
|
+
/**
|
|
37
|
+
* Map Transaction Data
|
|
38
|
+
* Converts provider transaction data into TransactionRow format
|
|
39
|
+
* @param providerTx
|
|
40
|
+
* @returns {TransactionRow} mapped transaction row
|
|
41
|
+
*/
|
|
42
|
+
function mapTransaction(providerTx) {
|
|
43
|
+
// determine presence of optional data
|
|
44
|
+
const hasMetadata = providerTx.metadata != null && (Array.isArray(providerTx.metadata));
|
|
45
|
+
const hasInputs = Array.isArray(providerTx.inputs) && providerTx.inputs.length > 0;
|
|
46
|
+
const hasOutputs = Array.isArray(providerTx.outputs) && providerTx.outputs.length > 0;
|
|
47
|
+
return {
|
|
48
|
+
hash: providerTx.hash,
|
|
49
|
+
blockHash: providerTx.blockHash,
|
|
50
|
+
blockHeight: providerTx.blockHeight ?? null,
|
|
51
|
+
blockTime: providerTx.blockTime ?? null,
|
|
52
|
+
slot: providerTx.slot ?? null,
|
|
53
|
+
txIndex: providerTx.index ?? null,
|
|
54
|
+
fee: providerTx.fee != null ? Number(providerTx.fee) : 0,
|
|
55
|
+
deposit: providerTx.deposit != null ? Number(providerTx.deposit) : 0,
|
|
56
|
+
size: providerTx.size ?? null,
|
|
57
|
+
hasInputs: hasInputs,
|
|
58
|
+
hasOutputs: hasOutputs,
|
|
59
|
+
hasMetadata: hasMetadata,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Map Transaction Inputs
|
|
64
|
+
* Converts provider transaction input data into TransactionInputRow format
|
|
65
|
+
* @param txHash transaction hash
|
|
66
|
+
* @param txInputs transaction inputs from provider
|
|
67
|
+
* @returns {TransactionInputRow[]} mapped transaction input rows
|
|
68
|
+
*/
|
|
69
|
+
function mapTransactionInputs(txHash, txInputs) {
|
|
70
|
+
return txInputs.map((input, idx) => {
|
|
71
|
+
// Use array index as the input index (position in this transaction's inputs)
|
|
72
|
+
// Note: input.outputIndex is the output index from the ORIGINAL UTxO being spent, not for keying here
|
|
73
|
+
const inputIndex = idx;
|
|
74
|
+
// check presence of address and amount arrays
|
|
75
|
+
const hasAddress = !!input.address?.length;
|
|
76
|
+
const hasAssets = Array.isArray(input.amount) && input.amount.length > 0;
|
|
77
|
+
return {
|
|
78
|
+
tx_hash: txHash,
|
|
79
|
+
inputIndex: inputIndex,
|
|
80
|
+
address_address: input.address,
|
|
81
|
+
utxoData_dataHash: input.dataHash || null,
|
|
82
|
+
utxoData_inlineDatum: input.inlineDatum || null,
|
|
83
|
+
utxoData_referenceScriptHash: input.referenceScriptHash || null,
|
|
84
|
+
isCollateral: Boolean(input.isCollateral),
|
|
85
|
+
isReference: Boolean(input.isReference),
|
|
86
|
+
hasAddresses: hasAddress,
|
|
87
|
+
hasAssets: hasAssets,
|
|
88
|
+
};
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Map Transaction Input Assets
|
|
93
|
+
* Converts provider transaction input asset data into TransactionInputAssetRow format
|
|
94
|
+
* @param txHash transaction hash
|
|
95
|
+
* @param inputs transaction inputs from provider
|
|
96
|
+
* @returns {TransactionInputAssetRow[]} mapped transaction input asset rows
|
|
97
|
+
*/
|
|
98
|
+
function mapTransactionInputAssets(txHash, inputs) {
|
|
99
|
+
return inputs.flatMap((input, idx) => {
|
|
100
|
+
// Use array index as the input index (must match mapTransactionInputs)
|
|
101
|
+
const inputIndex = idx;
|
|
102
|
+
if (!Array.isArray(input.amount))
|
|
103
|
+
return [];
|
|
104
|
+
return input.amount.map(a => {
|
|
105
|
+
const { policyId, assetName } = parseAssetUnit(a.unit);
|
|
106
|
+
return {
|
|
107
|
+
input_tx_hash: txHash,
|
|
108
|
+
input_inputIndex: inputIndex,
|
|
109
|
+
unit: a.unit,
|
|
110
|
+
asset_quantity: Number(a.quantity),
|
|
111
|
+
asset_policyId: policyId,
|
|
112
|
+
asset_assetName: assetName,
|
|
113
|
+
};
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Map Transaction Outputs
|
|
119
|
+
* Converts provider transaction output data into TransactionOutputRow format
|
|
120
|
+
* @param txHash transaction hash
|
|
121
|
+
* @param txOutputs transaction outputs from provider
|
|
122
|
+
* @returns {TransactionOutputRow[]} mapped transaction output rows
|
|
123
|
+
*/
|
|
124
|
+
function mapTransactionOutputs(txHash, txOutputs) {
|
|
125
|
+
return txOutputs.map((output, idx) => {
|
|
126
|
+
const outputIndex = output.outputIndex ?? idx;
|
|
127
|
+
const hasAddresses = !!output.address?.length;
|
|
128
|
+
const hasAssets = Array.isArray(output.amount) && output.amount.length > 0;
|
|
129
|
+
return {
|
|
130
|
+
tx_hash: txHash,
|
|
131
|
+
outputIndex: outputIndex,
|
|
132
|
+
address_address: output.address,
|
|
133
|
+
utxo_dataHash: output.dataHash || null,
|
|
134
|
+
utxo_inlineDatum: output.inlineDatum || null,
|
|
135
|
+
utxo_referenceScriptHash: output.referenceScriptHash || null,
|
|
136
|
+
hasAddresses: hasAddresses,
|
|
137
|
+
hasAssets: hasAssets,
|
|
138
|
+
};
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Map Transaction Output Assets
|
|
143
|
+
* Converts provider transaction output asset data into TransactionOutputAssetRow format
|
|
144
|
+
* @param txHash transaction hash
|
|
145
|
+
* @param outputs transaction outputs from provider
|
|
146
|
+
* @returns {TransactionOutputAssetRow[]} mapped transaction output asset rows
|
|
147
|
+
*/
|
|
148
|
+
function mapTransactionOutputAssets(txHash, outputs) {
|
|
149
|
+
return outputs.flatMap((output, idx) => {
|
|
150
|
+
const outputIndex = output.outputIndex ?? idx;
|
|
151
|
+
if (!Array.isArray(output.amount))
|
|
152
|
+
return [];
|
|
153
|
+
return output.amount.map(a => {
|
|
154
|
+
const { policyId, assetName } = parseAssetUnit(a.unit);
|
|
155
|
+
return {
|
|
156
|
+
output_tx_hash: txHash,
|
|
157
|
+
output_outputIndex: outputIndex,
|
|
158
|
+
unit: a.unit,
|
|
159
|
+
asset_quantity: Number(a.quantity),
|
|
160
|
+
asset_policyId: policyId,
|
|
161
|
+
asset_assetName: assetName,
|
|
162
|
+
};
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Map Address Data
|
|
168
|
+
* Converts provider address data into AddressRow format
|
|
169
|
+
* @param address address string
|
|
170
|
+
* @param addressData address data from provider
|
|
171
|
+
* @returns {AddressRow} mapped address row
|
|
172
|
+
*/
|
|
173
|
+
function mapAddress(address, addressData, maxAge) {
|
|
174
|
+
const now = Date.now();
|
|
175
|
+
const nowIso = new Date(now).toISOString();
|
|
176
|
+
const validToIso = new Date(now + maxAge).toISOString();
|
|
177
|
+
const totalLovelace = Array.isArray(addressData.amount)
|
|
178
|
+
? Number(addressData.amount.find((a) => a.unit === 'lovelace')?.quantity)
|
|
179
|
+
: 0;
|
|
180
|
+
const hasUtxos = Array.isArray(addressData.utxos) && addressData.utxos.length > 0;
|
|
181
|
+
const hasAssets = Array.isArray(addressData.amount) && addressData.amount.length > 0;
|
|
182
|
+
// Transactions are loaded separately via getAddressTransactions() - set to false initially
|
|
183
|
+
// Will be updated when transactions are indexed
|
|
184
|
+
const hasTransactions = false;
|
|
185
|
+
return {
|
|
186
|
+
address,
|
|
187
|
+
stakeAddress: addressData.stakeAddress || null,
|
|
188
|
+
type: addressData.type ?? 'base',
|
|
189
|
+
isScript: addressData.isScript ?? false,
|
|
190
|
+
totalLovelace: totalLovelace,
|
|
191
|
+
validFrom: nowIso,
|
|
192
|
+
validTo: validToIso,
|
|
193
|
+
hasAssets: hasAssets,
|
|
194
|
+
hasUTxOs: hasUtxos,
|
|
195
|
+
hasTransactions: hasTransactions,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Map Address Transactions
|
|
200
|
+
* Converts provider address transaction data into AddressTransactionRow format
|
|
201
|
+
* @param addr address string
|
|
202
|
+
* @param addressTxsData address transactions data from provider
|
|
203
|
+
* @returns {AddressTransactionRow[]} mapped address transaction rows
|
|
204
|
+
* */
|
|
205
|
+
function mapAddressTransactions(addr, addressTxsData, validFrom, validTo) {
|
|
206
|
+
return addressTxsData.map((tx) => {
|
|
207
|
+
// Calculate net amounts for this address in this transaction
|
|
208
|
+
const { netLovelace, netAssets } = calculateNetAmounts(addr, tx);
|
|
209
|
+
return {
|
|
210
|
+
address_address: addr,
|
|
211
|
+
tx_hash: tx.hash,
|
|
212
|
+
netAmount: netLovelace,
|
|
213
|
+
blockTime: tx.blockTime,
|
|
214
|
+
netAssets: netAssets.length > 0 ? JSON.stringify(netAssets) : null,
|
|
215
|
+
hasAssets: netAssets.length > 0,
|
|
216
|
+
validFrom: validFrom,
|
|
217
|
+
validTo: validTo,
|
|
218
|
+
};
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Calculate net lovelace and asset changes for an address in a transaction
|
|
223
|
+
* @param addr the address to calculate for
|
|
224
|
+
* @param tx the transaction data
|
|
225
|
+
* @returns object with netLovelace and netAssets array
|
|
226
|
+
*/
|
|
227
|
+
function calculateNetAmounts(addr, tx) {
|
|
228
|
+
let inputLovelace = 0;
|
|
229
|
+
let outputLovelace = 0;
|
|
230
|
+
const assetBalances = new Map(); // unit -> net quantity
|
|
231
|
+
// Process inputs belonging to this address (subtract)
|
|
232
|
+
for (const input of tx.inputs || []) {
|
|
233
|
+
if (input.address === addr) {
|
|
234
|
+
for (const amount of input.amount || []) {
|
|
235
|
+
if (amount.unit === 'lovelace') {
|
|
236
|
+
inputLovelace += parseInt(amount.quantity, 10) || 0;
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
// Native asset
|
|
240
|
+
const current = assetBalances.get(amount.unit) || BigInt(0);
|
|
241
|
+
assetBalances.set(amount.unit, current - BigInt(amount.quantity));
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
// Process outputs going to this address (add)
|
|
247
|
+
for (const output of tx.outputs || []) {
|
|
248
|
+
if (output.address === addr) {
|
|
249
|
+
for (const amount of output.amount || []) {
|
|
250
|
+
if (amount.unit === 'lovelace') {
|
|
251
|
+
outputLovelace += parseInt(amount.quantity, 10) || 0;
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
// Native asset
|
|
255
|
+
const current = assetBalances.get(amount.unit) || BigInt(0);
|
|
256
|
+
assetBalances.set(amount.unit, current + BigInt(amount.quantity));
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
// Convert asset map to array, filtering out zero balances
|
|
262
|
+
const netAssets = [];
|
|
263
|
+
for (const [unit, quantity] of assetBalances) {
|
|
264
|
+
if (quantity !== BigInt(0)) {
|
|
265
|
+
// Parse unit into policyId and assetName
|
|
266
|
+
// Format: policyId (56 chars) + assetNameHex
|
|
267
|
+
const policyId = unit.substring(0, 56);
|
|
268
|
+
const assetNameHex = unit.substring(56);
|
|
269
|
+
const assetName = hexToUtf8(assetNameHex);
|
|
270
|
+
netAssets.push({
|
|
271
|
+
unit,
|
|
272
|
+
policyId,
|
|
273
|
+
assetName,
|
|
274
|
+
assetNameHex,
|
|
275
|
+
quantity: quantity.toString()
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return {
|
|
280
|
+
netLovelace: outputLovelace - inputLovelace,
|
|
281
|
+
netAssets
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Map Address UTxOs
|
|
286
|
+
* @param addr address string
|
|
287
|
+
* @param validFrom validFrom
|
|
288
|
+
* @param validTo validTo
|
|
289
|
+
* @param addressUtxosData address UTxOs data from provider
|
|
290
|
+
* @returns {AddressUTxORow[]} mapped address UTxO rows
|
|
291
|
+
*/
|
|
292
|
+
function mapAddressUtxos(addr, validFrom, validTo, addressUtxosData) {
|
|
293
|
+
return addressUtxosData.map((utxo) => {
|
|
294
|
+
const lovelace = Number(utxo.amount.find((a) => a.unit === 'lovelace')?.quantity ?? 0);
|
|
295
|
+
const hasAssets = Array.isArray(utxo.amount) && utxo.amount.some((a) => a.unit !== 'lovelace');
|
|
296
|
+
return {
|
|
297
|
+
address_address: addr,
|
|
298
|
+
hash: utxo.txHash,
|
|
299
|
+
index: utxo.outputIndex,
|
|
300
|
+
blockHash: utxo.blockHash,
|
|
301
|
+
utxodata_dataHash: utxo.datumHash,
|
|
302
|
+
utxodata_inlineDatum: null,
|
|
303
|
+
utxodata_referenceScriptHash: utxo.scriptRef,
|
|
304
|
+
lovelace: lovelace,
|
|
305
|
+
validFrom: validFrom,
|
|
306
|
+
validTo: validTo,
|
|
307
|
+
hasAssets: hasAssets,
|
|
308
|
+
};
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Map Address Assets
|
|
313
|
+
* Converts provider address asset data into AddressAssetRow format
|
|
314
|
+
* @param addr address string
|
|
315
|
+
* @param validFrom validFrom
|
|
316
|
+
* @param validTo validTo
|
|
317
|
+
* @param AssetAssets address assets from provider
|
|
318
|
+
* @returns {AddressAssetRow[]} mapped address asset rows
|
|
319
|
+
*/
|
|
320
|
+
function mapAddressAssets(addr, validFrom, validTo, AssetAssets) {
|
|
321
|
+
return AssetAssets
|
|
322
|
+
.filter((asset) => asset.unit !== 'lovelace')
|
|
323
|
+
.map((asset) => {
|
|
324
|
+
const { policyId, assetName } = parseAssetUnit(asset.unit);
|
|
325
|
+
return {
|
|
326
|
+
address_address: addr,
|
|
327
|
+
unit: asset.unit,
|
|
328
|
+
validFrom: validFrom,
|
|
329
|
+
validTo: validTo,
|
|
330
|
+
asset_quantity: Number(asset.quantity),
|
|
331
|
+
asset_policyId: policyId,
|
|
332
|
+
asset_assetName: assetName,
|
|
333
|
+
};
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Map UTxO Assets
|
|
338
|
+
* Converts provider address UTxO asset data into UTxOAssetRow format
|
|
339
|
+
* @param addressUtxosData address UTxOs data from provider
|
|
340
|
+
* @param validFrom validFrom
|
|
341
|
+
* @param validTo validTo
|
|
342
|
+
* @returns {UTxOAssetRow[]} mapped UTxO asset rows
|
|
343
|
+
*/
|
|
344
|
+
function mapAddressUtxoAssets(addressUtxosData, validFrom, validTo) {
|
|
345
|
+
const assets = [];
|
|
346
|
+
addressUtxosData.forEach((utxo) => {
|
|
347
|
+
for (const asset of utxo.amount) {
|
|
348
|
+
if (!asset || !asset.unit || asset.unit === 'lovelace')
|
|
349
|
+
continue;
|
|
350
|
+
const { policyId, assetName } = parseAssetUnit(asset.unit);
|
|
351
|
+
assets.push({
|
|
352
|
+
utxo_address_address: utxo.address,
|
|
353
|
+
utxo_hash: utxo.txHash,
|
|
354
|
+
utxo_index: utxo.outputIndex,
|
|
355
|
+
unit: asset.unit,
|
|
356
|
+
validFrom: validFrom,
|
|
357
|
+
validTo: validTo,
|
|
358
|
+
asset_quantity: Number(asset.quantity),
|
|
359
|
+
asset_policyId: policyId,
|
|
360
|
+
asset_assetName: assetName,
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
});
|
|
364
|
+
return assets;
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Map Network Information
|
|
368
|
+
* Converts provider network information data into NetworkInfoRow format
|
|
369
|
+
* @param providerNetworkData
|
|
370
|
+
* @returns {NetworkInfoRow} mapped network information row
|
|
371
|
+
*/
|
|
372
|
+
function mapNetworkInfo(providerNetworkData, max_age, network) {
|
|
373
|
+
const now = Date.now();
|
|
374
|
+
const nowIso = new Date(now).toISOString();
|
|
375
|
+
const validToIso = new Date(now + max_age).toISOString();
|
|
376
|
+
return {
|
|
377
|
+
network: network,
|
|
378
|
+
validFrom: nowIso,
|
|
379
|
+
validTo: validToIso,
|
|
380
|
+
maxSupply: Number(providerNetworkData.supply.max),
|
|
381
|
+
circulatingSupply: Number(providerNetworkData.supply.circulating),
|
|
382
|
+
totalSupply: Number(providerNetworkData.supply.total),
|
|
383
|
+
lockedSupply: Number(providerNetworkData.supply.locked),
|
|
384
|
+
treasurySupply: Number(providerNetworkData.supply.treasury),
|
|
385
|
+
reservesSupply: Number(providerNetworkData.supply.reserves),
|
|
386
|
+
liveStake: Number(providerNetworkData.stake.live),
|
|
387
|
+
activeStake: Number(providerNetworkData.stake.active),
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Map Block Data
|
|
392
|
+
* Converts provider block data into BlockRow format
|
|
393
|
+
* @param providerBlockData block data from provider
|
|
394
|
+
* @param epochData epoch data for the block's epoch
|
|
395
|
+
* @returns {BlockRow} mapped block row
|
|
396
|
+
*/
|
|
397
|
+
function mapBlock(providerBlockData, epochData) {
|
|
398
|
+
return {
|
|
399
|
+
time: new Date(providerBlockData.time * 1000).toISOString(),
|
|
400
|
+
height: providerBlockData.height,
|
|
401
|
+
hash: providerBlockData.hash,
|
|
402
|
+
slotLeader: String(providerBlockData.slot ?? null),
|
|
403
|
+
epochNumber: epochData.epoch,
|
|
404
|
+
epoch: epochData,
|
|
405
|
+
epochSlot: providerBlockData.epochSlot,
|
|
406
|
+
size: providerBlockData.size,
|
|
407
|
+
txCount: providerBlockData.txCount,
|
|
408
|
+
fees: Number(providerBlockData.fees),
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Map Epoch Data
|
|
413
|
+
* Converts provider epoch data into EpochRow format
|
|
414
|
+
* @param providerEpochData epoch data from provider
|
|
415
|
+
* @returns {EpochRow} mapped epoch row
|
|
416
|
+
*/
|
|
417
|
+
function mapEpoch(providerEpochData) {
|
|
418
|
+
return {
|
|
419
|
+
epoch: providerEpochData.epoch,
|
|
420
|
+
startTime: providerEpochData.start_time,
|
|
421
|
+
endTime: providerEpochData.end_time,
|
|
422
|
+
firstBlockTime: providerEpochData.first_block_time,
|
|
423
|
+
lastBlockTime: providerEpochData.last_block_time,
|
|
424
|
+
blockCount: providerEpochData.block_count,
|
|
425
|
+
txCount: providerEpochData.tx_count,
|
|
426
|
+
output: providerEpochData.output,
|
|
427
|
+
fees: Number(providerEpochData.fees),
|
|
428
|
+
activeStake: Number(providerEpochData.active_stake),
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* Map Transaction Metadata
|
|
433
|
+
* Converts provider transaction metadata labels into TransactionMetadataRow format
|
|
434
|
+
* @param providerLabels array of metadata label data from provider
|
|
435
|
+
* @returns {TransactionMetadataRow[]} mapped transaction metadata rows
|
|
436
|
+
*/
|
|
437
|
+
function mapTransactionMetadata(providerLabels) {
|
|
438
|
+
const rows = [];
|
|
439
|
+
for (const [idx, lbl] of providerLabels.entries()) {
|
|
440
|
+
rows.push({
|
|
441
|
+
id: idx,
|
|
442
|
+
tx_hash: lbl.txHash,
|
|
443
|
+
label: lbl.label.toString(),
|
|
444
|
+
payload: lbl.json !== undefined ? JSON.stringify(lbl.json) : null,
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
return rows;
|
|
448
|
+
}
|
|
449
|
+
/**
|
|
450
|
+
* Map Pool Data
|
|
451
|
+
* Converts provider pool data into PoolRow format
|
|
452
|
+
* @param providerPoolData pool data from provider
|
|
453
|
+
* @returns {PoolRow} mapped pool row
|
|
454
|
+
*/
|
|
455
|
+
function mapPool(providerPoolData) {
|
|
456
|
+
return {
|
|
457
|
+
poolId: providerPoolData.poolId,
|
|
458
|
+
vrfKeyHash: providerPoolData.vrfKeyHash,
|
|
459
|
+
blocksMinted: providerPoolData.blocksMinted,
|
|
460
|
+
blocksEpoch: providerPoolData.blocksEpoch,
|
|
461
|
+
liveStake: Number(providerPoolData.liveStake),
|
|
462
|
+
liveSize: providerPoolData.liveSize,
|
|
463
|
+
liveDelegators: providerPoolData.liveDelegators,
|
|
464
|
+
liveSaturation: providerPoolData.liveSaturation,
|
|
465
|
+
activeStake: Number(providerPoolData.activeStake),
|
|
466
|
+
activeSize: providerPoolData.activeSize,
|
|
467
|
+
pledge: Number(providerPoolData.pledge),
|
|
468
|
+
margin: Number(providerPoolData.margin),
|
|
469
|
+
fixedCost: Number(providerPoolData.fixedCost),
|
|
470
|
+
rewardAccount: providerPoolData.rewardAccount,
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Map Drep Data
|
|
475
|
+
* Converts provider drep data into DrepRow format
|
|
476
|
+
* @param providerDrepData drep data from provider
|
|
477
|
+
* @returns {DrepRow} mapped drep row
|
|
478
|
+
*/
|
|
479
|
+
function mapDrep(providerDrepData) {
|
|
480
|
+
return {
|
|
481
|
+
drepId: providerDrepData.drepId,
|
|
482
|
+
hex: providerDrepData.hex,
|
|
483
|
+
amount: Number(providerDrepData.amount),
|
|
484
|
+
hasScript: Boolean(providerDrepData.hasScript),
|
|
485
|
+
lastActiveEpoch: providerDrepData.lastActiveEpoch,
|
|
486
|
+
retired: Boolean(providerDrepData.retired),
|
|
487
|
+
expired: Boolean(providerDrepData.expired),
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* Map Account Data
|
|
492
|
+
* Converts provider account data into AccountRow format
|
|
493
|
+
* @param providerAccountData account data from provider
|
|
494
|
+
* @returns {AccountRow} mapped account row
|
|
495
|
+
*/
|
|
496
|
+
function mapAccount(providerAccountData, max_age) {
|
|
497
|
+
const validFrom = new Date().toISOString();
|
|
498
|
+
const validTo = new Date(Date.now() + max_age).toISOString();
|
|
499
|
+
return {
|
|
500
|
+
validFrom: validFrom,
|
|
501
|
+
validTo: validTo,
|
|
502
|
+
stakeAddress: providerAccountData.stakeaddress,
|
|
503
|
+
active: providerAccountData.active,
|
|
504
|
+
activeEpoch: providerAccountData.activeEpoch,
|
|
505
|
+
controlledAmount: Number(providerAccountData.controlledAmount),
|
|
506
|
+
rewardsSum: Number(providerAccountData.rewardsSum),
|
|
507
|
+
withdrawalsSum: Number(providerAccountData.withdrawalsSum),
|
|
508
|
+
reservesSum: Number(providerAccountData.reservesSum),
|
|
509
|
+
treasurySum: Number(providerAccountData.treasurySum),
|
|
510
|
+
withdrawableAmount: Number(providerAccountData.withdrawableAmount),
|
|
511
|
+
hasAddresses: providerAccountData.addresses.length > 0,
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* Map Backend Error
|
|
516
|
+
* Converts BackendError or unknown error into OData request rejection
|
|
517
|
+
* @param req OData request
|
|
518
|
+
* @param err error object (BackendError or unknown)
|
|
519
|
+
* @param ctx context string for error message
|
|
520
|
+
*/
|
|
521
|
+
function mapError(req, err, ctx) {
|
|
522
|
+
if (err instanceof errors_1.BackendError) {
|
|
523
|
+
return req.reject(err.statusCode, fmt(err.code, ctx, err.message), err.target);
|
|
524
|
+
}
|
|
525
|
+
// Handle non-BackendError (plain Error, string, etc.)
|
|
526
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
527
|
+
return req.reject(500, fmt('INTERNAL_ERROR', ctx, message));
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
530
|
+
* Map Transaction Build Result
|
|
531
|
+
* Converts provider transaction build result into TransactionBuildRow format
|
|
532
|
+
* @param txbuildResult transaction build result from provider
|
|
533
|
+
* @returns {TransactionBuildRow} mapped transaction build row
|
|
534
|
+
*/
|
|
535
|
+
function mapBuildResult(txbuildResult, max_age) {
|
|
536
|
+
const buildId = cds_1.default.utils.uuid();
|
|
537
|
+
const now = Math.floor(Date.now() / 1000);
|
|
538
|
+
const validFrom = new Date().toISOString();
|
|
539
|
+
const validTo = new Date(Date.now() + max_age).toISOString();
|
|
540
|
+
const hasInputs = Array.isArray(txbuildResult.inputs) && txbuildResult.inputs.length > 0;
|
|
541
|
+
const hasOutputs = Array.isArray(txbuildResult.outputs) && txbuildResult.outputs.length > 0;
|
|
542
|
+
return {
|
|
543
|
+
id: buildId,
|
|
544
|
+
validFrom: validFrom,
|
|
545
|
+
validTo: validTo,
|
|
546
|
+
builderEngine: txbuildResult.builderEngine,
|
|
547
|
+
network: txbuildResult.network,
|
|
548
|
+
senderAddress: txbuildResult.senderAddress,
|
|
549
|
+
changeAddress: txbuildResult.senderAddress,
|
|
550
|
+
unsignedTxCbor: txbuildResult.unsignedTxCbor,
|
|
551
|
+
txBodyHash: txbuildResult.txBodyHash,
|
|
552
|
+
fee: Number(txbuildResult.feeLovelace),
|
|
553
|
+
size: txbuildResult.sizeBytes, // size in bytes
|
|
554
|
+
createdAt: now, // epoch seconds
|
|
555
|
+
submission: null,
|
|
556
|
+
hasInputs: hasInputs, // indicates if build has inputs
|
|
557
|
+
hasOutputs: hasOutputs, // indicates if build has outputs
|
|
558
|
+
wasSubmitted: false, // indicates if this build was submitted
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* Map Transaction Build Inputs
|
|
563
|
+
* Converts transaction build result inputs into TransactionBuildInputRow format
|
|
564
|
+
* @param buildId the transaction build ID
|
|
565
|
+
* @param inputs transaction build result inputs
|
|
566
|
+
* @returns {TransactionBuildInputRow[]} mapped transaction build input rows
|
|
567
|
+
*/
|
|
568
|
+
function mapBuildInputs(buildId, inputs) {
|
|
569
|
+
return inputs.map((input, idx) => ({
|
|
570
|
+
build_id: buildId,
|
|
571
|
+
inputIndex: idx,
|
|
572
|
+
txHash: input.txHash,
|
|
573
|
+
outputIndex: input.index,
|
|
574
|
+
address: input.address || null,
|
|
575
|
+
lovelace: Number(input.lovelace),
|
|
576
|
+
hasAssets: false, // simple ADA transfers don't have assets
|
|
577
|
+
}));
|
|
578
|
+
}
|
|
579
|
+
/**
|
|
580
|
+
* Map Transaction Build Outputs
|
|
581
|
+
* Converts transaction build result outputs into TransactionBuildOutputRow format
|
|
582
|
+
* @param buildId the transaction build ID
|
|
583
|
+
* @param outputs transaction build result outputs
|
|
584
|
+
* @param changeAddress the change address to identify change outputs
|
|
585
|
+
* @returns {TransactionBuildOutputRow[]} mapped transaction build output rows
|
|
586
|
+
*/
|
|
587
|
+
function mapBuildOutputs(buildId, outputs, changeAddress) {
|
|
588
|
+
return outputs.map((output, idx) => ({
|
|
589
|
+
build_id: buildId,
|
|
590
|
+
outputIndex: idx,
|
|
591
|
+
address: output.address,
|
|
592
|
+
lovelace: Number(output.lovelace),
|
|
593
|
+
isChange: changeAddress ? output.address === changeAddress : false,
|
|
594
|
+
hasAssets: false, // simple ADA transfers don't have assets
|
|
595
|
+
}));
|
|
596
|
+
}
|
|
597
|
+
/**
|
|
598
|
+
* Map Protocol Parameters
|
|
599
|
+
* Converts provider protocol parameters into ProtocolParameterRow format
|
|
600
|
+
* @param providerParams protocol parameters from provider
|
|
601
|
+
* @returns {ProtocolParameterRow} mapped protocol parameter row
|
|
602
|
+
*/
|
|
603
|
+
function mapProtocolParameters(providerParams) {
|
|
604
|
+
return {
|
|
605
|
+
network: providerParams.network,
|
|
606
|
+
epoch: providerParams.epoch,
|
|
607
|
+
minFeeA: providerParams.minFeeA,
|
|
608
|
+
minFeeB: providerParams.minFeeB,
|
|
609
|
+
maxBlockSize: providerParams.maxBlockSize,
|
|
610
|
+
maxTxSize: providerParams.maxTxSize,
|
|
611
|
+
maxBlockHeaderSize: providerParams.maxBlockHeaderSize,
|
|
612
|
+
keyDeposit: providerParams.keyDeposit,
|
|
613
|
+
poolDeposit: providerParams.poolDeposit,
|
|
614
|
+
eMax: providerParams.eMax,
|
|
615
|
+
nOpt: providerParams.nOpt,
|
|
616
|
+
a0: providerParams.a0,
|
|
617
|
+
rho: providerParams.rho,
|
|
618
|
+
tau: providerParams.tau,
|
|
619
|
+
minPoolCost: providerParams.minPoolCost,
|
|
620
|
+
decentralisationParam: providerParams.decentralisationParam,
|
|
621
|
+
extraEntropy: providerParams.extraEntropy,
|
|
622
|
+
protocolMajorVer: providerParams.protocolMajorVer,
|
|
623
|
+
protocolMinorVer: providerParams.protocolMinorVer,
|
|
624
|
+
minUtxo: providerParams.minUtxo,
|
|
625
|
+
nonce: providerParams.nonce,
|
|
626
|
+
costModels: providerParams.costModels,
|
|
627
|
+
priceMem: providerParams.priceMem,
|
|
628
|
+
priceStep: providerParams.priceStep,
|
|
629
|
+
maxTxExMem: providerParams.maxTxExMem,
|
|
630
|
+
maxTxExSteps: providerParams.maxTxExSteps,
|
|
631
|
+
maxBlockExMem: providerParams.maxBlockExMem,
|
|
632
|
+
maxBlockExSteps: providerParams.maxBlockExSteps,
|
|
633
|
+
maxValSize: providerParams.maxValSize,
|
|
634
|
+
collateralPercent: providerParams.collateralPercent,
|
|
635
|
+
maxCollateralInputs: providerParams.maxCollateralInputs,
|
|
636
|
+
coinsPerUtxoSize: providerParams.coinsPerUtxoSize,
|
|
637
|
+
fetchedAt: providerParams.fetchedAt,
|
|
638
|
+
source: providerParams.source,
|
|
639
|
+
};
|
|
640
|
+
}
|
|
641
|
+
/**
|
|
642
|
+
* Map Transaction Submission
|
|
643
|
+
* Converts signed transaction CBOR and hash into TransactionSubmissionRow format
|
|
644
|
+
* @param signedTxCbor signed transaction in CBOR hex format
|
|
645
|
+
* @param txHash transaction hash
|
|
646
|
+
* @returns {TransactionSubmissionRow} mapped transaction submission row
|
|
647
|
+
*/
|
|
648
|
+
function mapTransactionSubmission(signedTxCbor, txHash) {
|
|
649
|
+
const now = Math.floor(Date.now() / 1000);
|
|
650
|
+
return {
|
|
651
|
+
signedTxCbor: signedTxCbor,
|
|
652
|
+
txHash: txHash,
|
|
653
|
+
submittedAt: now,
|
|
654
|
+
};
|
|
655
|
+
}
|
|
656
|
+
/**
|
|
657
|
+
* Map Address Signing Requests
|
|
658
|
+
* Creates AddressSigningRequest row for address-signing request association
|
|
659
|
+
* @param addr bech32 address
|
|
660
|
+
* @param signingRequestId signing request UUID
|
|
661
|
+
* @returns {AddressSigningRequestRow} mapped address signing request row
|
|
662
|
+
*/
|
|
663
|
+
function mapAddressSigningRequest(addr, signingRequestId) {
|
|
664
|
+
return {
|
|
665
|
+
address_address: addr,
|
|
666
|
+
signingRequest_id: signingRequestId,
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
/**
|
|
670
|
+
* Map Address Transaction Builds
|
|
671
|
+
* Creates AddressTransactionBuild row for address-build association
|
|
672
|
+
* @param addr bech32 address
|
|
673
|
+
* @param buildId transaction build UUID
|
|
674
|
+
* @returns {AddressTransactionBuildRow} mapped address transaction build row
|
|
675
|
+
*/
|
|
676
|
+
function mapAddressTransactionBuild(addr, buildId) {
|
|
677
|
+
return {
|
|
678
|
+
address_address: addr,
|
|
679
|
+
txBuild_id: buildId,
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
//-----------------------------------------------------------------------
|
|
683
|
+
// Helper Functions
|
|
684
|
+
//-----------------------------------------------------------------------
|
|
685
|
+
/**
|
|
686
|
+
* Convert hex string to UTF-8 string, falling back to hex if conversion fails.
|
|
687
|
+
* This helper reduces code duplication and improves performance by centralizing
|
|
688
|
+
* the conversion logic.
|
|
689
|
+
*
|
|
690
|
+
* @param hex - Hexadecimal string to convert
|
|
691
|
+
* @returns {string} UTF-8 string or original hex if conversion fails
|
|
692
|
+
*/
|
|
693
|
+
function hexToUtf8(hex) {
|
|
694
|
+
if (!hex)
|
|
695
|
+
return hex;
|
|
696
|
+
return Buffer.from(hex, 'hex').toString('utf8');
|
|
697
|
+
}
|
|
698
|
+
/**
|
|
699
|
+
* Parse asset unit (policyId + assetNameHex) into components.
|
|
700
|
+
* Optimizes repeated parsing logic across multiple mapper functions.
|
|
701
|
+
* @param unit - Asset unit string (56 char policyId + asset name hex)
|
|
702
|
+
* @returns { policyId: string | null; assetName: string | null } Object with policyId and assetName
|
|
703
|
+
*/
|
|
704
|
+
function parseAssetUnit(unit) {
|
|
705
|
+
if (unit === 'lovelace') {
|
|
706
|
+
return { policyId: null, assetName: 'lovelace' };
|
|
707
|
+
}
|
|
708
|
+
const policyId = unit.slice(0, 56);
|
|
709
|
+
const assetNameHex = unit.slice(56);
|
|
710
|
+
const assetName = hexToUtf8(assetNameHex);
|
|
711
|
+
return { policyId, assetName };
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* Format error message
|
|
715
|
+
* @param code error code
|
|
716
|
+
* @param ctx context string
|
|
717
|
+
* @param msg error message
|
|
718
|
+
* @returns {string} formatted error message
|
|
719
|
+
*/
|
|
720
|
+
function fmt(code, ctx, msg) {
|
|
721
|
+
return `[${code}] ${ctx}: ${msg}`;
|
|
722
|
+
}
|
|
723
|
+
//# sourceMappingURL=mappers.js.map
|