Transaction
- class Legacy
Transaction version type that serializes to the string “legacy”
- Legacy = Legacy.Legacy
- class Transaction
An atomically-commited sequence of instructions.
While
Instructions are the basic unit of computation in Solana, they are submitted by clients inTransactions 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 theMessageobject, the first account is always the fee-payer, so if the caller has knowledge that the first account of the constructed transaction’sMessageis 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 aMessage.- Parameters:
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
instructionsvector of the transaction’smessage.- Returns:
The instruction data.
- Return type:
bytes
- static default()
Return a new default transaction.
- Returns:
The default transaction.
- Return type:
- static from_bytes(data)
Deserialize a serialized
Transactionobject.- Parameters:
data (bytes) – the serialized
Transaction.- Returns:
the deserialized
Transaction.- Return type:
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_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
Pubkeyof an account required by one of the instructions in the transaction.Returns
Noneif 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
instructionsvector of the transaction’smessage.account_index (int) – index into the
acountslist of the message’scompiled_instructions.
- Returns:
The account key.
- Return type:
Optional[Pubkey]
- 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:
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:
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:
- static new_with_payer(instructions, payer)
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:
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_blockhashis different than recorded in the transaction message’srecent_blockhashfield, then the message’srecent_blockhashwill be updated to the providedrecent_blockhash, and any prior signatures will be cleared.Errors:
Signing will fail if
The transaction’s
Messageis 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
keypairsis not a required signer of the message.Any of the signers is a
Presigner, and its provided signature is incorrect.
- static populate(message, signatures)
Create a fully-signed transaction from a message and its signatures.
- Parameters:
- Returns:
The signed transaction.
- Return type:
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.
- 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
keypairslist. To sign with only some of the required signers, useTransaction.partial_sign().If
recent_blockhashis different than recorded in the transaction message’srecent_blockhash] field, then the message’srecent_blockhashwill be updated to the providedrecent_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 inkeypairs. 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().
- signatures
A set of signatures of a serialized
Message, signed by the first keys of the message’saccount_keys, where the number of signatures is equal tonum_required_signaturesof the Message’sMessageHeader.- Type:
list[Signature]
- signer_key(instruction_index, accounts_index)
Get the
Pubkeyof 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
Noneif the indexed account is not required to sign the transaction. ReturnsNoneif 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
instructionsvector of the transaction’smessage.account_index (int) – index into the
acountslist of the message’scompiled_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:
- 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
Trueif that signer has signed, orFalseif 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:
- 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:
- into_legacy_transaction()
Returns a legacy transaction if the transaction message is legacy.
- Returns:
The legacy transaction.
- Return type:
Optional[Transaction]
- static populate(message, signatures)
Create a fully-signed transaction from a message and its signatures.
- Parameters:
- Returns:
The signed transaction.
- Return type:
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.
- 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
- exception SanitizeError
Raised when an error is encountered during transaction sanitization.
- exception TransactionError
Umbrella error for the
Transactionobject.