@@ -14,6 +14,7 @@ interface ChangeComputationArgs {
14
14
estimateTxFee : EstimateTxFeeWithOriginalOutputs ;
15
15
computeMinimumCoinQuantity : ComputeMinimumCoinQuantity ;
16
16
tokenBundleSizeExceedsLimit : TokenBundleSizeExceedsLimit ;
17
+ random : typeof Math . random ;
17
18
}
18
19
19
20
interface ChangeComputationResult {
@@ -167,10 +168,13 @@ const computeRequestedAssetChangeBundles = (
167
168
* Picks one UTxO from remaining set and puts it to the selected set.
168
169
* Precondition: utxoRemaining.length > 0
169
170
*/
170
- const pickExtraRandomUtxo = ( { utxoRemaining, utxoSelected } : UtxoSelection ) : UtxoSelection => {
171
+ const pickExtraRandomUtxo = (
172
+ { utxoRemaining, utxoSelected } : UtxoSelection ,
173
+ random : typeof Math . random
174
+ ) : UtxoSelection => {
171
175
const remainingUtxoOfOnlyCoin = utxoRemaining . filter ( ( [ _ , { value } ] ) => ! value . assets ) ;
172
176
const pickFrom = remainingUtxoOfOnlyCoin . length > 0 ? remainingUtxoOfOnlyCoin : utxoRemaining ;
173
- const pickIdx = Math . floor ( Math . random ( ) * pickFrom . length ) ;
177
+ const pickIdx = Math . floor ( random ( ) * pickFrom . length ) ;
174
178
const newUtxoSelected = [ ...utxoSelected , pickFrom [ pickIdx ] ] ;
175
179
const originalIdx = utxoRemaining . indexOf ( pickFrom [ pickIdx ] ) ;
176
180
const newUtxoRemaining = [ ...utxoRemaining . slice ( 0 , originalIdx ) , ...utxoRemaining . slice ( originalIdx + 1 ) ] ;
@@ -218,6 +222,7 @@ const computeChangeBundles = ({
218
222
uniqueOutputAssetIDs,
219
223
implicitCoin,
220
224
computeMinimumCoinQuantity,
225
+ random,
221
226
fee = 0n
222
227
} : {
223
228
utxoSelection : UtxoSelection ;
@@ -226,6 +231,7 @@ const computeChangeBundles = ({
226
231
implicitCoin : Required < Cardano . ImplicitCoin > ;
227
232
computeMinimumCoinQuantity : ComputeMinimumCoinQuantity ;
228
233
fee ?: bigint ;
234
+ random : typeof Math . random ;
229
235
} ) : UtxoSelection & { changeBundles : Cardano . Value [ ] } => {
230
236
const requestedAssetChangeBundles = computeRequestedAssetChangeBundles (
231
237
utxoSelection . utxoSelected ,
@@ -251,8 +257,9 @@ const computeChangeBundles = ({
251
257
fee,
252
258
implicitCoin,
253
259
outputValues,
260
+ random,
254
261
uniqueOutputAssetIDs,
255
- utxoSelection : pickExtraRandomUtxo ( utxoSelection )
262
+ utxoSelection : pickExtraRandomUtxo ( utxoSelection , random )
256
263
} ) ;
257
264
}
258
265
// This is not a great error type for this, because the spec says
@@ -294,12 +301,14 @@ export const computeChangeAndAdjustForFee = async ({
294
301
outputValues,
295
302
uniqueOutputAssetIDs,
296
303
implicitCoin,
304
+ random,
297
305
utxoSelection
298
306
} : ChangeComputationArgs ) : Promise < ChangeComputationResult > => {
299
307
const changeInclFee = computeChangeBundles ( {
300
308
computeMinimumCoinQuantity,
301
309
implicitCoin,
302
310
outputValues,
311
+ random,
303
312
uniqueOutputAssetIDs,
304
313
utxoSelection
305
314
} ) ;
@@ -325,9 +334,10 @@ export const computeChangeAndAdjustForFee = async ({
325
334
estimateTxFee,
326
335
implicitCoin,
327
336
outputValues,
337
+ random,
328
338
tokenBundleSizeExceedsLimit,
329
339
uniqueOutputAssetIDs,
330
- utxoSelection : pickExtraRandomUtxo ( changeInclFee )
340
+ utxoSelection : pickExtraRandomUtxo ( changeInclFee , random )
331
341
} ) ;
332
342
}
333
343
@@ -336,6 +346,7 @@ export const computeChangeAndAdjustForFee = async ({
336
346
fee,
337
347
implicitCoin,
338
348
outputValues,
349
+ random,
339
350
uniqueOutputAssetIDs,
340
351
utxoSelection : { utxoRemaining : changeInclFee . utxoRemaining , utxoSelected : changeInclFee . utxoSelected }
341
352
} ) ;
0 commit comments