solana-ruby-web3js 1.0.1.beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. checksums.yaml +7 -0
  2. data/.DS_Store +0 -0
  3. data/.rspec +1 -0
  4. data/.rubocop.yml +13 -0
  5. data/.ruby-version +1 -0
  6. data/CHANGELOG.md +5 -0
  7. data/CODE_OF_CONDUCT.md +84 -0
  8. data/Gemfile +17 -0
  9. data/Gemfile.lock +184 -0
  10. data/README.md +273 -0
  11. data/Rakefile +8 -0
  12. data/bin/console +15 -0
  13. data/bin/setup +8 -0
  14. data/lib/.DS_Store +0 -0
  15. data/lib/solana_ruby/.DS_Store +0 -0
  16. data/lib/solana_ruby/base_client.rb +46 -0
  17. data/lib/solana_ruby/http_client.rb +42 -0
  18. data/lib/solana_ruby/http_methods/account_methods.rb +98 -0
  19. data/lib/solana_ruby/http_methods/basic_methods.rb +108 -0
  20. data/lib/solana_ruby/http_methods/block_methods.rb +89 -0
  21. data/lib/solana_ruby/http_methods/blockhash_methods.rb +30 -0
  22. data/lib/solana_ruby/http_methods/lookup_table_methods.rb +55 -0
  23. data/lib/solana_ruby/http_methods/signature_methods.rb +30 -0
  24. data/lib/solana_ruby/http_methods/slot_methods.rb +44 -0
  25. data/lib/solana_ruby/http_methods/token_methods.rb +32 -0
  26. data/lib/solana_ruby/http_methods/transaction_methods.rb +85 -0
  27. data/lib/solana_ruby/version.rb +5 -0
  28. data/lib/solana_ruby/web_socket_client.rb +93 -0
  29. data/lib/solana_ruby/web_socket_handlers.rb +34 -0
  30. data/lib/solana_ruby/web_socket_methods/account_methods.rb +37 -0
  31. data/lib/solana_ruby/web_socket_methods/log_methods.rb +27 -0
  32. data/lib/solana_ruby/web_socket_methods/root_methods.rb +16 -0
  33. data/lib/solana_ruby/web_socket_methods/signature_methods.rb +25 -0
  34. data/lib/solana_ruby/web_socket_methods/slot_methods.rb +20 -0
  35. data/lib/solana_ruby.rb +10 -0
  36. metadata +136 -0
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'websocket-client-simple'
4
+ require 'securerandom'
5
+ require 'json'
6
+ require_relative 'web_socket_handlers'
7
+ Dir[File.join(__dir__, 'web_socket_methods', '*.rb')].each { |file| require file }
8
+
9
+ module SolanaRuby
10
+ class WebSocketClient
11
+ include WebSocketHandlers
12
+ include WebSocketMethods::AccountMethods
13
+ include WebSocketMethods::LogMethods
14
+ include WebSocketMethods::SignatureMethods
15
+ include WebSocketMethods::RootMethods
16
+ include WebSocketMethods::SlotMethods
17
+ attr_reader :subscriptions
18
+
19
+ def initialize(url, auto_reconnect: true, reconnect_delay: 5)
20
+ @url = url
21
+ @subscriptions = {}
22
+ @auto_reconnect = auto_reconnect
23
+ @reconnect_delay = reconnect_delay
24
+ @connected = false
25
+ @ws = nil
26
+ connect
27
+ end
28
+
29
+ def connect
30
+ return if @connected
31
+
32
+ # Close existing WebSocket if it exists
33
+ @ws&.close if @ws
34
+
35
+ @ws = WebSocket::Client::Simple.connect(@url)
36
+ setup_handlers(@ws, self)
37
+ @connected = true
38
+ rescue StandardError => e
39
+ puts "Failed to connect: #{e.message}"
40
+ attempt_reconnect
41
+ end
42
+
43
+ def reconnect
44
+ @connected = false
45
+ connect
46
+ end
47
+
48
+ def subscribe(method, params = nil, &block)
49
+ id = SecureRandom.uuid
50
+ @subscriptions[id] = block
51
+ message = {
52
+ jsonrpc: '2.0',
53
+ id: id,
54
+ method: method
55
+ }
56
+ message[:params] = params if params
57
+ sleep 2
58
+ @ws.send(message.to_json)
59
+ id
60
+ end
61
+
62
+ def unsubscribe(method, subscription_id)
63
+ message = {
64
+ jsonrpc: '2.0',
65
+ id: SecureRandom.uuid,
66
+ method: method,
67
+ params: [subscription_id]
68
+ }
69
+ @ws.send(message.to_json)
70
+ end
71
+
72
+ def handle_message(data)
73
+ if data['id'] && @subscriptions[data['id']]
74
+ @subscriptions[data['id']].call(data['result'])
75
+ elsif data['method'] && data['params']
76
+ @subscriptions.each do |id, block|
77
+ block.call(data['params']) if block
78
+ end
79
+ else
80
+ puts "Unhandled message: #{data}"
81
+ end
82
+ end
83
+
84
+ def attempt_reconnect
85
+ return unless @auto_reconnect
86
+
87
+ puts "Attempting to reconnect in #{@reconnect_delay} seconds..."
88
+ @connected = false
89
+ sleep @reconnect_delay
90
+ reconnect
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolanaRuby
4
+ module WebSocketHandlers
5
+ def setup_handlers(ws, client)
6
+ ws.on :message do |msg|
7
+ begin
8
+ data = JSON.parse(msg.data)
9
+ if data['error']
10
+ puts "Error: #{data['error']['message']} with the code #{data['error']['code']}"
11
+ else
12
+ client.handle_message(data)
13
+ end
14
+ rescue JSON::ParserError => e
15
+ puts "Failed to parse message: #{e.message}"
16
+ end
17
+ end
18
+
19
+ ws.on :open do
20
+ puts 'Web Socket connection established.'
21
+ end
22
+
23
+ ws.on :close do |e|
24
+ puts "Web Socket connection closed: #{e.inspect}"
25
+ client.attempt_reconnect if client.instance_variable_get(:@auto_reconnect)
26
+ end
27
+
28
+ ws.on :error do |e|
29
+ puts "Error: #{e.inspect}"
30
+ client.attempt_reconnect if client.instance_variable_get(:@auto_reconnect)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolanaRuby
4
+ module WebSocketMethods
5
+ # Acccount Related Web Socket Methods
6
+ module AccountMethods
7
+ FINALIZED_OPTIONS = { commitment: "finalized" }.freeze
8
+ ENCODING_OPTIONS = { encoding: "base64" }.freeze
9
+
10
+ def on_account_change(pubkey, options = FINALIZED_OPTIONS, &block)
11
+ params = [pubkey, options]
12
+
13
+ subscribe("accountSubscribe", params) do |account_info|
14
+ block.call(account_info)
15
+ end
16
+ end
17
+
18
+ # Unsubscribe from account change updates
19
+ def remove_account_change_listener(subscription_id)
20
+ unsubscribe("accountUnsubscribe", subscription_id)
21
+ @subscriptions.delete(subscription_id)
22
+ end
23
+
24
+ def on_program_account_change(program_id, options = ENCODING_OPTIONS.merge(FINALIZED_OPTIONS), filters = [],
25
+ &block)
26
+ params = [program_id, options]
27
+ params.last[:filters] = filters unless filters.empty?
28
+ subscribe("programSubscribe", params, &block)
29
+ end
30
+
31
+ # Unsubscribe from program account change updates
32
+ def remove_program_account_listener(subscription_id)
33
+ unsubscribe("programUnsubscribe", subscription_id)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolanaRuby
4
+ module WebSocketMethods
5
+ # Log Related Web Socket Methods
6
+ module LogMethods
7
+ def on_logs(params = ["all"], &block)
8
+ subscribe("logsSubscribe", params, &block)
9
+ end
10
+
11
+ def on_logs_for_account(public_key, &block)
12
+ params = [{ mentions: [public_key] }]
13
+ on_logs(params, &block)
14
+ end
15
+
16
+ def on_logs_for_program(program_id, &block)
17
+ params = [{ mentions: [program_id] }]
18
+ on_logs(params, &block)
19
+ end
20
+
21
+ # Unsubscribe from logs updates
22
+ def remove_logs_listener(subscription_id)
23
+ unsubscribe("logsUnsubscribe", subscription_id)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolanaRuby
4
+ module WebSocketMethods
5
+ # Root Related Web Socket Methods
6
+ module RootMethods
7
+ def on_root_change(&block)
8
+ subscribe("rootSubscribe", [], &block)
9
+ end
10
+
11
+ def remove_root_listener(subscription_id)
12
+ unsubscribe("rootUnsubscribe", subscription_id)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolanaRuby
4
+ module WebSocketMethods
5
+ # Signature Related Web Socket Methods
6
+ module SignatureMethods
7
+ FINALIZED_OPTIONS = { commitment: "finalized" }.freeze
8
+ BASE_64_ENCODING_OPTIONS = { encoding: "base64" }.freeze
9
+
10
+ def on_signature(signature, options = FINALIZED_OPTIONS, &block)
11
+ params = [signature, options]
12
+ subscribe("signatureSubscribe", params, &block)
13
+ end
14
+
15
+ def on_signature_with_options(signature, options = BASE_64_ENCODING_OPTIONS.merge(FINALIZED_OPTIONS), &block)
16
+ on_signature(signature, options, &block)
17
+ end
18
+
19
+ # Unsubscribe from signature updates
20
+ def remove_signature_listener(subscription_id)
21
+ unsubscribe("signatureUnsubscribe", subscription_id)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolanaRuby
4
+ module WebSocketMethods
5
+ # Slot Related Web Socket Methods
6
+ module SlotMethods
7
+ # Subscribe to slot change notifications.
8
+ # Options can include parameters such as commitment level, encoding, etc.
9
+ def on_slot_change(&block)
10
+ # Default to empty params if no options are provided.
11
+ subscribe("slotSubscribe", [], &block)
12
+ end
13
+
14
+ # Unsubscribe from slot change notifications.
15
+ def remove_slot_change_listener(subscription_id)
16
+ unsubscribe("slotUnsubscribe", subscription_id)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "solana_ruby/version"
4
+ require_relative "solana_ruby/http_client"
5
+ require_relative "solana_ruby/web_socket_client"
6
+ # Dir["solana_ruby/*.rb"].each { |f| require_relative f.delete(".rb") }
7
+
8
+ module SolanaRuby
9
+ class Error < StandardError; end
10
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: solana-ruby-web3js
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1.beta1
5
+ platform: ruby
6
+ authors:
7
+ - BuildSquad
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-10-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: websocket-client-simple
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: base58
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.2.3
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.2.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: base64
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.2.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.2.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: yard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: This gem allows to use JSON RPC API Methods from solana.
70
+ email:
71
+ - engineering@buildsquad.net
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".DS_Store"
77
+ - ".rspec"
78
+ - ".rubocop.yml"
79
+ - ".ruby-version"
80
+ - CHANGELOG.md
81
+ - CODE_OF_CONDUCT.md
82
+ - Gemfile
83
+ - Gemfile.lock
84
+ - README.md
85
+ - Rakefile
86
+ - bin/console
87
+ - bin/setup
88
+ - lib/.DS_Store
89
+ - lib/solana_ruby.rb
90
+ - lib/solana_ruby/.DS_Store
91
+ - lib/solana_ruby/base_client.rb
92
+ - lib/solana_ruby/http_client.rb
93
+ - lib/solana_ruby/http_methods/account_methods.rb
94
+ - lib/solana_ruby/http_methods/basic_methods.rb
95
+ - lib/solana_ruby/http_methods/block_methods.rb
96
+ - lib/solana_ruby/http_methods/blockhash_methods.rb
97
+ - lib/solana_ruby/http_methods/lookup_table_methods.rb
98
+ - lib/solana_ruby/http_methods/signature_methods.rb
99
+ - lib/solana_ruby/http_methods/slot_methods.rb
100
+ - lib/solana_ruby/http_methods/token_methods.rb
101
+ - lib/solana_ruby/http_methods/transaction_methods.rb
102
+ - lib/solana_ruby/version.rb
103
+ - lib/solana_ruby/web_socket_client.rb
104
+ - lib/solana_ruby/web_socket_handlers.rb
105
+ - lib/solana_ruby/web_socket_methods/account_methods.rb
106
+ - lib/solana_ruby/web_socket_methods/log_methods.rb
107
+ - lib/solana_ruby/web_socket_methods/root_methods.rb
108
+ - lib/solana_ruby/web_socket_methods/signature_methods.rb
109
+ - lib/solana_ruby/web_socket_methods/slot_methods.rb
110
+ homepage: https://github.com/Build-Squad/solana-ruby
111
+ licenses:
112
+ - MIT
113
+ metadata:
114
+ homepage_uri: https://github.com/Build-Squad/solana-ruby
115
+ source_code_uri: https://github.com/Build-Squad/solana-ruby
116
+ documentation_uri: https://www.rubydoc.info/gems/solana-ruby-web3js
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: 3.0.0
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubygems_version: 3.5.20
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: Solana Ruby SDK
136
+ test_files: []