@@ -50,7 +50,7 @@ const adjustOutputsForFee = async (
50
50
outputs : Set < Cardano . TxOut > ,
51
51
changeOutputs : Array < Cardano . TxOut > ,
52
52
currentFee : bigint
53
- ) : Promise < { fee : bigint ; change : Array < Cardano . TxOut > } > => {
53
+ ) : Promise < { fee : bigint ; change : Array < Cardano . TxOut > ; feeAccountedFor : boolean } > => {
54
54
const totalOutputs = new Set ( [ ...outputs , ...changeOutputs ] ) ;
55
55
const fee = await constraints . computeMinimumCost ( {
56
56
change : [ ] ,
@@ -59,7 +59,7 @@ const adjustOutputsForFee = async (
59
59
outputs : totalOutputs
60
60
} ) ;
61
61
62
- if ( fee === changeLovelace ) return { change : [ ] , fee } ;
62
+ if ( fee === changeLovelace ) return { change : [ ] , fee, feeAccountedFor : true } ;
63
63
64
64
if ( changeLovelace < fee ) throw new InputSelectionError ( InputSelectionFailure . UtxoBalanceInsufficient ) ;
65
65
@@ -78,11 +78,7 @@ const adjustOutputsForFee = async (
78
78
}
79
79
}
80
80
81
- if ( ! feeAccountedFor ) {
82
- throw new InputSelectionError ( InputSelectionFailure . UtxoFullyDepleted ) ;
83
- }
84
-
85
- return { change : [ ...updatedOutputs ] , fee } ;
81
+ return { change : [ ...updatedOutputs ] , fee, feeAccountedFor } ;
86
82
} ;
87
83
88
84
/**
@@ -104,7 +100,7 @@ const splitChangeAndComputeFee = async (
104
100
constraints : SelectionConstraints ,
105
101
getChangeAddresses : ( ) => Promise < Map < Cardano . PaymentAddress , number > > ,
106
102
fee : bigint
107
- ) : Promise < { fee : bigint ; change : Array < Cardano . TxOut > } > => {
103
+ ) : Promise < { fee : bigint ; change : Array < Cardano . TxOut > ; feeAccountedFor : boolean } > => {
108
104
const changeOutputs = await splitChange (
109
105
getChangeAddresses ,
110
106
changeLovelace ,
@@ -114,7 +110,7 @@ const splitChangeAndComputeFee = async (
114
110
fee
115
111
) ;
116
112
117
- const adjustedChangeOutputs = await adjustOutputsForFee (
113
+ let adjustedChangeOutputs = await adjustOutputsForFee (
118
114
changeLovelace ,
119
115
constraints ,
120
116
inputs ,
@@ -126,7 +122,7 @@ const splitChangeAndComputeFee = async (
126
122
// If the newly computed fee is higher than tha available balance for change,
127
123
// but there are unallocated native assets, return the assets as change with 0n coins.
128
124
if ( adjustedChangeOutputs . fee >= changeLovelace ) {
129
- return {
125
+ const result = {
130
126
change : [
131
127
{
132
128
address : stubMaxSizeAddress ,
@@ -136,12 +132,18 @@ const splitChangeAndComputeFee = async (
136
132
}
137
133
}
138
134
] ,
139
- fee : adjustedChangeOutputs . fee
135
+ fee : adjustedChangeOutputs . fee ,
136
+ feeAccountedFor : true
140
137
} ;
138
+
139
+ if ( result . change [ 0 ] . value . coins < constraints . computeMinimumCoinQuantity ( result . change [ 0 ] ) )
140
+ throw new InputSelectionError ( InputSelectionFailure . UtxoFullyDepleted ) ;
141
+
142
+ return result ;
141
143
}
142
144
143
145
if ( fee < adjustedChangeOutputs . fee ) {
144
- return splitChangeAndComputeFee (
146
+ adjustedChangeOutputs = await splitChangeAndComputeFee (
145
147
inputs ,
146
148
outputs ,
147
149
changeLovelace ,
@@ -150,8 +152,18 @@ const splitChangeAndComputeFee = async (
150
152
getChangeAddresses ,
151
153
adjustedChangeOutputs . fee
152
154
) ;
155
+
156
+ if ( adjustedChangeOutputs . change . length === 0 )
157
+ throw new InputSelectionError ( InputSelectionFailure . UtxoFullyDepleted ) ;
158
+ }
159
+
160
+ for ( const out of adjustedChangeOutputs . change ) {
161
+ if ( out . value . coins < constraints . computeMinimumCoinQuantity ( out ) )
162
+ throw new InputSelectionError ( InputSelectionFailure . UtxoFullyDepleted ) ;
153
163
}
154
164
165
+ if ( ! adjustedChangeOutputs . feeAccountedFor ) throw new InputSelectionError ( InputSelectionFailure . UtxoFullyDepleted ) ;
166
+
155
167
return adjustedChangeOutputs ;
156
168
} ;
157
169
0 commit comments