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
- #rpc_client ⇒ Object readonly
-
#rpc_url ⇒ Object
readonly
The URL of the Solana RPC node.
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(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_transaction(signature, options = { maxSupportedTransactionVersion: 0 }) ⇒ Solace::Transaction
Get the transaction by signature.
-
#initialize(rpc_url = 'http://localhost:8899', commitment: 'confirmed', http_open_timeout: 30, http_read_timeout: 60) ⇒ 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', 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: 'confirmed', http_open_timeout: 30, http_read_timeout: 60) ⇒ Solace::Connection
Initialize the connection with a default or custom RPC URL
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/solace/connection.rb', line 52 def initialize( rpc_url = 'http://localhost:8899', commitment: 'confirmed', http_open_timeout: 30, http_read_timeout: 60 ) # 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: 'base64' } end |
Instance Attribute Details
#default_options ⇒ Object (readonly)
43 44 45 |
# File 'lib/solace/connection.rb', line 43 def @default_options 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
#get_account_info(pubkey) ⇒ Object
Get the account information from the Solana node
125 126 127 |
# File 'lib/solace/connection.rb', line 125 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
133 134 135 |
# File 'lib/solace/connection.rb', line 133 def get_balance(pubkey) @rpc_client.rpc_request('getBalance', [pubkey, ]).dig('result', 'value') end |
#get_latest_blockhash ⇒ String
Get the latest blockhash from the Solana node
109 110 111 |
# File 'lib/solace/connection.rb', line 109 def get_latest_blockhash @rpc_client.rpc_request('getLatestBlockhash').dig('result', 'value', 'blockhash') end |
#get_minimum_lamports_for_rent_exemption(space) ⇒ Integer
Get the minimum required lamports for rent exemption
117 118 119 |
# File 'lib/solace/connection.rb', line 117 def get_minimum_lamports_for_rent_exemption(space) @rpc_client.rpc_request('getMinimumBalanceForRentExemption', [space])['result'] end |
#get_signature_status(signature) ⇒ Object
Get the signature status
167 168 169 |
# File 'lib/solace/connection.rb', line 167 def get_signature_status(signature) get_signature_statuses([signature]) end |
#get_signature_statuses(signatures) ⇒ Object
Get the signature status
158 159 160 161 |
# File 'lib/solace/connection.rb', line 158 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
141 142 143 |
# File 'lib/solace/connection.rb', line 141 def get_token_account_balance(token_account) @rpc_client.rpc_request('getTokenAccountBalance', [token_account, ]).dig('result', 'value') end |
#get_transaction(signature, options = { maxSupportedTransactionVersion: 0 }) ⇒ Solace::Transaction
Get the transaction by signature
150 151 152 |
# File 'lib/solace/connection.rb', line 150 def get_transaction(signature, = { maxSupportedTransactionVersion: 0 }) @rpc_client.rpc_request('getTransaction', [signature, .merge()])['result'] end |
#request_airdrop(pubkey, lamports, options = {}) ⇒ String
Request an airdrop of lamports to a given address
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/solace/connection.rb', line 95 def request_airdrop(pubkey, lamports, = {}) @rpc_client.rpc_request( 'requestAirdrop', [ pubkey, lamports, .merge() ] ) end |
#rpc_request(method, params = []) ⇒ Hash
Sends a JSON-RPC request to the configured Solana RPC server.
]
83 84 85 86 87 |
# File 'lib/solace/connection.rb', line 83 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
176 177 178 |
# File 'lib/solace/connection.rb', line 176 def send_transaction(transaction, = {}) @rpc_client.rpc_request('sendTransaction', [transaction, .merge()]) end |
#wait_for_confirmed_signature(commitment = 'confirmed', timeout: 60, interval: 0.1) ⇒ String
Wait until the yielded signature reaches the desired commitment or timeout.
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/solace/connection.rb', line 188 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 dealine_passed?(deadline) return signature if commitment_reached?(signature, commitment) sleep interval end raise Errors::ConfirmationTimeout.format(signature, commitment, timeout) end |