pub struct Transaction {
    pub signatures: Vec<Signature>,
    pub message: Message,
}
Expand description

An atomically-commited sequence of instructions.

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

See the 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 structure, 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.

Fields§

§signatures: Vec<Signature>

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.

§message: Message

The message to sign.

Implementations§

source§

impl Transaction

source

pub fn new_unsigned(message: Message) -> Self

Create an unsigned transaction from a Message.

Examples

This example uses the safecoin_client and anyhow crates.

use anyhow::Result;
use borsh::{BorshSerialize, BorshDeserialize};
use safecoin_client::rpc_client::RpcClient;
use solana_sdk::{
     instruction::Instruction,
     message::Message,
     pubkey::Pubkey,
     signature::{Keypair, Signer},
     transaction::Transaction,
};

// A custom program instruction. This would typically be defined in
// another crate so it can be shared between the on-chain program and
// the client.
#[derive(BorshSerialize, BorshDeserialize)]
enum BankInstruction {
    Initialize,
    Deposit { lamports: u64 },
    Withdraw { lamports: u64 },
}

fn send_initialize_tx(
    client: &RpcClient,
    program_id: Pubkey,
    payer: &Keypair
) -> Result<()> {

    let bank_instruction = BankInstruction::Initialize;

    let instruction = Instruction::new_with_borsh(
        program_id,
        &bank_instruction,
        vec![],
    );

    let message = Message::new(
        &[instruction],
        Some(&payer.pubkey()),
    );

    let mut tx = Transaction::new_unsigned(message);
    let blockhash = client.get_latest_blockhash()?;
    tx.sign(&[payer], blockhash);
    client.send_and_confirm_transaction(&tx)?;

    Ok(())
}
source

pub fn new<T: Signers>( from_keypairs: &T, message: Message, recent_blockhash: Hash ) -> Transaction

Create a fully-signed transaction from a Message.

Panics

Panics when signing fails. See Transaction::try_sign and Transaction::try_partial_sign for a full description of failure scenarios.

Examples

This example uses the safecoin_client and anyhow crates.

use anyhow::Result;
use borsh::{BorshSerialize, BorshDeserialize};
use safecoin_client::rpc_client::RpcClient;
use solana_sdk::{
     instruction::Instruction,
     message::Message,
     pubkey::Pubkey,
     signature::{Keypair, Signer},
     transaction::Transaction,
};

// A custom program instruction. This would typically be defined in
// another crate so it can be shared between the on-chain program and
// the client.
#[derive(BorshSerialize, BorshDeserialize)]
enum BankInstruction {
    Initialize,
    Deposit { lamports: u64 },
    Withdraw { lamports: u64 },
}

fn send_initialize_tx(
    client: &RpcClient,
    program_id: Pubkey,
    payer: &Keypair
) -> Result<()> {

    let bank_instruction = BankInstruction::Initialize;

    let instruction = Instruction::new_with_borsh(
        program_id,
        &bank_instruction,
        vec![],
    );

    let message = Message::new(
        &[instruction],
        Some(&payer.pubkey()),
    );

    let blockhash = client.get_latest_blockhash()?;
    let mut tx = Transaction::new(&[payer], message, blockhash);
    client.send_and_confirm_transaction(&tx)?;

    Ok(())
}
source

pub fn new_with_payer( instructions: &[Instruction], payer: Option<&Pubkey> ) -> Self

Create an unsigned transaction from a list of Instructions.

payer is the account responsible for paying the cost of executing the transaction. It is typically provided, but is optional in some cases. See the Transaction docs for more.

Examples

This example uses the safecoin_client and anyhow crates.

use anyhow::Result;
use borsh::{BorshSerialize, BorshDeserialize};
use safecoin_client::rpc_client::RpcClient;
use solana_sdk::{
     instruction::Instruction,
     message::Message,
     pubkey::Pubkey,
     signature::{Keypair, Signer},
     transaction::Transaction,
};

// A custom program instruction. This would typically be defined in
// another crate so it can be shared between the on-chain program and
// the client.
#[derive(BorshSerialize, BorshDeserialize)]
enum BankInstruction {
    Initialize,
    Deposit { lamports: u64 },
    Withdraw { lamports: u64 },
}

