Class: Solace::Connection

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

Overview

Connection to a Solana RPC node

This class provides methods for sending JSON-RPC requests to a Solana RPC node and parsing responses. It includes methods for sending transactions, getting account information, and getting blockhashes.

]

Examples:

# Initialize the connection
connection = Solace::Connection.new('http://localhost:8899', commitment: 'confirmed')

# Get account information
connection.(.address)

# Request an airdrop
result = connection.request_airdrop(.address, 1000000)

# Wait for the transaction to be finalized
connection.wait_for_confirmed_signature('finalized') { result['result'] }

Raises:

Since:

  • 0.0.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rpc_url = 'http://localhost:8899', commitment: 'processed', http_open_timeout: 30, http_read_timeout: 60, encoding: 'base64') ⇒ Solace::Connection

Initialize the connection with a default or custom RPC URL

Parameters:

  • rpc_url (String) (defaults to: 'http://localhost:8899')

    The URL of the Solana RPC node

  • commitment (String) (defaults to: 'processed')

    The commitment level for RPC requests

  • http_open_timeout (Integer) (defaults to: 30)

    The timeout for opening an HTTP connection

  • http_read_timeout (Integer) (defaults to: 60)

    The timeout for reading an HTTP response

  • encoding (String) (defaults to: 'base64')

    The encoding for the RPC requests

Since:

  • 0.0.1



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/solace/connection.rb', line 59

def initialize(
  rpc_url = 'http://localhost:8899',
  commitment: 'processed',
  http_open_timeout: 30,
  http_read_timeout: 60,
  encoding: 'base64'
)
  # Initialize the RPC client
  @rpc_client = Utils::RPCClient.new(
    rpc_url,
    open_timeout: http_open_timeout,
    read_timeout: http_read_timeout
  )

  # Set default options for rpc requests
  @default_options = {
    commitment: commitment,
    encoding: encoding
  }
end

Instance Attribute Details

#default_optionsObject (readonly)

Since:

  • 0.0.1



43
44
45
# File 'lib/solace/connection.rb', line 43

def default_options
  @default_options
end

#last_fetched_block_heightObject (readonly)

Last fetched blockhash timestamp

Since:

  • 0.0.1



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

def last_fetched_block_height
  @last_fetched_block_height
end

#last_fetched_blockhashObject (readonly)

Last fetched blockhash

Since:

  • 0.0.1



46
47
48
# File 'lib/solace/connection.rb', line 46

def last_fetched_blockhash
  @last_fetched_blockhash
end

#rpc_clientObject (readonly)

Since:

  • 0.0.1



39
40
41
# File 'lib/solace/connection.rb', line 39

def rpc_client
  @rpc_client
end

#rpc_urlObject (readonly)

The URL of the Solana RPC node



39
# File 'lib/solace/connection.rb', line 39

attr_reader :rpc_client

Instance Method Details

#build_get_latest_blockhash_optionsHash{Symbol => Object}

Build options for get_latest_blockhash

Returns:

  • (Hash{Symbol => Object})

Since:

  • 0.0.1



121
122
123
124
125
# File 'lib/solace/connection.rb', line 121

def build_get_latest_blockhash_options
  {
    commitment: default_options[:commitment]
  }
end

#build_send_transaction_options(overrides) ⇒ Hash

Builds send_transaction options

Parameters:

  • overrides (Hash)

    The overrides for the options

Returns:

  • (Hash)

    The options for the send_transaction call

Since:

  • 0.0.1



248
249
250
251
252
253
254
255
# File 'lib/solace/connection.rb', line 248

def build_send_transaction_options(overrides)
  {
    skipPreflight: false,
    encoding: default_options[:encoding],
    commitment: default_options[:commitment],
    preflightCommitment: default_options[:commitment]
  }.merge(overrides)
end

#get_account_info(pubkey) ⇒ Object

Get the account information from the Solana node

Parameters:

  • pubkey (String)

    The public key of the account

Returns:

  • (Object)

    The account information

Since:

  • 0.0.1



171
172
173
# File 'lib/solace/connection.rb', line 171

def (pubkey)
  @rpc_client.rpc_request('getAccountInfo', [pubkey, default_options]).dig('result', 'value')
end

#get_balance(pubkey) ⇒ Integer

Get the balance of a specific account

Parameters:

  • pubkey (String)

    The public key of the account

Returns:

  • (Integer)

    The balance of the account

Since:

  • 0.0.1



187
188
189
# File 'lib/solace/connection.rb', line 187

def get_balance(pubkey)
  @rpc_client.rpc_request('getBalance', [pubkey, default_options]).dig('result', 'value')
