Class: SubstrateClient

Inherits:
Object
  • Object
show all
Defined in:
lib/substrate_client.rb,
lib/substrate_client/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ SubstrateClient

Returns a new instance of SubstrateClient.



44
45
46
47
# File 'lib/substrate_client.rb', line 44

def initialize(url)
  @url = url
  @request_id = 1
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

############################ native rpc methods support ############################



64
65
66
67
# File 'lib/substrate_client.rb', line 64

def method_missing(method, *args)
  data = request(SubstrateClient.real_method_name(method), args)
  data["result"]
end

Instance Attribute Details

#metadataObject

Returns the value of attribute metadata.



42
43
44
# File 'lib/substrate_client.rb', line 42

def 
  @metadata
end

#spec_nameObject

Returns the value of attribute spec_name.



42
43
44
# File 'lib/substrate_client.rb', line 42

def spec_name
  @spec_name
end

#spec_versionObject

Returns the value of attribute spec_version.



42
43
44
# File 'lib/substrate_client.rb', line 42

def spec_version
  @spec_version
end

Class Method Details

.generate_storage_hash(storage_module_name, storage_function_name, params = nil, hasher = nil, hasher2 = nil, metadata_version = nil) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/substrate_client.rb', line 150

def generate_storage_hash(storage_module_name, storage_function_name, params = nil, hasher = nil, hasher2 = nil,  = nil)
  if  and  >= 9
    storage_hash = Crypto.twox128(storage_module_name) + Crypto.twox128(storage_function_name)

    if params
      params.each_with_index do |param, index|
        if index == 0
          param_hasher = hasher
        elsif index == 1
          param_hasher = hasher2
        else
          raise "Unexpected third parameter for storage call"
        end

        param_key = param.hex_to_bytes
        param_hasher = "Twox128" if param_hasher.nil?
        storage_hash += Crypto.send param_hasher.underscore, param_key
      end
    end

    "0x#{storage_hash}"
  else
    # TODO: add test
    storage_hash = storage_module_name + " " + storage_function_name

    unless params.nil?
      params = [params] if params.class != ::Array
      params_key = params.join("")
      hasher = "Twox128" if hasher.nil?
      storage_hash += params_key.hex_to_bytes.bytes_to_utf8 
    end

    "0x#{Crypto.send( hasher.underscore, storage_hash )}"
  end
end

.real_method_name(method_name) ⇒ Object

chain_unsubscribe_runtime_version

>

chain_unsubscribeRuntimeVersion



189
190
191
192
# File 'lib/substrate_client.rb', line 189

def real_method_name(method_name)
  segments = method_name.to_s.split("_")
  segments[0] + "_" + segments[1] + segments[2..].map(&:capitalize).join
end

Instance Method Details

#get_metadata(block_hash) ⇒ Object



88
89
90
91
92
# File 'lib/substrate_client.rb', line 88

def (block_hash)
  hex = self.(block_hash)
   = Scale::Types::Metadata.decode(Scale::Bytes.new(hex))
  .value.value[:metadata]
end

#get_storage_at(module_name, storage_function_name, params = nil) ⇒ Object

client.init(0x014e4248dd04a8c0342b603a66df0691361ac58e69595e248219afa7af87bdc7) Plain: client.get_storage_at(“Balances”, “TotalIssuance”) Map: client.get_storage_at(“System”, “Account”, [“0x30599dba50b5f3ba0b36f856a761eb3c0aee61e830d4beb448ef94b6ad92be39”]) DoubleMap: client.get_storage_at(“ImOnline”, “AuthoredBlocks”, [2818, “0x749ddc93a65dfec3af27cc7478212cb7d4b0c0357fef35a0163966ab5333b757”])



98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
138
139
140
141
142
143
144
145
146
147
# File 'lib/substrate_client.rb', line 98

def get_storage_at(module_name, storage_function_name, params = nil)

  # TODO: uninit raise a exception
  # find the storage item from metadata
   = [:modules]
   = .detect { |mm| mm[:name] == module_name }
  raise "Module '#{module_name}' not exist" unless 
  storage_item = [:storage][:items].detect { |item| item[:name] == storage_function_name }
  raise "Storage item '#{storage_function_name}' not exist. \n#{.inspect}" unless storage_item

  if storage_item[:type][:Plain]
    return_type = storage_item[:type][:Plain]
  elsif map = storage_item[:type][:Map]
    raise "Storage call of type \"Map\" requires 1 parameter" if params.nil? || params.length != 1

    hasher = map[:hasher]
    return_type = map[:value]
    # TODO: decode to account id if param is address
    # params[0] = decode(params[0]) if map[:key] == "AccountId"
    params[0] = Scale::Types.get(map[:key]).new(params[0]).encode
  elsif map = storage_item[:type][:DoubleMap]
    raise "Storage call of type \"DoubleMapType\" requires 2 parameters" if params.nil? || params.length != 2

    hasher = map[:hasher]
    hasher2 = map[:key2Hasher]
    return_type = map[:value]
    params[0] = Scale::Types.get(map[:key1]).new(params[0]).encode
    params[1] = Scale::Types.get(map[:key2]).new(params[1]).encode
  else
    raise NotImplementedError
  end

  storage_hash = SubstrateClient.generate_storage_hash(
    module_name,
    storage_function_name,
    params,
    hasher,
    hasher2,
    [:version]
  )

  # puts storage_hash

  result = self.state_get_storage_at(storage_hash, block_hash)
  return unless result
  Scale::Types.get(return_type).decode(Scale::Bytes.new(result)).value
rescue => ex
  puts ex.message
  puts ex.backtrace
end

#init(block_hash = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/substrate_client.rb', line 78

def init(block_hash = nil)
  block_runtime_version = self.state_get_runtime_version(block_hash)
  @spec_name = block_runtime_version["specName"]
  @spec_version = block_runtime_version["specVersion"]

  Scale::TypeRegistry.instance.load(spec_name, spec_version)
  @metadata = self.(block_hash)
  true
end

#method_listObject

################################################ custom methods wrapped from native rpc methods ################################################



72
73
74
75
76
# File 'lib/substrate_client.rb', line 72

def method_list
  methods = self.rpc_methods["methods"].map(&:underscore)
  methods << "method_list"
  methods << "get_storage_at"
end

#request(method, params) ⇒ Object

TODO: error



50
51
52
53
54
55
56
57
58
59
# File 'lib/substrate_client.rb', line 50

def request(method, params)
  payload = {
    "jsonrpc" => "2.0",
    "method" => method,
    "params" => params,
    "id" => @request_id
  }
  @request_id += 1
  ws_request(@url, payload)
end