fn send_initialize_tx(
    client: &RpcClient,
    program_id: Pubkey,
    payer: &Keypair
) -> Result<()> {

    let bank_instruction = BankInstruction::Initialize;

    let instruction = Instruction::new_with_borsh(
        program_id,
        &bank_instruction,
        vec![],
    );

    let mut tx = Transaction::new_with_payer(&[instruction], Some(&payer.pubkey()));
    let blockhash = client.get_latest_blockhash()?;
    tx.sign(&[payer], blockhash);
    client.send_and_confirm_transaction(&tx)?;

    Ok(())
}
source

pub fn new_signed_with_payer<T: Signers>( instructions: &[Instruction], payer: Option<&Pubkey>, signing_keypairs: &T, recent_blockhash: Hash ) -> Self

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

payer is the account responsible for paying the cost of executing the transaction. It is typically provided, but is optional in some cases. See the Transaction docs for more.

Panics

Panics when signing fails. See Transaction::try_sign and Transaction::try_partial_sign for a full description of failure scenarios.

Examples

This example uses the safecoin_client and anyhow crates.

use anyhow::Result;
use borsh::{BorshSerialize, BorshDeserialize};
use safecoin_client::rpc_client::RpcClient;
use solana_sdk::{
     instruction::Instruction,
     message::Message,
     pubkey::Pubkey,
     signature::{Keypair, Signer},
     transaction::Transaction,
};

// A custom program instruction. This would typically be defined in
// another crate so it can be shared between the on-chain program and
// the client.
#[derive(BorshSerialize, BorshDeserialize)]
enum BankInstruction {
    Initialize,
    Deposit { lamports: u64 },
    Withdraw { lamports: u64 },
}

fn send_initialize_tx(
    client: &RpcClient,
    program_id: Pubkey,
    payer: &Keypair
) -> Result<()> {

    let bank_instruction = BankInstruction::Initialize;

    let instruction = Instruction::new_with_borsh(
        program_id,
        &bank_instruction,
        vec![],
    );

    let blockhash = client.get_latest_blockhash()?;
    let mut tx = Transaction::new_signed_with_payer(
        &[instruction],
        Some(&payer.pubkey()),
        &[payer],
        blockhash,
    );
    client.send_and_confirm_transaction(&tx)?;

    Ok(())
}
source

pub fn new_with_compiled_instructions<T: Signers>( from_keypairs: &T, keys: &[Pubkey], recent_blockhash: Hash, program_ids: Vec<Pubkey>, instructions: Vec<CompiledInstruction> ) -> Self

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

Arguments
  • from_keypairs - The keys used to sign the transaction.
  • keys - The keys for the transaction. These are the program state instances or lamport recipient keys.
  • recent_blockhash - The PoH hash.
  • program_ids - The keys that identify programs used in the instruction vector.
  • instructions - Instructions that will be executed atomically.
Panics

Panics when signing fails. See Transaction::try_sign and for a full description of failure conditions.

source

pub fn data(&self, instruction_index: usize) -> &[u8]

Get the data for an instruction at the given index.

The instruction_index corresponds to the instructions vector of the Transaction’s Message value.

Panics

Panics if instruction_index is greater than or equal to the number of instructions in the transaction.

source

pub fn key( &self, instruction_index: usize, accounts_index: usize ) -> Option<&Pubkey>

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

The instruction_index corresponds to the instructions vector of the Transaction’s Message value; and the account_index to the accounts vector of the message’s CompiledInstructions.

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.

source

pub fn signer_key( &self, instruction_index: usize, accounts_index: usize ) -> Option<&Pubkey>

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.

source

pub fn message(&self) -> &Message

Return the message containing all data that should be signed.

source

pub fn message_data(&self) -> Vec<u8>

Return the serialized message data to sign.

source

pub fn sign<T: Signers>(&mut self, keypairs: &T, recent_blockhash: Hash)

Sign the transaction.

This method fully signs a transaction with all required signers, which must be present in the keypairs slice. 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.

Panics

Panics when signing fails. Use Transaction::try_sign to handle the error. See the documentation for Transaction::try_sign for a full description of failure conditions.

Examples

This example uses the safecoin_client and anyhow crates.

use anyhow::Result;
use borsh::{BorshSerialize, BorshDeserialize};
use safecoin_client::rpc_client::RpcClient;
use solana_sdk::{
     instruction::Instruction,
     message::Message,
     pubkey::Pubkey,
     signature::{Keypair, Signer},
     transaction::Transaction,
};

