Class: Solace::TransactionComposer

Inherits:
Object
  • Object
show all
Defined in:
lib/solace/transaction_composer.rb

Overview

Composes transactions with automatic account management and instruction building.

This class allows you to add multiple instruction composers, manage account contexts, and build a complete transaction in a flexible way. It is a high-level abstraction over the process of creating Solana transactions, making it easier to work with complex transaction scenarios.

For most use cases, you will create an instance of this class, add instruction composers, and then call the ‘compose_transaction` method to build the final transaction. That said, all of the individual pieces are also accessible for more advanced use cases.

Examples:

# Initialize a transaction composer
composer = Solace::TransactionComposer.new(connection: connection)

# Add an instruction composer
composer.add_instruction(
  Solace::Composers::SystemProgramTransferComposer.new(
    to: 'pubkey1',
    from: 'pubkey2',
    lamports: 100
  )
)

# Add another instruction composer
composer.add_instruction(
  Solace::Composers::SplTokenProgramTransferCheckedComposer.new(
    from: 'pubkey4',
    to: 'pubkey5',
    mint: 'pubkey6',
    authority: 'pubkey7',
    amount: 1_000_000,
    decimals: 6
  )
)

# Set the fee payer
composer.set_fee_payer('pubkey8')

# Compose the transaction
tx = composer.compose_transaction

# Sign the transaction with all required signers
tx.sign(*required_signers)
# Chaining methods will return the composer itself for further modifications. The add, prepend,
# and insert methods allow for dynamic insertion of instruction composers at required positions.
transaction_composer = Solace::TransactionComposer
  .new(connection: connection)
  .add_instruction(instruction_composer_1)
  .prepend_instruction(instruction_composer_2)
  .insert_instruction(1, instruction_composer_3)
  .set_fee_payer(fee_payer_pubkey)
  .compose_transaction

See Also:

Since:

  • 0.0.6

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection:) ⇒ TransactionComposer

Initialize the composer

Parameters:

Since:

  • 0.0.6



79
80
81
82
83
# File 'lib/solace/transaction_composer.rb', line 79

def initialize(connection:)
  @connection = connection
  @instruction_composers = []
  @context = Utils::AccountContext.new
end

Instance Attribute Details

#connectionObject

Since:

  • 0.0.6



66
67
68
# File 'lib/solace/transaction_composer.rb', line 66

def connection
  @connection
end

#contextObject

Since:

  • 0.0.6



70
71
72
# File 'lib/solace/transaction_composer.rb', line 70

def context
  @context
end

#instruction_composersObject

Since:

  • 0.0.6



74
75
76
# File 'lib/solace/transaction_composer.rb', line 74

def instruction_composers
  @instruction_composers
end

Instance Method Details

#add_instruction(composer) ⇒ TransactionComposer

Add an instruction composer to the transaction

Parameters:

Returns:

Since:

  • 0.0.6



89
90
91
92
93
# File 'lib/solace/transaction_composer.rb', line 89

def add_instruction(composer)
  merge_accounts(composer.)
  instruction_composers << composer
  self
end

#compose_transactionTransaction

Compose the final transaction

Returns:

  • (Transaction)

    The composed transaction (unsigned)

Since:

  • 0.0.6



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/solace/transaction_composer.rb', line 160

def compose_transaction
  context.compile

  message = Solace::Message.new(
    header: context.header,
    accounts: context.accounts,
    instructions: build_instructions,
    recent_blockhash: connection.get_latest_blockhash[0]
  )

  Solace::Transaction.new(message: message)
end

#insert_instruction(index, composer) ⇒ TransactionComposer

Insert an instruction composer at a specific index

Parameters:

  • index (Integer)

    The index to insert at

  • composer (Composers::Base)

    The instruction composer

Returns:

Since:

  • 0.1.0



114
115
116
117
118
# File 'lib/solace/transaction_composer.rb', line 114

def insert_instruction(index, composer)
  merge_accounts(composer.)
  instruction_composers.insert(index, composer)
  self
end

#merge(other, placement: :add, index: nil) ⇒ TransactionComposer

Merge another TransactionComposer into this one

Parameters:

  • other (TransactionComposer)

    The other composer to merge

  • placement (Symbol) (defaults to: :add)

    :add to append, :prepend to prepend

  • index (Integer, nil) (defaults to: nil)

    The index to insert at if placement is :insert

Returns:

Since:

  • 0.1.0



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/solace/transaction_composer.rb', line 128

def merge(other, placement: :add, index: nil)
  merge_accounts(other.context)

  case placement
  when :add
    # Appends the other's instruction composers to this one's list
    instruction_composers.concat(other.instruction_composers)
  when :insert
    # Inserts the other's instruction composers at the specified index
    instruction_composers.insert(index, *other.instruction_composers)
  when :prepend
    # Prepends the other's instruction composers to this one's list
    instruction_composers.unshift(*other.instruction_composers)
  else
    raise ArgumentError, "Invalid placement option: #{placement}"
  end

  self
end

#prepend_instruction(composer) ⇒ TransactionComposer

Prepend an instruction composer to the transaction

Parameters:

Returns:

Since:

  • 0.1.0



101
102
103
104
105
# File 'lib/solace/transaction_composer.rb', line 101

def prepend_instruction(composer)
  merge_accounts(composer.)
  instruction_composers.unshift(composer)
  self
end

#set_fee_payer(pubkey) ⇒ TransactionComposer

Set the fee payer for the transaction

Parameters:

  • pubkey (#to_s, PublicKey)

    The fee payer pubkey

Returns:

Since:

  • 0.0.6



152
153
154
155
# File 'lib/solace/transaction_composer.rb', line 152

def set_fee_payer(pubkey)
  context.set_fee_payer(pubkey.to_s)
  self
end