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

Create a new Message.

Examples

This example uses the solana_sdk, solana_client and anyhow crates.

use anyhow::Result;
use borsh::{BorshSerialize, BorshDeserialize};
use solana_client::rpc_client::RpcClient;
use solana_sdk::{
    instruction::Instruction,
    message::Message,
    pubkey::Pubkey,
    signature::Keypair,
    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(())
}

Create a new message while setting the blockhash.

Examples

This example uses the solana_sdk, solana_client and anyhow crates.

use anyhow::Result;
use borsh::{BorshSerialize, BorshDeserialize};
use solana_client::rpc_client::RpcClient;
use solana_sdk::{
    instruction::Instruction,
    message::Message,
    pubkey::Pubkey,
    signature::Keypair,
    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(())
}

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_client and anyhow crates.

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

Compute the blake3 hash of this transaction’s message.

Compute the blake3 hash of a raw transaction message.

👎 Deprecated
👎 Deprecated

Returns true if account_keys has any duplicate keys.

Returns true if any account is the BPF upgradeable loader.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Deserialize this value from the given Serde deserializer. Read more

Converts to this type from the input type.

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

Recover a Self from Self::Abi. Read more

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

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

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

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

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

The wasm ABI type references to Self are recovered from.

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. Read more

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

Same as RefFromWasmAbi::Abi

Same as RefFromWasmAbi::Anchor

Same as RefFromWasmAbi::ref_from_abi

Serialize this value into the given Serde serializer. Read more

The type returned in the event of a conversion error.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

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

Same as IntoWasmAbi::Abi

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

Should always be Self

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.