Class: Solace::Instructions::SystemProgram::CreateAccountInstruction

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

Overview

Instruction for creating a new account.

This instruction is used to create a new account for a given program.

Examples:

Build a CreateAccount instruction

instruction = Solace::Instructions::SystemProgram::CreateAccountInstruction.build(
  space: 1024,
  lamports: 1000,
  from_index: 0,
  new_account_index: 1,
  owner: owner.address,
  system_program_index: 2
)

Since:

  • 0.0.2

Constant Summary collapse

INSTRUCTION_INDEX =

Since:

  • 0.0.2

[0, 0, 0, 0].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#INSTRUCTION_INDEXObject (readonly)

Instruction index for SystemProgram::CreateAccount This is the same across all Solana clusters



25
# File 'lib/solace/instructions/system_program/create_account_instruction.rb', line 25

INSTRUCTION_INDEX = [0, 0, 0, 0].freeze

Class Method Details

.build(space:, lamports:, from_index:, new_account_index:, owner: Solace::Constants::SYSTEM_PROGRAM_ID, system_program_index: 2) ⇒ Solace::Instruction

Builds a SystemProgram::CreateAccount instruction

Parameters:

  • space (Integer)

    Number of bytes to allocate for the new account

  • lamports (Integer)

    Amount of lamports to fund the new account

  • owner (String) (defaults to: Solace::Constants::SYSTEM_PROGRAM_ID)

    The program_id of the owner of the new account

  • from_index (Integer)

    Index of the funding account (payer) in the transaction’s accounts

  • new_account_index (Integer)

    Index of the new account to create in the transaction’s accounts

  • system_program_index (Integer) (defaults to: 2)

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

Returns:

Since:

  • 0.0.2



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/solace/instructions/system_program/create_account_instruction.rb', line 36

def self.build(
  space:,
  lamports:,
  from_index:,
  new_account_index:,
  owner: Solace::Constants::SYSTEM_PROGRAM_ID,
  system_program_index: 2
)
  Solace::Instruction.new.tap do |ix|
    ix.program_index = system_program_index
    ix.accounts = [from_index, ]
    ix.data = data(lamports, space, owner)
  end
end

.data(lamports, space, owner) ⇒ Array

Builds the data for a SystemProgram::CreateAccount instruction

The BufferLayout is:

- [Instruction Index (4 bytes)]
- [Lamports (8 bytes)]
- [Space (8 bytes)]
- [Owner (32 bytes)]

Parameters:

  • lamports (Integer)

    Amount of lamports to fund the new account

  • space (Integer)

    Number of bytes to allocate for the new account

  • owner (String)

    The program_id of the owner of the new account

Returns:

  • (Array)

    4-byte instruction index + 8-byte lamports + 8-byte space + 32-byte owner

Since:

  • 0.0.2



64
65
66
67
68
69
# File 'lib/solace/instructions/system_program/create_account_instruction.rb', line 64

def self.data(lamports, space, owner)
  INSTRUCTION_INDEX +
    Solace::Utils::Codecs.encode_le_u64(lamports).bytes +
    Solace::Utils::Codecs.encode_le_u64(space).bytes +
    Solace::Utils::Codecs.base58_to_bytes(owner)
end