Class: Solace::Connection
- Inherits:
-
Object
- Object
- Solace::Connection
- 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.
] rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
- #default_options ⇒ Object readonly
-
#last_fetched_block_height ⇒ Object
readonly
Last fetched blockhash timestamp.
-
#last_fetched_blockhash ⇒ Object
readonly
Last fetched blockhash.
- #rpc_client ⇒ Object readonly
-
#rpc_url ⇒ Object
readonly
The URL of the Solana RPC node.
Instance Method Summary collapse
-
#build_get_latest_blockhash_options ⇒ Hash{Symbol => Object}
Build options for get_latest_blockhash.
-
#build_send_transaction_options(overrides) ⇒ Hash
Builds send_transaction options.
-
#get_account_info(pubkey) ⇒ Object
Get the account information from the Solana node.
-
#get_balance(pubkey) ⇒ Integer
Get the balance of a specific account.
-
#get_block_height(commitment: ) ⇒ Integer
Get the current block height from the Solana node.
-
#get_genesis_hash ⇒ String
Get the genesis hash of the Solana node.
-
#get_health ⇒ String
Get the health status of the Solana node.
-
#get_latest_blockhash ⇒ Array<String, Integer>
Get the latest blockhash from the Solana node.
-
#get_minimum_lamports_for_rent_exemption(space) ⇒ Integer
Get the minimum required lamports for rent exemption.
-
#get_mint_program_id(mint) ⇒ String?
Discovery-only helper: returns the program ID that owns a given mint.
-
#get_multiple_accounts(pubkeys) ⇒ Array<Object>
Get multiple accounts information from the Solana node.
-
#get_program_accounts(program_id, filters) ⇒ Array
Get the program accounts.
-
#get_signature_status(signature) ⇒ Object
Get the signature status.
-
#get_signature_statuses(signatures) ⇒ Object
Get the signature status.
-
#get_slot(commitment: ) ⇒ Integer
Get the current slot from the Solana node.
-
#get_token_account_balance(token_account) ⇒ Hash
Get the balance of a token account.
-
#get_token_accounts_by_owner(owner, token_program_id: Constants::TOKEN_PROGRAM_ID) ⇒ Array<Hash>
Gets the token accounts by owner.
-
#get_transaction(signature, options = { maxSupportedTransactionVersion: 0 }) ⇒ Transaction
Get the transaction by signature.
-
#get_version ⇒ Hash
Gets the version of the Solana node.
-
#initialize(rpc_url = 'http://localhost:8899', commitment: 'processed', http_open_timeout: 30, http_read_timeout: 60, encoding: 'base64') ⇒ Solace::Connection
constructor
Initialize the connection with a default or custom RPC URL.
-
#request_airdrop(pubkey, lamports, options = {}) ⇒ String
Request an airdrop of lamports to a given address.
-
#send_transaction(transaction, overrides = {}) ⇒ String
Send a transaction to the Solana node.
-
#simulate_transaction(transaction, overrides = {}) ⇒ Object
Simulate a transaction on the Solana node.
-
#wait_for_confirmed_signature(commitment = 'confirmed', timeout: 60, interval: 0.1) ⇒ String
Wait until the yielded signature reaches the desired commitment or timeout.
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
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/solace/connection.rb', line 60 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_options ⇒ Object (readonly)
44 45 46 |
# File 'lib/solace/connection.rb', line 44 def @default_options end |
#last_fetched_block_height ⇒ Object (readonly)
Last fetched blockhash timestamp
50 51 52 |
# File 'lib/solace/connection.rb', line 50 def last_fetched_block_height @last_fetched_block_height end |
#last_fetched_blockhash ⇒ Object (readonly)
Last fetched blockhash
47 48 49 |
# File 'lib/solace/connection.rb', line 47 def last_fetched_blockhash @last_fetched_blockhash end |
#rpc_client ⇒ Object (readonly)
40 41 42 |
# File 'lib/solace/connection.rb', line 40 def rpc_client @rpc_client end |
#rpc_url ⇒ Object (readonly)
The URL of the Solana RPC node
40 |
# File 'lib/solace/connection.rb', line 40 attr_reader :rpc_client |
Instance Method Details
#build_get_latest_blockhash_options ⇒ Hash{Symbol => Object}
Build options for get_latest_blockhash
122 123 124 125 126 |
# File 'lib/solace/connection.rb', line 122 def { commitment: [:commitment] } end |
#build_send_transaction_options(overrides) ⇒ Hash
Builds send_transaction options
288 289 290 291 292 293 294 295 |
# File 'lib/solace/connection.rb', line 288 def (overrides) { skipPreflight: false, encoding: [:encoding], commitment: [:commitment], preflightCommitment: [:commitment] }.merge(overrides) end |
#get_account_info(pubkey) ⇒ Object
Get the account information from the Solana node
193 194 195 |
# File 'lib/solace/connection.rb', line 193 def get_account_info(pubkey) @rpc_client.rpc_request('getAccountInfo', [pubkey, ]).dig('result', 'value') end |
#get_balance(pubkey) ⇒ Integer
Get the balance of a specific account
209 210 211 |
# File 'lib/solace/connection.rb', line 209 def get_balance(pubkey) @rpc_client.rpc_request('getBalance', [pubkey, ]).dig('result', 'value') end |
#get_block_height(commitment: ) ⇒ Integer
Get the current block height from the Solana node
Defaults to the connection's commitment so comparisons against the
lastValidBlockHeight from #get_latest_blockhash stay consistent.
167 168 169 |
# File 'lib/solace/connection.rb', line 167 def get_block_height(commitment: [:commitment]) @rpc_client.rpc_request('getBlockHeight', [{ commitment: commitment }])['result'] end |
#get_genesis_hash ⇒ String
Get the genesis hash of the Solana node
98 99 100 |
# File 'lib/solace/connection.rb', line 98 def get_genesis_hash @rpc_client.rpc_request('getGenesisHash')['result'] end |
#get_health ⇒ String
Get the health status of the Solana node
91 92 93 |
# File 'lib/solace/connection.rb', line 91 def get_health @rpc_client.rpc_request('getHealth')['result'] end |
#get_latest_blockhash ⇒ Array<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.
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/solace/connection.rb', line 148 def get_latest_blockhash result = @rpc_client .rpc_request('getLatestBlockhash', []) .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
185 186 187 |
# File 'lib/solace/connection.rb', line 185 def get_minimum_lamports_for_rent_exemption(space) @rpc_client.rpc_request('getMinimumBalanceForRentExemption', [space])['result'] end |
#get_mint_program_id(mint) ⇒ String?
Discovery-only helper: returns the program ID that owns a given mint.
Use this to dispatch between Solace::Constants::TOKEN_PROGRAM_ID and
Solace::Constants::TOKEN_2022_PROGRAM_ID when the mint's program is not known
ahead of time. If you also need the rest of the mint's account data
(decimals, supply, ...), call #get_account_info directly and read
'owner' yourself — calling both would cost two RPC roundtrips.
244 245 246 |
# File 'lib/solace/connection.rb', line 244 def get_mint_program_id(mint) get_account_info(mint)&.dig('owner') end |
#get_multiple_accounts(pubkeys) ⇒ Array<Object>
Get multiple accounts information from the Solana node
201 202 203 |
# File 'lib/solace/connection.rb', line 201 def get_multiple_accounts(pubkeys) @rpc_client.rpc_request('getMultipleAccounts', [pubkeys, ]).dig('result', 'value') end |
#get_program_accounts(program_id, filters) ⇒ Array
Get the program accounts
272 273 274 |
# File 'lib/solace/connection.rb', line 272 def get_program_accounts(program_id, filters) @rpc_client.rpc_request('getProgramAccounts', [program_id, .merge(filters: filters)])['result'] end |
#get_signature_status(signature) ⇒ Object
Get the signature status
280 281 282 |
# File 'lib/solace/connection.rb', line 280 def get_signature_status(signature) get_signature_statuses([signature]) end |
#get_signature_statuses(signatures) ⇒ Object
Get the signature status
261 262 263 264 |
# File 'lib/solace/connection.rb', line 261 def get_signature_statuses(signatures) @rpc_client.rpc_request('getSignatureStatuses', [signatures, .merge({ 'searchTransactionHistory' => true })])['result'] end |
#get_slot(commitment: ) ⇒ Integer
Get the current slot from the Solana node
177 178 179 |
# File 'lib/solace/connection.rb', line 177 def get_slot(commitment: [:commitment]) @rpc_client.rpc_request('getSlot', [{ commitment: commitment }])['result'] end |
#get_token_account_balance(token_account) ⇒ Hash
Get the balance of a token account
217 218 219 |
# File 'lib/solace/connection.rb', line 217 def get_token_account_balance(token_account) @rpc_client.rpc_request('getTokenAccountBalance', [token_account, ]).dig('result', 'value') end |
#get_token_accounts_by_owner(owner, token_program_id: Constants::TOKEN_PROGRAM_ID) ⇒ Array<Hash>
Gets the token accounts by owner.
Defaults to the legacy SPL Token program; pass
token_program_id: Solace::Constants::TOKEN_2022_PROGRAM_ID for Token-2022.
229 230 231 232 |
# File 'lib/solace/connection.rb', line 229 def get_token_accounts_by_owner(owner, token_program_id: Constants::TOKEN_PROGRAM_ID) params = [owner, { programId: token_program_id }, ] @rpc_client.rpc_request('getTokenAccountsByOwner', params).dig('result', 'value') end |
#get_transaction(signature, options = { maxSupportedTransactionVersion: 0 }) ⇒ Transaction
Get the transaction by signature
253 254 255 |
# File 'lib/solace/connection.rb', line 253 def get_transaction(signature, = { maxSupportedTransactionVersion: 0 }) @rpc_client.rpc_request('getTransaction', [signature, .merge()])['result'] end |
#get_version ⇒ Hash
Gets the version of the Solana node
84 85 86 |
# File 'lib/solace/connection.rb', line 84 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
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/solace/connection.rb', line 108 def request_airdrop(pubkey, lamports, = {}) @rpc_client.rpc_request( 'requestAirdrop', [ pubkey, lamports, .merge() ] ) end |
#send_transaction(transaction, overrides = {}) ⇒ String
Send a transaction to the Solana node
302 303 304 |
# File 'lib/solace/connection.rb', line 302 def send_transaction(transaction, overrides = {}) @rpc_client.rpc_request('sendTransaction', [transaction, (overrides)]) end |
#simulate_transaction(transaction, overrides = {}) ⇒ Object
Simulate a transaction on the Solana node
311 312 313 |
# File 'lib/solace/connection.rb', line 311 def simulate_transaction(transaction, overrides = {}) @rpc_client.rpc_request('simulateTransaction', [transaction, (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.
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/solace/connection.rb', line 323 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 |