// A custom program instruction. This would typically be defined in
// another crate so it can be shared between the on-chain program and
// the client.
#[derive(BorshSerialize, BorshDeserialize)]
enum BankInstruction {
    Initialize,
    Deposit { lamports: u64 },
    Withdraw { lamports: u64 },
}

fn send_initialize_tx(
    client: &RpcClient,
    program_id: Pubkey,
    payer: &Keypair
) -> Result<()> {

    let bank_instruction = BankInstruction::Initialize;

    let instruction = Instruction::new_with_borsh(
        program_id,
        &bank_instruction,
        vec![],
    );

    let mut tx = Transaction::new_with_payer(&[instruction], Some(&payer.pubkey()));
    let blockhash = client.get_latest_blockhash()?;
    tx.sign(&[payer], blockhash);
    client.send_and_confirm_transaction(&tx)?;

    Ok(())
}
source

pub fn partial_sign<T: Signers>(&mut self, keypairs: &T, recent_blockhash: Hash)

Sign the transaction with a subset of required keys.

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.

Panics

Panics when signing fails. Use Transaction::try_partial_sign to handle the error. See the documentation for Transaction::try_partial_sign for a full description of failure conditions.

source

pub fn partial_sign_unchecked<T: Signers>( &mut self, keypairs: &T, positions: Vec<usize>, recent_blockhash: Hash )

Sign the transaction with a subset of required keys.

This places each of the signatures created from keypairs in the corresponding position, as specified in the positions vector, in the transactions signatures field. It does not verify that the signature positions are correct.

Panics

Panics if signing fails. Use Transaction::try_partial_sign_unchecked to handle the error.

source

pub fn try_sign<T: Signers>( &mut self, keypairs: &T, recent_blockhash: Hash ) -> Result<(), SignerError>

Sign the transaction, returning any errors.

This method fully signs a transaction with all required signers, which must be present in the keypairs slice. To sign with only some of the required signers, use Transaction::try_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. The error is SignerError::NotEnoughSigners.

Signing will fail for any of the reasons described in the documentation for Transaction::try_partial_sign.

Examples

This example uses the safecoin_client and anyhow crates.

use anyhow::Result;
use borsh::{BorshSerialize, BorshDeserialize};
use safecoin_client::rpc_client::RpcClient;
use solana_sdk::{
     instruction::Instruction,
     message::Message,
     pubkey::Pubkey,
     signature::{Keypair, Signer},
     transaction::Transaction,
};

// A custom program instruction. This would typically be defined in
// another crate so it can be shared between the on-chain program and
// the client.
#[derive(BorshSerialize, BorshDeserialize)]
enum BankInstruction {
    Initialize,
    Deposit { lamports: u64 },
    Withdraw { lamports: u64 },
}

fn send_initialize_tx(
    client: &RpcClient,
    program_id: Pubkey,
    payer: &Keypair
) -> Result<()> {

    let bank_instruction = BankInstruction::Initialize;

    let instruction = Instruction::new_with_borsh(
        program_id,
        &bank_instruction,
        vec![],
    );

    let mut tx = Transaction::new_with_payer(&[instruction], Some(&payer.pubkey()));
    let blockhash = client.get_latest_blockhash()?;
    tx.try_sign(&[payer], blockhash)?;
    client.send_and_confirm_transaction(&tx)?;

    Ok(())
}
source

pub fn try_partial_sign<T: Signers>( &mut self, keypairs: &T, recent_blockhash: Hash ) -> Result<(), SignerError>

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

Unlike Transaction::try_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

See the documentation for the safecoin-remote-wallet crate for details on the operation of RemoteKeypair signers.

source

pub fn try_partial_sign_unchecked<T: Signers>( &mut self, keypairs: &T, positions: Vec<usize>, recent_blockhash: Hash ) -> Result<(), SignerError>

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

This places each of the signatures created from keypairs in the corresponding position, as specified in the positions vector, in the transactions signatures field. It does not verify that the signature positions are correct.

Errors

Returns an error if signing fails.

source

pub fn get_invalid_signature() -> Signature

Returns a signature that is not valid for signing this transaction.

source

pub fn verify(&self) -> Result<()>

Verifies that all signers have signed the message.

Errors

Returns TransactionError::SignatureFailure on error.

source

pub fn verify_and_hash_message(&self) -> Result<Hash>

Verify the transaction and hash its message.

Errors

Returns TransactionError::SignatureFailure on error.

source

pub fn verify_with_results(&self) -> Vec<bool>

Verifies that all signers have signed the message.

