Class: SolanaRpcRuby::WebsocketsMethodsWrapper

Inherits:
Object
  • Object
show all
Includes:
HelperMethods, RequestBody
Defined in:
lib/solana_rpc_ruby/websocket_methods_wrapper.rb

Overview

WebsocketsMethodsWrapper class serves as a wrapper for solana JSON RPC API websocket methods. All informations about params:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HelperMethods

#blank?, #create_method_name

Methods included from RequestBody

#base_body, #create_json_body

Constructor Details

#initialize(websocket_client: WebsocketClient, cluster: SolanaRpcRuby.ws_cluster, id: rand(1...99_999)) ⇒ WebsocketsMethodsWrapper

Initialize object with cluster address where requests will be sent.

Parameters:

  • api_client (ApiClient)
  • cluster (String) (defaults to: SolanaRpcRuby.ws_cluster)

    cluster where requests will be sent.

  • id (Integer) (defaults to: rand(1...99_999))

    unique client-generated identifying integer.


31
32
33
34
35
36
37
38
# File 'lib/solana_rpc_ruby/websocket_methods_wrapper.rb', line 31

def initialize(
  websocket_client: WebsocketClient,
  cluster: SolanaRpcRuby.ws_cluster,
  id: rand(1...99_999)
)
  @websocket_client = websocket_client.new(cluster: cluster)
  @id = id
end

Instance Attribute Details

#clusterString

Cluster where requests will be sent.

Returns:

  • (String)

20
21
22
# File 'lib/solana_rpc_ruby/websocket_methods_wrapper.rb', line 20

def cluster
  @cluster
end

#idInteger

Unique client-generated identifying integer.

Returns:

  • (Integer)

24
25
26
# File 'lib/solana_rpc_ruby/websocket_methods_wrapper.rb', line 24

def id
  @id
end

#websocket_clientSolanaRpcRuby::WebsocketClient

Determines which cluster will be used to send requests.


16
17
18
# File 'lib/solana_rpc_ruby/websocket_methods_wrapper.rb', line 16

def websocket_client
  @websocket_client
end

Instance Method Details

#account_subscribe(account_pubkey, commitment: nil, encoding: '', &block) ⇒ Integer

Subscribe to an account to receive notifications when the lamports or data for a given account public key changes

Parameters:

  • account_pubkey (String)
  • commitment (String) (defaults to: nil)
  • encoding (String) (defaults to: '')
  • &block (Proc)

Returns:

  • (Integer)

    Subscription id (needed to unsubscribe)

See Also:


49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/solana_rpc_ruby/websocket_methods_wrapper.rb', line 49

def (, commitment: nil, encoding: '', &block)
  method = create_method_name(__method__)

  params = []
  params_hash = {}
  params_hash['commitment'] = commitment unless blank?(commitment)
  params_hash['encoding'] = encoding unless blank?(encoding)

  params << 
  params << params_hash if params_hash.any?

  subscribe(method, method_params: params, &block)
end

#account_unsubscribe(subscription_id) ⇒ Bool

Unsubscribe from account change notifications

Parameters:

  • subscription_id (Integer)

Returns:

  • (Bool)

    unsubscribe success message

See Also:


69
70
71
72
# File 'lib/solana_rpc_ruby/websocket_methods_wrapper.rb', line 69

def (subscription_id)
  method = create_method_name(__method__)
  unsubscribe(method, subscription_id: subscription_id)
end

#block_subscribe(filter, commitment: nil, encoding: '', transaction_details: '', show_rewards: nil, &block) ⇒ Integer

This subscription is unstable and only available if the validator was started with the –rpc-pubsub-enable-block-subscription flag. The format of this subscription may change in the future

Subscribe to receive notification anytime a new block is Confirmed or Finalized.

Parameters:

  • filter (String)

    # 'all' or public key as base-58 endcoded string

  • commitment (String) (defaults to: nil)
  • encoding (String) (defaults to: '')
  • transaction_details (String) (defaults to: '')
  • show_rewards (Boolean) (defaults to: nil)
  • &block (Proc)

Returns:

  • (Integer)

    Subscription id (needed to unsubscribe)

See Also:


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/solana_rpc_ruby/websocket_methods_wrapper.rb', line 87

