Class: Solace::TransactionComposer
- Inherits:
-
Object
- Object
- Solace::TransactionComposer
- 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.
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#add_instruction(composer) ⇒ TransactionComposer
Add an instruction composer to the transaction.
-
#compose_transaction ⇒ Transaction
Compose the final transaction.
-
#initialize(connection:) ⇒ TransactionComposer
constructor
Initialize the composer.
-
#insert_instruction(index, composer) ⇒ TransactionComposer
Insert an instruction composer at a specific index.
-
#merge(other, placement: :add, index: nil) ⇒ TransactionComposer
Merge another TransactionComposer into this one.
-
#prepend_instruction(composer) ⇒ TransactionComposer
Prepend an instruction composer to the transaction.
-
#set_fee_payer(pubkey) ⇒ TransactionComposer
Set the fee payer for the transaction.
Constructor Details
#initialize(connection:) ⇒ TransactionComposer
Initialize the composer
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
#connection ⇒ Object
66 67 68 |
# File 'lib/solace/transaction_composer.rb', line 66 def connection @connection end |
#context ⇒ Object
70 71 72 |
# File 'lib/solace/transaction_composer.rb', line 70 def context @context end |
#instruction_composers ⇒ Object
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
89 90 91 92 93 |
# File 'lib/solace/transaction_composer.rb', line 89 def add_instruction(composer) merge_accounts(composer.account_context) instruction_composers << composer self end |
#compose_transaction ⇒ Transaction
Compose the final transaction
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 = Solace::Message.new( header: context.header, accounts: context.accounts, instructions: build_instructions, recent_blockhash: connection.get_latest_blockhash[0] ) Solace::Transaction.new(message: ) end |
#insert_instruction(index, composer) ⇒ TransactionComposer
Insert an instruction composer at a specific index
114 115 116 117 118 |
# File 'lib/solace/transaction_composer.rb', line 114 def insert_instruction(index, composer) merge_accounts(composer.account_context) instruction_composers.insert(index, composer) self end |
#merge(other, placement: :add, index: nil) ⇒ TransactionComposer
Merge another TransactionComposer into this one
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
101 102 103 104 105 |
# File 'lib/solace/transaction_composer.rb', line 101 def prepend_instruction(composer) merge_accounts(composer.account_context) instruction_composers.unshift(composer) self end |
#set_fee_payer(pubkey) ⇒ TransactionComposer
Set the fee payer for the transaction
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 |