SolanaTransaction constructor

SolanaTransaction({
  1. required SolAddress payerKey,
  2. required List<TransactionInstruction> instructions,
  3. required SolAddress recentBlockhash,
  4. List<List<int>> signatures = const [],
  5. TransactionType? type,
  6. List<AddressLookupTableAccount> addressLookupTableAccounts = const [],
})

Constructs a Solana transaction with provided parameters.

Implementation

factory SolanaTransaction({
  required SolAddress payerKey,
  required List<TransactionInstruction> instructions,
  required SolAddress recentBlockhash,
  List<List<int>> signatures = const [],
  TransactionType? type,
  List<AddressLookupTableAccount> addressLookupTableAccounts = const [],
}) {
  if (type == null) {
    if (addressLookupTableAccounts.isNotEmpty) {
      type = TransactionType.v0;
    } else {
      type = TransactionType.legacy;
    }
  } else {
    if (type == TransactionType.legacy &&
        addressLookupTableAccounts.isNotEmpty) {
      throw const MessageException(
          "Do not use addressLookupTableAccounts in legacy transactions.");
    }
  }
  VersionedMessage message;
  if (type == TransactionType.legacy) {
    message = VersionedMessage.toLegacy(
      payerKey: payerKey,
      recentBlockhash: recentBlockhash,
      instructions: instructions,
    );
  } else {
    message = VersionedMessage.toV0(
      payerKey: payerKey,
      recentBlockhash: recentBlockhash,
      instructions: instructions,
      addressLookupTableAccounts: addressLookupTableAccounts,
    );
  }
  if (signatures.isNotEmpty) {
    if (signatures.length != message.header.numRequiredSignatures) {
      throw const MessageException(
          "The expected length of signatures should match the number of required signatures.");
    }
  } else {
    signatures = List.generate(
      message.header.numRequiredSignatures,
      (index) => List<int>.unmodifiable(List<int>.filled(
          SolanaTransactionConstant.signatureLengthInBytes, 0)),
    );
  }
  return SolanaTransaction._(
    List<List<int>>.unmodifiable(signatures),
    message: message,
  );
}