polkadot-js / api

Promise and RxJS APIs around Polkadot and Substrate based chains via RPC calls. It is dynamically generated based on what the Substrate runtime provides in terms of metadata.
Apache License 2.0
1.07k stars 349 forks source link

Getting "wasm trap: wasm `unreachable` instruction executed" error when calling `paymentInfo` #5913

Closed wuzhong-papermoon closed 3 months ago

wuzhong-papermoon commented 3 months ago

I'm reaching out to report an unusual behavior encountered while using the paymentInfo API for XCM transfers. We're attempting to send an XCM transfer for multiple tokens, but we're receiving the following error:

RPC-CORE: call(method: Text, data: Bytes, at?: BlockHash): Bytes:: 4003: Client error: Execution failed: Execution aborted due to trap: wasm trap: wasm `unreachable` instruction executed
WASM backtrace:
error while executing at wasm backtrace:
    0: 0x3ef569 - <unknown>!rust_begin_unwind
    1: 0x20da - <unknown>!core::panicking::panic_fmt::hbb5a6b42001bdfec
    2: 0x348ac9 - <unknown>!TransactionPaymentApi_query_info

Below is a sample code for your reference, SDK version is the latest:

import { ApiPromise, WsProvider } from '@polkadot/api';

const wsProvider = new WsProvider('wss://statemint-rpc-tn.dwellir.com');

const main = async () => {
  const api = await ApiPromise.create({ provider: wsProvider });
  await api.isReady;

  const extrinsic = api.tx.polkadotXcm.limitedReserveTransferAssets(
    { "V2": { "parents": 1, X1: { "Parachain": 2004 } } },
    { "V2": { "parents": 0, X1: { "AccountKey20": { "network": "Any", "key": "0xf24ff3a9cf04c71dbc94d0b566f7a27b94566cac" } } } },
    {
      "V2": [
        { "Concrete": { "parents": 0, X2: { "PalletInstance": 50, "GeneralIndex": "42,069" } }, "Fungible": 100000000 },
        { "Concrete": { "parents": 0, X2: { "PalletInstance": 50, "GeneralIndex": "1,984" } }, "Fungible": 1000000 }
      ]
    },
    1,
    "Unlimited"
  );

  const info = await extrinsic.paymentInfo("15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5");
  console.log(info)
};

main();

The weird thing is if assets array contains only one item, for example:

  const extrinsic = api.tx.polkadotXcm.limitedReserveTransferAssets(
    { "V2": { "parents": 1, X1: { "Parachain": 2004 } } },
    { "V2": { "parents": 0, X1: { "AccountKey20": { "network": "Any", "key": "0xf24ff3a9cf04c71dbc94d0b566f7a27b94566cac" } } } },
    {
      "V2": [
        { "Concrete": { "parents": 0, X2: { "PalletInstance": 50, "GeneralIndex": "42,069" } }, "Fungible": 100000000 },
        // { "Concrete": { "parents": 0, X2: { "PalletInstance": 50, "GeneralIndex": "1,984" } }, "Fungible": 1000000 }
      ]
    },
    1,
    "Unlimited"
  );

The code works fine. Would you take a look at this issue please? Thanks!

TarikGul commented 3 months ago

I have seen this before, I think it's because the ordering of your assets are incorrect. Try switching the order of your assets and see if that works.

We have written something similar in the asset-trasnfer-api that ensures ordering is correct of assets: https://github.com/paritytech/asset-transfer-api/blob/main/src/createXcmTypes/util/sortAssetsAscending.ts

albertov19 commented 3 months ago

Hey @TarikGul - This is still an issue/bug that should be looked into, right?

Thanks in advance

TarikGul commented 3 months ago

Well it depends on if what I suggested above is the issue. If im correct than no its not a bug. Unfortunately it's not a helpful error from the chain, but this api is just acting as a mirror and giving whatever error the chain would.

albertov19 commented 3 months ago

Yeah, I remember this happening some time ago in which the order of the assets was crucial to avoid errors. This is in AssetHub.

I don't really understand the origin of the issue, but why would the order matter when calling the extrinsic? So the issue is actually within the pallet I guess?

Probably related to https://github.com/paritytech/polkadot-sdk/issues/3661 or https://github.com/paritytech/polkadot-sdk/issues/2123

TarikGul commented 3 months ago

I don't really understand the origin of the issue, but why would the order matter when calling the extrinsic? So the issue is actually within the pallet I guess?

Yea exactly the issue is within the pallet, i'll find the exact code and link it here in a second.

TarikGul commented 3 months ago

So this is part of it, and as you look further into the implementation, you'll find how the ordering works exactly.

albertov19 commented 3 months ago

Ok I mean this is kind of weird from a DX perspective, but I understand now it is not an issue with the API - Feel free to close this issue as the problem lies in the pallet

TarikGul commented 3 months ago

Awesome, closing as resolved.

polkadot-js-bot commented 3 months ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue if you think you have a related problem or query.