lib/client/client_ext.rb in scale_rb-0.3.5 vs lib/client/client_ext.rb in scale_rb-0.4.0
- old
+ new
@@ -1,40 +1,34 @@
module ScaleRb
# This module is used to add extra methods to both the ScaleRb::WsClient ScaleRb::HttpClient
module ClientExt
- StorageQuery = Struct.new(:pallet_name, :storage_name, :key_part1, :key_part2, keyword_init: true) do
- def initialize(pallet_name:, storage_name:, key_part1: nil, key_part2: nil)
- super
- end
- end
-
# get decoded metadata at block_hash
- def get_metadata(block_hash)
+ def get_metadata(block_hash = nil)
+ block_hash ||= chain_getHead
metadata_hex = state_getMetadata(block_hash)
ScaleRb::Metadata.decode_metadata(metadata_hex.strip._to_bytes)
end
# Get decoded storage at block_hash
- def get_storage(block_hash, storage_query, metadata = nil)
+ def get_storage(pallet_name, storage_name, params = [], block_hash: nil, metadata: nil)
+ block_hash ||= chain_getHead
metadata ||= get_metadata(block_hash)
# storeage item
- pallet_name = convert_to_camel_case storage_query.pallet_name
- storage_name = convert_to_camel_case storage_query.storage_name
+ pallet_name = convert_to_camel_case pallet_name
+ storage_name = convert_to_camel_case storage_name
# storage param
- key = [storage_query.key_part1, storage_query.key_part2].compact
- ScaleRb.logger.debug "#{pallet_name}.#{storage_name}(#{key.join(', ')})"
- key = key.map { |part_of_key| c(part_of_key) }
- ScaleRb.logger.debug "converted key: #{key}"
+ ScaleRb.logger.debug "#{pallet_name}.#{storage_name}(#{params.inspect})"
+ params = params.map { |param| c(param) }
get_storage2(
block_hash, # at
pallet_name,
storage_name,
- key,
+ params,
metadata
)
end
private
@@ -100,11 +94,11 @@
# },
# ..
#
# key is for the param, value is for the return
def get_storage1(block_hash, pallet_name, item_name, key, value, registry)
- ScaleRb::logger.debug "get_storage1: #{pallet_name}.#{item_name} key: #{key} value: #{value}"
+ ScaleRb.logger.debug "#{pallet_name}.#{item_name}, key: #{key.inspect}, value: #{value}"
if key
if key[:value].nil? || key[:value].empty?
# map, but no key's value provided. get all storages under the partial storage key
partial_storage_key = StorageHelper.encode_storage_key(pallet_name, item_name)._to_hex
@@ -136,10 +130,9 @@
StorageHelper.decode_storage(data, value[:type], value[:modifier] == 'Optional', value[:fallback], registry)
end
end
def get_storage2(block_hash, pallet_name, item_name, params, metadata)
- ScaleRb.logger.debug "get_storage2: #{pallet_name}.#{item_name} params: #{params}"
raise 'Metadata should not be nil' if metadata.nil?
registry = Metadata.build_registry(metadata)
item = Metadata.get_storage_item(
pallet_name, item_name, metadata