solana-ruby 0.1.2 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 301f452c31d1989f2565adbf428c90081ca667eac36089e7b0817807a22d1a87
4
- data.tar.gz: dfec2238bd6552db7b625f3016619837d57bd15735ef2c447b98acb3cad97a31
3
+ metadata.gz: '0009e34787d1786fde5b184a9e9d4ba090b52175abd8cc5d2efba47dbbc8eea9'
4
+ data.tar.gz: 3761ee4dcb2b182b8469c60814045581180770e555844dc74caf27ef27c3364a
5
5
  SHA512:
6
- metadata.gz: 4a655cc6132c2c281e7aa798785a33b1cbb10754940d82d17f1ec33d34ce847d3bec11f4ac35a038973cc976e0e69c8b95b4fabaea0d6d007504e536dfe0c390
7
- data.tar.gz: 3af5cefad5a04134b07cc0562a2ed3ed7720cb1d1289b89483480686f181ddc719ec72d9e3a5320d1b7ec46f4446e2e6d6acdd69e7c30e805056d60317a50bf4
6
+ metadata.gz: 77af58126eb32a211745baa0e1ce8d9a22249723bd2e77f4669d497e841ba7f55c74d0e5cb464f0ee75a195351a4ae1f73bf82677d3bcb19ad09d891088ab773
7
+ data.tar.gz: 70694ab44025b3fe3ad4310f5e49f1649fabb6254c766a369ae4da4f2c8b4bb15a8c6f0c8d2ecd1f9b9803e5c907d62949cc463b16634a8ac0ae7b2ee93a4f2c
@@ -1,6 +1,7 @@
1
1
  require 'faye/websocket'
2
2
  require 'httpx'
3
3
  require 'json'
4
+ require 'thread'
4
5
 
5
6
  require_relative 'utils'
6
7
 
@@ -81,19 +82,18 @@ module Solana
81
82
  # @param [Integer] slot_number The slot number of the block.
82
83
  # @param [Hash] options Optional parameters for the request.
83
84
  # @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)
85
+ def get_block_time(slot_number, &block)
86
+ request_http('getBlockTime', [slot_number], &block)
86
87
  end
87
88
 
88
89
  ##
89
90
  # Retrieves a list of confirmed blocks between two slot numbers.
90
91
  #
91
92
  # @param [Integer] start_slot The start slot number.
92
- # @param [Integer] end_slot The end slot number.
93
93
  # @param [Hash] options Optional parameters for the request.
94
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)
95
+ def get_blocks(start_slot, options = {}, &block)
96
+ request_http('getBlocks', [start_slot, options], &block)
97
97
  end
98
98
 
99
99
  ##
@@ -110,10 +110,9 @@ module Solana
110
110
  ##
111
111
  # Retrieves the list of cluster nodes.
112
112
  #
113
- # @param [Hash] options Optional parameters for the request.
114
113
  # @return [Array<Hash>] The list of cluster nodes.
115
- def get_cluster_nodes(options = {}, &block)
116
- request_http('getClusterNodes', [options], &block)
114
+ def get_cluster_nodes(&block)
115
+ request_http('getClusterNodes', &block)
117
116
  end
118
117
 
119
118
  ##
@@ -128,10 +127,9 @@ module Solana
128
127
  ##
129
128
  # Retrieves the epoch schedule.
130
129
  #
131
- # @param [Hash] options Optional parameters for the request.
132
130
  # @return [Hash] The epoch schedule.
133
- def get_epoch_schedule(options = {}, &block)
134
- request_http('getEpochSchedule', [options], &block)
131
+ def get_epoch_schedule(&block)
132
+ request_http('getEpochSchedule', &block)
135
133
  end
136
134
 
137
135
  ##
@@ -147,46 +145,41 @@ module Solana
147
145
  ##
148
146
  # Retrieves the slot of the first available block.
149
147
  #
150
- # @param [Hash] options Optional parameters for the request.
151
148
  # @return [Integer] The slot of the first available block.
152
- def get_first_available_block(options = {}, &block)
153
- request_http('getFirstAvailableBlock', [options], &block)
149
+ def get_first_available_block(&block)
150
+ request_http('getFirstAvailableBlock', &block)
154
151
  end
155
152
 
156
153
  ##
157
154
  # Retrieves the genesis hash.
158
155
  #
159
- # @param [Hash] options Optional parameters for the request.
160
156
  # @return [String] The genesis hash.
161
- def get_genesis_hash(options = {}, &block)
162
- request_http('getGenesisHash', [options], &block)
157
+ def get_genesis_hash(&block)
158
+ request_http('getGenesisHash', &block)
163
159
  end
164
160
 
165
161
  ##
166
162
  # Checks the health of the node.
167
163
  #
168
- # @param [Hash] options Optional parameters for the request.
169
164
  # @return [String] The health status of the node.
170
- def get_health(options = {}, &block)
171
- request_http('getHealth', [options], &block)
165
+ def get_health(&block)
166
+ request_http('getHealth', &block)
172
167
  end
