Instruction

class AccountMeta

Describes a single account read or written by a program during instruction execution.

When constructing an Instruction, a list of all accounts that may be read or written during the execution of that instruction must be supplied. Any account that may be mutated by the program during execution, either its data or metadata such as held lamports, must be writable.

Note that because the Solana runtime schedules parallel transaction execution around which accounts are writable, care should be taken that only accounts which actually may be mutated are specified as writable.

Parameters:
  • pubkey (Pubkey) – An account’s public key.

  • is_signer (bool) – True if an Instruction requires a Transaction signature matching pubkey.

  • is_writable (bool) – True if the account data or metadata may be mutated during program execution.

Example

>>> from solders.pubkey import Pubkey
>>> from solders.instruction import AccountMeta, Instruction
>>> from_pubkey = Pubkey.new_unique()
>>> to_pubkey = Pubkey.new_unique()
>>> program_id = Pubkey.new_unique()
>>> instruction_data = bytes([1])
>>> accs = [AccountMeta(from_pubkey, is_signer=True, is_writable=True), AccountMeta(to_pubkey, is_signer=True, is_writable=True)]
>>> instruction = Instruction(program_id, instruction_data, accs)
static from_bytes(data)

Deserialize a serialized AccountMeta object.

Parameters:

data (bytes) – the serialized AccountMeta.

Returns:

the deserialized AccountMeta.

Return type:

AccountMeta

static from_json(raw)

Build from a JSON string.

is_signer
is_writable
pubkey
to_json()

Convert to a JSON string.

class Instruction

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.

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.

Specifying Account Metadata

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.

Parameters:
  • program_id (Pubkey) – Pubkey of the program that executes this instruction.

  • data (bytes) – Opaque data passed to the program for its own interpretation.

  • accounts (list[AccountMeta]) – Metadata describing accounts that should be passed to the program.

accounts
data
static from_bytes(data)

Deserialize a serialized Instruction object.

Parameters:

data (bytes) – the serialized Instruction.

Returns:

the deserialized Instruction.

Return type:

Instruction

Example

>>> from solders.pubkey import Pubkey
>>> from solders.instruction import AccountMeta, Instruction
>>> from_pubkey = Pubkey.new_unique()
>>> to_pubkey = Pubkey.new_unique()
>>> program_id = Pubkey.new_unique()
>>> instruction_data = bytes([1])
>>> accounts = [AccountMeta(from_pubkey, is_signer=True, is_writable=True), AccountMeta(to_pubkey, is_signer=True, is_writable=True),]
>>> instruction = Instruction(program_id, instruction_data, accounts)
>>> serialized = bytes(instruction)
>>> assert Instruction.from_bytes(serialized) == instruction
static from_json(raw)

Build from a JSON string.

program_id
to_json()

Convert to a JSON string.

class CompiledInstruction

A compact encoding of an instruction.

A CompiledInstruction is a component of a multi-instruction Message, which is the core of a Solana transaction. It is created during the construction of Message. Most users will not interact with it directly.

Parameters:
  • program_id_index (int) – Index into the transaction keys array indicating the program account that executes this instruction.

  • data (bytes) – The program input data.

  • accounts (bytes) – Ordered indices into the transaction keys array indicating which accounts to pass to the program.

accounts
data
static from_bytes(data)

Deserialize a serialized CompiledInstruction object.

Parameters:

data (bytes) – the serialized CompiledInstruction.

Returns:

The deserialized CompiledInstruction.

Return type:

CompiledInstruction

static from_json(raw)

Build from a JSON string.

program_id(program_ids)

Return the pubkey of the program that executes this instruction.

Returns:

The program ID.

Return type:

Pubkey

program_id_index
to_json()

Convert to a JSON string.