diff --git a/lib/helper.rb b/lib/helper.rb index 868b68c..cedc76b 100644 --- a/lib/helper.rb +++ b/lib/helper.rb @@ -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 diff --git a/lib/scale.rb b/lib/scale.rb index ef44005..a9d33e5 100644 --- a/lib/scale.rb +++ b/lib/scale.rb @@ -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 diff --git a/spec/substrate_client_spec.rb b/spec/substrate_client_spec.rb index 2f0f548..92d80b4 100644 --- a/spec/substrate_client_spec.rb +++ b/spec/substrate_client_spec.rb @@ -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") @@ -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)