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 354 forks source link

Add decoration of runtimeApis #5845

Closed TarikGul closed 1 month ago

TarikGul commented 7 months ago

rel: https://github.com/polkadot-js/api/issues/5725

The next step in getting runtimeApis from the metadata is to have them decorated at runtime via the metadata. This can be seen as similar to decorateExtrinsics. This should only be available for V15.

Once this issue is complete we can then add it to the static typegen.

TarikGul commented 7 months ago

This would actually replace decorateCalls inside of the api base decoration.

peetzweg commented 3 months ago

@TarikGul Is there a way to manually add runtime API calls for now?

Unable to find hints in the docs, so I tried using the runtime option during ApiPromise creation like below. However, it's not even added to ApiPromise.call (undefined is not an object), so probably not the way how to do it in the first place. 🤔

import { DefinitionsCall } from "@polkadot/types/types";
import { ApiPromise, WsProvider } from "@polkadot/api";

const wsProvider = new WsProvider(`wss://...`);

export const api = await ApiPromise.create({
  provider: wsProvider,
  runtime: {
    camelCasedPalletNameApi: [
      {
        version: 1,
        methods: {
          camelCasedRuntimeFn {
            description: "This is my function",
            params: [],
            type: "Balance",
          },
        },
      },
    ],
  } as DefinitionsCall,
});

const result = await pjsApi.call.camelCasedPalletNameApi.camelCasedRuntimeFn();
console.log({result})

--- update

Never mind got it to work. Seems like it's important to use camel case for the api name key and an underscored runtime call function name.

Found out how to do it here: https://github.com/polkadot-js/api/blob/3b7b44f048ff515579dd233ea6964acec39c0589/packages/types/src/interfaces/system/runtime.ts#L6-L24

All in all looking forward for metadata v15 support. 👍 🚀

decentration commented 2 months ago

this should go in V16

polkadot-js-bot commented 1 month 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.