pub struct Message {
    pub header: MessageHeader,
    pub account_keys: Vec<Pubkey>,
    pub recent_blockhash: Hash,
    pub instructions: Vec<CompiledInstruction>,
}
Expand description

A Solana transaction message (legacy).

See the message module documentation for further description.

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§

§header: MessageHeader

The message header, identifying signed and read-only account_keys.

§account_keys: Vec<Pubkey>

All the account keys used by this transaction.

§recent_blockhash: Hash

The id of a recent ledger entry.

§instructions: Vec<CompiledInstruction>

Programs that will be executed in sequence and committed in one atomic transaction if all succeed.

Implementations§

§

impl Message

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

Create a new Message.

§Examples

This example uses the solana_sdk, solana_rpc_client and anyhow crates.

use anyhow::Result;
use borsh::{BorshSerialize, BorshDeserialize};
use solana_rpc_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(())
}

pub fn new_with_blockhash( instructions: &[Instruction], payer: Option<&Pubkey>, blockhash: &Hash ) -> Message

Create a new message while setting the blockhash.

§Examples

This example uses the solana_sdk, solana_rpc_client and anyhow crates.

use anyhow::Result;
use borsh::{BorshSerialize, BorshDeserialize};
use solana_rpc_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 message = Message::new_with_blockhash(
        &[instruction],
        Some(&payer.pubkey()),
        &blockhash,
    );

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

    Ok(())
}

pub fn new_with_nonce( instructions: Vec<Instruction>, payer: Option<&Pubkey>, nonce_account_pubkey: &Pubkey, nonce_authority_pubkey: &Pubkey ) -> Message

Create a new message for a nonced transaction.

In this type of transaction, the blockhash is replaced with a durable transaction nonce, allowing for extended time to pass between the transaction’s signing and submission to the blockchain.

§Examples

This example uses the solana_sdk, solana_rpc_client and anyhow crates.

use anyhow::Result;
use borsh::{BorshSerialize, BorshDeserialize};
use solana_rpc_client::rpc_client::RpcClient;
use solana_sdk::{
    hash::Hash,
    instruction::Instruction,
    message::Message,
    nonce,
    pubkey::Pubkey,
    signature::{Keypair, Signer},
    system_instruction,
    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 },
}

// Create a nonced transaction for later signing and submission,
// returning it and the nonce account's pubkey.
fn create_offline_initialize_tx(
    client: &RpcClient,
    program_id: Pubkey,
    payer: &Keypair
) -> Result<(Transaction, Pubkey)> {

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

    // This will create a nonce account and assign authority to the
    // payer so they can sign to advance the nonce and withdraw its rent.
    let nonce_account = make_nonce_account(client, payer)?;

    let mut message = Message::new_with_nonce(
        vec![bank_instruction],
        Some(&payer.pubkey()),
        &nonce_account,
        &payer.pubkey()
    );

    // This transaction will need to be signed later, using the blockhash
    // stored in the nonce account.
    let tx = Transaction::new_unsigned(message);

    Ok((tx, nonce_account))
}

fn make_nonce_account(client: &RpcClient, payer: &Keypair)
    -> Result<Pubkey>
{
    let nonce_account_address = Keypair::new();
    let nonce_account_size = nonce::State::size();
    let nonce_rent = client.get_minimum_balance_for_rent_exemption(nonce_account_size)?;

    // Assigning the nonce authority to the payer so they can sign for the withdrawal,
    // and we can throw away the nonce address secret key.
    let create_nonce_instr = system_instruction::create_nonce_account(
        &payer.pubkey(),
        &nonce_account_address.pubkey(),
        &payer.pubkey(),
        nonce_rent,
    );

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

    Ok(nonce_account_address.pubkey())
}

pub fn new_with_compiled_instructions( num_required_signatures: u8, num_readonly_signed_accounts: u8, num_readonly_unsigned_accounts: u8, account_keys: Vec<Pubkey>, recent_blockhash: Hash, instructions: Vec<CompiledInstruction> ) -> Message

pub fn hash(&self) -> Hash

Compute the blake3 hash of this transaction’s message.

pub fn hash_raw_message(message_bytes: &[u8]) -> Hash

Compute the blake3 hash of a raw transaction message.

pub fn compile_instruction(&self, ix: &Instruction) -> CompiledInstruction

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

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

