@odatano/core 2.0.0-rc.34 → 2.0.0-rc.36
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/package.json
CHANGED
|
@@ -162,6 +162,7 @@ export declare class BuildooorTxBuilder implements CardanoTxBuilder {
|
|
|
162
162
|
* (Buildooor sets no collateralReturn for ADA-only collateral). Throws without an ADA-only UTxO.
|
|
163
163
|
* When the rest cannot pay `requiredLovelace` plus fee and change, the collateral UTxO is
|
|
164
164
|
* funding too. The ledger allows one UTxO as input and as collateral.
|
|
165
|
+
* When no UTxO in `utxos` covers the floor, a forced input of the sender that does is the collateral.
|
|
165
166
|
*/
|
|
166
167
|
private _setupCollateral;
|
|
167
168
|
/** Build a Ledger Value from lovelace + optional multi-asset array */
|
|
@@ -250,7 +250,7 @@ class BuildooorTxBuilder {
|
|
|
250
250
|
}
|
|
251
251
|
}
|
|
252
252
|
// Partition forced vs candidate UTxOs. Forced inputs are already committed;
|
|
253
|
-
//
|
|
253
|
+
// coin selection operates on the remainder only.
|
|
254
254
|
const { forced, rest } = this._partitionForcedInputs(ctx.utxos, req.forceInputs, this._referencedKeys(ctx, req.referenceInputs), req.protectInputs);
|
|
255
255
|
const forcedInputs = forced.map(u => ({ utxo: this._mapMultiAssetUtxoToLedgerUtxo(u) }));
|
|
256
256
|
// Funding must cover the lovelace plus, for extra outputs, their lovelace and any asset
|
|
@@ -260,9 +260,9 @@ class BuildooorTxBuilder {
|
|
|
260
260
|
const { lovelace, assets } = this._extraOutputsFundingAfterMint(req.mintActions, req.extraOutputs);
|
|
261
261
|
requiredFundingValue = cardano_ledger_ts_1.Value.add(requiredFundingValue, this._buildLedgerValue(lovelace, assets));
|
|
262
262
|
}
|
|
263
|
-
// Collateral + funding separation
|
|
264
|
-
const { collateralUtxos, fundingUtxos, collateralReturn } = this._setupCollateral(rest, requiredFundingValue.lovelaces);
|
|
265
|
-
coinSelectionContext = this._collateralPartitionContext(collateralUtxos, fundingUtxos);
|
|
263
|
+
// Collateral + funding separation from the candidates; a forced input is collateral only as a fallback
|
|
264
|
+
const { collateralUtxos, fundingUtxos, collateralReturn } = this._setupCollateral(rest, requiredFundingValue.lovelaces, forced, req.senderAddress);
|
|
265
|
+
coinSelectionContext = this._collateralPartitionContext(collateralUtxos, fundingUtxos, forced);
|
|
266
266
|
const fundingLedgerUtxos = fundingUtxos.map(utxo => this._mapMultiAssetUtxoToLedgerUtxo(utxo));
|
|
267
267
|
const allFundingInputs = fundingLedgerUtxos.map(utxo => ({ utxo }));
|
|
268
268
|
const selectedFunding = this.txBuilder.keepRelevant(requiredFundingValue, allFundingInputs);
|
|
@@ -356,11 +356,11 @@ class BuildooorTxBuilder {
|
|
|
356
356
|
requiredFundingValue = cardano_ledger_ts_1.Value.add(requiredFundingValue, this._buildLedgerValue(BigInt(extra.lovelaceAmount), extra.assets));
|
|
357
357
|
}
|
|
358
358
|
}
|
|
359
|
-
// Collateral + funding separation
|
|
359
|
+
// Collateral + funding separation from the candidates; a forced input is collateral only as a fallback.
|
|
360
360
|
// The script UTxO pays its part of the outputs, so the collateral check counts only the rest.
|
|
361
361
|
const fundingGap = requiredFundingValue.lovelaces - (0, tx_build_helper_1.getLovelace)(scriptOdatanoUtxo);
|
|
362
|
-
const { collateralUtxos, fundingUtxos, collateralReturn } = this._setupCollateral(rest, fundingGap > 0n ? fundingGap : 0n);
|
|
363
|
-
coinSelectionContext = this._collateralPartitionContext(collateralUtxos, fundingUtxos);
|
|
362
|
+
const { collateralUtxos, fundingUtxos, collateralReturn } = this._setupCollateral(rest, fundingGap > 0n ? fundingGap : 0n, forced, req.senderAddress);
|
|
363
|
+
coinSelectionContext = this._collateralPartitionContext(collateralUtxos, fundingUtxos, forced);
|
|
364
364
|
const fundingLedgerUtxos = fundingUtxos.map(utxo => this._mapMultiAssetUtxoToLedgerUtxo(utxo));
|
|
365
365
|
const allFundingInputs = fundingLedgerUtxos.map(utxo => ({ utxo }));
|
|
366
366
|
const selectedFundingInputs = this.txBuilder.keepRelevant(requiredFundingValue, allFundingInputs);
|
|
@@ -590,9 +590,9 @@ class BuildooorTxBuilder {
|
|
|
590
590
|
const runsScript = scriptEntries.length > 0 || mintEntries.length > 0 ||
|
|
591
591
|
[...withdrawalEntries, ...certEntries].some(e => e.script || e.refLedgerUtxo);
|
|
592
592
|
const { collateralUtxos, fundingUtxos, collateralReturn } = runsScript
|
|
593
|
-
? this._setupCollateral(rest, requiredFundingValue.lovelaces)
|
|
593
|
+
? this._setupCollateral(rest, requiredFundingValue.lovelaces, forced, req.senderAddress)
|
|
594
594
|
: { collateralUtxos: [], fundingUtxos: rest, collateralReturn: undefined };
|
|
595
|
-
coinSelectionContext = this._collateralPartitionContext(collateralUtxos, fundingUtxos);
|
|
595
|
+
coinSelectionContext = this._collateralPartitionContext(collateralUtxos, fundingUtxos, forced);
|
|
596
596
|
const allFundingInputs = fundingUtxos.map(utxo => ({ utxo: this._mapMultiAssetUtxoToLedgerUtxo(utxo) }));
|
|
597
597
|
const selectedFundingInputs = this.txBuilder.keepRelevant(requiredFundingValue, allFundingInputs);
|
|
598
598
|
// Reference inputs: the caller's, minus the reference-script UTxOs Buildooor adds itself
|
|
@@ -1221,16 +1221,18 @@ class BuildooorTxBuilder {
|
|
|
1221
1221
|
* Context for insufficient-funds rejections after the collateral partition: the builder's
|
|
1222
1222
|
* message counts only the funding pool, so the reservation would otherwise be invisible.
|
|
1223
1223
|
*/
|
|
1224
|
-
_collateralPartitionContext(collateralUtxos, fundingUtxos) {
|
|
1224
|
+
_collateralPartitionContext(collateralUtxos, fundingUtxos, forced = []) {
|
|
1225
1225
|
const fundingLovelace = fundingUtxos.reduce((s, u) => s + (0, tx_build_helper_1.getLovelace)(u), 0n);
|
|
1226
1226
|
if (collateralUtxos.length === 0) {
|
|
1227
1227
|
return `no script runs, so no collateral; ${fundingUtxos.length} UTxO(s) with ${fundingLovelace} lovelace for coin selection`;
|
|
1228
1228
|
}
|
|
1229
1229
|
const collateralLovelace = collateralUtxos.reduce((s, u) => s + u.resolved.value.lovelaces, 0n);
|
|
1230
1230
|
const collateralKeys = new Set(collateralUtxos.map(u => `${u.utxoRef.id.toString()}#${u.utxoRef.index}`));
|
|
1231
|
-
const
|
|
1231
|
+
const isCollateral = (u) => collateralKeys.has(`${u.txHash}#${u.outputIndex}`);
|
|
1232
|
+
const alsoFunding = fundingUtxos.some(isCollateral);
|
|
1233
|
+
const alsoForced = forced.some(isCollateral);
|
|
1232
1234
|
return `${collateralUtxos.length} UTxO(s) with ${collateralLovelace} lovelace reserved as collateral` +
|
|
1233
|
-
`${alsoFunding ? ' and also offered as funding' : ''}; ` +
|
|
1235
|
+
`${alsoFunding ? ' and also offered as funding' : alsoForced ? ' and also spent as a forced input' : ''}; ` +
|
|
1234
1236
|
`${fundingUtxos.length} UTxO(s) with ${fundingLovelace} lovelace for coin selection`;
|
|
1235
1237
|
}
|
|
1236
1238
|
/**
|
|
@@ -1239,19 +1241,25 @@ class BuildooorTxBuilder {
|
|
|
1239
1241
|
* (Buildooor sets no collateralReturn for ADA-only collateral). Throws without an ADA-only UTxO.
|
|
1240
1242
|
* When the rest cannot pay `requiredLovelace` plus fee and change, the collateral UTxO is
|
|
1241
1243
|
* funding too. The ledger allows one UTxO as input and as collateral.
|
|
1244
|
+
* When no UTxO in `utxos` covers the floor, a forced input of the sender that does is the collateral.
|
|
1242
1245
|
*/
|
|
1243
|
-
_setupCollateral(utxos, requiredLovelace) {
|
|
1246
|
+
_setupCollateral(utxos, requiredLovelace, forced = [], senderAddress = '') {
|
|
1247
|
+
const byLovelace = (a, b) => {
|
|
1248
|
+
const diff = (0, tx_build_helper_1.getLovelace)(a) - (0, tx_build_helper_1.getLovelace)(b);
|
|
1249
|
+
return diff < 0n ? -1 : diff > 0n ? 1 : 0;
|
|
1250
|
+
};
|
|
1251
|
+
const coversFloor = (u) => (0, tx_build_helper_1.getLovelace)(u) >= const_1.COLLATERAL_LOVELACE;
|
|
1244
1252
|
// ADA-only and without a reference script (a deployed script is not collateral); SetCollateral uses the same rule
|
|
1245
|
-
const
|
|
1246
|
-
|
|
1253
|
+
const sorted = utxos.filter(tx_build_helper_1.isCollateralCandidate).sort(byLovelace);
|
|
1254
|
+
const sender = senderAddress.toLowerCase();
|
|
1255
|
+
const forcedChoice = sorted.some(coversFloor)
|
|
1256
|
+
? undefined
|
|
1257
|
+
: forced.filter(u => u.address.toLowerCase() === sender && (0, tx_build_helper_1.isCollateralCandidate)(u) && coversFloor(u)).sort(byLovelace)[0];
|
|
1258
|
+
if (sorted.length === 0 && !forcedChoice) {
|
|
1247
1259
|
throw new errors_1.TransactionValidationError('No ADA-only UTxO available for collateral. Plutus scripts require ADA-only collateral; create one with SetCollateral.');
|
|
1248
1260
|
}
|
|
1249
|
-
const sorted = [...adaOnly].sort((a, b) => {
|
|
1250
|
-
const diff = (0, tx_build_helper_1.getLovelace)(a) - (0, tx_build_helper_1.getLovelace)(b);
|
|
1251
|
-
return diff < 0n ? -1 : diff > 0n ? 1 : 0;
|
|
1252
|
-
});
|
|
1253
1261
|
// smallest sufficient; if none reaches the floor, the largest available is the best bet
|
|
1254
|
-
const chosen = sorted.find(
|
|
1262
|
+
const chosen = forcedChoice ?? sorted.find(coversFloor) ?? sorted[sorted.length - 1];
|
|
1255
1263
|
if ((0, tx_build_helper_1.getLovelace)(chosen) < const_1.COLLATERAL_LOVELACE) {
|
|
1256
1264
|
logger.warn(`Largest ADA-only UTxO (${(0, tx_build_helper_1.getLovelace)(chosen)} lovelace) is below the ${const_1.COLLATERAL_LOVELACE} lovelace ` +
|
|
1257
1265
|
`collateral floor — the node will reject the transaction if collateralPercentage × fee exceeds it`);
|
|
@@ -1259,7 +1267,8 @@ class BuildooorTxBuilder {
|
|
|
1259
1267
|
const collateralUtxos = [this._mapOdatanoUtxoToLedgerUtxo(chosen)];
|
|
1260
1268
|
const rest = utxos.filter(u => !(u.txHash === chosen.txHash && u.outputIndex === chosen.outputIndex));
|
|
1261
1269
|
const restLovelace = rest.reduce((sum, u) => sum + (0, tx_build_helper_1.getLovelace)(u), 0n);
|
|
1262
|
-
|
|
1270
|
+
// a forced input is already spent, so it is never offered to coin selection as well
|
|
1271
|
+
const fundingUtxos = forcedChoice || restLovelace >= requiredLovelace + const_1.FEE_BUFFER_LOVELACE + BigInt(const_1.MIN_CHANGE_LOVELACE)
|
|
1263
1272
|
? rest
|
|
1264
1273
|
: [...rest, chosen];
|
|
1265
1274
|
// Cap the at-risk amount at the floor: return the excess to the owner.
|
package/srv/cardano-service.js
CHANGED
|
@@ -92,6 +92,91 @@ function indexOnMissAction(actionName, entity, reqKeyField, validate, indexFn, o
|
|
|
92
92
|
});
|
|
93
93
|
};
|
|
94
94
|
}
|
|
95
|
+
/** Addresses one collection read may index on a miss; more is a 400. */
|
|
96
|
+
const MAX_ADDRESSES_PER_READ = 10;
|
|
97
|
+
const isRef = (t) => typeof t === 'object' && 'ref' in t;
|
|
98
|
+
const isVal = (t) => typeof t === 'object' && 'val' in t;
|
|
99
|
+
const isList = (t) => typeof t === 'object' && 'list' in t;
|
|
100
|
+
const isXpr = (t) => typeof t === 'object' && 'xpr' in t;
|
|
101
|
+
/** `address_address` or the association path `address/address`. */
|
|
102
|
+
function isAddressRef(t) {
|
|
103
|
+
if (!isRef(t))
|
|
104
|
+
return false;
|
|
105
|
+
const path = t.ref;
|
|
106
|
+
return (path.length === 1 && path[0] === 'address_address') || (path.length === 2 && path[0] === 'address' && path[1] === 'address');
|
|
107
|
+
}
|
|
108
|
+
const stringVal = (t) => t !== undefined && isVal(t) && typeof t.val === 'string' ? t.val : undefined;
|
|
109
|
+
/**
|
|
110
|
+
* Addresses a read of an address child set names: the parent of a navigation
|
|
111
|
+
* (`Addresses('…')/utxos`) or the `$filter` terms `address_address eq '…'` (either operand
|
|
112
|
+
* order) and `address_address in ('…', …)`.
|
|
113
|
+
*/
|
|
114
|
+
function addressesInReadQuery(req) {
|
|
115
|
+
const found = new Set();
|
|
116
|
+
const parent = req.data?.address_address;
|
|
117
|
+
if (typeof parent === 'string')
|
|
118
|
+
found.add(parent);
|
|
119
|
+
const walk = (tokens) => {
|
|
120
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
121
|
+
const token = tokens[i];
|
|
122
|
+
if (typeof token === 'string')
|
|
123
|
+
continue;
|
|
124
|
+
if (isXpr(token)) {
|
|
125
|
+
walk(token.xpr);
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
const op = tokens[i + 1];
|
|
129
|
+
const right = tokens[i + 2];
|
|
130
|
+
if (isAddressRef(token) && op === '=') {
|
|
131
|
+
const value = stringVal(right);
|
|
132
|
+
if (value)
|
|
133
|
+
found.add(value);
|
|
134
|
+
}
|
|
135
|
+
else if (isAddressRef(token) && op === 'in' && right !== undefined && isList(right)) {
|
|
136
|
+
for (const item of right.list) {
|
|
137
|
+
const value = stringVal(item);
|
|
138
|
+
if (value)
|
|
139
|
+
found.add(value);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
else if (right !== undefined && isAddressRef(right) && op === '=') {
|
|
143
|
+
const value = stringVal(token);
|
|
144
|
+
if (value)
|
|
145
|
+
found.add(value);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
const query = req.query;
|
|
150
|
+
walk(('SELECT' in query && query.SELECT?.where) || []);
|
|
151
|
+
return Array.from(found);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Factory: READ handler for a child set of Addresses with index-on-miss. The address is taken
|
|
155
|
+
* from the navigation parent or the `$filter`; without one the read passes through.
|
|
156
|
+
* `isIndexed` says whether the address needs no fetch, `indexFn` fetches it.
|
|
157
|
+
*/
|
|
158
|
+
function addressChildRead(isIndexed, indexFn) {
|
|
159
|
+
return async (req) => {
|
|
160
|
+
const addresses = addressesInReadQuery(req);
|
|
161
|
+
if (addresses.length > MAX_ADDRESSES_PER_READ) {
|
|
162
|
+
(0, errors_1.rejectInvalid)(req, 'address_address', `A read may name at most ${MAX_ADDRESSES_PER_READ} addresses`, 'address_address');
|
|
163
|
+
}
|
|
164
|
+
for (const address of addresses) {
|
|
165
|
+
if (!(0, validators_1.isValidBech32Address)(address))
|
|
166
|
+
(0, errors_1.rejectInvalid)(req, 'address_address', 'Invalid bech32 address format', 'address_address');
|
|
167
|
+
}
|
|
168
|
+
return (0, backend_request_handler_1.handleRequest)(req, async (db) => {
|
|
169
|
+
if (addresses.length) {
|
|
170
|
+
widenTemporalWindow(req); // before the first DB statement
|
|
171
|
+
for (const address of addresses) {
|
|
172
|
+
if (!(await isIndexed(db, address)))
|
|
173
|
+
await indexFn(db, address);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return db.run(req.query);
|
|
177
|
+
});
|
|
178
|
+
};
|
|
179
|
+
}
|
|
95
180
|
// ---------------------------------------------------------------------------
|
|
96
181
|
// Service Registration
|
|
97
182
|
// ---------------------------------------------------------------------------
|
|
@@ -208,7 +293,28 @@ module.exports = (srv) => {
|
|
|
208
293
|
return latestSlices(assets, (asset) => asset.unit ?? '');
|
|
209
294
|
});
|
|
210
295
|
});
|
|
211
|
-
//
|
|
296
|
+
// Prefer the full address index; when no configured backend supports getAddress
|
|
297
|
+
// (AllBackendsFailedError with zero collected errors, e.g. Ogmios only), fall back to a
|
|
298
|
+
// UTxO-only index. getAddress is indexAddress's first call, so nothing is persisted before.
|
|
299
|
+
const indexAddressOrUtxos = async (db, address) => {
|
|
300
|
+
try {
|
|
301
|
+
await indexer().indexAddress(db, address);
|
|
302
|
+
}
|
|
303
|
+
catch (err) {
|
|
304
|
+
if (err instanceof errors_1.AllBackendsFailedError && err.errors.length === 0) {
|
|
305
|
+
await indexer().indexAddressUtxos(db, address);
|
|
306
|
+
}
|
|
307
|
+
else {
|
|
308
|
+
throw err;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
// An address is indexed when its Addresses row is valid. The UTxO-only fallback writes no
|
|
313
|
+
// Addresses row, so for UTxOs a valid AddressUTxOs row counts as well.
|
|
314
|
+
const addressIndexed = async (db, address) => !!(await db.run(SELECT.one.from(Addresses).where({ address })));
|
|
315
|
+
const addressUtxosIndexed = async (db, address) => (await addressIndexed(db, address))
|
|
316
|
+
|| !!(await db.run(SELECT.one.from(AddressUTxOs).where({ address_address: address })));
|
|
317
|
+
// GetUTxOsByAddress — indexes the address if needed, then queries child UTxOs.
|
|
212
318
|
srv.on('GetUTxOsByAddress', async (req) => {
|
|
213
319
|
const { address } = req.data;
|
|
214
320
|
if (!address)
|
|
@@ -217,28 +323,16 @@ module.exports = (srv) => {
|
|
|
217
323
|
(0, errors_1.rejectInvalid)(req, 'GetUTxOsByAddress', 'Invalid bech32 address format', 'address');
|
|
218
324
|
return (0, backend_request_handler_1.handleRequest)(req, async (db) => {
|
|
219
325
|
widenTemporalWindow(req); // before the first DB statement
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
// UTxO-only index. getAddress is indexAddress's first call, so nothing is persisted before.
|
|
225
|
-
try {
|
|
226
|
-
await indexer().indexAddress(db, address);
|
|
227
|
-
}
|
|
228
|
-
catch (err) {
|
|
229
|
-
if (err instanceof errors_1.AllBackendsFailedError && err.errors.length === 0) {
|
|
230
|
-
await indexer().indexAddressUtxos(db, address);
|
|
231
|
-
}
|
|
232
|
-
else {
|
|
233
|
-
throw err;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
const fresh = await db.run(SELECT.from(AddressUTxOs).where({ address_address: address }));
|
|
237
|
-
return fresh;
|
|
238
|
-
}
|
|
239
|
-
return latestSlices(existing, (utxo) => `${utxo.hash}#${utxo.index}`);
|
|
326
|
+
if (!(await addressUtxosIndexed(db, address)))
|
|
327
|
+
await indexAddressOrUtxos(db, address);
|
|
328
|
+
const utxos = await db.run(SELECT.from(AddressUTxOs).where({ address_address: address }));
|
|
329
|
+
return latestSlices(utxos, (utxo) => `${utxo.hash}#${utxo.index}`);
|
|
240
330
|
});
|
|
241
331
|
});
|
|
332
|
+
// Collection reads of the address child sets fill like the parent: an address named in the
|
|
333
|
+
// navigation or the $filter is indexed on a miss, then the client's own query runs.
|
|
334
|
+
srv.on('READ', AddressUTxOs, addressChildRead(addressUtxosIndexed, indexAddressOrUtxos));
|
|
335
|
+
srv.on('READ', AddressAssets, addressChildRead(addressIndexed, (db, address) => indexer().indexAddress(db, address)));
|
|
242
336
|
// GetUTxOsByCredential — always-fresh credential-keyed UTxO query (dApp state reads need current
|
|
243
337
|
// data). Crawled UTxO set + node first, else Koios; ProviderUnavailableError without either.
|
|
244
338
|
srv.on('GetUTxOsByCredential', async (req) => {
|
|
@@ -103,7 +103,7 @@ exports.AGENT_ALWAYS_ALLOWED_EVENTS = new Set([
|
|
|
103
103
|
// CardanoODataService: chain reads through the backends
|
|
104
104
|
'GetNetworkInformation', 'GetBlockByHash', 'GetEpochByNumber', 'GetPoolById', 'GetDrepById',
|
|
105
105
|
'GetAssetInfo', 'GetAssetHistory', 'GetAccountByStakeAddress', 'GetTransactionByHash',
|
|
106
|
-
'GetMetadataByTxHash', 'GetAddressByBech32', 'GetUTxOsByAddress', 'GetUTxOsByCredential',
|
|
106
|
+
'GetTransactionConfirmations', 'GetMetadataByTxHash', 'GetAddressByBech32', 'GetUTxOsByAddress', 'GetUTxOsByCredential',
|
|
107
107
|
'GetAssetsByAddress', 'GetLatestTransactionsByAddress', 'GetLatestBlock', 'GetLatestEpoch',
|
|
108
108
|
'GetLedgerProtocolParameters', 'ParseTransactionCbor',
|
|
109
109
|
// CardanoTransactionService: compute-only
|