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.
]
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_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) ⇒ 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
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_options ⇒ Object (readonly)
43 44 45 |
# File 'lib/solace/connection.rb', line 43 def @default_options end |
#last_fetched_block_height ⇒ Object (readonly)
Last fetched blockhash timestamp
49 50 51 |
# File 'lib/solace/connection.rb', line 49 def last_fetched_block_height @last_fetched_block_height end |
#last_fetched_blockhash ⇒ Object (readonly)
Last fetched blockhash
46 47 48 |
# File 'lib/solace/connection.rb', line 46 def last_fetched_blockhash @last_fetched_blockhash end |
#rpc_client ⇒ Object (readonly)
39 40 41 |
# File 'lib/solace/connection.rb', line 39 def rpc_client @rpc_client end |
#rpc_url ⇒ Object (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_options ⇒ Hash{Symbol => Object}
Build options for get_latest_blockhash
121 122 123 124 125 |
# File 'lib/solace/connection.rb', line 121 def { commitment: [:commitment] } end |
#build_send_transaction_options(overrides) ⇒ Hash
Builds send_transaction options
248 249 250 251 252 253 254 255 |
# File 'lib/solace/connection.rb', line 248 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
171 172 173 |
# File 'lib/solace/connection.rb', line 171 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
187 188 189 |
# File 'lib/solace/connection.rb', line 187 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
97 98 99 |
# File 'lib/solace/connection.rb', line 97 def get_genesis_hash @rpc_client.rpc_request('getGenesisHash')['result'] end |
#get_health ⇒ String
Get the health status of the Solana node
90 91 92 |
# File 'lib/solace/connection.rb', line 90 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.
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', []) .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
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
179 180 181 |
# File 'lib/solace/connection.rb', line 179 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
232 233 234 |
# File 'lib/solace/connection.rb', line 232 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
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
221 222 223 224 |
# File 'lib/solace/connection.rb', line 221 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
195 196 197 |
# File 'lib/solace/connection.rb', line 195 def get_token_account_balance(token_account) @rpc_client.rpc_request('getTokenAccountBalance', [token_account, ]).dig('result', 'value') end |
#get_token_accounts_by_owner(owner) ⇒ Array<Hash>
Gets the token accounts by owner
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 }, ] @rpc_client.rpc_request('getTokenAccountsByOwner', params).dig('result', 'value') end |
#get_transaction(signature, options = { maxSupportedTransactionVersion: 0 }) ⇒ Transaction
Get the transaction by signature
213 214 215 |
# File 'lib/solace/connection.rb', line 213 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
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
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/solace/connection.rb', line 107 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
262 263 264 |
# File 'lib/solace/connection.rb', line 262 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
271 272 273 |
# File 'lib/solace/connection.rb', line 271 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.
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 |