estimateTransactionSize static method

Future<int> estimateTransactionSize(
  1. RawTransaction transaction
)

Returns the estimated encoded size of the transaction, including the signature.

This function is useful for calculating the fee from suggested fee per byte.

Returns an estimated byte size for the transaction.

Implementation

static Future<int> estimateTransactionSize(RawTransaction transaction) async {
  try {
    // Create a random account to sign the transaction
    final account = await Account.random();

    // Sign the transaction
    final signature = await account.sign(transaction.getEncodedTransaction());
    final signedTransaction = SignedTransaction(
      transaction: transaction,
      signature: signature.bytes,
    );

    return Encoder.encodeMessagePack(signedTransaction.toMessagePack())
        .length;
  } catch (ex) {
    return 0;
  }
}