pub fn program_index(&self, instruction_index: usize) -> Option<usize>

pub fn program_ids(&self) -> Vec<&Pubkey>

pub fn is_key_passed_to_program(&self, key_index: usize) -> bool

pub fn is_key_called_as_program(&self, key_index: usize) -> bool

pub fn is_non_loader_key(&self, key_index: usize) -> bool

pub fn program_position(&self, index: usize) -> Option<usize>

pub fn maybe_executable(&self, i: usize) -> bool

pub fn demote_program_id(&self, i: usize) -> bool

pub fn is_writable(&self, i: usize) -> bool

pub fn is_signer(&self, i: usize) -> bool

pub fn get_account_keys_by_lock_type(&self) -> (Vec<&Pubkey>, Vec<&Pubkey>)

👎Deprecated

pub fn deserialize_instruction( index: usize, data: &[u8] ) -> Result<Instruction, SanitizeError>

👎Deprecated

pub fn signer_keys(&self) -> Vec<&Pubkey>

pub fn has_duplicates(&self) -> bool

Returns true if account_keys has any duplicate keys.

pub fn is_upgradeable_loader_present(&self) -> bool

Returns true if any account is the BPF upgradeable loader.

Trait Implementations§

§

impl Clone for Message

§

fn clone(&self) -> Message

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
§

impl Debug for Message

§

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

Formats the value using the given formatter. Read more
§

impl Default for Message

§

fn default() -> Message

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

impl<'de> Deserialize<'de> for Message

§

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

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

impl Encodable for Message

§

type Encoded = UiMessage

§

fn encode( &self, encoding: UiTransactionEncoding ) -> <Message as Encodable>::Encoded

§

impl ExtractMemos for Message

§

fn extract_memos(&self) -> Vec<String>

§

impl FromWasmAbi for Message

§

type Abi = u32

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

unsafe fn from_abi(js: u32) -> Message

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

impl IntoWasmAbi for Message

§

type Abi = u32

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

fn into_abi(self) -> u32

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

impl LongRefFromWasmAbi for Message

§

type Abi = u32

Same as RefFromWasmAbi::Abi
§

type Anchor = Ref<'static, Message>

Same as RefFromWasmAbi::Anchor
§

unsafe fn long_ref_from_abi( js: <Message as LongRefFromWasmAbi>::Abi ) -> <Message as LongRefFromWasmAbi>::Anchor

Same as RefFromWasmAbi::ref_from_abi
§

impl OptionFromWasmAbi for Message

§

fn is_none(abi: &<Message as FromWasmAbi>::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.
§

impl OptionIntoWasmAbi for Message

§

fn none() -> <Message as IntoWasmAbi>::Abi

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

impl PartialEq for Message

§

fn eq(&self, other: &Message) -> 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.
§

impl RefFromWasmAbi for Message

§

type Abi = u32

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

type Anchor = Ref<'static, Message>

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.
§

unsafe fn ref_from_abi( js: <Message as RefFromWasmAbi>::Abi ) -> <Message as RefFromWasmAbi>::Anchor

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

impl RefMutFromWasmAbi for Message

§

type Abi = u32

Same as RefFromWasmAbi::Abi
§

type Anchor = RefMut<'static, Message>

Same as RefFromWasmAbi::Anchor
§

unsafe fn ref_mut_from_abi( js: <Message as RefMutFromWasmAbi>::Abi ) -> <Message as RefMutFromWasmAbi>::Anchor

Same as RefFromWasmAbi::ref_from_abi
§

impl Sanitize for Message

§

fn sanitize(&self) -> Result<(), SanitizeError>

§

impl Serialize for Message

§

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

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

impl TryFromJsValue for Message

§

type Error = JsValue

The type returned in the event of a conversion error.
§

fn try_from_js_value( value: JsValue ) -> Result<Message, <Message as TryFromJsValue>::Error>

Performs the conversion.
§

impl VectorFromWasmAbi for Message

§

impl VectorIntoWasmAbi for Message

§

impl Eq for Message

§

impl SerializableMessage for Message

§

impl StructuralPartialEq for Message

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32 ) -> TaggedParser<'a, Implicit, Self, E>

source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

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

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 = _

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 T
where 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 for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where 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 T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
§

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

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

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