1
1
import { Cardano , cmlUtil } from '@cardano-sdk/core' ;
2
+ import { ChangeAddressResolver } from '../ChangeAddress' ;
2
3
import { InputSelectionError , InputSelectionFailure } from '../InputSelectionError' ;
3
4
import { InputSelectionParameters , InputSelector , SelectionResult } from '../types' ;
4
- import { assertIsBalanceSufficient , preProcessArgs , toValues } from '../util' ;
5
+ import { assertIsBalanceSufficient , preProcessArgs , stubMaxSizeAddress , toValues } from '../util' ;
5
6
import { computeChangeAndAdjustForFee } from './change' ;
6
7
import { roundRobinSelection } from './roundRobin' ;
7
8
8
9
interface RoundRobinRandomImproveOptions {
9
- getChangeAddress : ( ) => Promise < Cardano . PaymentAddress > ;
10
+ changeAddressResolver : ChangeAddressResolver ;
10
11
random ?: typeof Math . random ;
11
12
}
12
13
13
14
export const roundRobinRandomImprove = ( {
14
- getChangeAddress ,
15
+ changeAddressResolver ,
15
16
random = Math . random
16
17
} : RoundRobinRandomImproveOptions ) : InputSelector => ( {
17
18
select : async ( {
@@ -20,7 +21,7 @@ export const roundRobinRandomImprove = ({
20
21
constraints : { computeMinimumCost, computeSelectionLimit, computeMinimumCoinQuantity, tokenBundleSizeExceedsLimit } ,
21
22
implicitValue : partialImplicitValue = { }
22
23
} : InputSelectionParameters ) : Promise < SelectionResult > => {
23
- const changeAddress = await getChangeAddress ( ) ;
24
+ const changeAddress = stubMaxSizeAddress ;
24
25
const { utxo, outputs, uniqueTxAssetIDs, implicitValue } = preProcessArgs (
25
26
utxoSet ,
26
27
outputSet ,
@@ -63,23 +64,29 @@ export const roundRobinRandomImprove = ({
63
64
} ) ;
64
65
65
66
const inputs = new Set ( result . inputs ) ;
66
- const change = result . change . map ( ( value ) => ( {
67
- address : changeAddress ,
68
- value
69
- } ) ) ;
70
67
71
- if ( result . inputs . length > ( await computeSelectionLimit ( { change, fee : result . fee , inputs, outputs : outputSet } ) ) ) {
68
+ const selection = {
69
+ change : result . change . map ( ( value ) => ( {
70
+ address : changeAddress ,
71
+ value
72
+ } ) ) ,
73
+ fee : result . fee ,
74
+ inputs,
75
+ outputs : outputSet
76
+ } ;
77
+
78
+ selection . change = await changeAddressResolver . resolve ( selection ) ;
79
+
80
+ if (
81
+ result . inputs . length >
82
+ ( await computeSelectionLimit ( { change : selection . change , fee : selection . fee , inputs, outputs : outputSet } ) )
83
+ ) {
72
84
throw new InputSelectionError ( InputSelectionFailure . MaximumInputCountExceeded ) ;
73
85
}
74
86
75
87
return {
76
88
remainingUTxO : new Set ( result . remainingUTxO ) ,
77
- selection : {
78
- change,
79
- fee : result . fee ,
80
- inputs,
81
- outputs : outputSet
82
- }
89
+ selection
83
90
} ;
84
91
}
85
92
} ) ;
0 commit comments