solana-ruby 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,249 +1,770 @@
1
- require 'httparty'
1
+ require 'faye/websocket'
2
+ require 'httpx'
2
3
  require 'json'
3
4
 
4
- module SolanaRB
5
- class Client
6
- include HTTParty
7
- base_uri 'https://api.mainnet-beta.solana.com'
5
+ require_relative 'utils'
8
6
 
9
- def initialize(api_key = nil)
7
+ module Solana
8
+ ##
9
+ # Client class for interacting with the Solana JSON RPC API over HTTP and WS.
10
+ class Client
11
+ ##
12
+ # Initializes a new Client.
13
+ #
14
+ # @param [String, nil] api_key Optional API key for authentication.
15
+ def initialize(api_endpoint = Solana::Utils::MAINNET, api_key = nil)
10
16
  @api_key = api_key
11
- end
12
-
13
- def get_account_info(pubkey, options = {})
14
- request('getAccountInfo', [pubkey, options])
15
- end
16
-
17
- def get_balance(pubkey, options = {})
18
- request('getBalance', [pubkey, options])
19
- end
20
-
21
- def get_block(slot_number, options = {})
22
- request('getBlock', [slot_number, options])
23
- end
24
-
25
- def get_block_commitment(slot_number, options = {})
26
- request('getBlockCommitment', [slot_number, options])
27
- end
28
-
29
- def get_block_height(options = {})
30
- request('getBlockHeight', [options])
31
- end
32
-
33
- def get_block_production(options = {})
34
- request('getBlockProduction', [options])
35
- end
36
-
37
- def get_block_time(slot_number, options = {})
38
- request('getBlockTime', [slot_number, options])
39
- end
40
-
41
- def get_blocks(start_slot, end_slot, options = {})
42
- request('getBlocks', [start_slot, end_slot, options])
43
- end
44
-
45
- def get_blocks_with_limit(start_slot, limit, options = {})
46
- request('getBlocksWithLimit', [start_slot, limit, options])
47
- end
48
-
49
- def get_cluster_nodes(options = {})
50
- request('getClusterNodes', [options])
51
- end
52
-
53
- def get_epoch_info(options = {})
54
- request('getEpochInfo', [options])
55
- end
56
-
57
- def get_epoch_schedule(options = {})
58
- request('getEpochSchedule', [options])
59
- end
60
-
61
- def get_fee_for_message(message, options = {})
62
- request('getFeeForMessage', [message, options])
63
- end
64
-
65
- def get_first_available_block(options = {})
66
- request('getFirstAvailableBlock', [options])
67
- end
68
-
69
- def get_genesis_hash(options = {})
70
- request('getGenesisHash', [options])
71
- end
72
-
73
- def get_health(options = {})
74
- request('getHealth', [options])
75
- end
76
-
77
- def get_highest_snapshot_slot(options = {})
78
- request('getHighestSnapshotSlot', [options])
79
- end
80
-
81
- def get_identity(options = {})
82
- request('getIdentity', [options])
83
- end
84
-
85
- def get_inflation_governor(options = {})
86
- request('getInflationGovernor', [options])
87
- end
88
-
89
- def get_inflation_rate(options = {})
90
- request('getInflationRate', [options])
91
- end
92
-
93
- def get_inflation_reward(addresses, options = {})
94
- request('getInflationReward', [addresses, options])
95
- end
96
-
97
- def get_largest_accounts(options = {})
98
- request('getLargestAccounts', [options])
99
- end
100
-
101
- def get_latest_blockhash(options = {})
102
- request('getLatestBlockhash', [options])
103
- end
104
-
105
- def get_leader_schedule(options = {})
106
- request('getLeaderSchedule', [options])
107
- end
108
-
109
- def get_max_retransmit_slot(options = {})
110
- request('getMaxRetransmitSlot', [options])
111
- end
112
-
113
- def get_max_shred_insert_slot(options = {})
114
- request('getMaxShredInsertSlot', [options])
115
- end
116
-
117
- def get_minimum_balance_for_rent_exemption(data_length, options = {})
118
- request('getMinimumBalanceForRentExemption', [data_length, options])
119
- end
120
-
121
- def get_multiple_accounts(pubkeys, options = {})
122
- request('getMultipleAccounts', [pubkeys, options])
123
- end
124
-
125
- def get_program_accounts(pubkey, options = {})
126
- request('getProgramAccounts', [pubkey, options])
127
- end
128
-
129
- def get_recent_performance_samples(options = {})
130
- request('getRecentPerformanceSamples', [options])
131
- end
132
-
133
- def get_recent_prioritization_fees(options = {})
134
- request('getRecentPrioritizationFees', [options])
135
- end
136
-
137
- def get_signature_statuses(signatures, options = {})
138
- request('getSignatureStatuses', [signatures, options])
139
- end
140
-
141
- def get_signatures_for_address(address, options = {})
142
- request('getSignaturesForAddress', [address, options])
143
- end
144
-
145
- def get_slot(options = {})
146
- request('getSlot', [options])
147
- end
148
-
149
- def get_slot_leader(options = {})
150
- request('getSlotLeader', [options])
151
- end
152
-
153
- def get_slot_leaders(start_slot, limit, options = {})
154
- request('getSlotLeaders', [start_slot, limit, options])
155
- end
156
-
157
- def get_stake_activation(pubkey, options = {})
158
- request('getStakeActivation', [pubkey, options])
159
- end
160
-
161
- def get_stake_minimum_delegation(options = {})
162
- request('getStakeMinimumDelegation', [options])
163
- end
164
-
165
- def get_supply(options = {})
166
- request('getSupply', [options])
167
- end
168
-
169
- def get_token_account_balance(pubkey, options = {})
170
- request('getTokenAccountBalance', [pubkey, options])
171
- end
172
-
173
- def get_token_accounts_by_delegate(delegate, opts = {}, options = {})
174
- request('getTokenAccountsByDelegate', [delegate, opts, options])
175
- end
176
-
177
- def get_token_accounts_by_owner(owner, opts = {}, options = {})
178
- request('getTokenAccountsByOwner', [owner, opts, options])
179
- end
180
-
181
- def get_token_largest_accounts(pubkey, options = {})
182
- request('getTokenLargestAccounts', [pubkey, options])
183
- end
184
-
185
- def get_token_supply(pubkey, options = {})
186
- request('getTokenSupply', [pubkey, options])
187
- end
188
-
189
- def get_transaction(signature, options = {})
190
- request('getTransaction', [signature, options])
191
- end
192
-
193
- def get_transaction_count(options = {})
194
- request('getTransactionCount', [options])
195
- end
196
-
197
- def get_version(options = {})
198
- request('getVersion', [options])
199
- end
200
-
201
- def get_vote_accounts(options = {})
202
- request('getVoteAccounts', [options])
203
- end
204
-
205
- def is_blockhash_valid(blockhash, options = {})
206
- request('isBlockhashValid', [blockhash, options])
207
- end
208
-
209
- def minimum_ledger_slot(options = {})
210
- request('minimumLedgerSlot', [options])
211
- end
212
-
213
- def request_airdrop(pubkey, lamports, options = {})
214
- request('requestAirdrop', [pubkey, lamports, options])
215
- end
216
-
217
- def send_transaction(transaction, options = {})
218
- request('sendTransaction', [transaction, options])
219
- end
220
-
221
- def simulate_transaction(transaction, options = {})
222
- request('simulateTransaction', [transaction, options])
17
+ @api_endpoint = api_endpoint
18
+ end
19
+
20
+ ##
21
+ # Retrieves account information for a given public key.
22
+ #
23
+ # @param [String] pubkey The public key of the account.
24
+ # @param [Hash] options Optional parameters for the request.
25
+ # @return [Hash] The account information.
26
+ def get_account_info(pubkey, options = {}, &block)
27
+ request_http('getAccountInfo', [pubkey, options], &block)
28
+ end
29
+
30
+ ##
31
+ # Retrieves the balance for a given public key.
32
+ #
33
+ # @param [String] pubkey The public key of the account.
34
+ # @param [Hash] options Optional parameters for the request.
35
+ # @return [Integer] The balance in lamports.
36
+ def get_balance(pubkey, options = {}, &block)
37
+ request_http('getBalance', [pubkey, options], &block)
38
+ end
39
+
40
+ ##
41
+ # Retrieves information about a specific block.
42
+ #
43
+ # @param [Integer] slot_number The slot number of the block.
44
+ # @param [Hash] options Optional parameters for the request.
45
+ # @return [Hash] The block information.
46
+ def get_block(slot_number, options = {}, &block)
47
+ request_http('getBlock', [slot_number, options], &block)
48
+ end
49
+
50
+ ##
51
+ # Retrieves block commitment information for a specific block.
52
+ #
53
+ # @param [Integer] slot_number The slot number of the block.
54
+ # @param [Hash] options Optional parameters for the request.
55
+ # @return [Hash] The block commitment information.
56
+ def get_block_commitment(slot_number, options = {}, &block)
57
+ request_http('getBlockCommitment', [slot_number, options], &block)
58
+ end
59
+
60
+ ##
61
+ # Retrieves the current block height.
62
+ #
63
+ # @param [Hash] options Optional parameters for the request.
64
+ # @return [Integer] The current block height.
65
+ def get_block_height(options = {}, &block)
66
+ request_http('getBlockHeight', [options], &block)
67
+ end
68
+
69
+ ##
70
+ # Retrieves block production information.
71
+ #
72
+ # @param [Hash] options Optional parameters for the request.
73
+ # @return [Hash] The block production information.
74
+ def get_block_production(options = {}, &block)
75
+ request_http('getBlockProduction', [options], &block)
76
+ end
77
+
78
+ ##
79
+ # Retrieves the estimated production time of a specific block.
80
+ #
81
+ # @param [Integer] slot_number The slot number of the block.
82
+ # @param [Hash] options Optional parameters for the request.
83
+ # @return [Integer] The estimated production time in seconds.
84
+ def get_block_time(slot_number, options = {}, &block)
85
+ request_http('getBlockTime', [slot_number, options], &block)
86
+ end
87
+
88
+ ##
89
+ # Retrieves a list of confirmed blocks between two slot numbers.
90
+ #
91
+ # @param [Integer] start_slot The start slot number.
92
+ # @param [Integer] end_slot The end slot number.
93
+ # @param [Hash] options Optional parameters for the request.
94
+ # @return [Array<Integer>] The list of confirmed blocks.
95
+ def get_blocks(start_slot, end_slot, options = {}, &block)
96
+ request_http('getBlocks', [start_slot, end_slot, options], &block)
97
+ end
98
+
99
+ ##
100
+ # Retrieves a list of confirmed blocks starting from a given slot number with a limit on the number of blocks.
101
+ #
102
+ # @param [Integer] start_slot The start slot number.
103
+ # @param [Integer] limit The maximum number of blocks to return.
104
+ # @param [Hash] options Optional parameters for the request.
105
+ # @return [Array<Integer>] The list of confirmed blocks.
106
+ def get_blocks_with_limit(start_slot, limit, options = {}, &block)
107
+ request_http('getBlocksWithLimit', [start_slot, limit, options], &block)
108
+ end
109
+
110
+ ##
111
+ # Retrieves the list of cluster nodes.
112
+ #
113
+ # @param [Hash] options Optional parameters for the request.
114
+ # @return [Array<Hash>] The list of cluster nodes.
115
+ def get_cluster_nodes(options = {}, &block)
116
+ request_http('getClusterNodes', [options], &block)
117
+ end
118
+
119
+ ##
120
+ # Retrieves epoch information.
121
+ #
122
+ # @param [Hash] options Optional parameters for the request.
123
+ # @return [Hash] The epoch information.
124
+ def get_epoch_info(options = {}, &block)
125
+ request_http('getEpochInfo', [options], &block)
126
+ end
127
+
128
+ ##
129
+ # Retrieves the epoch schedule.
130
+ #
131
+ # @param [Hash] options Optional parameters for the request.
132
+ # @return [Hash] The epoch schedule.
133
+ def get_epoch_schedule(options = {}, &block)
134
+ request_http('getEpochSchedule', [options], &block)
135
+ end
136
+
137
+ ##
138
+ # Retrieves the fee for a given message.
139
+ #
140
+ # @param [String] message The message for which the fee is to be calculated.
141
+ # @param [Hash] options Optional parameters for the request.
142
+ # @return [Integer] The fee for the message.
143
+ def get_fee_for_message(message, options = {}, &block)
144
+ request_http('getFeeForMessage', [message, options], &block)
145
+ end
146
+
147
+ ##
148
+ # Retrieves the slot of the first available block.
149
+ #
150
+ # @param [Hash] options Optional parameters for the request.
151
+ # @return [Integer] The slot of the first available block.
152
+ def get_first_available_block(options = {}, &block)
153
+ request_http('getFirstAvailableBlock', [options], &block)
154
+ end
155
+
156
+ ##
157
+ # Retrieves the genesis hash.
158
+ #
159
+ # @param [Hash] options Optional parameters for the request.
160
+ # @return [String] The genesis hash.
161
+ def get_genesis_hash(options = {}, &block)
162
+ request_http('getGenesisHash', [options], &block)
163
+ end
164
+
165
+ ##
166
+ # Checks the health of the node.
167
+ #
168
+ # @param [Hash] options Optional parameters for the request.
169
+ # @return [String] The health status of the node.
170
+ def get_health(options = {}, &block)
171
+ request_http('getHealth', [options], &block)
172
+ end
173
+
174
+ ##
175
+ # Retrieves the highest snapshot slot.
176
+ #
177
+ # @param [Hash] options Optional parameters for the request.
178
+ # @return [Integer] The highest snapshot slot.
179
+ def get_highest_snapshot_slot(options = {}, &block)
180
+ request_http('getHighestSnapshotSlot', [options], &block)
181
+ end
182
+
183
+ ##
184
+ # Retrieves the identity of the node.
185
+ #
186
+ # @param [Hash] options Optional parameters for the request.
187
+ # @return [Hash] The identity information of the node.
188
+ def get_identity(options = {}, &block)
189
+ request_http('getIdentity', [options], &block)
190
+ end
191
+
192
+ ##
193
+ # Retrieves the current inflation governor settings.
194
+ #
195
+ # @param [Hash] options Optional parameters for the request.
196
+ # @return [Hash] The inflation governor settings.
197
+ def get_inflation_governor(options = {}, &block)
198
+ request_http('getInflationGovernor', [options], &block)
199
+ end
200
+
201
+ ##
202
+ # Retrieves the current inflation rate.
203
+ #
204
+ # @param [Hash] options Optional parameters for the request.
205
+ # @return [Hash] The inflation rate.
206
+ def get_inflation_rate(options = {}, &block)
207
+ request_http('getInflationRate', [options], &block)
208
+ end
209
+
210
+ ##
211
+ # Retrieves the inflation reward for a given list of addresses.
212
+ #
213
+ # @param [Array<String>] addresses The list of addresses.
214
+ # @param [Hash] options Optional parameters for the request.
215
+ # @return [Array<Hash>] The inflation rewards for the addresses.
216
+ def get_inflation_reward(addresses, options = {}, &block)
217
+ request_http('getInflationReward', [addresses, options], &block)
218
+ end
219
+
220
+ ##
221
+ # Retrieves the largest accounts.
222
+ #
223
+ # @param [Hash] options Optional parameters for the request.
224
+ # @return [Array<Hash>] The largest accounts.
225
+ def get_largest_accounts(options = {}, &block)
226
+ request_http('getLargestAccounts', [options], &block)
227
+ end
228
+
229
+ ##
230
+ # Retrieves the latest blockhash.
231
+ #
232
+ # @param [Hash] options Optional parameters for the request.
233
+ # @return [Hash] The latest blockhash.
234
+ def get_latest_blockhash(options = {}, &block)
235
+ request_http('getLatestBlockhash', [options], &block)
236
+ end
237
+
238
+ ##
239
+ # Retrieves the leader schedule.
240
+ #
241
+ # @param [Hash] options Optional parameters for the request.
242
+ # @return [Hash] The leader schedule.
243
+ def get_leader_schedule(options = {}, &block)
244
+ request_http('getLeaderSchedule', [options], &block)
245
+ end
246
+
247
+ ##
248
+ # Retrieves the maximum retransmit slot.
249
+ #
250
+ # @param [Hash] options Optional parameters for the request.
251
+ # @return [Integer] The maximum retransmit slot.
252
+ def get_max_retransmit_slot(options = {}, &block)
253
+ request_http('getMaxRetransmitSlot', [options], &block)
254
+ end
255
+
256
+ ##
257
+ # Retrieves the maximum shred insert slot.
258
+ #
259
+ # @param [Hash] options Optional parameters for the request.
260
+ # @return [Integer] The maximum shred insert slot.
261
+ def get_max_shred_insert_slot(options = {}, &block)
262
+ request_http('getMaxShredInsertSlot', [options], &block)
263
+ end
264
+
265
+ ##
266
+ # Retrieves the minimum balance required for rent exemption for a given data length.
267
+ #
268
+ # @param [Integer] data_length The length of the data in bytes.
269
+ # @param [Hash] options Optional parameters for the request.
270
+ # @return [Integer] The minimum balance for rent exemption.
271
+ def get_minimum_balance_for_rent_exemption(data_length, options = {}, &block)
272
+ request_http('getMinimumBalanceForRentExemption', [data_length, options], &block)
273
+ end
274
+
275
+ ##
276
+ # Retrieves information for multiple accounts.
277
+ #
278
+ # @param [Array<String>] pubkeys The list of public keys.
279
+ # @param [Hash] options Optional parameters for the request.
280
+ # @return [Array<Hash>] The information for the accounts.
281
+ def get_multiple_accounts(pubkeys, options = {}, &block)
282
+ request_http('getMultipleAccounts', [pubkeys, options], &block)
283
+ end
284
+
285
+ ##
286
+ # Retrieves information for accounts owned by a specific program.
287
+ #
288
+ # @param [String] pubkey The public key of the program.
289
+ # @param [Hash] options Optional parameters for the request.
290
+ # @return [Array<Hash>] The information for the program accounts.
291
+ def get_program_accounts(pubkey, options = {}, &block)
292
+ request_http('getProgramAccounts', [pubkey, options], &block)
293
+ end
294
+
295
+ ##
296
+ # Retrieves recent performance samples.
297
+ #
298
+ # @param [Hash] options Optional parameters for the request.
299
+ # @return [Array<Hash>] The recent performance samples.
300
+ def get_recent_performance_samples(options = {}, &block)
301
+ request_http('getRecentPerformanceSamples', [options], &block)
302
+ end
303
+
304
+ ##
305
+ # Retrieves recent prioritization fees.
306
+ #
307
+ # @param [Hash] options Optional parameters for the request.
308
+ # @return [Hash] The recent prioritization fees.
309
+ def get_recent_prioritization_fees(options = {}, &block)
310
+ request_http('getRecentPrioritizationFees', [options], &block)
311
+ end
312
+
313
+ ##
314
+ # Retrieves the status of given transaction signatures.
315
+ #
316
+ # @param [Array<String>] signatures The list of transaction signatures.
317
+ # @param [Hash] options Optional parameters for the request.
318
+ # @return [Array<Hash>] The status of the transaction signatures.
319
+ def get_signature_statuses(signatures, options = {}, &block)
320
+ request_http('getSignatureStatuses', [signatures, options], &block)
321
+ end
322
+
323
+ ##
324
+ # Retrieves the signatures for a given address.
325
+ #
326
+ # @param [String] address The address for which to retrieve signatures.
327
+ # @param [Hash] options Optional parameters for the request.
328
+ # @return [Array<Hash>] The signatures for the address.
329
+ def get_signatures_for_address(address, options = {}, &block)
330
+ request_http('getSignaturesForAddress', [address, options], &block)
331
+ end
332
+
333
+ ##
334
+ # Retrieves the current slot.
335
+ #
336
+ # @param [Hash] options Optional parameters for the request.
337
+ # @return [Integer] The current slot.
338
+ def get_slot(options = {}, &block)
339
+ request_http('getSlot', [options], &block)
340
+ end
341
+
342
+ ##
343
+ # Retrieves the current slot leader.
344
+ #
345
+ # @param [Hash] options Optional parameters for the request.
346
+ # @return [String] The current slot leader.
347
+ def get_slot_leader(options = {}, &block)
348
+ request_http('getSlotLeader', [options], &block)
349
+ end
350
+
351
+ ##
352
+ # Retrieves the slot leaders starting from a given slot with a limit on the number of leaders.
353
+ #
354
+ # @param [Integer] start_slot The start slot number.
355
+ # @param [Integer] limit The maximum number of leaders to return.
356
+ # @param [Hash] options Optional parameters for the request.
357
+ # @return [Array<String>] The slot leaders.
358
+ def get_slot_leaders(start_slot, limit, options = {}, &block)
359
+ request_http('getSlotLeaders', [start_slot, limit, options], &block)
360
+ end
361
+
362
+ ##
363
+ # Retrieves the stake activation information for a given public key.
364
+ #
365
+ # @param [String] pubkey The public key of the stake account.
366
+ # @param [Hash] options Optional parameters for the request.
367
+ # @return [Hash] The stake activation information.
368
+ def get_stake_activation(pubkey, options = {}, &block)
369
+ request_http('getStakeActivation', [pubkey, options], &block)
370
+ end
371
+
372
+ ##
373
+ # Retrieves the minimum delegation for a stake account.
374
+ #
375
+ # @param [Hash] options Optional parameters for the request.
376
+ # @return [Integer] The minimum delegation.
377
+ def get_stake_minimum_delegation(options = {}, &block)
378
+ request_http('getStakeMinimumDelegation', [options], &block)
379
+ end
380
+
381
+ ##
382
+ # Retrieves the supply information.
383
+ #
384
+ # @param [Hash] options Optional parameters for the request.
385
+ # @return [Hash] The supply information.
386
+ def get_supply(options = {}, &block)
387
+ request_http('getSupply', [options], &block)
388
+ end
389
+
390
+ ##
391
+ # Retrieves the token balance for a given token account.
392
+ #
393
+ # @param [String] pubkey The public key of the token account.
394
+ # @param [Hash] options Optional parameters for the request.
395
+ # @return [Hash] The token balance.
396
+ def get_token_account_balance(pubkey, options = {}, &block)
397
+ request_http('getTokenAccountBalance', [pubkey, options], &block)
398
+ end
399
+
400
+ ##
401
+ # Retrieves token accounts by delegate.
402
+ #
403
+ # @param [String] delegate The delegate address.
404
+ # @param [Hash] opts Additional options for the request.
405
+ # @param [Hash] options Optional parameters for the request.
406
+ # @return [Array<Hash>] The token accounts by delegate.
407
+ def get_token_accounts_by_delegate(delegate, opts = {}, options = {}, &block)
408
+ request_http('getTokenAccountsByDelegate', [delegate, opts, options], &block)
409
+ end
410
+
411
+ ##
412
+ # Retrieves token accounts by owner.
413
+ #
414
+ # @param [String] owner The owner address.
415
+ # @param [Hash] opts Additional options for the request.
416
+ # @param [Hash] options Optional parameters for the request.
417
+ # @return [Array<Hash>] The token accounts by owner.
418
+ def get_token_accounts_by_owner(owner, opts = {}, options = {}, &block)
419
+ request_http('getTokenAccountsByOwner', [owner, opts, options], &block)
420
+ end
421
+
422
+ ##
423
+ # Retrieves the largest accounts for a given token.
424
+ #
425
+ # @param [String] pubkey The public key of the token.
426
+ # @param [Hash] options Optional parameters for the request.
427
+ # @return [Array<Hash>] The largest accounts for the token.
428
+ def get_token_largest_accounts(pubkey, options = {}, &block)
429
+ request_http('getTokenLargestAccounts', [pubkey, options], &block)
430
+ end
431
+
432
+ ##
433
+ # Retrieves the supply of a given token.
434
+ #
435
+ # @param [String] pubkey The public key of the token.
436
+ # @param [Hash] options Optional parameters for the request.
437
+ # @return [Hash] The token supply.
438
+ def get_token_supply(pubkey, options = {}, &block)
439
+ request_http('getTokenSupply', [pubkey, options], &block)
440
+ end
441
+
442
+ ##
443
+ # Retrieves transaction details for a given signature.
444
+ #
445
+ # @param [String] signature The transaction signature.
446
+ # @param [Hash] options Optional parameters for the request.
447
+ # @return [Hash] The transaction details.
448
+ def get_transaction(signature, options = {}, &block)
449
+ request_http('getTransaction', [signature, options], &block)
450
+ end
451
+
452
+ ##
453
+ # Retrieves the total number of transactions processed by the network.
454
+ #
455
+ # @param [Hash] options Optional parameters for the request.
456
+ # @return [Integer] The total number of transactions.
457
+ def get_transaction_count(options = {}, &block)
458
+ request_http('getTransactionCount', [options], &block)
459
+ end
460
+
461
+ ##
462
+ # Retrieves the current version of the Solana software.
463
+ #
464
+ # @return [Hash] The current version information.
465
+ def get_version(&block)
466
+ request_http('getVersion', &block)
467
+ end
468
+
469
+ ##
470
+ # Retrieves the list of vote accounts.
471
+ #
472
+ # @param [Hash] options Optional parameters for the request.
473
+ # @return [Hash] The list of vote accounts.
474
+ def get_vote_accounts(options = {}, &block)
475
+ request_http('getVoteAccounts', [options], &block)
476
+ end
477
+
478
+ ##
479
+ # Checks if a given blockhash is valid.
480
+ #
481
+ # @param [String] blockhash The blockhash to check.
482
+ # @param [Hash] options Optional parameters for the request.
483
+ # @return [Boolean] Whether the blockhash is valid.
484
+ def is_blockhash_valid(blockhash, options = {}, &block)
485
+ request_http('isBlockhashValid', [blockhash, options], &block)
486
+ end
487
+
488
+ ##
489
+ # Retrieves the minimum ledger slot.
490
+ #
491
+ # @param [Hash] options Optional parameters for the request.
492
+ # @return [Integer] The minimum ledger slot.
493
+ def minimum_ledger_slot(options = {}, &block)
494
+ request_http('minimumLedgerSlot', [options], &block)
495
+ end
496
+
497
+ ##
498
+ # Requests an airdrop to a given public key.
499
+ #
500
+ # @param [String] pubkey The public key to receive the airdrop.
501
+ # @param [Integer] lamports The amount of lamports to airdrop.
502
+ # @param [Hash] options Optional parameters for the request.
503
+ # @return [Hash] The airdrop request response.
504
+ def request_airdrop(pubkey, lamports, options = {}, &block)
505
+ request_http('requestAirdrop', [pubkey, lamports, options], &block)
506
+ end
507
+
508
+ ##
509
+ # Sends a transaction.
510
+ #
511
+ # @param [Hash] transaction The transaction to send.
512
+ # @return [Hash] The response from the send transaction request.
513
+ def send_transaction(transaction, &block)
514
+ request_http('sendTransaction', [transaction.to_json], &block)
515
+ end
516
+
517
+ ##
518
+ # Simulates a transaction.
519
+ #
520
+ # @param [Hash] transaction The transaction to simulate.
521
+ # @param [Hash] options Optional parameters for the request.
522
+ # @return [Hash] The simulation response.
523
+ def simulate_transaction(transaction, options = {}, &block)
524
+ request_http('simulateTransaction', [transaction.to_json, options], &block)
525
+ end
526
+
527
+ ##
528
+ # Subscribes to account changes.
529
+ #
530
+ # @param [String] pubkey The public key of the account.
531
+ # @param [Hash] options Optional parameters for the subscription.
532
+ # @yield [Object] The response from the subscription.
533
+ def account_subscribe(pubkey, options = {}, &block)
534
+ request_ws('accountSubscribe', [pubkey, options], &block)
535
+ end
536
+
537
+ ##
538
+ # Unsubscribes from account changes.
539
+ #
540
+ # @param [Integer] subscription_id The subscription ID.
541
+ # @yield [Object] The response from the unsubscription.
542
+ def account_unsubscribe(subscription_id, &block)
543
+ request_ws('accountUnsubscribe', [subscription_id], &block)
544
+ end
545
+
546
+ ##
547
+ # Subscribes to new blocks.
548
+ #
549
+ # @param [Hash] options Optional parameters for the subscription.
550
+ # @yield [Object] The response from the subscription.
551
+ def block_subscribe(options = {}, &block)
552
+ request_ws('blockSubscribe', [options], &block)
553
+ end
554
+
555
+ ##
556
+ # Unsubscribes from new blocks.
557
+ #
558
+ # @param [Integer] subscription_id The subscription ID.
559
+ # @yield [Object] The response from the unsubscription.
560
+ def block_unsubscribe(subscription_id, &block)
561
+ request_ws('blockUnsubscribe', [subscription_id], &block)
562
+ end
563
+
564
+ ##
565
+ # Subscribes to log messages.
566
+ #
567
+ # @param [String, Hash] filter The filter for log messages (e.g., a public key or a set of options).
568
+ # @param [Hash] options Optional parameters for the subscription.
569
+ # @yield [Object] The response from the subscription.
570
+ def logs_subscribe(filter, options = {}, &block)
571
+ request_ws('logsSubscribe', [filter, options], &block)
572
+ end
573
+
574
+ ##
575
+ # Unsubscribes from log messages.
576
+ #
577
+ # @param [Integer] subscription_id The subscription ID.
578
+ # @yield [Object] The response from the unsubscription.
579
+ def logs_unsubscribe(subscription_id, &block)
580
+ request_ws('logsUnsubscribe', [subscription_id], &block)
581
+ end
582
+
583
+ ##
584
+ # Subscribes to program changes.
585
+ #
586
+ # @param [String] pubkey The public key of the program.
587
+ # @param [Hash] options Optional parameters for the subscription.
588
+ # @yield [Object] The response from the subscription.
589
+ def program_subscribe(pubkey, options = {}, &block)
590
+ request_ws('programSubscribe', [pubkey, options], &block)
591
+ end
592
+
593
+ ##
594
+ # Unsubscribes from program changes.
595
+ #
596
+ # @param [Integer] subscription_id The subscription ID.
597
+ # @yield [Object] The response from the unsubscription.
598
+ def program_unsubscribe(subscription_id, &block)
599
+ request_ws('programUnsubscribe', [subscription_id], &block)
600
+ end
601
+
602
+ ##
603
+ # Subscribes to root changes.
604
+ #
605
+ # @yield [Object] The response from the subscription.
606
+ def root_subscribe(&block)
607
+ request_ws('rootSubscribe', &block)
608
+ end
609
+
610
+ ##
611
+ # Unsubscribes from root changes.
612
+ #
613
+ # @param [Integer] subscription_id The subscription ID.
614
+ # @yield [Object] The response from the unsubscription.
615
+ def root_unsubscribe(subscription_id, &block)
616
+ request_ws('rootUnsubscribe', [subscription_id], &block)
617
+ end
618
+
619
+ ##
620
+ # Subscribes to signature status changes.
621
+ #
622
+ # @param [String] signature The signature to monitor.
623
+ # @param [Hash] options Optional parameters for the subscription.
624
+ # @yield [Object] The response from the subscription.
625
+ def signature_subscribe(signature, options = {}, &block)
626
+ request_ws('signatureSubscribe', [signature, options], &block)
627
+ end
628
+
629
+ ##
630
+ # Unsubscribes from signature status changes.
631
+ #
632
+ # @param [Integer] subscription_id The subscription ID.
633
+ # @yield [Object] The response from the unsubscription.
634
+ def signature_unsubscribe(subscription_id, &block)
635
+ request_ws('signatureUnsubscribe', [subscription_id], &block)
636
+ end
637
+
638
+ ##
639
+ # Subscribes to slot changes.
640
+ #
641
+ # @yield [Object] The response from the subscription.
642
+ def slot_subscribe(&block)
643
+ request_ws('slotSubscribe', &block)
644
+ end
645
+
646
+ ##
647
+ # Unsubscribes from slot changes.
648
+ #
649
+ # @param [Integer] subscription_id The subscription ID.
650
+ # @yield [Object] The response from the unsubscription.
651
+ def slot_unsubscribe(subscription_id, &block)
652
+ request_ws('slotUnsubscribe', [subscription_id], &block)
653
+ end
654
+
655
+ ##
656
+ # Subscribes to slot updates.
657
+ #
658
+ # @yield [Object] The response from the subscription.
659
+ def slots_updates_subscribe(&block)
660
+ request_ws('slotsUpdatesSubscribe', &block)
661
+ end
662
+
663
+ ##
664
+ # Unsubscribes from slot updates.
665
+ #
666
+ # @param [Integer] subscription_id The subscription ID.
667
+ # @yield [Object] The response from the unsubscription.
668
+ def slots_updates_unsubscribe(subscription_id, &block)
669
+ request_ws('slotsUpdatesUnsubscribe', [subscription_id], &block)
670
+ end
671
+
672
+ ##
673
+ # Subscribes to vote updates.
674
+ #
675
+ # @yield [Object] The response from the subscription.
676
+ def vote_subscribe(&block)
677
+ request_ws('voteSubscribe', &block)
678
+ end
679
+
680
+ ##
681
+ # Unsubscribes from vote updates.
682
+ #
683
+ # @param [Integer] subscription_id The subscription ID.
684
+ # @yield [Object] The response from the unsubscription.
685
+ def vote_unsubscribe(subscription_id, &block)
686
+ request_ws('voteUnsubscribe', [subscription_id], &block)
223
687
  end