173
168
 
174
169
  ##
175
170
  # Retrieves the highest snapshot slot.
176
171
  #
177
- # @param [Hash] options Optional parameters for the request.
178
172
  # @return [Integer] The highest snapshot slot.
179
- def get_highest_snapshot_slot(options = {}, &block)
180
- request_http('getHighestSnapshotSlot', [options], &block)
173
+ def get_highest_snapshot_slot(&block)
174
+ request_http('getHighestSnapshotSlot', &block)
181
175
  end
182
176
 
183
177
  ##
184
178
  # Retrieves the identity of the node.
185
179
  #
186
- # @param [Hash] options Optional parameters for the request.
187
180
  # @return [Hash] The identity information of the node.
188
- def get_identity(options = {}, &block)
189
- request_http('getIdentity', [options], &block)
181
+ def get_identity(&block)
182
+ request_http('getIdentity', &block)
190
183
  end
191
184
 
192
185
  ##
@@ -201,10 +194,9 @@ module Solana
201
194
  ##
202
195
  # Retrieves the current inflation rate.
203
196
  #
204
- # @param [Hash] options Optional parameters for the request.
205
197
  # @return [Hash] The inflation rate.
206
- def get_inflation_rate(options = {}, &block)
207
- request_http('getInflationRate', [options], &block)
198
+ def get_inflation_rate(&block)
199
+ request_http('getInflationRate', &block)
208
200
  end
209
201
 
210
202
  ##
@@ -235,31 +227,30 @@ module Solana
235
227
  request_http('getLatestBlockhash', [options], &block)
236
228
  end
237
229
 
230
+ ## TODO
238
231
  ##
239
232
  # Retrieves the leader schedule.
240
233
  #
241
234
  # @param [Hash] options Optional parameters for the request.
242
235
  # @return [Hash] The leader schedule.
243
- def get_leader_schedule(options = {}, &block)
244
- request_http('getLeaderSchedule', [options], &block)
236
+ def get_leader_schedule(slot_number = nil, options = {}, &block)
237
+ request_http('getLeaderSchedule', [slot_number, options], &block)
245
238
  end
246
239
 
247
240
  ##
248
241
  # Retrieves the maximum retransmit slot.
249
242
  #
250
- # @param [Hash] options Optional parameters for the request.
251
243
  # @return [Integer] The maximum retransmit slot.
252
- def get_max_retransmit_slot(options = {}, &block)
253
- request_http('getMaxRetransmitSlot', [options], &block)
244
+ def get_max_retransmit_slot(&block)
245
+ request_http('getMaxRetransmitSlot', &block)
254
246
  end
255
247
 
256
248
  ##
257
249
  # Retrieves the maximum shred insert slot.
258
250
  #
259
- # @param [Hash] options Optional parameters for the request.
260
251
  # @return [Integer] The maximum shred insert slot.
261
- def get_max_shred_insert_slot(options = {}, &block)
262
- request_http('getMaxShredInsertSlot', [options], &block)
252
+ def get_max_shred_insert_slot(&block)
253
+ request_http('getMaxShredInsertSlot', &block)
263
254
  end
264
255
 
265
256
  ##
@@ -297,8 +288,8 @@ module Solana
297
288
  #
298
289
  # @param [Hash] options Optional parameters for the request.
299
290
  # @return [Array<Hash>] The recent performance samples.
300
- def get_recent_performance_samples(options = {}, &block)
301
- request_http('getRecentPerformanceSamples', [options], &block)
291
+ def get_recent_performance_samples(limit = 720, options = {}, &block)
292
+ request_http('getRecentPerformanceSamples', [limit, options], &block)
302
293
  end
303
294
 
304
295
  ##
@@ -306,8 +297,8 @@ module Solana
306
297
  #
307
298
  # @param [Hash] options Optional parameters for the request.
308
299
  # @return [Hash] The recent prioritization fees.
309
- def get_recent_prioritization_fees(options = {}, &block)
310
- request_http('getRecentPrioritizationFees', [options], &block)
300
+ def get_recent_prioritization_fees(addresses = [], &block)
301
+ request_http('getRecentPrioritizationFees', [addresses], &block)
311
302
  end
312
303
 
313
304
  ##
@@ -419,6 +410,7 @@ module Solana
419
410
  request_http('getTokenAccountsByOwner', [owner, opts, options], &block)
420
411
  end
421
412
 
413
+ ## TODO
422
414
  ##
423
415
  # Retrieves the largest accounts for a given token.
424
416
  #
@@ -490,8 +482,8 @@ module Solana
490
482
  #
491
483
  # @param [Hash] options Optional parameters for the request.
492
484
  # @return [Integer] The minimum ledger slot.
493
- def minimum_ledger_slot(options = {}, &block)
494
- request_http('minimumLedgerSlot', [options], &block)
485
+ def minimum_ledger_slot(&block)
486
+ request_http('minimumLedgerSlot', &block)
495
487
  end
