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_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_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
267 268 269 270 271 272 273 274 |
# File 'lib/solace/connection.rb', line 267 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
172 173 174 |
# File 'lib/solace/connection.rb', line 172 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
188 189 190 |
# File 'lib/solace/connection.rb', line 188 def get_balance(pubkey) @rpc_client.rpc_request('getBalance', [pubkey, ]).dig('result', 'value') 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
164 165 166 |
# File 'lib/solace/connection.rb', line 164 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.
223 224 225 |
# File 'lib/solace/connection.rb', line 223 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
180 181 182 |
# File 'lib/solace/connection.rb', line 180 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
251 252 253 |
# File 'lib/solace/connection.rb', line 251 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
259 260 261 |
# File 'lib/solace/connection.rb', line 259 def get_signature_status(signature) get_signature_statuses([signature]) end |
#get_signature_statuses(signatures) ⇒ Object
Get the signature status
240 241 242 243 |
# File 'lib/solace/connection.rb', line 240 def get_signature_statuses(signatures) @rpc_client.rpc_request('getSignatureStatuses', [signatures, .merge({ 'searchTransactionHistory' => true })])['result'] end |
#get_token_account_balance(token_account) ⇒ Hash
Get the balance of a token account
196 197 198 |
# File 'lib/solace/connection.rb', line 196 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.
208 209 210 211 |
# File 'lib/solace/connection.rb', line 208 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
232 233 234 |
# File 'lib/solace/connection.rb', line 232 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
281 282 283 |
# File 'lib/solace/connection.rb', line 281 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
290 291 292 |
# File 'lib/solace/connection.rb', line 290 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.
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/solace/connection.rb', line 302 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 |