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: BorshSerialize>( program_id: Pubkey, data: &T, accounts: Vec<AccountMeta> ) -> Self

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 prefered 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: Serialize>( program_id: Pubkey, data: &T, accounts: Vec<AccountMeta> ) -> Self

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> ) -> Self

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> {
    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),
        ],
   ))
}
source

pub fn new<T: Serialize>( program_id: Pubkey, data: &T, accounts: Vec<AccountMeta> ) -> Self

👎Deprecated since 1.6.0: Please use another Instruction constructor instead, such as Instruction::new_with_borsh

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

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

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

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<Instruction> for JsValue

source§

fn from(value: Instruction) -> Self

Converts to this type from the input type.
source§

impl FromWasmAbi for Instruction

§

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 Instruction

§

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 Instruction

§

type Abi = u32

Same as RefFromWasmAbi::Abi
§

type Anchor = Ref<'static, Instruction>

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 Instruction

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 Instruction

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<Instruction> for Instruction

source§

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

§

type Abi = u32

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

type Anchor = Ref<'static, Instruction>

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 Instruction

§

type Abi = u32

Same as RefFromWasmAbi::Abi
§

type Anchor = RefMut<'static, Instruction>

Same as RefFromWasmAbi::Anchor
source§

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

Same as RefFromWasmAbi::ref_from_abi
source§

impl Serialize for Instruction

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 Instruction

source§

impl Eq for Instruction

source§

impl StructuralEq for Instruction

source§

impl StructuralPartialEq for Instruction

Auto Trait Implementations§

Blanket Implementations§

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> AbiEnumVisitor for Twhere T: Serialize + AbiExample + ?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>,