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
- #address_lookup_tables ⇒ Object
- #connection ⇒ Object
- #context ⇒ Object
- #instruction_composers ⇒ Object
- #version ⇒ Object
Instance Method Summary collapse
-
#add_address_lookup_table(account:, addresses: nil) ⇒ TransactionComposer
Make an address lookup table available to the transaction.
-
#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
87 88 89 90 91 92 93 |
# File 'lib/solace/transaction_composer.rb', line 87 def initialize(connection:) @connection = connection @instruction_composers = [] @context = Utils::AccountContext.new @address_lookup_tables = [] @version = nil end |
Instance Attribute Details
#address_lookup_tables ⇒ Object
78 79 80 |
# File 'lib/solace/transaction_composer.rb', line 78 def address_lookup_tables @address_lookup_tables end |
#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 |
#version ⇒ Object
82 83 84 |
# File 'lib/solace/transaction_composer.rb', line 82 def version @version end |
Instance Method Details
#add_address_lookup_table(account:, addresses: nil) ⇒ TransactionComposer
Make an address lookup table available to the transaction
Registering a table opts the transaction into the v0 format: every compiled account found in a table that is allowed to load (a non-signer that is not the fee payer and not an invoked program id) is referenced by table index instead of occupying a static account slot. Adding the same table (by account) twice is a no-op.
184 185 186 187 188 189 190 191 192 193 |
# File 'lib/solace/transaction_composer.rb', line 184 def add_address_lookup_table(account:, addresses: nil) account = account.to_s @version = 0 # Lookup tables require a v0 transaction unless address_lookup_tables.any? { |table| table.account == account } address_lookup_tables << Solace::Accounts::AddressLookupTable.new(account: account, addresses: addresses) end self end |
#add_instruction(composer) ⇒ TransactionComposer
Add an instruction composer to the transaction
99 100 101 102 103 |
# File 'lib/solace/transaction_composer.rb', line 99 def add_instruction(composer) merge_accounts(composer.account_context) instruction_composers << composer self end |
#compose_transaction ⇒ Transaction
Compose the final transaction
Emits a message at the composer's #version — legacy by default, or v0 once a lookup table has been added — loading eligible accounts through any registered tables.
202 203 204 205 206 207 208 209 |
# File 'lib/solace/transaction_composer.rb', line 202 def compose_transaction context.compile writable, readonly, references = resolve_address_lookup_tables context.compile(loaded_accounts: writable + readonly) Solace::Transaction.new(message: (references)) end |
#insert_instruction(index, composer) ⇒ TransactionComposer
Insert an instruction composer at a specific index
124 125 126 127 128 |
# File 'lib/solace/transaction_composer.rb', line 124 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
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/solace/transaction_composer.rb', line 138 def merge(other, placement: :add, index: nil) merge_accounts(other.context) merge_address_lookup_tables(other.address_lookup_tables) 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
111 112 113 114 115 |
# File 'lib/solace/transaction_composer.rb', line 111 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
163 164 165 166 |
# File 'lib/solace/transaction_composer.rb', line 163 def set_fee_payer(pubkey) context.set_fee_payer(pubkey.to_s) self end |