@@ -11,11 +11,12 @@ import {
11
11
dimensionsToGas ,
12
12
} from '../../common/fees/dimensions' ;
13
13
import { consolidateOutputs } from '../../utils/consolidateOutputs' ;
14
+ import type { FeeState } from '../models' ;
14
15
import { getInputComplexity , getOutputComplexity } from '../txs/fee' ;
15
16
16
17
export interface SpendHelperProps {
17
18
changeOutputs : readonly TransferableOutput [ ] ;
18
- gasPrice : bigint ;
19
+ feeState : FeeState ;
19
20
initialComplexity : Dimensions ;
20
21
inputs : readonly TransferableInput [ ] ;
21
22
shouldConsolidateOutputs : boolean ;
@@ -32,7 +33,7 @@ export interface SpendHelperProps {
32
33
* @class
33
34
*/
34
35
export class SpendHelper {
35
- private readonly gasPrice : bigint ;
36
+ private readonly feeState : FeeState ;
36
37
private readonly initialComplexity : Dimensions ;
37
38
private readonly shouldConsolidateOutputs : boolean ;
38
39
private readonly toBurn : Map < string , bigint > ;
@@ -47,7 +48,7 @@ export class SpendHelper {
47
48
48
49
constructor ( {
49
50
changeOutputs,
50
- gasPrice ,
51
+ feeState ,
51
52
initialComplexity,
52
53
inputs,
53
54
shouldConsolidateOutputs,
@@ -56,7 +57,7 @@ export class SpendHelper {
56
57
toStake,
57
58
weights,
58
59
} : SpendHelperProps ) {
59
- this . gasPrice = gasPrice ;
60
+ this . feeState = feeState ;
60
61
this . initialComplexity = initialComplexity ;
61
62
this . shouldConsolidateOutputs = shouldConsolidateOutputs ;
62
63
this . toBurn = toBurn ;
@@ -217,13 +218,13 @@ export class SpendHelper {
217
218
}
218
219
219
220
/**
220
- * Calculates the fee for the SpendHelper based on its complexity and gas price .
221
+ * Calculates the gas usage for the SpendHelper based on its complexity and the weights .
221
222
* Provide an empty change output as a parameter to calculate the fee as if the change output was already added.
222
223
*
223
224
* @param {TransferableOutput } additionalOutput - The change output that has not yet been added to the SpendHelper.
224
- * @returns {bigint } The fee for the SpendHelper.
225
+ * @returns {bigint } The gas usage for the SpendHelper.
225
226
*/
226
- calculateFee ( additionalOutput ?: TransferableOutput ) : bigint {
227
+ private calculateGas ( additionalOutput ?: TransferableOutput ) : bigint {
227
228
this . consolidateOutputs ( ) ;
228
229
229
230
const gas = dimensionsToGas (
@@ -233,7 +234,22 @@ export class SpendHelper {
233
234
this . weights ,
234
235
) ;
235
236
236
- return gas * this . gasPrice ;
237
+ return gas ;
238
+ }
239
+
240
+ /**
241
+ * Calculates the fee for the SpendHelper based on its complexity and gas price.
242
+ * Provide an empty change output as a parameter to calculate the fee as if the change output was already added.
243
+ *
244
+ * @param {TransferableOutput } additionalOutput - The change output that has not yet been added to the SpendHelper.
245
+ * @returns {bigint } The fee for the SpendHelper.
246
+ */
247
+ calculateFee ( additionalOutput ?: TransferableOutput ) : bigint {
248
+ const gas = this . calculateGas ( additionalOutput ) ;
249
+
250
+ const gasPrice = this . feeState . price ;
251
+
252
+ return gas * gasPrice ;
237
253
}
238
254
239
255
/**
@@ -281,6 +297,22 @@ export class SpendHelper {
281
297
return null ;
282
298
}
283
299
300
+ /**
301
+ * Verifies that gas usage does not exceed the fee state maximum.
302
+ *
303
+ * @returns {Error | null } An error if gas usage exceeds maximum, null otherwise.
304
+ */
305
+ verifyGasUsage ( ) : Error | null {
306
+ const gas = this . calculateGas ( ) ;
307
+ if ( this . feeState . capacity < gas ) {
308
+ return new Error (
309
+ `Gas usage of transaction (${ gas . toString ( ) } ) exceeds capacity (${ this . feeState . capacity . toString ( ) } )` ,
310
+ ) ;
311
+ }
312
+
313
+ return null ;
314
+ }
315
+
284
316
/**
285
317
* Gets the inputs, outputs, and UTXOs for the SpendHelper.
286
318
*
0 commit comments