solana_sdk::instruction

Struct Instruction

Source
pub struct Instruction {
    pub program_id: Pubkey,
    pub accounts: Vec<AccountMeta>,
    pub data: Vec<u8>,
}
Expand description

A directive for a single invocation of a Solana program.

An instruction specifies which program it is calling, which accounts it may read or modify, and additional data that serves as input to the program. One or more instructions are included in transactions submitted by Solana clients. Instructions are also used to describe cross-program invocations.

During execution, a program will receive a list of account data as one of its arguments, in the same order as specified during Instruction construction.

While Solana is agnostic to the format of the instruction data, it has built-in support for serialization via borsh and bincode.

§Specifying account metadata

When constructing an Instruction, a list of all accounts that may be read or written during the execution of that instruction must be supplied as AccountMeta values.

Any account whose data may be mutated by the program during execution must be specified as writable. During execution, writing to an account that was not specified as writable will cause the transaction to fail. Writing to an account that is not owned by the program will cause the transaction to fail.

Any account whose lamport balance may be mutated by the program during execution must be specified as writable. During execution, mutating the lamports of an account that was not specified as writable will cause the transaction to fail. While subtracting lamports from an account not owned by the program will cause the transaction to fail, adding lamports to any account is allowed, as long is it is mutable.

Accounts that are not read or written by the program may still be specified in an Instruction’s account list. These will affect scheduling of program execution by the runtime, but will otherwise be ignored.

When building a transaction, the Solana runtime coalesces all accounts used by all instructions in that transaction, along with accounts and permissions required by the runtime, into a single account list. Some accounts and account permissions required by the runtime to process a transaction are not required to be included in an Instructions account list. These include:

  • The program ID — it is a separate field of Instruction
  • The transaction’s fee-paying account — it is added during Message construction. A program may still require the fee payer as part of the account list if it directly references it.

Programs may require signatures from some accounts, in which case they should be specified as signers during Instruction construction. The program must still validate during execution that the account is a signer.

Fields§

§program_id: Pubkey

Pubkey of the program that executes this instruction.

§accounts: Vec<AccountMeta>

Metadata describing accounts that should be passed to the program.

§data: Vec<u8>

Opaque data passed to the program for its own interpretation.

Implementations§

Source§

impl Instruction

Source

pub fn new_with_borsh<T>( program_id: Pubkey, data: &T, accounts: Vec<AccountMeta>, ) -> Instruction
where T: BorshSerialize,

Create a new instruction from a value, encoded with borsh.

program_id is the address of the program that will execute the instruction. accounts contains a description of all accounts that may be accessed by the program.

Borsh serialization is often preferred over bincode as it has a stable specification and an implementation in JavaScript, neither of which are true of bincode.

§Examples
#[derive(BorshSerialize, BorshDeserialize)]
pub struct MyInstruction {
    pub lamports: u64,
}

pub fn create_instruction(
    program_id: &Pubkey,
    from: &Pubkey,
    to: &Pubkey,
    lamports: u64,
) -> Instruction {
    let instr = MyInstruction { lamports };

    Instruction::new_with_borsh(
        *program_id,
        &instr,
        vec![
            AccountMeta::new(*from, true),
            AccountMeta::new(*to, false),
        ],
   )
}
Source

pub fn new_with_bincode<T>( program_id: Pubkey, data: &T, accounts: Vec<AccountMeta>, ) -> Instruction
where T: Serialize,

Create a new instruction from a value, encoded with bincode.

program_id is the address of the program that will execute the instruction. accounts contains a description of all accounts that may be accessed by the program.

§Examples
#[derive(Serialize, Deserialize)]
pub struct MyInstruction {
    pub lamports: u64,
}

pub fn create_instruction(
    program_id: &Pubkey,
    from: &Pubkey,
    to: &Pubkey,
    lamports: u64,
) -> Instruction {
    let instr = MyInstruction { lamports };

    Instruction::new_with_bincode(
        *program_id,
        &instr,
        vec![
            AccountMeta::new(*from, true),
            AccountMeta::new(*to, false),
        ],
   )
}
Source

pub fn new_with_bytes( program_id: Pubkey, data: &[u8], accounts: Vec<AccountMeta>, ) -> Instruction

Create a new instruction from a byte slice.

program_id is the address of the program that will execute the instruction. accounts contains a description of all accounts that may be accessed by the program.

The caller is responsible for ensuring the correct encoding of data as expected by the callee program.

§Examples
#[derive(BorshSerialize, BorshDeserialize)]
pub struct MyInstruction {
    pub lamports: u64,
}

pub fn create_instruction(
    program_id: &Pubkey,
    from: &Pubkey,
    to: &Pubkey,
    lamports: u64,
) -> Result<Instruction, Error> {
    let instr = MyInstruction { lamports };

    let mut instr_in_bytes: Vec<u8> = Vec::new();
    instr.serialize(&mut instr_in_bytes)?;

    Ok(Instruction::new_with_bytes(
        *program_id,
        &instr_in_bytes,
        vec![
            AccountMeta::new(*from, true),
            AccountMeta::new(*to, false),
        ],
   ))
}

Trait Implementations§

Source§

impl Clone for Instruction

Source§

fn clone(&self) -> Instruction

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 Instruction

Source§

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

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

impl<'de> Deserialize<'de> for Instruction

Source§

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

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

impl PartialEq for Instruction

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Instruction

Source§

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

impl Eq for Instruction

Source§

impl StructuralPartialEq for Instruction

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

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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>,

Source§

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>,

Source§

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

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

Source§

fn vzip(self) -> V

Source§

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