polkadot-api: Issues Migrating XCM SDK from PJS to PAPI - getChainSpecData() error

Hello there,

we are in the process of migrating our XCM SDK from PJS to PAPI, but we’ve encountered a few obstacles along the way.

The first issue is that we need to call the equivalent of api.rpc.system.properties() for every parachain in the ecosystem. I discovered that getChainSpecData() is the PAPI equivalent. However, this method throws an error for some chains, indicating that some RPC method is not found. Is there an alternative way to obtain the system properties?

The second issue is that we need the ability to create a transaction object from an unsigned transaction hex string. Do you plan on implementing this feature soon?

Code example:

import { createClient } from 'polkadot-api'
import { getWsProvider } from 'polkadot-api/ws-provider/node'

void (async () => {
  // Example Polkadot AssetHub WebSocket URL
  const wsUrl = 'wss://dot-rpc.stakeworld.io/assethub'
  const client = createClient(getWsProvider(wsUrl))
  const chainSpec = await client.getChainSpecData()
  console.log(chainSpec)
})()

Error message:

file:///Users/michael/Projects/xcm-tools/node_modules/.pnpm/@polkadot-api+substrate-client@0.2.1/node_modules/@polkadot-api/substrate-client/dist/esm/client/createClient.mjs:31
        return error ? cb.onError(new RpcError(error)) : cb.onSuccess(result, (opaqueId, subscriber) => {
                                  ^

RpcError: Method not found
    at onMessage (file:///Users/michael/Projects/xcm-tools/node_modules/.pnpm/@polkadot-api+substrate-client@0.2.1/node_modules/@polkadot-api/substrate-client/dist/esm/client/createClient.mjs:31:35)
    at onMessageProxy (file:///Users/michael/Projects/xcm-tools/node_modules/.pnpm/@polkadot-api+json-rpc-provider-proxy@0.2.0/node_modules/@polkadot-api/json-rpc-provider-proxy/dist/esm/get-sync-provider.mjs:23:5)
    at _onMessageProxy (file:///Users/michael/Projects/xcm-tools/node_modules/.pnpm/@polkadot-api+json-rpc-provider-proxy@0.2.0/node_modules/@polkadot-api/json-rpc-provider-proxy/dist/esm/get-sync-provider.mjs:61:20)
    at WS._onMessage (file:///Users/michael/Projects/xcm-tools/node_modules/.pnpm/@polkadot-api+ws-provider@0.2.0_bufferutil@4.0.8_utf-8-validate@5.0.10/node_modules/@polkadot-api/ws-provider/dist/node/esm/ws-provider.mjs:22:39)
    at callListener (/Users/michael/Projects/xcm-tools/node_modules/.pnpm/ws@8.18.0_bufferutil@4.0.8_utf-8-validate@5.0.10/node_modules/ws/lib/event-target.js:290:14)
    at WS.onMessage (/Users/michael/Projects/xcm-tools/node_modules/.pnpm/ws@8.18.0_bufferutil@4.0.8_utf-8-validate@5.0.10/node_modules/ws/lib/event-target.js:209:9)
    at WS.emit (node:events:519:28)
    at Receiver.receiverOnMessage (/Users/michael/Projects/xcm-tools/node_modules/.pnpm/ws@8.18.0_bufferutil@4.0.8_utf-8-validate@5.0.10/node_modules/ws/lib/websocket.js:1220:20)
    at Receiver.emit (node:events:519:28)
    at Receiver.dataMessage (/Users/michael/Projects/xcm-tools/node_modules/.pnpm/ws@8.18.0_bufferutil@4.0.8_utf-8-validate@5.0.10/node_modules/ws/lib/receiver.js:596:14) {
  code: -32601,
  data: undefined
}

Thank you for your assistance.

With kind regards,

Michael and @dudo50 from ParaSpell✨

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 28 (13 by maintainers)

Most upvoted comments

After updating to v1.6.2 the KiltSpiritnet, Zeitgeist and Curio work as expected. Thank you for the update! I have also opened a separate issue for Mythos, Crab and Darwinia with a minimal reproduction repository. https://github.com/polkadot-api/polkadot-api/issues/813. Hope it helps identify the problem.

With kind regards, team ParaSpell ✨

The issue that you experienced with KiltSpiritnet, Zeitgeist and Curio should have been fixed on polkadot-api@1.6.2 (which we actually released last week, but we forgot to publish the release notes, so I just did that). The thing is that these chains use a u64 for their block-number. Could you please upgrade to the latest version of polkadot-api and let us know if that solves the problem? Otherwise, please open a separate issue.

On the other hand, could you please open a separate issue for the error that you are experiencing with Mythos, Crab and Darwinia? 🙏 Also, please provide as much detail as you can. A repro would be ideal, of course. Thanks again!

Hi @michaeldev5 I will be looking into this today. I suspect that the first issue could be related to this. Also, I don’t think that the second issue is related to the first one… I could be wrong, though.

If you could please open 2 separate issues and provide as much context as possible, that would be greatly appreciated. If you could provide a minimal repro on Paseo that would be incredibly helpful. Otherwise, we will take what we can get, no worries.

Thanks for reporting this! We will try to have a fix ASAP!

PS: On a different topic, there have been quite a few improvements since we started creating awareness about the state of the new JSON-RPC API. Also, yesterday I published this post on the Polkadot forum. Let’s hope that things keep on getting better 🤞.

Hello @josepot,

we are continuing to integrate PAPI into our SDK, majority of chains work great! However we run into some issues with some of the chains, we are unable to solve.

When submitting a transaction from KiltSpiritnet, Zeitgeist and Curio we are receiving this error:

TypeError: Cannot read properties of null (reading 'length')
 ❯ fromHex ../../node_modules/.pnpm/@polkadot-api+utils@0.1.2/node_modules/@polkadot-api/utils/src/hex.ts:42:27
 ❯ ../../node_modules/.pnpm/polkadot-api@1.6.1_@swc+core@1.7.26_bufferutil@4.0.8_jiti@1.21.3_postcss@8.4.47_rxjs@7.8.1_sm_isbp6aj7uiqmjgdvyeeerr634m/node_modules/polkadot-api/src/tx/signed-extensions/utils.ts:20:27
 ❯ ../../node_modules/.pnpm/rxjs@7.8.1/node_modules/rxjs/src/internal/operators/map.ts:58:33
 ❯ OperatorSubscriber._this._next ../../node_modules/.pnpm/rxjs@7.8.1/node_modules/rxjs/src/internal/operators/OperatorSubscriber.ts:70:13
 ❯ OperatorSubscriber.Subscriber.next ../../node_modules/.pnpm/rxjs@7.8.1/node_modules/rxjs/src/internal/Subscriber.ts:75:12
 ❯ ../../node_modules/.pnpm/rxjs@7.8.1/node_modules/rxjs/src/internal/operators/mergeInternals.ts:85:24
 ❯ OperatorSubscriber._this._next ../../node_modules/.pnpm/rxjs@7.8.1/node_modules/rxjs/src/internal/operators/OperatorSubscriber.ts:70:13
 ❯ OperatorSubscriber.Subscriber.next ../../node_modules/.pnpm/rxjs@7.8.1/node_modules/rxjs/src/internal/Subscriber.ts:75:12
 ❯ ../../node_modules/.pnpm/rxjs@7.8.1/node_modules/rxjs/src/internal/operators/mergeInternals.ts:85:24
 ❯ OperatorSubscriber._this._next ../../node_modules/.pnpm/rxjs@7.8.1/node_modules/rxjs/src/internal/operators/OperatorSubscriber.ts:70:13

After investigating I believe that the error maybe because the genesis hash is null for some reason. This line in the source code is producing error.

const genesisHashFromCtx = (ctx) => ctx.chainHead.storage$(ctx.at, "value", () => genesisHashStorageKey, null).pipe(map((result) => fromHex(result)));

This is how I am making a transactions. Similarly for other chains also.:

const addr = FixedSizeBinary.fromAccountId32<32>(addressSS58);

const tx = client.getTypedApi(curio).tx.PolkadotXcm.reserve_transfer_assets({
    dest: XcmVersionedLocation.V2({
      parents: 1,
      interior: XcmV2MultilocationJunctions.X1(XcmV2Junction.Parachain(2000)),
    }),
    beneficiary: XcmVersionedLocation.V2({
      parents: 0,
      interior: XcmV2MultilocationJunctions.X1(
        XcmV2Junction.AccountId32({
          network: XcmV2NetworkId.Any(),
          id: addr,
        })
      ),
    }),
    assets: XcmVersionedAssets.V2([
      {
        id: XcmV2MultiassetAssetId.Concrete({
          parents: 0,
          interior: XcmV2MultilocationJunctions.Here(),
        }),
        fun: XcmV2MultiassetFungibility.Fungible(BigInt(2000)),
      },
    ]),
    fee_asset_item: 0,
  });

  const hex = await tx.sign(signer);

Another type of error we are experiencing with Mythos, Crab and Darwinia is:

OperationError: Execution failed: Execution aborted due to trap: wasm trap: wasm `unreachable` instruction executed
WASM backtrace:
error while executing at wasm backtrace:
    0: 0x401a8a - <unknown>!rust_begin_unwind
    1: 0xb826 - <unknown>!core::panicking::panic_fmt::h7d22643b0becf577
    2: 0x2ccd01 - <unknown>!AccountNonceApi_account_nonce
 ❯ Object.next ../../node_modules/.pnpm/@polkadot-api+substrate-client@0.2.2/node_modules/@polkadot-api/substrate-client/src/chainhead/operation-promise.ts:65:21
 ❯ Object.next ../../node_modules/.pnpm/@polkadot-api+substrate-client@0.2.2/node_modules/@polkadot-api/substrate-client/src/internal-utils/subscriptions-manager.ts:18:30
 ❯ Object.onAllFollowEventsNext [as next] ../../node_modules/.pnpm/@polkadot-api+substrate-client@0.2.2/node_modules/@polkadot-api/substrate-client/src/chainhead/chainhead.ts:63:30
 ❯ Object.next ../../node_modules/.pnpm/@polkadot-api+substrate-client@0.2.2/node_modules/@polkadot-api/substrate-client/src/internal-utils/subscriptions-manager.ts:18:30
 ❯ onMessage ../../node_modules/.pnpm/@polkadot-api+substrate-client@0.2.2/node_modules/@polkadot-api/substrate-client/src/client/createClient.ts:91:23
 ❯ ../../node_modules/.pnpm/@polkadot-api+polkadot-sdk-compat@2.3.1/node_modules/@polkadot-api/polkadot-sdk-compat/src/parsed.ts:39:14
 ❯ ../../node_modules/.pnpm/@polkadot-api+polkadot-sdk-compat@2.3.1/node_modules/@polkadot-api/polkadot-sdk-compat/src/parsed-enhancers/fix-descendant-values.ts:67:5
 ❯ ../../node_modules/.pnpm/@polkadot-api+polkadot-sdk-compat@2.3.1/node_modules/@polkadot-api/polkadot-sdk-compat/src/utils.ts:45:33
 ❯ ../../node_modules/.pnpm/@polkadot-api+polkadot-sdk-compat@2.3.1/node_modules/@polkadot-api/polkadot-sdk-compat/src/parsed-enhancers/fix-unordered-blocks.ts:104:7
 ❯ ../../node_modules/.pnpm/@polkadot-api+polkadot-sdk-compat@2.3.1/node_modules/@polkadot-api/polkadot-sdk-compat/src/parsed-enhancers/fix-premature-blocks.ts:100:7

We are using EVM signer for these. But I am not sure where could be the problem.

Thank you so much for your time!

With kind regards, Team ParaSpell

Hey @josepot , any news regarding the problematic endpoints?

Is there any fix in plan/in works/merged?

Thanks for the information!

With kind regards, Team ParaSpell

First and foremost, thank you for providing this detailed list. It has been very helpful.

We are still considering the best course of action:

On one hand, it’s quite unfortunate that so many chains haven’t upgraded their PolkadotSDK nodes in a long time. The proper solution would be to require these chains to upgrade their nodes, which we have started doing.

We have also brought this issue to the attention of Parity and asked for their assistance, as they have an “ecosystem success” team responsible for such matters. However, Parity has shown zero commitment in this regard so far. Perhaps if more people reach out to them, they might take action, but I’m not optimistic because, as far as I know, no team has included this in their KRs, meaning no one at Parity is currently incentivized to address this issue. It’s silly, I know, but please don’t shoot the messenger. I’m afraid that unless they feel some pressure, they are unlikely to act.

At this point, my plan is to collect data on all the problematic chains, create a script to monitor their status, open issues in their respective GitHub projects, and possibly create a forum post on the Polkadot forum titled “The Wall of Shame” (or something similar), where I intend to highlight the chains that are hindering the technical progress of the ecosystem by not upgrading their nodes.

On the other hand, we could potentially develop a middleware that translates the legacy JSON-RPC API to the current JSON-RPC specification. However, that approach would be quite hacky, time-consuming to implement properly, and unfortunate to resort to. That being said, anyone could undertake this task, so if someone really wants this middleware, they might consider developing it themselves.

@josepot Here is the full list of nodes along with failure reason and array of tried endpoints.

[
  {
    node: 'ComposableFinance',
    reason: 'TIMED_OUT',
    providers: [
      'wss://rpc.composable.finance',
      'wss://composable-rpc.dwellir.com'
    ]
  },
  {
    node: 'Interlay',
    reason: 'TIMED_OUT',
    providers: [
      'wss://interlay-rpc.dwellir.com',
      'wss://api.interlay.io/parachain',
      'wss://rpc-interlay.luckyfriday.io/'
    ]
  },
  {
    node: 'Parallel',
    reason: 'TIMED_OUT',
    providers: [ 'wss://parallel-rpc.dwellir.com' ]
  },
  {
    node: 'Amplitude',
    reason: 'TIMED_OUT',
    providers: [
      'wss://amplitude-rpc.dwellir.com',
      'wss://rpc-amplitude.pendulumchain.tech'
    ]
  },
  {
    node: 'Pioneer',
    reason: 'TIMED_OUT',
    providers: [ 'wss://pioneer-rpc-3.bit.country/wss' ]
  },
  {
    node: 'CrustShadow',
    reason: 'RPC_ERROR',
    providers: [
      'wss://rpc-shadow.crust.network/',
      'wss://rpc-shadow.crustnetwork.app',
      'wss://rpc-shadow.crustnetwork.cc',
      'wss://rpc-shadow.crustnetwork.xyz'
    ]
  },
  {
    node: 'Kintsugi',
    reason: 'TIMED_OUT',
    providers: [
      'wss://kintsugi-rpc.dwellir.com',
      'wss://api-kusama.interlay.io/parachain',
      'wss://kintsugi.api.onfinality.io/public-ws'
    ]
  },
  {
    node: 'ParallelHeiko',
    reason: 'TIMED_OUT',
    providers: [ 'wss://heiko-rpc.parallel.fi' ]
  },
  {
    node: 'Picasso',
    reason: 'TIMED_OUT',
    providers: [
      'wss://rpc.composablenodes.tech',
      'wss://picasso-rpc.dwellir.com'
    ]
  },
  {
    node: 'Robonomics',
    reason: 'RPC_ERROR',
    providers: [
      'wss://kusama.rpc.robonomics.network/',
      'wss://robonomics.0xsamsara.com'
    ]
  },
  {
    node: 'Turing',
    reason: 'RPC_ERROR',
    providers: [ 'wss://turing-rpc.dwellir.com', 'wss://rpc.turing.oak.tech' ]
  },
  {
    node: 'Pendulum',
    reason: 'TIMED_OUT',
    providers: [ 'wss://rpc-pendulum.prd.pendulumchain.tech' ]
  },
  {
    node: 'Polkadex',
    reason: 'TIMED_OUT',
    providers: [
      'wss://polkadex-parachain-rpc.dwellir.com',
      'wss://polkadex-parachain.api.onfinality.io/public-ws',
      'wss://polkadex-parachain.public.curie.radiumblock.co/ws'
    ]
  },
  {
    node: 'Subsocial',
    reason: 'TIMED_OUT',
    providers: [
      'wss://para.subsocial.network',
      'wss://subsocial-rpc.dwellir.com'
    ]
  }
]

I just checked the 3 examples that you provided and in all those cases their RPC node exposes a super old (and unstable) versions of the JSON-RPC API.

The withPolkadotSdkCompat enhancer is able to fix many old versions of the node, but the ones that you mentioned are so old that they are out of scope. This is something that’s well explained in our docs.

I mean, we could spend 1 or 2 weeks creating yet another enhancer for those ancient nodes. However, we must draw the line at some point, and we decided that those versions are too problematic to try to “bridge” them.

The only solution that I can think for now for those parachains is to use the light-client. Otherwise, you can ask them to upgrade their nodes. Another thing that you could try is to ask Parity to have its “ecosystem success” team go after the parachains that are using those old versions and ask them to upgrade.

The API for creating a transaction from a binary call-data was added on v1.2.0, see the release notes. Regarding the enhancer for making the chainspec group of functions work over a WS provider against PolkadotSDK nodes that still don’t expose those group of functions: the PR has been merged, but it hasn’t been released yet. It should be released in a matter of days, I guess. Although, if you really need it, then you could use it before it gets released, like this:

import { createClient } from "polkadot-api"
import { getWsProvider } from "polkadot-api/ws-provider/web"
import { withPolkadotSdkCompat, parsed } from "polkadot-api/polkadot-sdk-compat"

const fixChainSpec = parsed((base) => (onMsg) => {
  const { send: originalSend, disconnect } = base(onMsg)

  const send = (msg: any) => {
    switch (msg.method) {
      case "chainSpec_v1_chainName":
        return originalSend({ ...msg, method: "system_chain" })
      case "chainSpec_v1_genesisHash":
        return originalSend({
          ...msg,
          method: "chain_getBlockHash",
          params: [0],
        })
      case "chainSpec_v1_properties":
        return originalSend({ ...msg, method: "system_properties" })
    }
    originalSend(msg)
  }

  return {
    send,
    disconnect,
  }
})

const client = createClient(
  fixChainSpec(
    withPolkadotSdkCompat(getWsProvider("wss://your-rpc.your-url.xyz"))
  ),
)