end

#get_genesis_hashString

Get the genesis hash of the Solana node

Returns:

  • (String)

    The genesis hash

Since:

  • 0.0.1



97
98
99
# File 'lib/solace/connection.rb', line 97

def get_genesis_hash
  @rpc_client.rpc_request('getGenesisHash')['result']
end

#get_healthString

Get the health status of the Solana node

Returns:

  • (String)

    The health status

Since:

  • 0.0.1



90
91
92
# File 'lib/solace/connection.rb', line 90

def get_health
  @rpc_client.rpc_request('getHealth')['result']
end

#get_latest_blockhashArray<String, Integer>

Get the latest blockhash from the Solana node

Stores the last fetched blockhash and last valid block height in the connection, as different clients may need to access these values.

Examples:

# Initialize the connection
connection = Solace::Connection.new('http://localhost:8899', commitment: 'confirmed')

# Get the latest blockhash
blockhash, last_valid_block_height = connection.get_latest_blockhash

puts "Latest blockhash: #{blockhash}"
puts "Last valid block height: #{last_valid_block_height}"

# Access the last fetched blockhash and height from the connection
puts "Stored blockhash: #{connection.last_fetched_blockhash}"
puts "Stored last valid block height: #{connection.last_fetched_block_height}"

Returns:

  • (Array<String, Integer>)

    The latest blockhash and lastValidBlockHeight

Since:

  • 0.0.1



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/solace/connection.rb', line 147

def get_latest_blockhash
  result = @rpc_client
           .rpc_request('getLatestBlockhash', [build_get_latest_blockhash_options])
           .dig('result', 'value')

  # Set the last fetched blockhash and last valid block height in the connection
  @last_fetched_blockhash, @last_fetched_block_height = result.values_at('blockhash', 'lastValidBlockHeight')

  # Return the blockhash and last valid block height
  [@last_fetched_blockhash, @last_fetched_block_height]
end

#get_minimum_lamports_for_rent_exemption(space) ⇒ Integer

Get the minimum required lamports for rent exemption

Parameters:

  • space (Integer)

    Number of bytes to allocate for the account

Returns:

  • (Integer)

    The minimum required lamports

Since:

  • 0.0.1



163
164
165
# File 'lib/solace/connection.rb', line 163

def get_minimum_lamports_for_rent_exemption(space)
  @rpc_client.rpc_request('getMinimumBalanceForRentExemption', [space])['result']
end

#get_multiple_accounts(pubkeys) ⇒ Array<Object>

Get multiple accounts information from the Solana node

Parameters:

  • pubkeys (Array<String>)

    The public keys of the accounts

Returns:

  • (Array<Object>)

    The accounts information

Since:

  • 0.0.1



179
180
181
# File 'lib/solace/connection.rb', line 179

def get_multiple_accounts(pubkeys)
  @rpc_client.rpc_request('getMultipleAccounts', [pubkeys, default_options]).dig('result', 'value')
end

#get_program_accounts(program_id, filters) ⇒ Array

Get the program accounts

Parameters:

  • program_id (String)

    The program ID

  • filters (Array)

    The filters

  • options (Hash{Symbol => Object})

Returns:

  • (Array)

    The program accounts

Since:

  • 0.0.1



232
233
234
# File 'lib/solace/connection.rb', line 232

def get_program_accounts(program_id, filters)
  @rpc_client.rpc_request('getProgramAccounts', [program_id, default_options.merge(filters: filters)])['result']
end

#get_signature_status(signature) ⇒ Object

Get the signature status

Parameters:

  • signature (String)

    The signature of the transaction

Returns:

  • (Object)

    The signature status

Since:

  • 0.0.1



240
241
242
# File 'lib/solace/connection.rb', line 240

def get_signature_status(signature)
  get_signature_statuses([signature])
end

#get_signature_statuses(signatures) ⇒ Object

Get the signature status

Parameters:

  • signatures (Array)

    The signatures of the transactions

Returns:

  • (Object)

    The signature status

Since:

  • 0.0.1



221
222
223
224
# File 'lib/solace/connection.rb', line 221

def get_signature_statuses(signatures)
  @rpc_client.rpc_request('getSignatureStatuses',
                          [signatures, default_options.merge({ 'searchTransactionHistory' => true })])['result']
end

#get_token_account_balance(token_account) ⇒ Hash

Get the balance of a token account

Parameters:

  • token_account (String)

    The public key of the token account

Returns:

  • (Hash)

    Token account balance information with amount and decimals

Since:

  • 0.0.1



195
196
197
# File 'lib/solace/connection.rb', line 195

