Class: Solace::Connection
- Inherits:
-
Object
- Object
- Solace::Connection
- Defined in:
- lib/solace/connection.rb
Instance Attribute Summary collapse
-
#default_options ⇒ Hash
readonly
The default options for RPC requests.
-
#rpc_url ⇒ String
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(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 = []) ⇒ Object
Make an RPC request to the Solana node.
-
#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
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/solace/connection.rb', line 25 def initialize(rpc_url = 'http://localhost:8899', commitment: 'confirmed') @request_id = nil @rpc_url = rpc_url # Set default options = { commitment: commitment, encoding: 'base64' } end |
Instance Attribute Details
#default_options ⇒ Hash (readonly)
Returns The default options for RPC requests.
19 20 21 |
# File 'lib/solace/connection.rb', line 19 def end |
#rpc_url ⇒ String (readonly)
Returns The URL of the Solana RPC node.
13 14 15 |
# File 'lib/solace/connection.rb', line 13 def rpc_url @rpc_url end |
Instance Method Details
#get_account_info(pubkey) ⇒ Object
Get the account information from the Solana node
105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/solace/connection.rb', line 105 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
123 124 125 126 127 128 129 130 131 |
# File 'lib/solace/connection.rb', line 123 def get_balance(pubkey) rpc_request( 'getBalance', [ pubkey, ] )['result']['value'] end |
#get_latest_blockhash ⇒ String
Get the latest blockhash from the Solana node
89 90 91 |
# File 'lib/solace/connection.rb', line 89 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
97 98 99 |
# File 'lib/solace/connection.rb', line 97 def get_minimum_lamports_for_rent_exemption(space) rpc_request('getMinimumBalanceForRentExemption', [space])['result'] end |
#get_signature_status(signatures) ⇒ Object
Get the signature status
165 166 167 168 169 170 171 172 173 |
# File 'lib/solace/connection.rb', line 165 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
137 138 139 140 141 142 143 144 145 |
# File 'lib/solace/connection.rb', line 137 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
151 152 153 154 155 156 157 158 159 |
# File 'lib/solace/connection.rb', line 151 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
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/solace/connection.rb', line 75 def request_airdrop(pubkey, lamports, = {}) rpc_request( 'requestAirdrop', [ pubkey, lamports, .merge() ] ) end |
#rpc_request(method, params = []) ⇒ Object
Make an RPC request to the Solana node
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/solace/connection.rb', line 41 def rpc_request(method, params = []) uri = URI(rpc_url) req = Net::HTTP::Post.new(uri) req['Accept'] = 'application/json' req['Content-Type'] = 'application/json' @request_id = SecureRandom.uuid req.body = { jsonrpc: '2.0', id: @request_id, method: method, params: params }.to_json res = Net::HTTP.start( uri.hostname, uri.port, use_ssl: uri.scheme == 'https' ) do |http| http.request(req) end raise "RPC error: #{res.body}" unless res.is_a?(Net::HTTPSuccess) JSON.parse(res.body) end |
#send_transaction(transaction, options = {}) ⇒ String
Send a transaction to the Solana node
179 180 181 182 183 184 185 186 187 |
# File 'lib/solace/connection.rb', line 179 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
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/solace/connection.rb', line 193 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 |