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

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

Overview

Instruction for transferring SOL.

This instruction is used to transfer SOL from one account to another.

Examples:

Build a Transfer instruction

instruction = Solace::Instructions::SystemProgram::TransferInstruction.build(
  lamports: 100,
  to_index: 1,
  from_index: 2,
  program_index: 3
)

Since:

  • 0.0.2

Constant Summary collapse

INSTRUCTION_ID =

Instruction ID for System Transfer

Since:

  • 0.0.2

[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

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:

Since:

  • 0.0.2



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/solace/instructions/system_program/transfer_instruction.rb', line 30

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

Since:

  • 0.0.2



51
52
53
54
# File 'lib/solace/instructions/system_program/transfer_instruction.rb', line 51

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