224
688
 
225
689
  private
226
-
227
- def request(method, params = [])
228
- options = {
229
- headers: { 'Content-Type' => 'application/json' },
230
- body: {
231
- jsonrpc: '2.0',
232
- method: method,
233
- params: params,
234
- id: 1
235
- }.to_json
690
+ ##
691
+ # Sends a JSON-RPC request to the Solana API.
692
+ #
693
+ # @param [String] method The RPC method to call.
694
+ # @param [Array] params The parameters for the RPC method.
695
+ # @yield [Object] The parsed response from the API.
696
+ def request_http(method, params = nil, &block)
697
+ body = {
698
+ jsonrpc: '2.0',
699
+ method: method,
700
+ id: 1
236
701
  }
702
+ body[:params] = params if params
237
703
 
238
- response = self.class.post('/', options)
239
- handle_response(response)
704
+ HTTPX.post(@api_endpoint::HTTP, json: body).then do |response|
705
+ handle_response_http(response, &block)
706
+ rescue => e
707
+ puts "HTTP request failed: #{e}"
708
+ end
240
709
  end
241
710
 
242
- def handle_response(response)
243
- if response.success?
244
- response.parsed_response['result']
711
+ ##
712
+ # Handles the API response, checking for success and parsing the result.
713
+ #
714
+ # @param [Faraday::Response] response The HTTP response object.
715
+ # @raise [RuntimeError] If the request fails (non-success response).
716
+ # @yield [Object] The parsed result from the API response.
717
+ def handle_response_http(response, &block)
718
+ if response.status == 200
719
+ result = JSON.parse(response.body)['result']
720
+ if block_given?
721
+ yield result
722
+ else
723
+ result
724
+ end
245
725
  else
246
- raise "Request failed: #{response.code} #{response.message}"
726
+ raise "Request failed"
727
+ end
728
+ end
729
+
730
+ ##
731
+ # Sends a JSON-RPC request to the Solana API over WebSocket.
732
+ #
733
+ # @param [String] method The RPC method to call.
734
+ # @param [Array] params The parameters for the RPC method.
735
+ # @yield [Object] The parsed response from the API.
736
+
737
+ def request_ws(method, params = nil, &block)
738
+ EM.run do
739
+ ws = Faye::WebSocket::Client.new(@api_endpoint::WS)
740
+
741
+ ws.on :open do |event|
742
+ body = {
743
+ jsonrpc: '2.0',
744
+ method: method,
745
+ id: 1
746
+ }
747
+ body[:params] = params if params
748
+
749
+ ws.send(body.to_json)
750
+ end
751
+
752
+ ws.on :message do |event|
753
+ response = JSON.parse(event.data)
754
+ yield response['result'] if block_given?
755
+ ws.close
756
+ end
757
+
758
+ ws.on :close do |event|
759
+ ws = nil
760
+ EM.stop
761
+ end
762
+
763
+ ws.on :error do |event|
764
+ puts "WebSocket error: #{event.message}"
765
+ ws = nil
766
+ EM.stop
767
+ end
247
768
  end
248
769
  end
249
770
  end