Class: Solace::Instructions::SplToken::TransferCheckedInstruction

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

Overview

Instruction for transferring SPL tokens.

This instruction is used to transfer SPL tokens from one token account to another while checking the decimals of the token to ensure the transfer amount is correct.

Examples:

Build a TransferChecked instruction

instruction = Solace::Instructions::SplToken::TransferCheckedInstruction.build(
  amount: 100,
  decimals: 6,
  to_index: 1,
  from_index: 2,
  mint_index: 3,
  authority_index: 4,
  program_index: 5
)

Since:

  • 0.0.2

Constant Summary collapse

INSTRUCTION_INDEX =

SPL Token Program instruction index for Transfer Checked

Since:

  • 0.0.2

[12].freeze

Class Method Summary collapse

Class Method Details

.build(amount:, decimals:, to_index:, from_index:, mint_index:, authority_index:, program_index: 3) ⇒ Solace::Instruction

Builds a Solace::Instruction for transferring SPL tokens

SPL Token Program transfer instruction layout:

- 1 byte: instruction index (12 for transfer checked)
- 8 bytes: amount (u64, little-endian)
- 8 bytes: decimals (u64, little-endian)

Parameters:

  • amount (Integer)

    Amount to transfer (in tokens, according to mint’s decimals)

  • decimals (Integer)

    Number of decimals for the token

  • to_index (Integer)

    Index of the destination token account in the transaction’s accounts

  • from_index (Integer)

    Index of the source token account in the transaction’s accounts

  • mint_index (Integer)

    Index of the mint in the transaction’s accounts

  • authority_index (Integer)

    Index of the authority (owner) in the transaction’s accounts

  • program_index (Integer) (defaults to: 3)

    Index of the SPL Token Program in the transaction’s accounts (default: 3)

Returns:

Since:

  • 0.0.2



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/solace/instructions/spl_token/transfer_checked_instruction.rb', line 42

def self.build(
  amount:,
  decimals:,
  to_index:,
  from_index:,
  mint_index:,
  authority_index:,
  program_index: 3
)
  Solace::Instruction.new.tap do |ix|
    ix.program_index = program_index
    ix.accounts = [from_index, mint_index, to_index, authority_index]
    ix.data = data(amount, decimals)
  end
end

.data(amount, decimals) ⇒ Array

Instruction data for a token transfer instruction

The BufferLayout is:

- [Instruction Index (1 byte)]
- [Amount (8 bytes little-endian u64)]
- [Decimals (8 bytes little-endian u64)]

Parameters:

  • amount (Integer)

    Amount to transfer

  • decimals (Integer)

    Number of decimals for the token

Returns:

  • (Array)

    1-byte instruction index + 8-byte amount + decimals

Since:

  • 0.0.2



68
69
70
71
72
# File 'lib/solace/instructions/spl_token/transfer_checked_instruction.rb', line 68

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