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.
- checksums.yaml +4 -4
- data/lib/solana-ruby/client.rb +755 -234
- data/lib/solana-ruby/keypair.rb +105 -0
- data/lib/solana-ruby/utils.rb +107 -0
- data/lib/solana-ruby/version.rb +3 -3
- data/lib/solana-ruby.rb +4 -2
- metadata +94 -7
data/lib/solana-ruby/client.rb
CHANGED
@@ -1,249 +1,770 @@
|
|
1
|
-
require '
|
1
|
+
require 'faye/websocket'
|
2
|
+
require 'httpx'
|
2
3
|
require 'json'
|
3
4
|
|
4
|
-
|
5
|
-
class Client
|
6
|
-
include HTTParty
|
7
|
-
base_uri 'https://api.mainnet-beta.solana.com'
|
5
|
+
require_relative 'utils'
|
8
6
|
|
9
|
-
|
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
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
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
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
def
|
90
|
-
|
91
|
-
end
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
def
|
110
|
-
|
111
|
-
end
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
def
|
138
|
-
|
139
|
-
end
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
def
|
174
|
-
|
175
|
-
end
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
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
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
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
|
-
|
239
|
-
|
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
|
-
|
243
|
-
|
244
|
-
|
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
|
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
|