substrate_client 0.1.0
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.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +79 -0
- data/LICENSE.txt +21 -0
- data/README.md +57 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/metadata +12 -0
- data/exe/substrate_client +18 -0
- data/lib/substrate_client.rb +198 -0
- data/lib/substrate_client/version.rb +3 -0
- data/substrate_client.gemspec +49 -0
- metadata +189 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: c0a2d543a99ac687e6b1b7564caae1d247f29a9f059d764cfee1ae87abfb7e98
|
|
4
|
+
data.tar.gz: 33400033f8f05935efe24b6ba4c350122f319e6a765d203a97a7a29ad795f97c
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: e55be15e12046bda72615b8caf08c553ea5b8ee4003fb43ebd557bdfd62bf94bf2655bcd54d32f01d37e57dd80e3c7e227cfd45e47fa5f66f0370aaf66c46304
|
|
7
|
+
data.tar.gz: 215c71aa6abc4b640032296b2e30925be70ed843d8d0aa99be681c7637bb9d2b763a9f8e1fa37b1bcafc3a80e8573d77c6b2679f387c3ef9a37276edfbda1f36
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
|
10
|
+
orientation.
|
|
11
|
+
|
|
12
|
+
## Our Standards
|
|
13
|
+
|
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
|
15
|
+
include:
|
|
16
|
+
|
|
17
|
+
* Using welcoming and inclusive language
|
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
|
19
|
+
* Gracefully accepting constructive criticism
|
|
20
|
+
* Focusing on what is best for the community
|
|
21
|
+
* Showing empathy towards other community members
|
|
22
|
+
|
|
23
|
+
Examples of unacceptable behavior by participants include:
|
|
24
|
+
|
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
|
26
|
+
advances
|
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
28
|
+
* Public or private harassment
|
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
|
30
|
+
address, without explicit permission
|
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
32
|
+
professional setting
|
|
33
|
+
|
|
34
|
+
## Our Responsibilities
|
|
35
|
+
|
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
|
38
|
+
response to any instances of unacceptable behavior.
|
|
39
|
+
|
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
|
44
|
+
threatening, offensive, or harmful.
|
|
45
|
+
|
|
46
|
+
## Scope
|
|
47
|
+
|
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
|
49
|
+
when an individual is representing the project or its community. Examples of
|
|
50
|
+
representing a project or community include using an official project e-mail
|
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
|
53
|
+
further defined and clarified by project maintainers.
|
|
54
|
+
|
|
55
|
+
## Enforcement
|
|
56
|
+
|
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
58
|
+
reported by contacting the project team at wuminzhe@gmail.com. All
|
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
|
63
|
+
|
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
|
66
|
+
members of the project's leadership.
|
|
67
|
+
|
|
68
|
+
## Attribution
|
|
69
|
+
|
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
|
72
|
+
|
|
73
|
+
[homepage]: http://contributor-covenant.org
|
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
substrate_client (0.1.0)
|
|
5
|
+
activesupport (~> 5.0.0)
|
|
6
|
+
eventmachine (~> 1.2.7)
|
|
7
|
+
faye-websocket (~> 0.10.9)
|
|
8
|
+
scale.rb (~> 0.2.0)
|
|
9
|
+
substrate_common.rb (~> 0.1.8)
|
|
10
|
+
|
|
11
|
+
GEM
|
|
12
|
+
remote: https://rubygems.org/
|
|
13
|
+
specs:
|
|
14
|
+
activesupport (5.0.7.2)
|
|
15
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
16
|
+
i18n (>= 0.7, < 2)
|
|
17
|
+
minitest (~> 5.1)
|
|
18
|
+
tzinfo (~> 1.1)
|
|
19
|
+
base58 (0.2.3)
|
|
20
|
+
blake2b (0.10.0)
|
|
21
|
+
coderay (1.1.2)
|
|
22
|
+
concurrent-ruby (1.1.6)
|
|
23
|
+
diff-lcs (1.3)
|
|
24
|
+
eventmachine (1.2.7)
|
|
25
|
+
faye-websocket (0.10.9)
|
|
26
|
+
eventmachine (>= 0.12.0)
|
|
27
|
+
websocket-driver (>= 0.5.1)
|
|
28
|
+
i18n (1.8.2)
|
|
29
|
+
concurrent-ruby (~> 1.0)
|
|
30
|
+
json (2.3.0)
|
|
31
|
+
method_source (0.9.2)
|
|
32
|
+
minitest (5.14.0)
|
|
33
|
+
pry (0.12.2)
|
|
34
|
+
coderay (~> 1.1.0)
|
|
35
|
+
method_source (~> 0.9.0)
|
|
36
|
+
rake (13.0.1)
|
|
37
|
+
rspec (3.9.0)
|
|
38
|
+
rspec-core (~> 3.9.0)
|
|
39
|
+
rspec-expectations (~> 3.9.0)
|
|
40
|
+
rspec-mocks (~> 3.9.0)
|
|
41
|
+
rspec-core (3.9.1)
|
|
42
|
+
rspec-support (~> 3.9.1)
|
|
43
|
+
rspec-expectations (3.9.1)
|
|
44
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
45
|
+
rspec-support (~> 3.9.0)
|
|
46
|
+
rspec-mocks (3.9.1)
|
|
47
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
48
|
+
rspec-support (~> 3.9.0)
|
|
49
|
+
rspec-support (3.9.2)
|
|
50
|
+
scale.rb (0.2.0)
|
|
51
|
+
activesupport (>= 4.0.0)
|
|
52
|
+
json (~> 2.3.0)
|
|
53
|
+
substrate_common.rb (~> 0.1.7)
|
|
54
|
+
thor (~> 0.20.3)
|
|
55
|
+
substrate_common.rb (0.1.8)
|
|
56
|
+
base58
|
|
57
|
+
blake2b
|
|
58
|
+
xxhash
|
|
59
|
+
thor (0.20.3)
|
|
60
|
+
thread_safe (0.3.6)
|
|
61
|
+
tzinfo (1.2.7)
|
|
62
|
+
thread_safe (~> 0.1)
|
|
63
|
+
websocket-driver (0.7.1)
|
|
64
|
+
websocket-extensions (>= 0.1.0)
|
|
65
|
+
websocket-extensions (0.1.4)
|
|
66
|
+
xxhash (0.4.0)
|
|
67
|
+
|
|
68
|
+
PLATFORMS
|
|
69
|
+
ruby
|
|
70
|
+
|
|
71
|
+
DEPENDENCIES
|
|
72
|
+
bundler (~> 1.17)
|
|
73
|
+
pry
|
|
74
|
+
rake (>= 12.3.3)
|
|
75
|
+
rspec (~> 3.0)
|
|
76
|
+
substrate_client!
|
|
77
|
+
|
|
78
|
+
BUNDLED WITH
|
|
79
|
+
1.17.3
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Akiwu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# SubstrateClient
|
|
2
|
+
|
|
3
|
+
This is a library of interfaces for communicating with Substrate nodes. It provides application developers the ability to query a node and interact with the Substrate chains using Ruby.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'substrate_client'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install substrate_client
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### Api list
|
|
24
|
+
|
|
25
|
+
```ruby
|
|
26
|
+
require "substrate_client"
|
|
27
|
+
|
|
28
|
+
client = SubstrateClient.new("wss://kusama-rpc.polkadot.io/")
|
|
29
|
+
puts client.method_list
|
|
30
|
+
```
|
|
31
|
+
The rpc api methods is dynamically generated, so the methods returned by this method can be called.
|
|
32
|
+
|
|
33
|
+
## TODO
|
|
34
|
+
|
|
35
|
+
- [x] ws wss request support
|
|
36
|
+
- [ ] http request support
|
|
37
|
+
- [x] generate storage key
|
|
38
|
+
- [x] call any api supported by substrate node with ruby's method missing function
|
|
39
|
+
- [ ] metadata caching
|
|
40
|
+
|
|
41
|
+
## Development
|
|
42
|
+
|
|
43
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
44
|
+
|
|
45
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
46
|
+
|
|
47
|
+
## Contributing
|
|
48
|
+
|
|
49
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/substrate_client. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
|
50
|
+
|
|
51
|
+
## License
|
|
52
|
+
|
|
53
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
54
|
+
|
|
55
|
+
## Code of Conduct
|
|
56
|
+
|
|
57
|
+
Everyone interacting in the SubstrateClient project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/substrate_client/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "substrate_client"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require 'pry'
|
|
14
|
+
Pry.start(__FILE__)
|
data/bin/setup
ADDED
data/exe/metadata
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "substrate_client"
|
|
4
|
+
require "json"
|
|
5
|
+
|
|
6
|
+
# metadata url block_hash
|
|
7
|
+
url = ARGV[0] || "wss://cc3-5.kusama.network/"
|
|
8
|
+
block_hash = ARGV[1]
|
|
9
|
+
|
|
10
|
+
client = SubstrateClient.new(url)
|
|
11
|
+
client.init(block_hash)
|
|
12
|
+
puts JSON.pretty_generate(client.metadata.value.value)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "substrate_client"
|
|
4
|
+
require "json"
|
|
5
|
+
|
|
6
|
+
client = SubstrateClient.new("wss://cc3-5.kusama.network/")
|
|
7
|
+
# puts client.system_name
|
|
8
|
+
# puts client.system_chain
|
|
9
|
+
# puts client.system_version
|
|
10
|
+
# puts client.system_peers
|
|
11
|
+
# puts client.state_getStorage "0x0b76934f4cc08dee01012d059e1b83ee5e0621c4869aa60c02be9adcc98a0d1d"
|
|
12
|
+
# puts client.state_getChildKeys "0x","0x",1,"0x"
|
|
13
|
+
# puts client.runtime_getState "System", "Events",[],"0xe0303fbe2bc8482e06fb649f934c41349d12a25bd618c92e3331a3347e434f7b"
|
|
14
|
+
# puts client.rpc_methods
|
|
15
|
+
|
|
16
|
+
client.init
|
|
17
|
+
metadata = client.metadata
|
|
18
|
+
puts JSON.pretty_generate(metadata)
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
require "substrate_client/version"
|
|
2
|
+
|
|
3
|
+
require "substrate_common"
|
|
4
|
+
require "scale"
|
|
5
|
+
|
|
6
|
+
require "faye/websocket"
|
|
7
|
+
require "eventmachine"
|
|
8
|
+
require "json"
|
|
9
|
+
require "active_support"
|
|
10
|
+
require "active_support/core_ext/string"
|
|
11
|
+
|
|
12
|
+
def ws_request(url, payload)
|
|
13
|
+
result = nil
|
|
14
|
+
|
|
15
|
+
EM.run do
|
|
16
|
+
ws = Faye::WebSocket::Client.new(url)
|
|
17
|
+
|
|
18
|
+
ws.on :open do |event|
|
|
19
|
+
# p [:open]
|
|
20
|
+
ws.send(payload.to_json)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
ws.on :message do |event|
|
|
24
|
+
# p [:message, event.data]
|
|
25
|
+
if event.data.include?("jsonrpc")
|
|
26
|
+
result = JSON.parse event.data
|
|
27
|
+
ws.close(3001, "data received")
|
|
28
|
+
EM.stop
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
ws.on :close do |event|
|
|
33
|
+
# p [:close, event.code, event.reason]
|
|
34
|
+
ws = nil
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
result
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
class SubstrateClient
|
|
42
|
+
attr_accessor :spec_name, :spec_version, :metadata
|
|
43
|
+
|
|
44
|
+
def initialize(url)
|
|
45
|
+
@url = url
|
|
46
|
+
@request_id = 1
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# TODO: error
|
|
50
|
+
def request(method, params)
|
|
51
|
+
payload = {
|
|
52
|
+
"jsonrpc" => "2.0",
|
|
53
|
+
"method" => method,
|
|
54
|
+
"params" => params,
|
|
55
|
+
"id" => @request_id
|
|
56
|
+
}
|
|
57
|
+
@request_id += 1
|
|
58
|
+
ws_request(@url, payload)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# ############################
|
|
62
|
+
# native rpc methods support
|
|
63
|
+
# ############################
|
|
64
|
+
def method_missing(method, *args)
|
|
65
|
+
data = request(SubstrateClient.real_method_name(method), args)
|
|
66
|
+
data["result"]
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# ################################################
|
|
70
|
+
# custom methods wrapped from native rpc methods
|
|
71
|
+
# ################################################
|
|
72
|
+
def method_list
|
|
73
|
+
methods = self.rpc_methods["methods"].map(&:underscore)
|
|
74
|
+
methods << "method_list"
|
|
75
|
+
methods << "get_storage_at"
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def init(block_hash = nil)
|
|
79
|
+
block_runtime_version = self.state_get_runtime_version(block_hash)
|
|
80
|
+
@spec_name = block_runtime_version["specName"]
|
|
81
|
+
@spec_version = block_runtime_version["specVersion"]
|
|
82
|
+
|
|
83
|
+
Scale::TypeRegistry.instance.load(spec_name, spec_version)
|
|
84
|
+
@metadata = self.get_metadata(block_hash)
|
|
85
|
+
true
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def get_metadata(block_hash)
|
|
89
|
+
hex = self.state_get_metadata(block_hash)
|
|
90
|
+
metadata = Scale::Types::Metadata.decode(Scale::Bytes.new(hex))
|
|
91
|
+
metadata.value.value[:metadata]
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# client.init(0x014e4248dd04a8c0342b603a66df0691361ac58e69595e248219afa7af87bdc7)
|
|
95
|
+
# Plain: client.get_storage_at("Balances", "TotalIssuance")
|
|
96
|
+
# Map: client.get_storage_at("System", "Account", ["0x30599dba50b5f3ba0b36f856a761eb3c0aee61e830d4beb448ef94b6ad92be39"])
|
|
97
|
+
# DoubleMap: client.get_storage_at("ImOnline", "AuthoredBlocks", [2818, "0x749ddc93a65dfec3af27cc7478212cb7d4b0c0357fef35a0163966ab5333b757"])
|
|
98
|
+
def get_storage_at(module_name, storage_function_name, params = nil)
|
|
99
|
+
|
|
100
|
+
# TODO: uninit raise a exception
|
|
101
|
+
# find the storage item from metadata
|
|
102
|
+
metadata_modules = metadata[:modules]
|
|
103
|
+
metadata_module = metadata_modules.detect { |mm| mm[:name] == module_name }
|
|
104
|
+
raise "Module '#{module_name}' not exist" unless metadata_module
|
|
105
|
+
storage_item = metadata_module[:storage][:items].detect { |item| item[:name] == storage_function_name }
|
|
106
|
+
raise "Storage item '#{storage_function_name}' not exist. \n#{metadata_module.inspect}" unless storage_item
|
|
107
|
+
|
|
108
|
+
if storage_item[:type][:Plain]
|
|
109
|
+
return_type = storage_item[:type][:Plain]
|
|
110
|
+
elsif map = storage_item[:type][:Map]
|
|
111
|
+
raise "Storage call of type \"Map\" requires 1 parameter" if params.nil? || params.length != 1
|
|
112
|
+
|
|
113
|
+
hasher = map[:hasher]
|
|
114
|
+
return_type = map[:value]
|
|
115
|
+
# TODO: decode to account id if param is address
|
|
116
|
+
# params[0] = decode(params[0]) if map[:key] == "AccountId"
|
|
117
|
+
params[0] = Scale::Types.get(map[:key]).new(params[0]).encode
|
|
118
|
+
elsif map = storage_item[:type][:DoubleMap]
|
|
119
|
+
raise "Storage call of type \"DoubleMapType\" requires 2 parameters" if params.nil? || params.length != 2
|
|
120
|
+
|
|
121
|
+
hasher = map[:hasher]
|
|
122
|
+
hasher2 = map[:key2Hasher]
|
|
123
|
+
return_type = map[:value]
|
|
124
|
+
params[0] = Scale::Types.get(map[:key1]).new(params[0]).encode
|
|
125
|
+
params[1] = Scale::Types.get(map[:key2]).new(params[1]).encode
|
|
126
|
+
else
|
|
127
|
+
raise NotImplementedError
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
storage_hash = SubstrateClient.generate_storage_hash(
|
|
131
|
+
module_name,
|
|
132
|
+
storage_function_name,
|
|
133
|
+
params,
|
|
134
|
+
hasher,
|
|
135
|
+
hasher2,
|
|
136
|
+
metadata[:version]
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
# puts storage_hash
|
|
140
|
+
|
|
141
|
+
result = self.state_get_storage_at(storage_hash, block_hash)
|
|
142
|
+
return unless result
|
|
143
|
+
Scale::Types.get(return_type).decode(Scale::Bytes.new(result)).value
|
|
144
|
+
rescue => ex
|
|
145
|
+
puts ex.message
|
|
146
|
+
puts ex.backtrace
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
class << self
|
|
150
|
+
def generate_storage_hash(storage_module_name, storage_function_name, params = nil, hasher = nil, hasher2 = nil, metadata_version = nil)
|
|
151
|
+
if metadata_version and metadata_version >= 9
|
|
152
|
+
storage_hash = Crypto.twox128(storage_module_name) + Crypto.twox128(storage_function_name)
|
|
153
|
+
|
|
154
|
+
if params
|
|
155
|
+
params.each_with_index do |param, index|
|
|
156
|
+
if index == 0
|
|
157
|
+
param_hasher = hasher
|
|
158
|
+
elsif index == 1
|
|
159
|
+
param_hasher = hasher2
|
|
160
|
+
else
|
|
161
|
+
raise "Unexpected third parameter for storage call"
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
param_key = param.hex_to_bytes
|
|
165
|
+
param_hasher = "Twox128" if param_hasher.nil?
|
|
166
|
+
storage_hash += Crypto.send param_hasher.underscore, param_key
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
"0x#{storage_hash}"
|
|
171
|
+
else
|
|
172
|
+
# TODO: add test
|
|
173
|
+
storage_hash = storage_module_name + " " + storage_function_name
|
|
174
|
+
|
|
175
|
+
unless params.nil?
|
|
176
|
+
params = [params] if params.class != ::Array
|
|
177
|
+
params_key = params.join("")
|
|
178
|
+
hasher = "Twox128" if hasher.nil?
|
|
179
|
+
storage_hash += params_key.hex_to_bytes.bytes_to_utf8
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
"0x#{Crypto.send( hasher.underscore, storage_hash )}"
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
# chain_unsubscribe_runtime_version
|
|
187
|
+
# =>
|
|
188
|
+
# chain_unsubscribeRuntimeVersion
|
|
189
|
+
def real_method_name(method_name)
|
|
190
|
+
segments = method_name.to_s.split("_")
|
|
191
|
+
segments[0] + "_" + segments[1] + segments[2..].map(&:capitalize).join
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
end
|
|
198
|
+
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
|
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require "substrate_client/version"
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "substrate_client"
|
|
8
|
+
spec.version = SubstrateClient::VERSION
|
|
9
|
+
spec.authors = ["Wu Minzhe"]
|
|
10
|
+
spec.email = ["wuminzhe@gmail.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{Ruby Substrate RPC Client}
|
|
13
|
+
spec.description = %q{Ruby Substrate RPC Client}
|
|
14
|
+
spec.homepage = "https://github.com/itering/substrate_client.rb"
|
|
15
|
+
spec.license = "MIT"
|
|
16
|
+
|
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
|
19
|
+
if spec.respond_to?(:metadata)
|
|
20
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
|
21
|
+
|
|
22
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
23
|
+
# spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
|
|
24
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
|
25
|
+
else
|
|
26
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
|
27
|
+
"public gem pushes."
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Specify which files should be added to the gem when it is released.
|
|
31
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
32
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
|
33
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
34
|
+
end
|
|
35
|
+
spec.bindir = "exe"
|
|
36
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
37
|
+
spec.require_paths = ["lib"]
|
|
38
|
+
|
|
39
|
+
spec.add_dependency "substrate_common.rb", "~> 0.1.8"
|
|
40
|
+
spec.add_dependency "faye-websocket", "~> 0.10.9"
|
|
41
|
+
spec.add_dependency "eventmachine", "~> 1.2.7"
|
|
42
|
+
spec.add_dependency "activesupport", "~> 5.0.0"
|
|
43
|
+
spec.add_dependency "scale.rb", "~> 0.2.0"
|
|
44
|
+
|
|
45
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
|
46
|
+
spec.add_development_dependency "rake", ">= 12.3.3"
|
|
47
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
48
|
+
spec.add_development_dependency "pry"
|
|
49
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: substrate_client
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Wu Minzhe
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2020-04-08 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: substrate_common.rb
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 0.1.8
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 0.1.8
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: faye-websocket
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: 0.10.9
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: 0.10.9
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: eventmachine
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: 1.2.7
|
|
48
|
+
type: :runtime
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: 1.2.7
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: activesupport
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: 5.0.0
|
|
62
|
+
type: :runtime
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: 5.0.0
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: scale.rb
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - "~>"
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: 0.2.0
|
|
76
|
+
type: :runtime
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - "~>"
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: 0.2.0
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: bundler
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - "~>"
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '1.17'
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - "~>"
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: '1.17'
|
|
97
|
+
- !ruby/object:Gem::Dependency
|
|
98
|
+
name: rake
|
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
|
100
|
+
requirements:
|
|
101
|
+
- - ">="
|
|
102
|
+
- !ruby/object:Gem::Version
|
|
103
|
+
version: 12.3.3
|
|
104
|
+
type: :development
|
|
105
|
+
prerelease: false
|
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
107
|
+
requirements:
|
|
108
|
+
- - ">="
|
|
109
|
+
- !ruby/object:Gem::Version
|
|
110
|
+
version: 12.3.3
|
|
111
|
+
- !ruby/object:Gem::Dependency
|
|
112
|
+
name: rspec
|
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
|
114
|
+
requirements:
|
|
115
|
+
- - "~>"
|
|
116
|
+
- !ruby/object:Gem::Version
|
|
117
|
+
version: '3.0'
|
|
118
|
+
type: :development
|
|
119
|
+
prerelease: false
|
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
121
|
+
requirements:
|
|
122
|
+
- - "~>"
|
|
123
|
+
- !ruby/object:Gem::Version
|
|
124
|
+
version: '3.0'
|
|
125
|
+
- !ruby/object:Gem::Dependency
|
|
126
|
+
name: pry
|
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
|
128
|
+
requirements:
|
|
129
|
+
- - ">="
|
|
130
|
+
- !ruby/object:Gem::Version
|
|
131
|
+
version: '0'
|
|
132
|
+
type: :development
|
|
133
|
+
prerelease: false
|
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
135
|
+
requirements:
|
|
136
|
+
- - ">="
|
|
137
|
+
- !ruby/object:Gem::Version
|
|
138
|
+
version: '0'
|
|
139
|
+
description: Ruby Substrate RPC Client
|
|
140
|
+
email:
|
|
141
|
+
- wuminzhe@gmail.com
|
|
142
|
+
executables:
|
|
143
|
+
- metadata
|
|
144
|
+
- substrate_client
|
|
145
|
+
extensions: []
|
|
146
|
+
extra_rdoc_files: []
|
|
147
|
+
files:
|
|
148
|
+
- ".gitignore"
|
|
149
|
+
- ".rspec"
|
|
150
|
+
- ".travis.yml"
|
|
151
|
+
- CODE_OF_CONDUCT.md
|
|
152
|
+
- Gemfile
|
|
153
|
+
- Gemfile.lock
|
|
154
|
+
- LICENSE.txt
|
|
155
|
+
- README.md
|
|
156
|
+
- Rakefile
|
|
157
|
+
- bin/console
|
|
158
|
+
- bin/setup
|
|
159
|
+
- exe/metadata
|
|
160
|
+
- exe/substrate_client
|
|
161
|
+
- lib/substrate_client.rb
|
|
162
|
+
- lib/substrate_client/version.rb
|
|
163
|
+
- substrate_client.gemspec
|
|
164
|
+
homepage: https://github.com/itering/substrate_client.rb
|
|
165
|
+
licenses:
|
|
166
|
+
- MIT
|
|
167
|
+
metadata:
|
|
168
|
+
allowed_push_host: https://rubygems.org
|
|
169
|
+
homepage_uri: https://github.com/itering/substrate_client.rb
|
|
170
|
+
post_install_message:
|
|
171
|
+
rdoc_options: []
|
|
172
|
+
require_paths:
|
|
173
|
+
- lib
|
|
174
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
175
|
+
requirements:
|
|
176
|
+
- - ">="
|
|
177
|
+
- !ruby/object:Gem::Version
|
|
178
|
+
version: '0'
|
|
179
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
180
|
+
requirements:
|
|
181
|
+
- - ">="
|
|
182
|
+
- !ruby/object:Gem::Version
|
|
183
|
+
version: '0'
|
|
184
|
+
requirements: []
|
|
185
|
+
rubygems_version: 3.0.6
|
|
186
|
+
signing_key:
|
|
187
|
+
specification_version: 4
|
|
188
|
+
summary: Ruby Substrate RPC Client
|
|
189
|
+
test_files: []
|