Returns a vector with the length of required signatures, where each element is either true if that signer has signed, or false if not.

source

pub fn verify_precompiles(&self, feature_set: &Arc<FeatureSet>) -> Result<()>

Verify the precompiled programs in this transaction.

source

pub fn get_signing_keypair_positions( &self, pubkeys: &[Pubkey] ) -> Result<Vec<Option<usize>>>

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

source

pub fn replace_signatures( &mut self, signers: &[(Pubkey, Signature)] ) -> Result<()>

Replace all the signatures and pubkeys.

source

pub fn is_signed(&self) -> bool

Trait Implementations§

source§

impl AbiExample for Transaction

source§

fn example() -> Self

source§

impl Clone for Transaction

source§

fn clone(&self) -> Transaction

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Transaction

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Transaction

source§

fn default() -> Transaction

Returns the “default value” for a type. Read more
source§

impl<'de> Deserialize<'de> for Transaction

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl From<Transaction> for JsValue

source§

fn from(value: Transaction) -> Self

Converts to this type from the input type.
source§

impl From<Transaction> for VersionedTransaction

source§

fn from(transaction: Transaction) -> Self

Converts to this type from the input type.
source§

impl FromWasmAbi for Transaction

§

type Abi = u32

The wasm ABI type that this converts from when coming back out from the ABI boundary.
source§

unsafe fn from_abi(js: u32) -> Self

Recover a Self from Self::Abi. Read more
source§

impl IntoWasmAbi for Transaction

§

type Abi = u32

The wasm ABI type that this converts into when crossing the ABI boundary.
source§

fn into_abi(self) -> u32

Convert self into Self::Abi so that it can be sent across the wasm ABI boundary.
source§

impl LongRefFromWasmAbi for Transaction

§

type Abi = u32

Same as RefFromWasmAbi::Abi
§

type Anchor = Ref<'static, Transaction>

Same as RefFromWasmAbi::Anchor
source§

unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor

Same as RefFromWasmAbi::ref_from_abi
source§

impl OptionFromWasmAbi for Transaction

source§

fn is_none(abi: &Self::Abi) -> bool

Tests whether the argument is a “none” instance. If so it will be deserialized as None, and otherwise it will be passed to FromWasmAbi.
source§

impl OptionIntoWasmAbi for Transaction

source§

fn none() -> Self::Abi

Returns an ABI instance indicating “none”, which JS will interpret as the None branch of this option. Read more
source§

impl PartialEq<Transaction> for Transaction

source§

fn eq(&self, other: &Transaction) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl RefFromWasmAbi for Transaction

§

type Abi = u32

The wasm ABI type references to Self are recovered from.
§

type Anchor = Ref<'static, Transaction>

The type that holds the reference to Self for the duration of the invocation of the function that has an &Self parameter. This is required to ensure that the lifetimes don’t persist beyond one function call, and so that they remain anonymous.
source§

unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor

Recover a Self::Anchor from Self::Abi. Read more
source§

impl RefMutFromWasmAbi for Transaction

§

type Abi = u32

Same as RefFromWasmAbi::Abi
§

type Anchor = RefMut<'static, Transaction>

Same as RefFromWasmAbi::Anchor
source§

unsafe fn ref_mut_from_abi(js: Self::Abi) -> Self::Anchor

Same as RefFromWasmAbi::ref_from_abi
source§

impl Sanitize for Transaction

source§

impl Serialize for Transaction

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl WasmDescribe for Transaction

source§

impl Eq for Transaction

source§

impl StructuralEq for Transaction

source§

impl StructuralPartialEq for Transaction

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> AbiEnumVisitor for Twhere T: Serialize + AbiExample + ?Sized,

source§

default fn visit_for_abi( &self, digester: &mut AbiDigester ) -> Result<AbiDigester, DigestError>

source§

impl<T> AbiEnumVisitor for Twhere T: Serialize + ?Sized,

source§

default fn visit_for_abi( &self, _digester: &mut AbiDigester ) -> Result<AbiDigester, DigestError>

source§

impl<T> AbiExample for T

source§

default fn example() -> T

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ReturnWasmAbi for Twhere T: IntoWasmAbi,

§

type Abi = <T as IntoWasmAbi>::Abi

Same as IntoWasmAbi::Abi
source§

fn return_abi(self) -> <T as ReturnWasmAbi>::Abi

Same as IntoWasmAbi::into_abi, except that it may throw and never return in the case of Err.
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,