Struct switchboard_solana::prelude::Instruction
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 Instruction
s 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§
§impl Instruction
impl Instruction
pub fn new_with_borsh<T>(
program_id: Pubkey,
data: &T,
accounts: Vec<AccountMeta>
) -> Instructionwhere
T: BorshSerialize,
pub fn new_with_borsh<T>(
program_id: Pubkey,
data: &T,
accounts: Vec<AccountMeta>
) -> Instructionwhere
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),
],
)
}
pub fn new_with_bincode<T>(
program_id: Pubkey,
data: &T,
accounts: Vec<AccountMeta>
) -> Instructionwhere
T: Serialize,
pub fn new_with_bincode<T>(
program_id: Pubkey,
data: &T,
accounts: Vec<AccountMeta>
) -> Instructionwhere
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),
],
)
}
pub fn new_with_bytes(
program_id: Pubkey,
data: &[u8],
accounts: Vec<AccountMeta>
) -> Instruction
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),
],
))
}
pub fn new<T>(
program_id: Pubkey,
data: &T,
accounts: Vec<AccountMeta>
) -> Instructionwhere
T: Serialize,
Instruction::new_with_borsh
Trait Implementations§
§impl Clone for Instruction
impl Clone for Instruction
§fn clone(&self) -> Instruction
fn clone(&self) -> Instruction
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more§impl Debug for Instruction
impl Debug for Instruction
§impl<'de> Deserialize<'de> for Instruction
impl<'de> Deserialize<'de> for Instruction
§fn deserialize<__D>(
__deserializer: __D
) -> Result<Instruction, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D
) -> Result<Instruction, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
§impl FromWasmAbi for Instruction
impl FromWasmAbi for Instruction
§impl IntoWasmAbi for Instruction
impl IntoWasmAbi for Instruction
§impl LongRefFromWasmAbi for Instruction
impl LongRefFromWasmAbi for Instruction
§type Anchor = Ref<'static, Instruction>
type Anchor = Ref<'static, Instruction>
RefFromWasmAbi::Anchor
§unsafe fn long_ref_from_abi(
js: <Instruction as LongRefFromWasmAbi>::Abi
) -> <Instruction as LongRefFromWasmAbi>::Anchor
unsafe fn long_ref_from_abi( js: <Instruction as LongRefFromWasmAbi>::Abi ) -> <Instruction as LongRefFromWasmAbi>::Anchor
RefFromWasmAbi::ref_from_abi
§impl OptionFromWasmAbi for Instruction
impl OptionFromWasmAbi for Instruction
§fn is_none(abi: &<Instruction as FromWasmAbi>::Abi) -> bool
fn is_none(abi: &<Instruction as FromWasmAbi>::Abi) -> bool
None
, and otherwise it will be passed to
FromWasmAbi
.§impl OptionIntoWasmAbi for Instruction
impl OptionIntoWasmAbi for Instruction
§fn none() -> <Instruction as IntoWasmAbi>::Abi
fn none() -> <Instruction as IntoWasmAbi>::Abi
None
branch of this option. Read more§impl PartialEq for Instruction
impl PartialEq for Instruction
§fn eq(&self, other: &Instruction) -> bool
fn eq(&self, other: &Instruction) -> bool
self
and other
values to be equal, and is used
by ==
.§impl RefFromWasmAbi for Instruction
impl RefFromWasmAbi for Instruction
§type Anchor = Ref<'static, Instruction>
type Anchor = Ref<'static, Instruction>
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: <Instruction as RefFromWasmAbi>::Abi
) -> <Instruction as RefFromWasmAbi>::Anchor
unsafe fn ref_from_abi( js: <Instruction as RefFromWasmAbi>::Abi ) -> <Instruction as RefFromWasmAbi>::Anchor
§impl RefMutFromWasmAbi for Instruction
impl RefMutFromWasmAbi for Instruction
§type Anchor = RefMut<'static, Instruction>
type Anchor = RefMut<'static, Instruction>
RefFromWasmAbi::Anchor
§unsafe fn ref_mut_from_abi(
js: <Instruction as RefMutFromWasmAbi>::Abi
) -> <Instruction as RefMutFromWasmAbi>::Anchor
unsafe fn ref_mut_from_abi( js: <Instruction as RefMutFromWasmAbi>::Abi ) -> <Instruction as RefMutFromWasmAbi>::Anchor
RefFromWasmAbi::ref_from_abi
§impl Serialize for Instruction
impl Serialize for Instruction
§fn serialize<__S>(
&self,
__serializer: __S
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
§impl TryFromJsValue for Instruction
impl TryFromJsValue for Instruction
§fn try_from_js_value(
value: JsValue
) -> Result<Instruction, <Instruction as TryFromJsValue>::Error>
fn try_from_js_value( value: JsValue ) -> Result<Instruction, <Instruction as TryFromJsValue>::Error>
§impl VectorFromWasmAbi for Instruction
impl VectorFromWasmAbi for Instruction
type Abi = <Box<[JsValue]> as FromWasmAbi>::Abi
unsafe fn vector_from_abi( js: <Instruction as VectorFromWasmAbi>::Abi ) -> Box<[Instruction]>
§impl VectorIntoWasmAbi for Instruction
impl VectorIntoWasmAbi for Instruction
type Abi = <Box<[JsValue]> as IntoWasmAbi>::Abi
fn vector_into_abi( vector: Box<[Instruction]> ) -> <Instruction as VectorIntoWasmAbi>::Abi
impl Eq for Instruction
impl StructuralPartialEq for Instruction
Auto Trait Implementations§
impl RefUnwindSafe for Instruction
impl Send for Instruction
impl Sync for Instruction
impl Unpin for Instruction
impl UnwindSafe for Instruction
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
§impl<T> Pointable for T
impl<T> Pointable for T
source§impl<T> ReturnWasmAbi for Twhere
T: IntoWasmAbi,
impl<T> ReturnWasmAbi for Twhere
T: IntoWasmAbi,
§type Abi = <T as IntoWasmAbi>::Abi
type Abi = <T as IntoWasmAbi>::Abi
IntoWasmAbi::Abi
source§fn return_abi(self) -> <T as ReturnWasmAbi>::Abi
fn return_abi(self) -> <T as ReturnWasmAbi>::Abi
IntoWasmAbi::into_abi
, except that it may throw and never
return in the case of Err
.