Class: Solace::Instructions::SplToken::TransferInstruction

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

Overview

Instruction for transferring SPL tokens.

This instruction is used to transfer SPL tokens from one token account to another.

Examples:

Build a Transfer instruction

instruction = Solace::Instructions::SplToken::TransferInstruction.build(
  amount: 100,
  owner_index: 1,
  source_index: 2,
  destination_index: 3,
  program_index: 4
)

Since:

  • 0.0.2

Constant Summary collapse

INSTRUCTION_INDEX =

Since:

  • 0.0.2

[3].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#INSTRUCTION_INDEXObject (readonly)

Instruction index for SPL Token Program’s Transfer instruction.



23
# File 'lib/solace/instructions/spl_token/transfer_instruction.rb', line 23

INSTRUCTION_INDEX = [3].freeze

Class Method Details

.build(amount:, owner_index:, source_index:, destination_index:, program_index:) ⇒ Solace::Instruction

Builds a Transfer instruction.

Parameters:

  • amount (Integer)

    The amount of tokens to transfer.

  • source_index (Integer)

    The index of the source token account.

  • destination_index (Integer)

    The index of the destination token account.

  • owner_index (Integer)

    The index of the source account’s owner.

  • program_index (Integer)

    The index of the SPL Token Program.

Returns:

Since:

  • 0.0.2



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/solace/instructions/spl_token/transfer_instruction.rb', line 33

def self.build(
  amount:,
  owner_index:,
  source_index:,
  destination_index:,
  program_index:
)
  Solace::Instruction.new.tap do |ix|
    ix.program_index = program_index
    ix.accounts = [source_index, destination_index, owner_index]
    ix.data = data(amount)
  end
end

.data(amount) ⇒ Array<Integer>

Builds the data for a Transfer instruction.

The BufferLayout is:

- [Instruction Index (1 byte)]
- [Amount (8 bytes)]

Parameters:

  • amount (Integer)

    The amount of tokens to transfer.

Returns:

  • (Array<Integer>)

    1-byte instruction index + 8-byte amount

Since:

  • 0.0.2



55
56
57
# File 'lib/solace/instructions/spl_token/transfer_instruction.rb', line 55

def self.data(amount)
  INSTRUCTION_INDEX + Solace::Utils::Codecs.encode_le_u64(amount).bytes
end