calculateFees static method

Future<void> calculateFees(
  1. XRPLRpc client,
  2. XRPTransaction transaction
)

This asynchronous function calculates transaction fees for an XRPL transaction.

Implementation

static Future<void> calculateFees(
    XRPLRpc client, XRPTransaction transaction) async {
  /// Fetch the net fee from the XRPL server.
  final int netFee =
      (await client.request(RPCFee())).getFeeType(type: XrplFeeType.open);

  /// Initialize the base fee with the net fee.
  int baseFee = netFee;

  /// Check if the transaction type is ESCROW_FINISH.
  if (transaction.transactionType == XRPLTransactionType.escrowFinish) {
    /// Cast the transaction as an EscrowFinish to access specific properties.
    transaction as EscrowFinish;
    if (transaction.fulfillment != null) {
      /// Calculate the length of the fulfillment in bytes.
      int fulfillmentBytesLength = transaction.fulfillment!.codeUnits.length;

      /// Adjust the base fee based on the fulfillment length.
      baseFee = (netFee * (33 + (fulfillmentBytesLength / 16)).ceil()).ceil();
    }
  }

  /// Check if the transaction type is AMM_CREATE or ACCOUNT_DELETE.
  if (transaction.transactionType == XRPLTransactionType.ammCreate ||
      transaction.transactionType == XRPLTransactionType.accountDelete) {
    /// Fetch the reserve fee and set it as the base fee.
    baseFee = await fetchReserveFee(client);
  }

  /// Adjust the base fee if the transaction involves multi-signers.
  if (transaction.multiSigSigners.isNotEmpty) {
    baseFee += netFee * (1 + transaction.multiSigSigners.length);
  }

  /// Set the calculated base fee in the transaction.
  transaction.setFee(BigInt.from(baseFee));
}