Polymesh icon indicating copy to clipboard operation
Polymesh copied to clipboard

Is Polymesh compatible with substrate?

Open Wangmmx opened this issue 3 years ago • 14 comments

Hi, I tried to call substrate.get_events, but the data I got could not be decoded successfully. Is there any special type? I am currently referring to https://github.com/ PolymeshAssociation/Polymesh/blob/develop/polymesh_schema.json

Btw, the polkadot.js.org could not display block details which has transfer event like me: https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Fmainnet-rpc.polymesh.network#/explorer/query/4921857

Wangmmx avatar Oct 12 '22 07:10 Wangmmx

We have our own Polkadot.js webapp here: https://mainnet-app.polymesh.network/#/explorer/query/4921857 The error you are seeing is with how the webapp tries to summarize POLYX transfer. We had added some extra fields to the transfer event.

Neopallium avatar Oct 12 '22 08:10 Neopallium

This works, thanks for your reply!

Wangmmx avatar Oct 12 '22 09:10 Wangmmx

What substrate.get_events are you calling? Is it a TypeScript/JS package or Rust package?

Neopallium avatar Oct 12 '22 09:10 Neopallium

What substrate.get_events are you calling? Is it a TypeScript/JS package or Rust package?

I used polkascan-lib to decode and call, get_events from https://github.com/polkascan/py-substrate-interface/blob/b48f51538b46ec6bcebcb40b023aa41257211021/substrateinterface/base.py#L1509

Wangmmx avatar Oct 12 '22 09:10 Wangmmx

That library says that it supports most MetadataV14 chains, which we support now (since our v5.0 release last month). With the v14 metadata, the polymesh_schema.json file shouldn't be needed, at least not for blocks produced after the v5.0 upgrade.

Neopallium avatar Oct 12 '22 09:10 Neopallium

Maybe it needs some extra info for the type_registry_preset to support Polymesh.

Neopallium avatar Oct 12 '22 09:10 Neopallium

Yes, I have tried most of the substrate coins successfully, but the polymesh has been failing to decode, maybe there are some infos missing.

Thank you for your willingness to help me, cause I have not updated this library synchronously for a long time, so it is not clear whether it is the problem with the library or the types I configured, I will try again

Wangmmx avatar Oct 12 '22 09:10 Wangmmx

I am going to try that python library locally here.

Neopallium avatar Oct 12 '22 09:10 Neopallium

Found the problem. Most substrate chains have their runtime package just one level deep (Polkadot polkadot_runtime::Event), but Polymesh has an extra package level polymesh_runtime_mainnet::runtime::Event. The python package scalecodec uses a *:: for each package level when searching for the Event type.

Add these line to the https://github.com/polkascan/py-scale-codec/blob/master/scalecodec/type_registry/polymesh-mainnet.json type registry file:

    "polymesh_runtime_develop::runtime::Event": "*::Event",
    "polymesh_runtime_testnet::runtime::Event": "*::Event",
    "polymesh_runtime_mainnet::runtime::Event": "*::Event",

I think you can download that file and use a local file instead of the built-in type registry files.

I will open a PR with scalecodec to add those lines.

Neopallium avatar Oct 12 '22 10:10 Neopallium

It works! Thank you very much! I'm so grateful for your willingness to help me and try it yourself ❤️

Wangmmx avatar Oct 12 '22 10:10 Wangmmx

Here is another way to patch in those types:

from substrateinterface import SubstrateInterface, Keypair
from substrateinterface.exceptions import SubstrateRequestException

# import logging
# logging.basicConfig(level=logging.DEBUG)

custom_types = {
    "types": {
        "polymesh_runtime_develop::runtime::Event": "*::Event",
        "polymesh_runtime_testnet::runtime::Event": "*::Event",
        "polymesh_runtime_mainnet::runtime::Event": "*::Event",
    }
}

try:
    substrate = SubstrateInterface(
        url="wss://mainnet-rpc.polymesh.network",
        type_registry=custom_types,
    )
except ConnectionRefusedError:
    print("⚠️ No local Substrate node running")
    exit()

events = substrate.get_events()
for event in events:
    print(f'* {event.value}')

Neopallium avatar Oct 12 '22 10:10 Neopallium

Ok, now I am using a local JSON file 😄, I recorded the way you said.

Wangmmx avatar Oct 12 '22 11:10 Wangmmx

Hi guys,

As @Neopallium pointed out, the path used for the Event type in the Polymesh runtime was incompatible with the current wildcard search in scalecodec (*::runtime::Event)

I suspect though this is because a recent change in Substrate (https://github.com/paritytech/substrate/pull/11981) so I made a generic solution for this.

I just released a fix in scalecodec package covering this: https://github.com/polkascan/py-scale-codec/releases/tag/v1.0.45

arjanz avatar Oct 12 '22 11:10 arjanz

@arjanz Confirmed that works. Thanks for the quick fix.

Neopallium avatar Oct 12 '22 12:10 Neopallium