Skip to content
This repository has been archived by the owner on Oct 10, 2022. It is now read-only.

Commit

Permalink
Change generate_storage_key_from_metadata params to use scale type
Browse files Browse the repository at this point in the history
  • Loading branch information
wuminzhe committed May 28, 2021
1 parent f84bbbd commit 627a17f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
20 changes: 17 additions & 3 deletions lib/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,30 @@ def generate_storage_key_from_metadata(metadata, module_name, storage_name, para
return_type = map[:value]
# TODO: decode to account id if param is address
# params[0] = decode(params[0]) if map[:key] == "AccountId"

type = Scale::Types.get(map[:key])
params[0] = type.new(params[0]).encode
if params[0].class != type
raise Scale::StorageInputTypeError.new("The type of first param is not equal to the type from metadata: #{map[:key]} => #{type}")
end
params[0] = 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

type1 = Scale::Types.get(map[:key1])
if params[0].class != type1
raise Scale::StorageInputTypeError.new("The type of 1st param is not equal to the type from metadata: #{map[:key1]} => #{type1.class.name}")
end
params[0] = params[0].encode

type2 = Scale::Types.get(map[:key2])
if params[1].class != type2
raise Scale::StorageInputTypeError.new("The type of 2nd param is not equal to the type from metadata: #{map[:key2]} => #{type2.class.name}")
end
params[1] = params[1].encode
else
raise NotImplementedError
end
Expand Down
1 change: 1 addition & 0 deletions lib/scale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class ScaleError < StandardError; end
class TypeBuildError < ScaleError; end
class BadDataError < ScaleError; end
class TypeRegistryNotLoadYet < ScaleError; end
class StorageInputTypeError < ScaleError; end

module Types
class << self
Expand Down
16 changes: 9 additions & 7 deletions spec/substrate_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,14 @@
expect(size).to be_instance_of Integer
end

it "can call state_getKeys" do
keys = @client.state_getKeys("0x26aa394eea5630e07c48ae0c9558cef7") # System
expect(keys).to be_instance_of Array
# Too time consuming
# it "can call state_getKeys" do
# keys = @client.state_getKeys("0x26aa394eea5630e07c48ae0c9558cef7") # System
# expect(keys).to be_instance_of Array

keys = @client.state_getKeys("0x26aa394eea5630e07c48ae0c9558cef7", "0x860e0ed04bd1b2a1efa70c9db13cc73f830d7e14204680316db52f05fd91ba37") # System
expect(keys.length).to eq(24860)
end
# keys = @client.state_getKeys("0x26aa394eea5630e07c48ae0c9558cef7", "0x860e0ed04bd1b2a1efa70c9db13cc73f830d7e14204680316db52f05fd91ba37") # System
# expect(keys.length).to eq(24860)
# end

it "can call state_getReadProof" do
read_proof = @client.state_getReadProof(["0x26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7"], "0x860e0ed04bd1b2a1efa70c9db13cc73f830d7e14204680316db52f05fd91ba37")
Expand Down Expand Up @@ -170,7 +171,8 @@
end

it "can get decoded storage" do
account = @client.get_storage "System", "Account", ["0x50be873393f9e3f5705d8b573729cd35b080e5f9029534e8b848371a9cdecc1e"], "0xedf6ff93fb6dd1c00b96dafb576e01975e85710ff3b0eea7244e576579f28388"
account_id = Scale::Types::AccountId.new("0x50be873393f9e3f5705d8b573729cd35b080e5f9029534e8b848371a9cdecc1e")
account = @client.get_storage "System", "Account", [account_id], "0xedf6ff93fb6dd1c00b96dafb576e01975e85710ff3b0eea7244e576579f28388"

expect_result = {"c"=>2, "data"=>{"feeFrozen"=>499192308000, "free"=>378566339746251, "miscFrozen"=>499192308000, "reserved"=>0}, "nonce"=>0}
expect(account.to_human).to eq(expect_result)
Expand Down

0 comments on commit 627a17f

Please sign in to comment.