5

I declared runtime api, implemented it in runtime, but i can't call my runtime api from polkadot.js. What i need to do to be able to call it?

Declaration:

sp_api::decl_runtime_apis! {
    pub trait Staking<AccountId, Balance>
    where
        AccountId: Codec,
        Balance: Codec
    {
        fn get_unclaimed_staking_rewards(account: &AccountId) -> Balance;

        fn get_staking_rates() -> StakingRates;
    }
}

Implementation:

impl runtime_api_staking::Staking<Block, AccountId, Balance> for Runtime {
        fn get_unclaimed_staking_rewards(account: &AccountId) -> Balance {
            ParachainStaking::get_unclaimed_staking_rewards(account)
        }

        fn get_staking_rates() -> runtime_api_staking::StakingRates {
            ParachainStaking::get_staking_rates()
        }
    }

2 Answers 2

3

The runtime calls shown on https://polkadot.js.org/app are not loaded from node metadata but probably hardcoded.

To invoke runtime api calls, you will need to use the rpc extension method state_call(on the tab Developer > RPC Calls) and pass method name and input as hex encoded value.

An example of calling runtime api pending_rewards defined in NominationPoolsApi is here.

example of invoking runtime api via polkadot.js app

The output is scale encoded so you will need to decode it manually.

You can also build your custom rpc calls which will show up automatically on polkadot js app. To do that, you will need to add your own rpc extension. There is a substrate recipe to do that https://substrate.recipes/custom-rpc.html.

The extensions for polkadot node are declared here https://github.com/paritytech/polkadot/blob/master/rpc/src/lib.rs.

Update: There is an open issue to add runtime api definitions in the metadata.

1

Polkadot JS API now supports runtime calls. Ankan pointed out that an issue was opened to add such support, and the subsequent PRs are now merged.

await api.call.<endpoint>.<call>(args)

or a concrete example:

const pendingRewards = await api.call.nominationPoolsApi.pendingRewards(member);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.