Class: Solace::TransactionComposer

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

Overview

Composes multi-instruction transactions with automatic account management

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)

Since:

  • 0.0.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection:) ⇒ TransactionComposer

Initialize the composer

Parameters:

Since:

  • 0.0.1



58
59
60
61
62
# File 'lib/solace/transaction_composer.rb', line 58

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

Instance Attribute Details

#connectionObject

Since:

  • 0.0.1



45
46
47
# File 'lib/solace/transaction_composer.rb', line 45

def connection
  @connection
end

#contextObject

Since:

  • 0.0.1



49
50
51
# File 'lib/solace/transaction_composer.rb', line 49

def context
  @context
end

#instruction_composersObject

Since:

  • 0.0.1



53
54
55
# File 'lib/solace/transaction_composer.rb', line 53

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.1



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

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

#compose_transactionSolace::Transaction

Compose the final transaction

Returns:

Since:

  • 0.0.1



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/solace/transaction_composer.rb', line 86

def compose_transaction
  context.compile

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

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

#set_fee_payer(pubkey) ⇒ TransactionComposer

Set the fee payer for the transaction

Parameters:

Returns:

Since:

  • 0.0.1



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

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