Transaction

class Transaction

An atomically-commited sequence of instructions.

While Instructions are the basic unit of computation in Solana, they are submitted by clients in Transactions containing one or more instructions, and signed by one or more signers.

See the Rust module documentation for more details about transactions.

Some constructors accept an optional payer, the account responsible for paying the cost of executing a transaction. In most cases, callers should specify the payer explicitly in these constructors. In some cases though, the caller is not required to specify the payer, but is still allowed to: in the Message object, the first account is always the fee-payer, so if the caller has knowledge that the first account of the constructed transaction’s Message is both a signer and the expected fee-payer, then redundantly specifying the fee-payer is not strictly required.

The main Transaction() constructor creates a fully-signed transaction from a Message.

Parameters:
  • from_keypairs (Sequence[Keypair | Presigner]) – The keypairs that are to sign the transaction.

  • message (Message) – The message to sign.

  • recent_blockhash (Hash) – The id of a recent ledger entry.

Example

>>> from solders.message import Message
>>> from solders.keypair import Keypair
>>> from solders.instruction import Instruction
>>> from solders.hash import Hash
>>> from solders.transaction import Transaction
>>> from solders.pubkey import Pubkey
>>> program_id = Pubkey.default()
>>> arbitrary_instruction_data = bytes([1])
>>> accounts = []
>>> instruction = Instruction(program_id, arbitrary_instruction_data, accounts)
>>> payer = Keypair()
>>> message = Message([instruction], payer.pubkey())
>>> blockhash = Hash.default()  # replace with a real blockhash
>>> tx = Transaction([payer], message, blockhash)
data(instruction_index)

Get the data for an instruction at the given index.

Parameters:

instruction_index (int) – index into the instructions vector of the transaction’s message.

Returns:

The instruction data.

Return type:

bytes

static default()

Return a new default transaction.

Returns:

The default transaction.

Return type:

Transaction

static from_bytes(data)

Deserialize a serialized Transaction object.

Parameters:

data (bytes) – the serialized Transaction.

Returns:

the deserialized Transaction.

Return type:

Transaction

Example

>>> from solders.transaction import Transaction
>>> tx = Transaction.default()
>>> assert Transaction.from_bytes(bytes(tx)) == tx
static from_json(raw)

Build from a JSON string.

get_nonce_pubkey_from_instruction(ix)

Deprecated in the Solana Rust SDK, expose here only for testing.

get_signing_keypair_positions(pubkeys)

Get the positions of the pubkeys in account_keys associated with signing keypairs.

Parameters:
  • pubkeys (Sequence[Pubkey]) – The pubkeys to find.

  • Returns – list[Optional[int]]: The pubkey positions.

is_signed()

Check if the transaction has been signed.

Returns:

True if the transaction has been signed.

Return type:

bool

key(instruction_index, accounts_index)

Get the Pubkey of an account required by one of the instructions in the transaction.

Returns None if instruction_index is greater than or equal to the number of instructions in the transaction; or if accounts_index is greater than or equal to the number of accounts in the instruction.

Parameters:
  • instruction_index (int) – index into the instructions vector of the transaction’s message.

  • account_index (int) – index into the acounts list of the message’s compiled_instructions.

Returns:

The account key.

Return type:

Optional[Pubkey]

message

The message to sign.

Type:

Message

message_data()

Return the serialized message data to sign.

Returns:

The serialized message data.

Return type:

bytes

static new_signed_with_payer(instructions, payer, signing_keypairs, recent_blockhash)

Create a fully-signed transaction from a list of Instructions.

Parameters:
  • instructions (Sequence[Instruction]) – The instructions to include in the transaction message.

  • payer (Optional[Pubkey], optional) – The transaction fee payer.

  • signing_keypairs (Sequence[Keypair | Presigner]) – The keypairs that will sign the transaction.

  • recent_blockhash (Hash) – The id of a recent ledger entry.

Returns:

The signed transaction.

Return type:

Transaction

Example

>>> from solders.keypair import Keypair
>>> from solders.instruction import Instruction
>>> from solders.transaction import Transaction
>>> from solders.pubkey import Pubkey
>>> program_id = Pubkey.default()
>>> arbitrary_instruction_data = bytes([1])
>>> accounts = []
>>> instruction = Instruction(program_id, arbitrary_instruction_data, accounts)
>>> payer = Keypair()
>>> blockhash = Hash.default()  # replace with a real blockhash
>>> tx = Transaction.new_signed_with_payer([instruction], payer.pubkey(), [payer], blockhash);
static new_unsigned(message)

Create an unsigned transaction from a Message.

Parameters:

message (Message) – The transaction’s message.

Returns:

