@@ -11,11 +11,12 @@ import {
1111 dimensionsToGas ,
1212} from '../../common/fees/dimensions' ;
1313import { consolidateOutputs } from '../../utils/consolidateOutputs' ;
14+ import type { FeeState } from '../models' ;
1415import { getInputComplexity , getOutputComplexity } from '../txs/fee' ;
1516
1617export interface SpendHelperProps {
1718 changeOutputs : readonly TransferableOutput [ ] ;
18- gasPrice : bigint ;
19+ feeState : FeeState ;
1920 initialComplexity : Dimensions ;
2021 inputs : readonly TransferableInput [ ] ;
2122 shouldConsolidateOutputs : boolean ;
@@ -32,7 +33,7 @@ export interface SpendHelperProps {
3233 * @class
3334 */
3435export class SpendHelper {
35- private readonly gasPrice : bigint ;
36+ private readonly feeState : FeeState ;
3637 private readonly initialComplexity : Dimensions ;
3738 private readonly shouldConsolidateOutputs : boolean ;
3839 private readonly toBurn : Map < string , bigint > ;
@@ -47,7 +48,7 @@ export class SpendHelper {
4748
4849 constructor ( {
4950 changeOutputs,
50- gasPrice ,
51+ feeState ,
5152 initialComplexity,
5253 inputs,
5354 shouldConsolidateOutputs,
@@ -56,7 +57,7 @@ export class SpendHelper {
5657 toStake,
5758 weights,
5859 } : SpendHelperProps ) {
59- this . gasPrice = gasPrice ;
60+ this . feeState = feeState ;
6061 this . initialComplexity = initialComplexity ;
6162 this . shouldConsolidateOutputs = shouldConsolidateOutputs ;
6263 this . toBurn = toBurn ;
@@ -217,13 +218,13 @@ export class SpendHelper {
217218 }
218219
219220 /**
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 .
221222 * Provide an empty change output as a parameter to calculate the fee as if the change output was already added.
222223 *
223224 * @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.
225226 */
226- calculateFee ( additionalOutput ?: TransferableOutput ) : bigint {
227+ private calculateGas ( additionalOutput ?: TransferableOutput ) : bigint {
227228 this . consolidateOutputs ( ) ;
228229
229230 const gas = dimensionsToGas (
@@ -233,7 +234,22 @@ export class SpendHelper {
233234 this . weights ,
234235 ) ;
235236
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 ;
237253 }
238254
239255 /**
@@ -281,6 +297,22 @@ export class SpendHelper {
281297 return null ;
282298 }
283299
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+
284316 /**
285317 * Gets the inputs, outputs, and UTXOs for the SpendHelper.
286318 *
0 commit comments