def ()
  @rpc_client.rpc_request('getTokenAccountBalance', [, default_options]).dig('result', 'value')
end

#get_token_accounts_by_owner(owner) ⇒ Array<Hash>

Gets the token accounts by owner

Parameters:

  • owner (String)

    The public key of the owner

Returns:

  • (Array<Hash>)

    The token accounts owned by the owner for the specified mint

Since:

  • 0.0.1



203
204
205
206
# File 'lib/solace/connection.rb', line 203

def get_token_accounts_by_owner(owner)
  params = [owner, { programId: Constants::TOKEN_PROGRAM_ID }, default_options]
  @rpc_client.rpc_request('getTokenAccountsByOwner', params).dig('result', 'value')
end

#get_transaction(signature, options = { maxSupportedTransactionVersion: 0 }) ⇒ Transaction

Get the transaction by signature

Parameters:

  • signature (String)

    The signature of the transaction

  • options (Hash{Symbol => Object}) (defaults to: { maxSupportedTransactionVersion: 0 })

Returns:

Since:

  • 0.0.1



213
214
215
# File 'lib/solace/connection.rb', line 213

def get_transaction(signature, options = { maxSupportedTransactionVersion: 0 })
  @rpc_client.rpc_request('getTransaction', [signature, default_options.merge(options)])['result']
end

#get_versionHash

Gets the version of the Solana node

Returns:

  • (Hash)

    The version information

Since:

  • 0.0.1



83
84
85
# File 'lib/solace/connection.rb', line 83

def get_version
  @rpc_client.rpc_request('getVersion')['result']
end

#request_airdrop(pubkey, lamports, options = {}) ⇒ String

Request an airdrop of lamports to a given address

Parameters:

  • pubkey (String)

    The public key of the account to receive the airdrop

  • lamports (Integer)

    Amount of lamports to airdrop

  • options (Hash{Symbol => Object}) (defaults to: {})

    The options for the request

Returns:

  • (String)

    The transaction signature of the airdrop

Since:

  • 0.0.1



107
108
109
110
111
112
113
114
115
116
# File 'lib/solace/connection.rb', line 107

def request_airdrop(pubkey, lamports, options = {})
  @rpc_client.rpc_request(
    'requestAirdrop',
    [
      pubkey,
      lamports,
      default_options.merge(options)
    ]
  )
end

#send_transaction(transaction, overrides = {}) ⇒ String

Send a transaction to the Solana node

Parameters:

  • transaction (Transaction)

    The transaction to send

  • overrides (Hash{Symbol => Object}) (defaults to: {})

Returns:

  • (String)

    The signature of the transaction

Since:

  • 0.0.1



262
263
264
# File 'lib/solace/connection.rb', line 262

def send_transaction(transaction, overrides = {})
  @rpc_client.rpc_request('sendTransaction', [transaction, build_send_transaction_options(overrides)])
end

#simulate_transaction(transaction, overrides = {}) ⇒ Object

Simulate a transaction on the Solana node

Parameters:

  • transaction (Transaction)

    The transaction to simulate

  • overrides (Hash{Symbol => Object}) (defaults to: {})

Returns:

  • (Object)

    The result of the simulation

Since:

  • 0.0.1



271
272
273
# File 'lib/solace/connection.rb', line 271

def simulate_transaction(transaction, overrides = {})
  @rpc_client.rpc_request('simulateTransaction', [transaction, build_send_transaction_options(overrides)])
end

#wait_for_confirmed_signature(commitment = 'confirmed', timeout: 60, interval: 0.1) ⇒ String

Wait until the yielded signature reaches the desired commitment or timeout.

Parameters:

  • commitment (String) (defaults to: 'confirmed')

    One of “processed”, “confirmed”, “finalized”

  • timeout (Numeric) (defaults to: 60)

    seconds to wait before raising

  • interval (Numeric) (defaults to: 0.1)

    polling interval in seconds

Yield Returns:

  • (String, Hash)

    a signature string or a JSON-RPC hash with “result”

Returns:

  • (String)

    the signature when the commitment is reached

Raises:

Since:

  • 0.0.1



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/solace/connection.rb', line 283

def wait_for_confirmed_signature(
  commitment = 'confirmed',
  timeout: 60,
  interval: 0.1
)
  raise ArgumentError, 'Block required' unless block_given?

  signature = extract_signature_from(yield)
  deadline = monotonic_deadline(timeout)

  # Wait for confirmation
  until deadline_passed?(deadline)
    return signature if commitment_reached?(signature, commitment)

    sleep interval
  end

  raise Errors::ConfirmationTimeout.format(signature, commitment, timeout)
end