Class: SubstrateClient
- Inherits:
-
Object
- Object
- SubstrateClient
- Defined in:
- lib/substrate_client.rb,
lib/substrate_client/version.rb
Defined Under Namespace
Classes: Helper, RpcError, RpcTimeout, WebsocketError
Constant Summary collapse
- VERSION =
"0.1.9"
Instance Attribute Summary collapse
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#spec_name ⇒ Object
Returns the value of attribute spec_name.
-
#spec_version ⇒ Object
Returns the value of attribute spec_version.
Instance Method Summary collapse
-
#compose_call(module_name, call_name, params, block_hash = nil) ⇒ Object
compose_call “Balances”, “Transfer”, { dest: “0x586cb27c291c813ce74e86a60dad270609abf2fc8bee107e44a80ac00225c409”, value: 1_000_000_000_000 }.
- #generate_storage_hash_from_data(storage_hex_data) ⇒ Object
- #generate_storage_key(module_name, storage_name, params = nil, block_hash = nil) ⇒ Object
- #get_block(block_hash = nil) ⇒ Object
- #get_block_events(block_hash = nil) ⇒ Object
- #get_block_number(block_hash) ⇒ Object
- #get_metadata(block_hash = nil) ⇒ Object
-
#get_storage(module_name, storage_name, params = nil, block_hash = nil) ⇒ Object
Plain: client.get_storage(“Sudo”, “Key”) Plain: client.get_storage(“Balances”, “TotalIssuance”) Map: client.get_storage(“System”, “Account”, [“0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d”]) DoubleMap: client.get_storage(“ImOnline”, “AuthoredBlocks”, [2818, “0x749ddc93a65dfec3af27cc7478212cb7d4b0c0357fef35a0163966ab5333b757”]).
- #init_runtime(block_hash = nil) ⇒ Object
-
#initialize(url, spec_name: nil) ⇒ SubstrateClient
constructor
A new instance of SubstrateClient.
- #invoke(method, *params) ⇒ Object
-
#method_missing(method, *args) ⇒ Object
################################################ origin rpc methods ################################################.
-
#methods ⇒ Object
################################################ custom methods based on origin rpc methods ################################################.
- #request(method, params) ⇒ Object
Constructor Details
#initialize(url, spec_name: nil) ⇒ SubstrateClient
Returns a new instance of SubstrateClient.
36 37 38 39 40 41 |
# File 'lib/substrate_client.rb', line 36 def initialize(url, spec_name: nil) @url = url @request_id = 1 @spec_name = spec_name Scale::TypeRegistry.instance.load(spec_name: spec_name) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
################################################ origin rpc methods ################################################
79 80 81 |
# File 'lib/substrate_client.rb', line 79 def method_missing(method, *args) invoke method, *args end |
Instance Attribute Details
#metadata ⇒ Object
Returns the value of attribute metadata.
34 35 36 |
# File 'lib/substrate_client.rb', line 34 def @metadata end |
#spec_name ⇒ Object
Returns the value of attribute spec_name.
34 35 36 |
# File 'lib/substrate_client.rb', line 34 def spec_name @spec_name end |
#spec_version ⇒ Object
Returns the value of attribute spec_version.
34 35 36 |
# File 'lib/substrate_client.rb', line 34 def spec_version @spec_version end |
Instance Method Details
#compose_call(module_name, call_name, params, block_hash = nil) ⇒ Object
compose_call “Balances”, “Transfer”, { dest: “0x586cb27c291c813ce74e86a60dad270609abf2fc8bee107e44a80ac00225c409”, value: 1_000_000_000_000 }
136 137 138 139 |
# File 'lib/substrate_client.rb', line 136 def compose_call(module_name, call_name, params, block_hash=nil) self.init_runtime(block_hash) SubstrateClient::Helper.(@metadata, module_name, call_name, params) end |
#generate_storage_hash_from_data(storage_hex_data) ⇒ Object
141 142 143 |
# File 'lib/substrate_client.rb', line 141 def generate_storage_hash_from_data(storage_hex_data) "0x" + Crypto.blake2_256(Scale::Bytes.new(storage_hex_data).bytes) end |
#generate_storage_key(module_name, storage_name, params = nil, block_hash = nil) ⇒ Object
130 131 132 133 |
# File 'lib/substrate_client.rb', line 130 def generate_storage_key(module_name, storage_name, params = nil, block_hash = nil) self.init_runtime(block_hash) SubstrateClient::Helper.(@metadata, module_name, storage_name, params) end |
#get_block(block_hash = nil) ⇒ Object
100 101 102 103 104 |
# File 'lib/substrate_client.rb', line 100 def get_block(block_hash=nil) self.init_runtime(block_hash) block = self.chain_getBlock(block_hash) SubstrateClient::Helper.decode_block(block) end |
#get_block_events(block_hash = nil) ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'lib/substrate_client.rb', line 106 def get_block_events(block_hash=nil) self.init_runtime(block_hash) storage_key = "0x26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7" events_data = state_getStorage storage_key, block_hash scale_bytes = Scale::Bytes.new(events_data) Scale::Types.get("Vec<EventRecord>").decode(scale_bytes).to_human end |
#get_block_number(block_hash) ⇒ Object
90 91 92 93 |
# File 'lib/substrate_client.rb', line 90 def get_block_number(block_hash) header = self.chain_getHeader(block_hash) header["number"].to_i(16) end |
#get_metadata(block_hash = nil) ⇒ Object
95 96 97 98 |
# File 'lib/substrate_client.rb', line 95 def (block_hash=nil) hex = self.state_getMetadata(block_hash) Scale::Types::Metadata.decode(Scale::Bytes.new(hex)) end |
#get_storage(module_name, storage_name, params = nil, block_hash = nil) ⇒ Object
Plain: client.get_storage(“Sudo”, “Key”) Plain: client.get_storage(“Balances”, “TotalIssuance”) Map: client.get_storage(“System”, “Account”, [“0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d”]) DoubleMap: client.get_storage(“ImOnline”, “AuthoredBlocks”, [2818, “0x749ddc93a65dfec3af27cc7478212cb7d4b0c0357fef35a0163966ab5333b757”])
120 121 122 123 124 125 126 127 128 |
# File 'lib/substrate_client.rb', line 120 def get_storage(module_name, storage_name, params = nil, block_hash = nil) self.init_runtime(block_hash) storage_key, return_type = SubstrateClient::Helper.(@metadata, module_name, storage_name, params) result = self.state_getStorage(storage_key, block_hash) return unless result Scale::Types.get(return_type).decode(Scale::Bytes.new(result)) end |
#init_runtime(block_hash = nil) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/substrate_client.rb', line 59 def init_runtime(block_hash=nil) # set current runtime spec version runtime_version = self.state_getRuntimeVersion(block_hash) @spec_version = runtime_version["specVersion"] Scale::TypeRegistry.instance.spec_version = @spec_version # set current metadata @metadata = self.(block_hash) Scale::TypeRegistry.instance. = @metadata.value true end |
#invoke(method, *params) ⇒ Object
71 72 73 74 |
# File 'lib/substrate_client.rb', line 71 def invoke(method, *params) # params.reject! { |param| param.nil? } request(method, params) end |
#methods ⇒ Object
################################################ custom methods based on origin rpc methods ################################################
86 87 88 |
# File 'lib/substrate_client.rb', line 86 def methods invoke("rpc_methods")["methods"] end |
#request(method, params) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/substrate_client.rb', line 43 def request(method, params) payload = { "jsonrpc" => "2.0", "method" => method, "params" => params, "id" => @request_id } data = ws_request(@url, payload) if data["error"] raise RpcError, data["error"] else data["result"] end end |