496
488
 
497
489
  ##
@@ -509,9 +501,10 @@ module Solana
509
501
  # Sends a transaction.
510
502
  #
511
503
  # @param [Hash] transaction The transaction to send.
504
+ # @param [Hash] options Optional parameters for the request.
512
505
  # @return [Hash] The response from the send transaction request.
513
- def send_transaction(transaction, &block)
514
- request_http('sendTransaction', [transaction.to_json], &block)
506
+ def send_transaction(transaction, options = {}, &block)
507
+ request_http('sendTransaction', [transaction.to_json, options], &block)
515
508
  end
516
509
 
517
510
  ##
@@ -548,8 +541,8 @@ module Solana
548
541
  #
549
542
  # @param [Hash] options Optional parameters for the subscription.
550
543
  # @yield [Object] The response from the subscription.
551
- def block_subscribe(options = {}, &block)
552
- request_ws('blockSubscribe', [options], &block)
544
+ def block_subscribe(filter, options = {}, &block)
545
+ request_ws('blockSubscribe', [filter, options], &block)
553
546
  end
554
547
 
555
548
  ##
@@ -688,11 +681,16 @@ module Solana
688
681
 
689
682
  private
690
683
  ##
691
- # Sends a JSON-RPC request to the Solana API.
684
+ # Sends a JSON-RPC request to the Solana API over HTTP.
685
+ #
686
+ # This method constructs a JSON-RPC request and sends it to the Solana API endpoint using HTTP.
687
+ # It then handles the response asynchronously.
692
688
  #
693
689
  # @param [String] method The RPC method to call.
694
690
  # @param [Array] params The parameters for the RPC method.
695
691
  # @yield [Object] The parsed response from the API.
692
+ # @return [Object, nil] The parsed response from the API if no block is given, otherwise nil.
693
+ # @raise [RuntimeError] If the request fails (non-success response).
696
694
  def request_http(method, params = nil, &block)
697
695
  body = {
698
696
  jsonrpc: '2.0',
@@ -711,12 +709,16 @@ module Solana
711
709
  ##
712
710
  # Handles the API response, checking for success and parsing the result.
713
711
  #
714
- # @param [Faraday::Response] response The HTTP response object.
715
- # @raise [RuntimeError] If the request fails (non-success response).
712
+ # This method processes the HTTP response from the Solana API, checking if the request was successful.
713
+ # If successful, it parses the JSON response and yields the result to the provided block.
714
+ #
715
+ # @param [HTTPX::Response] response The HTTP response object.
716
716
  # @yield [Object] The parsed result from the API response.
717
+ # @return [Object] The parsed result from the API response.
718
+ # @raise [RuntimeError] If the request fails (non-success response).
717
719
  def handle_response_http(response, &block)
718
720
  if response.status == 200
719
- result = JSON.parse(response.body)['result']
721
+ result = JSON.parse(response.body)
720
722
  if block_given?
721
723
  yield result
722
724
  else
@@ -730,11 +732,16 @@ module Solana
730
732
  ##
731
733
  # Sends a JSON-RPC request to the Solana API over WebSocket.
732
734
  #
735
+ # This method constructs a JSON-RPC request and sends it to the Solana API endpoint using WebSocket.
736
+ # It then handles the response asynchronously, providing the result to the provided block or returning it via a queue.
737
+ #
733
738
  # @param [String] method The RPC method to call.
734
739
  # @param [Array] params The parameters for the RPC method.
735
740
  # @yield [Object] The parsed response from the API.
736
-
741
+ # @return [Object, nil] The parsed response from the API if no block is given, otherwise nil.
742
+ # @raise [RuntimeError] If the WebSocket connection fails or an error occurs during communication.
737
743
  def request_ws(method, params = nil, &block)
744
+ result_queue = Queue.new
738
745
  EM.run do
739
746
  ws = Faye::WebSocket::Client.new(@api_endpoint::WS)
740
747
 
@@ -751,7 +758,11 @@ module Solana
751
758
 
752
759
  ws.on :message do |event|
753
760
  response = JSON.parse(event.data)
754
- yield response['result'] if block_given?
761
+ if block_given?
762
+ yield response['result']
763
+ else
764
+ result_queue.push(response['result'])
765
+ end
755
766
  ws.close
756
767
  end
757
768
 
@@ -762,10 +773,12 @@ module Solana
762
773
 
763
774
  ws.on :error do |event|
764
775
  puts "WebSocket error: #{event.message}"
776
+ result_queue.push(nil)
765
777
  ws = nil
766
778
  EM.stop
767
779
  end
768
780
  end
781
+ result_queue.pop unless block_given?
769
782
  end
770
783
  end
771
784
  end
@@ -1,3 +1,3 @@
1
1
  module Solana
2
- VERSION = '0.1.2'.freeze
2
+ VERSION = '0.1.4'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solana-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fabrice Renard
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-30 00:00:00.000000000 Z
11
+ date: 2024-07-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httpx