def block_subscribe(
  filter,
  commitment: nil,
  encoding: '',
  transaction_details: '',
  show_rewards: nil,
  &block
)
  method = create_method_name(__method__)

  params = []
  param_filter = nil
  params_hash = {}

  param_filter = filter == 'all' ? filter : { 'mentionsAccountOrProgram': filter}

  params_hash['commitment'] = commitment unless blank?(commitment)
  params_hash['encoding'] = encoding unless blank?(encoding)
  params_hash['transactionDetails'] = transaction_details unless blank?(transaction_details)
  params_hash['showRewards'] = show_rewards unless blank?(show_rewards)

  params << param_filter
  params << params_hash if params_hash.any?

  subscribe(method, method_params: params, &block)
end

#block_unsubscribe(subscription_id) ⇒ Bool

Unsubscribe from block notifications

Parameters:

  • subscription_id (Integer)

Returns:

  • (Bool)

    unsubscribe success message

See Also:


120
121
122
123
# File 'lib/solana_rpc_ruby/websocket_methods_wrapper.rb', line 120

def block_unsubscribe(subscription_id)
  method = create_method_name(__method__)
  unsubscribe(method, subscription_id: subscription_id)
end

#logs_subscribe(filter, commitment: nil, &block) ⇒ Integer

Subscribe to transaction logging

Parameters:

  • filter (String)

    |[Hash]

  • commitment (String) (defaults to: nil)
  • &block (Proc)

Options Hash (filter):

  • :mentions (Array)

Returns:

  • (Integer)

    Subscription id (needed to unsubscribe)

See Also:


134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/solana_rpc_ruby/websocket_methods_wrapper.rb', line 134

def logs_subscribe(filter, commitment: nil, &block)
  method = create_method_name(__method__)

  params = []
  params_hash = {}
  params_hash['commitment'] = commitment unless blank?(commitment)

  params << filter
  params << params_hash

  subscribe(method, method_params: params, &block)
end

#logs_unsubscribe(subscription_id) ⇒ Bool

Unsubscribe from transaction logging

Parameters:

  • subscription_id (Integer)

Returns:

  • (Bool)

    unsubscribe success message

See Also:


154
155
156
157
# File 'lib/solana_rpc_ruby/websocket_methods_wrapper.rb', line 154

def logs_unsubscribe(subscription_id)
  method = create_method_name(__method__)
  unsubscribe(method, subscription_id: subscription_id)
end

#program_subscribe(program_id_pubkey, commitment: nil, encoding: '', filters: [], &block) ⇒ Integer

Subscribe to a program to receive notifications when the lamports or data for a given account owned by the program changes

Parameters:

  • account_pubkey (String)
  • commitment (String) (defaults to: nil)
  • encoding (String) (defaults to: '')
  • filters (Array) (defaults to: [])
  • &block (Proc)

Returns:

  • (Integer)

    Subscription id (needed to unsubscribe)

See Also:


169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/solana_rpc_ruby/websocket_methods_wrapper.rb', line 169

def program_subscribe(
  program_id_pubkey,
  commitment: nil,
  encoding: '',
  filters: [],
  &block
)
  method = create_method_name(__method__)

  params = []
  params_hash = {}
  params_hash['commitment'] = commitment unless blank?(commitment)
  params_hash['encoding'] = encoding unless blank?(encoding)
  params_hash['filters'] = filters unless blank?(filters)

  params << program_id_pubkey
  params << params_hash if params_hash.any?

  subscribe(method, method_params: params, &block)
end

#program_unsubscribe(subscription_id) ⇒ Bool

Unsubscribe from program-owned account change notifications

Parameters:

  • subscription_id (Integer)

Returns:

  • (Bool)

    unsubscribe success message

See Also:


197
198
199
200
# File 'lib/solana_rpc_ruby/websocket_methods_wrapper.rb', line 197

def program_unsubscribe(subscription_id)
  method = create_method_name(__method__)
  unsubscribe(method, subscription_id: subscription_id)
end

#root_subscribe(&block) ⇒ Integer

Subscribe to receive notification anytime a new root is set by the validator.

Parameters:

  • &block (Proc)

Returns:

  • (Integer)

    Subscription id (needed to unsubscribe)

See Also:


294
295
296
297
298
# File 'lib/solana_rpc_ruby/websocket_methods_wrapper.rb', line 294

def root_subscribe(&block)
  method = create_method_name(__method__)

  subscribe(method, &block)
end

#root_unsubscribe(subscription_id) ⇒ Bool

Unsubscribe from root notifications

Parameters:

  • subscription_id (Integer)

Returns:

  • (Bool)

    unsubscribe success message

See Also:


306
307
308
309
# File 'lib/solana_rpc_ruby/websocket_methods_wrapper.rb', line 306

