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
- #rpc_url ⇒ Object readonly
Instance Method Summary collapse
-
#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_latest_blockhash ⇒ String
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_signature_status(signatures) ⇒ Object
Get the signature status.
-
#get_token_account_balance(token_account) ⇒ Hash
Get the balance of a token account.
-
#get_transaction(signature, options = { maxSupportedTransactionVersion: 0 }) ⇒ Solace::Transaction
Get the transaction by signature.
-
#initialize(rpc_url = 'http://localhost:8899', commitment: 'confirmed') ⇒ 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.
-
#rpc_request(method, params = []) ⇒ Hash
Sends a JSON-RPC request to the configured Solana RPC server.
-
#send_transaction(transaction, options = {}) ⇒ String
Send a transaction to the Solana node.
-
#wait_for_confirmed_signature(commitment = 'confirmed') ⇒ Boolean
Wait for a confirmed signature from the transaction.
Constructor Details
#initialize(rpc_url = 'http://localhost:8899', commitment: 'confirmed') ⇒ Solace::Connection
Initialize the connection with a default or custom RPC URL
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/solace/connection.rb', line 43 def initialize(rpc_url = 'http://localhost:8899', commitment: 'confirmed') @request_id = nil @rpc_url = rpc_url # Set default options @default_options = { commitment: commitment, encoding: 'base64' } end |
Instance Attribute Details
#default_options ⇒ Object (readonly)
36 37 38 |
# File 'lib/solace/connection.rb', line 36 def @default_options end |
#rpc_url ⇒ Object (readonly)
32 33 34 |
# File 'lib/solace/connection.rb', line 32 def rpc_url @rpc_url end |
Instance Method Details
#get_account_info(pubkey) ⇒ Object
Get the account information from the Solana node
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/solace/connection.rb', line 102 def get_account_info(pubkey) response = rpc_request( 'getAccountInfo', [ pubkey, ] )['result'] return if response.nil? response['value'] end |
#get_balance(pubkey) ⇒ Integer
Get the balance of a specific account
120 121 122 123 124 125 126 127 128 |
# File 'lib/solace/connection.rb', line 120 def get_balance(pubkey) rpc_request( 'getBalance', [ pubkey, ] )['result']['value'] end |
#get_latest_blockhash ⇒ String
Get the latest blockhash from the Solana node
86 87 88 |
# File 'lib/solace/connection.rb', line 86 def get_latest_blockhash rpc_request('getLatestBlockhash')['result']['value']['blockhash'] end |
#get_minimum_lamports_for_rent_exemption(space) ⇒ Integer
Get the minimum required lamports for rent exemption
94 95 96 |
# File 'lib/solace/connection.rb', line 94 def get_minimum_lamports_for_rent_exemption(space) rpc_request('getMinimumBalanceForRentExemption', [space])['result'] end |
#get_signature_status(signatures) ⇒ Object
Get the signature status
163 164 165 166 167 168 169 170 171 |
# File 'lib/solace/connection.rb', line 163 def get_signature_status(signatures) rpc_request( 'getSignatureStatuses', [ signatures, .merge({ 'searchTransactionHistory' => true }) ] )['result'] end |
#get_token_account_balance(token_account) ⇒ Hash
Get the balance of a token account
134 135 136 137 138 139 140 141 142 |
# File 'lib/solace/connection.rb', line 134 def get_token_account_balance(token_account) rpc_request( 'getTokenAccountBalance', [ token_account, ] )['result']['value'] end |
#get_transaction(signature, options = { maxSupportedTransactionVersion: 0 }) ⇒ Solace::Transaction
Get the transaction by signature
149 150 151 152 153 154 155 156 157 |
# File 'lib/solace/connection.rb', line 149 def get_transaction(signature, = { maxSupportedTransactionVersion: 0 }) rpc_request( 'getTransaction', [ signature, .merge() ] )['result'] end |
#request_airdrop(pubkey, lamports, options = {}) ⇒ String
Request an airdrop of lamports to a given address
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/solace/connection.rb', line 72 def request_airdrop(pubkey, lamports, = {}) rpc_request( 'requestAirdrop', [ pubkey, lamports, .merge() ] ) end |
#rpc_request(method, params = []) ⇒ Hash
Sends a JSON-RPC request to the configured Solana RPC server.
60 61 62 63 64 |
# File 'lib/solace/connection.rb', line 60 def rpc_request(method, params = []) request = build_rpc_request(method, params) response = perform_http_request(request) handle_rpc_response(response) end |
#send_transaction(transaction, options = {}) ⇒ String
Send a transaction to the Solana node
178 179 180 181 182 183 184 185 186 |
# File 'lib/solace/connection.rb', line 178 def send_transaction(transaction, = {}) rpc_request( 'sendTransaction', [ transaction, .merge() ] ) end |
#wait_for_confirmed_signature(commitment = 'confirmed') ⇒ Boolean
Wait for a confirmed signature from the transaction
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/solace/connection.rb', line 192 def wait_for_confirmed_signature(commitment = 'confirmed') raise ArgumentError, 'Block required' unless block_given? # Get the signature from the block signature = yield interval = 0.1 # Wait for confirmation loop do status = get_signature_status([signature]).dig('value', 0) break if status && status['confirmationStatus'] == commitment sleep interval end signature end |