Class: Solace::Instructions::SystemProgram::TransferInstruction

Inherits:
Base
  • Object
show all
Defined in:
lib/solace/instructions/system_program/transfer_instruction.rb

Overview

Service object for building a System Program transfer instruction

Constant Summary collapse

INSTRUCTION_ID =

Instruction ID for System Transfer

[2, 0, 0, 0].freeze

Class Method Summary collapse

Class Method Details

.build(lamports:, to_index:, from_index:, program_index: 2) ⇒ Solace::Instruction

Builds a Solace::Instruction for transferring SOL

System Program transfer instruction layout:

- 4 bytes: instruction index (0 for transfer)
- 8 bytes: amount (u64, little-endian)

Parameters:

  • lamports (Integer)

    Amount to transfer (in lamports)

  • to_index (Integer)

    Index of the recipient in the transaction’s accounts

  • from_index (Integer)

    Index of the sender in the transaction’s accounts

  • program_index (Integer) (defaults to: 2)

    Index of the program in the transaction’s accounts (default: 2)

Returns:



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/solace/instructions/system_program/transfer_instruction.rb', line 22

def self.build(
  lamports:,
  to_index:,
  from_index:,
  program_index: 2
)
  Instruction.new.tap do |ix|
    ix.program_index = program_index
    ix.accounts = [from_index, to_index]
    ix.data = data(lamports)
  end
end

.data(lamports) ⇒ Array

Instruction data for a transfer instruction

The BufferLayout is:

- [Instruction ID (4 bytes)]
- [Amount (8 bytes little-endian u64)]

Parameters:

  • lamports (Integer)

    Amount to transfer (in lamports)

Returns:

  • (Array)

    4-byte instruction ID + 8-byte amount



43
44
45
46
# File 'lib/solace/instructions/system_program/transfer_instruction.rb', line 43

def self.data(lamports)
  INSTRUCTION_ID +
    Utils::Codecs.encode_le_u64(lamports).bytes
end