def root_unsubscribe(subscription_id)
  method = create_method_name(__method__)
  unsubscribe(method, subscription_id: subscription_id)
end

#signature_subscribe(transaction_signature, commitment: nil, &block) ⇒ Integer

Subscribe to a transaction signature to receive notification when the transaction is confirmed On signatureNotification, the subscription is automatically cancelled

Parameters:

  • transaction_signature (String)
  • commitment (String) (defaults to: nil)
  • &block (Proc)

Returns:

  • (Integer)

    Subscription id (needed to unsubscribe)

See Also:


211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/solana_rpc_ruby/websocket_methods_wrapper.rb', line 211

def signature_subscribe(
  transaction_signature,
  commitment: nil,
  &block
)
  method = create_method_name(__method__)

  params = []
  params_hash = {}
  params_hash['commitment'] = commitment unless blank?(commitment)

  params << transaction_signature
  params << params_hash

  subscribe(method, method_params: params, &block)
end

#signature_unsubscribe(subscription_id) ⇒ Bool

Unsubscribe from signature confirmation notification

Parameters:

  • subscription_id (Integer)

Returns:

  • (Bool)

    unsubscribe success message

See Also:


234
235
236
237
# File 'lib/solana_rpc_ruby/websocket_methods_wrapper.rb', line 234

def signature_unsubscribe(subscription_id)
  method = create_method_name(__method__)
  unsubscribe(method, subscription_id: subscription_id)
end

#slot_subscribe(&block) ⇒ Integer

Subscribe to receive notification anytime a slot is processed by the validator

Parameters:

  • &block (Proc)

Returns:

  • (Integer)

    Subscription id (needed to unsubscribe)

See Also:


245
246
247
248
249
# File 'lib/solana_rpc_ruby/websocket_methods_wrapper.rb', line 245

def slot_subscribe(&block)
  method = create_method_name(__method__)

  subscribe(method, &block)
end

#slot_unsubscribe(subscription_id) ⇒ Bool

Unsubscribe from slot notifications

Parameters:

  • subscription_id (Integer)

Returns:

  • (Bool)

    unsubscribe success message

See Also:


257
258
259
260
# File 'lib/solana_rpc_ruby/websocket_methods_wrapper.rb', line 257

def slot_unsubscribe(subscription_id)
  method = create_method_name(__method__)
  unsubscribe(method, subscription_id: subscription_id)
end

#slots_updates_subscribe(&block) ⇒ Integer

This subscription is unstable; the format of this subscription may change in the future and it may not always be supported Subscribe to receive a notification from the validator on a variety of updates on every slot

Parameters:

  • &block (Proc)

Returns:

  • (Integer)

    Subscription id (needed to unsubscribe)

See Also:


270
271
272
273
274
# File 'lib/solana_rpc_ruby/websocket_methods_wrapper.rb', line 270

def slots_updates_subscribe(&block)
  method = create_method_name(__method__)

  subscribe(method, &block)
end

#slots_updates_unsubscribe(subscription_id) ⇒ Bool

Unsubscribe from slot-update notifications

Parameters:

  • subscription_id (Integer)

Returns:

  • (Bool)

    unsubscribe success message

See Also:


282
283
284
285
# File 'lib/solana_rpc_ruby/websocket_methods_wrapper.rb', line 282

def slots_updates_unsubscribe(subscription_id)
  method = create_method_name(__method__)
  unsubscribe(method, subscription_id: subscription_id)
end

#vote_subscribe(&block) ⇒ Integer

This subscription is unstable and only available if the validator was started with the –rpc-pubsub-enable-vote-subscription flag. The format of this subscription may change in the future

Subscribe to receive notification anytime a new vote is observed in gossip. These votes are pre-consensus therefore there is no guarantee these votes will enter the ledger.

Parameters:

  • &block (Proc)

Returns:

  • (Integer)

    Subscription id (needed to unsubscribe)

See Also:


322
323
324
325
326
# File 'lib/solana_rpc_ruby/websocket_methods_wrapper.rb', line 322

def vote_subscribe(&block)
  method = create_method_name(__method__)

  subscribe(method, &block)
end

#vote_unsubscribe(subscription_id) ⇒ Bool

Unsubscribe from vote notifications

Parameters:

  • subscription_id (Integer)

Returns:

  • (Bool)

    unsubscribe success message

See Also:


334
335
336
337
# File 'lib/solana_rpc_ruby/websocket_methods_wrapper.rb', line 334

def vote_unsubscribe(subscription_id)
  method = create_method_name(__method__)
  unsubscribe(method, subscription_id: subscription_id)
end