fee property

int get fee

Get the fee of the transaction.

Implementation

int get fee => () {
      int totalInput = 0;
      int totalOutput = 0;

      for (int i = 0; i < inputs.length; i++) {
        PsbtInput input = inputs[i];

        if (input.witnessUtxo != null) {
          //  witnessUtxo (PSBT key type 01)
          int amount = input.witnessUtxo!.amount;
          totalInput += amount;
        } else if (input.previousTransaction != null) {
          // nonWitnessUtxo (PSBT key type 00)
          Transaction prevTx = input.previousTransaction!;
          int outputIndex = unsignedTransaction!.inputs[i].index;
          if (outputIndex < prevTx.outputs.length) {
            int amount = prevTx.outputs[outputIndex].amount;
            totalInput += amount;
          } else {
            throw Exception(
                'Invalid output index $outputIndex in previous transaction');
          }
        } else {
          throw Exception('No UTXO information found for input $i');
        }
      }

      for (int i = 0; i < unsignedTransaction!.outputs.length; i++) {
        int amount = unsignedTransaction!.outputs[i].amount;
        totalOutput += amount;
      }
      int fee = totalInput - totalOutput;

      return fee;
    }();