polkascan / py-substrate-interface

Python Substrate Interface
https://polkascan.github.io/py-substrate-interface/
Apache License 2.0
240 stars 116 forks source link

ValueError: Provided data is not in supported format: provided '<class 'abc.scale_info::135'>' #282

Closed wowvwow closed 1 year ago

wowvwow commented 1 year ago
result = Substrate().substrate.query_map('RmrkCore', 'Properties', [10324, 31])
print(result)
for k, v in result:
    print(k, type(k), v, type(v))
    if k == 'stake-info':
        print(v)
        # obj = RuntimeConfiguration().create_scale_object(
        #     'Compact<Balance>', data=ScaleBytes('0x068500be5822de000000000000000000')
        # )
        # obj.decode()
        # print(obj.value)
        print(Substrate().substrate.decode_scale(type_string='scale_info::135', scale_bytes=ScaleBytes(v)))

<substrateinterface.base.QueryMapResult object at 0x7f637357fac0> description <class 'abc.scale_info::134'> Khala - Stakepool - #265 <class 'abc.scale_info::135'> createtime <class 'abc.scale_info::134'> 1671991536 <class 'abc.scale_info::135'> name <class 'abc.scale_info::134'> Khala - Stakepool Delegation NFT - #265 - 31 <class 'abc.scale_info::135'> stake-info <class 'abc.scale_info::134'> 0xa45d3830020000000000000000000000 <class 'abc.scale_info::135'> 0xa45d3830020000000000000000000000

Traceback (most recent call last): File "/root/.pycharm_helpers/pydev/pydevconsole.py", line 364, in runcode coro = func() File "", line 1, in File "/root/.pycharm_helpers/pydev/_pydev_bundle/pydev_umd.py", line 198, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script File "/root/.pycharm_helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/opt/01x/src/phala.py", line 3778, in print(Substrate().substrate.decode_scale(type_string='scale_info::135', scale_bytes=ScaleBytes(v))) File "/usr/local/lib/python3.8/dist-packages/scalecodec/base.py", line 537, in init raise ValueError("Provided data is not in supported format: provided '{}'".format(type(data))) ValueError: Provided data is not in supported format: provided '<class 'abc.scale_info::135'>'

wowvwow commented 1 year ago

I tried many times and read the code of scalecodec, but I still can't find a suitable way to convert this type, I need your help, pls

arjanz commented 1 year ago

A pointer I can give is that in the latest version of SubstrateInterface there is a function that can provide type composition info for storage functions: https://github.com/polkascan/py-substrate-interface#type-decomposition-information

Last week I actually took some effort to collect this information it generates for well-known Substrate runtimes into a extensive Substrate metadata website, so this is the information about the storage function you mentioned:

https://polkascan.github.io/py-substrate-metadata-docs/khala/#properties

You should read this as follows:

call = substrate.query(
    'RmrkCore', 'Properties', ['u32', (None, 'u32'), 'Bytes']
)

Let me know if this helps

wowvwow commented 1 year ago
result = Substrate().substrate.query('RmrkCore', 'Properties',
                                     ['10324', (None, '31'), '0x068500be5822de000000000000000000']
                                     )
print(result)

Traceback (most recent call last): File "/usr/lib/python3.8/code.py", line 90, in runcode exec(code, self.locals) File "", line 1, in File "/root/.pycharm_helpers/pydev/_pydev_bundle/pydev_umd.py", line 198, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script File "/root/.pycharm_helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/opt/01x/src/phala.py", line 3761, in result = Substrate().substrate.query('RmrkCore', 'Properties', File "/usr/local/lib/python3.8/dist-packages/substrateinterface/base.py", line 1437, in query params[idx] = param_obj.encode(param) File "/usr/local/lib/python3.8/dist-packages/scalecodec/base.py", line 736, in encode self.data = self.process_encode(self.value_serialized) File "/usr/local/lib/python3.8/dist-packages/scalecodec/types.py", line 153, in process_encode return ScaleBytes('0x01') + sub_type_obj.encode(value) File "/usr/local/lib/python3.8/dist-packages/scalecodec/base.py", line 736, in encode self.data = self.process_encode(self.value_serialized) File "/usr/local/lib/python3.8/dist-packages/scalecodec/types.py", line 324, in process_encode if 0 <= int(value) <= 2**32 - 1: TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'

Before that, I tried https://polkascan.github.io/py-substrate-metadata-docs/khala/#properties to pass parameters, but the above error will appear

arjanz commented 1 year ago

result = Substrate().substrate.query('RmrkCore', 'Properties', ['10324', (None, '31'), '0x068500be5822de000000000000000000'] ) print(result)

It should be either None or 31, that parameter is a Option :

result = Substrate().substrate.query('RmrkCore', 'Properties',
                                     [10324, 31, '0x068500be5822de000000000000000000']
                                     )