The unsigned transaction.

Return type:

Transaction

Example

>>> from typing import List
>>> from solders.message import Message
>>> from solders.keypair import Keypair
>>> from solders.pubkey import Pubkey
>>> from solders.instruction import Instruction, AccountMeta
>>> from solders.hash import Hash
>>> from solders.transaction import Transaction
>>> program_id = Pubkey.default()
>>> blockhash = Hash.default()  # replace with a real blockhash
>>> arbitrary_instruction_data = bytes([1])
>>> accounts: List[AccountMeta] = []
>>> instruction = Instruction(program_id, arbitrary_instruction_data, accounts)
>>> payer = Keypair()
>>> message = Message.new_with_blockhash([instruction], payer.pubkey(), blockhash)
>>> tx = Transaction.new_unsigned(message)
>>> tx.sign([payer], tx.message.recent_blockhash)
static new_with_compiled_instructions(from_keypairs, keys, recent_blockhash, program_ids, instructions)

Create a fully-signed transaction from pre-compiled instructions.

Parameters:
  • from_keypairs (Sequence[Keypair | Presigner]) – The keys used to sign the transaction.

  • keys (Sequence[Pubkey]) – The keys for the transaction. These are the program state instances or lamport recipient keys.

  • recent_blockhash (Hash) – The PoH hash.

  • program_ids (Sequence[Pubkey]) – The keys that identify programs used in the instruction vector.

  • instructions (Sequence[Instruction]) – Instructions that will be executed atomically.

Returns:

The signed transaction.

Return type:

Transaction

static new_with_payer(instructions, payer=None)

Create an unsigned transaction from a list of Instructions.

Parameters:
  • instructions (Sequence[Instruction]) – The instructions to include in the transaction message.

  • payer (Optional[Pubkey], optional) – The transaction fee payer. Defaults to None.

Returns:

The unsigned transaction.

Return type:

Transaction

Example

>>> from solders.keypair import Keypair
>>> from solders.instruction import Instruction
>>> from solders.transaction import Transaction
>>> from solders.pubkey import Pubkey
>>> program_id = Pubkey.default()
>>> arbitrary_instruction_data = bytes([1])
>>> accounts = []
>>> instruction = Instruction(program_id, arbitrary_instruction_data, accounts)
>>> payer = Keypair()
>>> tx = Transaction.new_with_payer([instruction], payer.pubkey())
partial_sign(keypairs, recent_blockhash)

Sign the transaction with a subset of required keys, returning any errors.

Unlike Transaction.sign(), this method does not require all keypairs to be provided, allowing a transaction to be signed in multiple steps.

It is permitted to sign a transaction with the same keypair multiple times.

If recent_blockhash is different than recorded in the transaction message’s recent_blockhash field, then the message’s recent_blockhash will be updated to the provided recent_blockhash, and any prior signatures will be cleared.

Errors:

Signing will fail if

  • The transaction’s Message is malformed such that the number of required signatures recorded in its header (num_required_signatures) is greater than the length of its account keys (account_keys).

  • Any of the provided signers in keypairs is not a required signer of the message.

  • Any of the signers is a Presigner, and its provided signature is incorrect.

Parameters:
  • keypairs (Sequence[Keypair | Presigner]) – The signers for the transaction.

  • recent_blockhash (Hash) – The id of a recent ledger entry.

static populate(message, signatures)

Create a fully-signed transaction from a message and its signatures.

Parameters:
  • message (Message) – The transaction message.

  • signatures (Sequence[Signature]) – The message’s signatures.

Returns:

The signed transaction.

Return type:

Message

Example

>>> from solders.keypair import Keypair
>>> from solders.instruction import Instruction
>>> from solders.transaction import Transaction
>>> from solders.pubkey import Pubkey
>>> program_id = Pubkey.default()
>>> arbitrary_instruction_data = bytes([1])
>>> accounts = []
>>> instruction = Instruction(program_id, arbitrary_instruction_data, accounts)
>>> payer = Keypair()
>>> blockhash = Hash.default()  # replace with a real blockhash
>>> tx = Transaction.new_signed_with_payer([instruction], payer.pubkey(), [payer], blockhash);
>>> assert tx == Transaction.populate(tx.message, tx.signatures)
replace_signatures(signers)

Replace all the signatures and pubkeys.

Parameters:

signers (Sequence[Tuple[Pubkey, Signature]]) – The replacement pubkeys and signatures.

sanitize()

Sanity checks the Transaction properties.

sign(keypairs, recent_blockhash)

Sign the transaction, returning any errors.

This method fully signs a transaction with all required signers, which must be present in the keypairs list. To sign with only some of the required signers, use Transaction.partial_sign().

