|
1 | 1 | /* eslint-disable func-style, max-params */
|
2 | 2 | import { BigNumber } from 'bignumber.js';
|
3 | 3 | import { Cardano, coalesceValueQuantities } from '@cardano-sdk/core';
|
4 |
| -import { ComputeMinimumCoinQuantity, TokenBundleSizeExceedsLimit } from '../types'; |
| 4 | +import { ComputeMinimumCoinQuantity, SelectionConstraints, TokenBundleSizeExceedsLimit } from '../types'; |
5 | 5 | import { InputSelectionError, InputSelectionFailure } from '../InputSelectionError';
|
6 |
| -import { addTokenMaps, isValidValue, sortByCoins } from '../util'; |
| 6 | +import { addTokenMaps, isValidValue, sortByCoins, stubMaxSizeAddress } from '../util'; |
7 | 7 |
|
8 | 8 | const PERCENTAGE_TOLERANCE = 0.05;
|
9 | 9 |
|
@@ -227,22 +227,141 @@ export const splitChange = async (
|
227 | 227 | };
|
228 | 228 |
|
229 | 229 | /**
|
230 |
| - * Sorts the given TxIn set first by txId and then by index. |
| 230 | + * Given a set of input and outputs, compute the fee. Then extract the fee from the change output |
| 231 | + * with the highest value. |
231 | 232 | *
|
232 |
| - * @param lhs The left-hand side of the comparison operation. |
233 |
| - * @param rhs The left-hand side of the comparison operation. |
| 233 | + * @param changeLovelace The available amount of lovelace to be used as change. |
| 234 | + * @param constraints The selection constraints. |
| 235 | + * @param inputs The inputs of the transaction. |
| 236 | + * @param outputs The outputs of the transaction. |
| 237 | + * @param changeOutputs The list of change outputs. |
| 238 | + * @param currentFee The current computed fee for this selection. |
234 | 239 | */
|
235 |
| -export const sortTxIn = (lhs: Cardano.TxIn, rhs: Cardano.TxIn) => { |
236 |
| - const txIdComparison = lhs.txId.localeCompare(rhs.txId); |
237 |
| - if (txIdComparison !== 0) return txIdComparison; |
| 240 | +export const adjustOutputsForFee = async ( |
| 241 | + changeLovelace: bigint, |
| 242 | + constraints: SelectionConstraints, |
| 243 | + inputs: Set<Cardano.Utxo>, |
| 244 | + outputs: Set<Cardano.TxOut>, |
| 245 | + changeOutputs: Array<Cardano.TxOut>, |
| 246 | + currentFee: bigint |
| 247 | +): Promise<{ |
| 248 | + fee: bigint; |
| 249 | + change: Array<Cardano.TxOut>; |
| 250 | + feeAccountedFor: boolean; |
| 251 | + redeemers?: Array<Cardano.Redeemer>; |
| 252 | +}> => { |
| 253 | + const totalOutputs = new Set([...outputs, ...changeOutputs]); |
| 254 | + const { fee, redeemers } = await constraints.computeMinimumCost({ |
| 255 | + change: [], |
| 256 | + fee: currentFee, |
| 257 | + inputs, |
| 258 | + outputs: totalOutputs |
| 259 | + }); |
| 260 | + |
| 261 | + if (fee === changeLovelace) return { change: [], fee, feeAccountedFor: true, redeemers }; |
| 262 | + |
| 263 | + if (changeLovelace < fee) throw new InputSelectionError(InputSelectionFailure.UtxoBalanceInsufficient); |
| 264 | + |
| 265 | + const updatedOutputs = [...changeOutputs]; |
| 266 | + |
| 267 | + updatedOutputs.sort(sortByCoins); |
| 268 | + |
| 269 | + let feeAccountedFor = false; |
| 270 | + for (const output of updatedOutputs) { |
| 271 | + const adjustedCoins = output.value.coins - fee; |
| 272 | + |
| 273 | + if (adjustedCoins >= constraints.computeMinimumCoinQuantity(output)) { |
| 274 | + output.value.coins = adjustedCoins; |
| 275 | + feeAccountedFor = true; |
| 276 | + break; |
| 277 | + } |
| 278 | + } |
238 | 279 |
|
239 |
| - return lhs.index - rhs.index; |
| 280 | + return { change: [...updatedOutputs], fee, feeAccountedFor, redeemers }; |
240 | 281 | };
|
241 | 282 |
|
242 | 283 | /**
|
243 |
| - * Sorts the given Utxo set first by TxIn. |
| 284 | + * Recursively compute the fee and compute change outputs until it finds a set of change outputs that satisfies the fee. |
244 | 285 | *
|
245 |
| - * @param lhs The left-hand side of the comparison operation. |
246 |
| - * @param rhs The left-hand side of the comparison operation. |
| 286 | + * @param inputs The inputs of the transaction. |
| 287 | + * @param outputs The outputs of the transaction. |
| 288 | + * @param changeLovelace The total amount of lovelace in the change. |
| 289 | + * @param changeAssets The total assets to be distributed as change. |
| 290 | + * @param constraints The selection constraints. |
| 291 | + * @param getChangeAddresses A callback that returns a list of addresses and their proportions. |
| 292 | + * @param fee The current computed fee for this selection. |
247 | 293 | */
|
248 |
| -export const sortUtxoByTxIn = (lhs: Cardano.Utxo, rhs: Cardano.Utxo) => sortTxIn(lhs[0], rhs[0]); |
| 294 | +export const splitChangeAndComputeFee = async ( |
| 295 | + inputs: Set<Cardano.Utxo>, |
| 296 | + outputs: Set<Cardano.TxOut>, |
| 297 | + changeLovelace: bigint, |
| 298 | + changeAssets: Cardano.TokenMap | undefined, |
| 299 | + constraints: SelectionConstraints, |
| 300 | + getChangeAddresses: () => Promise<Map<Cardano.PaymentAddress, number>>, |
| 301 | + fee: bigint |
| 302 | +): Promise<{ fee: bigint; change: Array<Cardano.TxOut>; feeAccountedFor: boolean }> => { |
| 303 | + const changeOutputs = await splitChange( |
| 304 | + getChangeAddresses, |
| 305 | + changeLovelace, |
| 306 | + changeAssets, |
| 307 | + constraints.computeMinimumCoinQuantity, |
| 308 | + constraints.tokenBundleSizeExceedsLimit, |
| 309 | + fee |
| 310 | + ); |
| 311 | + |
| 312 | + let adjustedChangeOutputs = await adjustOutputsForFee( |
| 313 | + changeLovelace, |
| 314 | + constraints, |
| 315 | + inputs, |
| 316 | + outputs, |
| 317 | + changeOutputs, |
| 318 | + fee |
| 319 | + ); |
| 320 | + |
| 321 | + // If the newly computed fee is higher than the available balance for change, |
| 322 | + // but there are unallocated native assets, return the assets as change with 0n coins. |
| 323 | + if (adjustedChangeOutputs.fee >= changeLovelace) { |
| 324 | + const result = { |
| 325 | + change: [ |
| 326 | + { |
| 327 | + address: stubMaxSizeAddress, |
| 328 | + value: { |
| 329 | + assets: changeAssets, |
| 330 | + coins: 0n |
| 331 | + } |
| 332 | + } |
| 333 | + ], |
| 334 | + fee: adjustedChangeOutputs.fee, |
| 335 | + feeAccountedFor: true |
| 336 | + }; |
| 337 | + |
| 338 | + if (result.change[0].value.coins < constraints.computeMinimumCoinQuantity(result.change[0])) |
| 339 | + throw new InputSelectionError(InputSelectionFailure.UtxoFullyDepleted); |
| 340 | + |
| 341 | + return result; |
| 342 | + } |
| 343 | + |
| 344 | + if (fee < adjustedChangeOutputs.fee) { |
| 345 | + adjustedChangeOutputs = await splitChangeAndComputeFee( |
| 346 | + inputs, |
| 347 | + outputs, |
| 348 | + changeLovelace, |
| 349 | + changeAssets, |
| 350 | + constraints, |
| 351 | + getChangeAddresses, |
| 352 | + adjustedChangeOutputs.fee |
| 353 | + ); |
| 354 | + |
| 355 | + if (adjustedChangeOutputs.change.length === 0) |
| 356 | + throw new InputSelectionError(InputSelectionFailure.UtxoFullyDepleted); |
| 357 | + } |
| 358 | + |
| 359 | + for (const out of adjustedChangeOutputs.change) { |
| 360 | + if (out.value.coins < constraints.computeMinimumCoinQuantity(out)) |
| 361 | + throw new InputSelectionError(InputSelectionFailure.UtxoFullyDepleted); |
| 362 | + } |
| 363 | + |
| 364 | + if (!adjustedChangeOutputs.feeAccountedFor) throw new InputSelectionError(InputSelectionFailure.UtxoFullyDepleted); |
| 365 | + |
| 366 | + return adjustedChangeOutputs; |
| 367 | +}; |
0 commit comments