If recent_blockhash is different than recorded in the transaction message’s recent_blockhash] field, then the message’s recent_blockhash will be updated to the provided recent_blockhash, and any prior signatures will be cleared.

Errors:

Signing will fail if some required signers are not provided in keypairs; or, if the transaction has previously been partially signed, some of the remaining required signers are not provided in keypairs. In other words, the transaction must be fully signed as a result of calling this function.

Signing will fail for any of the reasons described in the documentation for Transaction.partial_sign().

Parameters:
  • keypairs (Sequence[Keypair | Presigner]) – The signers for the transaction.

  • recent_blockhash (Hash) – The id of a recent ledger entry.

signatures

A set of signatures of a serialized Message, signed by the first keys of the message’s account_keys, where the number of signatures is equal to num_required_signatures of the Message’s MessageHeader.

Type:

list[Signature]

signer_key(instruction_index, accounts_index)

Get the Pubkey of a signing account required by one of the instructions in the transaction.

The transaction does not need to be signed for this function to return a signing account’s pubkey.

Returns None if the indexed account is not required to sign the transaction. Returns None if the [signatures] field does not contain enough elements to hold a signature for the indexed account (this should only be possible if Transaction has been manually constructed).

Returns None if instruction_index is greater than or equal to the number of instructions in the transaction; or if accounts_index is greater than or equal to the number of accounts in the instruction.

Parameters:
  • instruction_index (int) – index into the instructions vector of the transaction’s message.

  • account_index (int) – index into the acounts list of the message’s compiled_instructions.

Returns:

The account key.

Return type:

Optional[Pubkey]

to_json()

Convert to a JSON string.

uses_durable_nonce()

See https://docs.rs/solana-sdk/latest/solana_sdk/transaction/fn.uses_durable_nonce.html

verify()

Verifies that all signers have signed the message.

Raises:

TransactionError – if the check fails.

verify_and_hash_message()

Verify the transaction and hash its message.

Returns:

The blake3 hash of the message.

Return type:

Hash

Raises:

TransactionError – if the check fails.

verify_with_results()

Verifies that all signers have signed the message.

Returns:

a list with the length of required signatures, where each element is either True if that signer has signed, or False if not.

Return type:

list[bool]

class VersionedTransaction

An atomic transaction

The __init__ method signs a versioned message to create a signed transaction.

Parameters:
static default()

Return a new default transaction.

Returns:

The default transaction.

Return type:

VersionedTransaction

static from_bytes(data)

Deserialize from bytes.

Parameters:

data (bytes) – the serialized object.

Returns: the deserialized object.

static from_json(raw)

Build from a JSON string.

static from_legacy(tx)

Convert a legacy transaction to a VersionedTransaction.

Returns:

The versioned tx.

Return type:

VersionedTransaction

into_legacy_transaction()

Returns a legacy transaction if the transaction message is legacy.

Returns:

The legacy transaction.

Return type:

Optional[Transaction]

message

The transaction message.

Type:

Message | MessageV0

static populate(message, signatures)

Create a fully-signed transaction from a message and its signatures.

Parameters:
  • message (Message | MessageV0) – The transaction message.

  • signatures (Sequence[Signature]) – The message’s signatures.

Returns:

The signed transaction.

Return type:

Transaction

Example

>>> from solders.pubkey import Pubkey
>>> from solders.instruction import Instruction
>>> from solders.message import MessageV0
>>> from solders.hash import Hash
>>> from solders.keypair import Keypair
>>> from solders.transaction import VersionedTransaction
>>> payer = Keypair()
>>> program_id = Pubkey.default()
>>> instructions = [Instruction(program_id, bytes([]), [])]
>>> recent_blockhash = Hash.new_unique()
>>> message = MessageV0.try_compile(payer.pubkey(), instructions, [], recent_blockhash)
>>> tx = VersionedTransaction(message, [payer])
>>> assert VersionedTransaction.populate(message, tx.signatures) == tx
sanitize()

Sanity checks the Transaction properties.

signatures

The transaction signatures.

Type:

List[Signature]

to_json()

Convert to a JSON string.

uses_durable_nonce()

Returns true if transaction begins with a valid advance nonce instruction.

Returns:

bool

verify_and_hash_message()

Verify the transaction and hash its message

verify_with_results()

Verify the transaction and return a list of verification results

version()

Returns the version of the transaction.

Returns:

Transaction version.

Return type:

Legacy | int

class Legacy

Transaction version type that serializes to the string “legacy”

Legacy = Legacy.Legacy
exception SanitizeError

Raised when an error is encountered during transaction sanitization.

exception TransactionError

Umbrella error for the Transaction object.