polkadot-js / docs

This is the documentation portal for all Polkadot and Substrate related tools and libraries in the polkadot-js family of repos.
https://polkadot.js.org/docs/
Creative Commons Zero v1.0 Universal
164 stars 199 forks source link

How to extract and parse runtime APIs from the metadata? #477

Open decentration opened 2 months ago

decentration commented 2 months ago

I am trying to find the correct way to parse api.call.metadata.metadata() so i can automatically render the "Runtime Calls" for the selected chain.

I am trying to get the runtime calls from the runtime metadata...

async function fetchRuntimeMetadata(api: ApiPromise) {
    const metadata = await api.call.metadata.metadata();
    const readableMetadata = hexToString(metadata.toString());

    console.log('readableMetadata',readableMetadata);
    return readableMetadata;
}

to convert the hex to string i naively flatten the object to a string, which makes it a big challenge to parse...

if i use the above, and try and decode the hex to string, i get this:

runtimeMetdata from queryMetadata: meta�
sp_corecrypto,AccountId32 [u8; 32] 0frame_system,AccountInfoNonce,AccountDatanonceNonce$consumers RefCount$providers RefCount,sufficients RefCountdata,AccountData<pallet_balancestypes,AccountDataBalancefreeBalance reservedBalancefrozenBalanceflags(ExtraFlags<pallet_balancestypes(ExtraFlagsu128 4frame_support dispatch@PerDispatchClassT$normal$T,operational$T$mandatory$T$(sp_weights$weight_v2Weight ref_time(u64(proof_size(u64(,,0<primitive_typesH256 [u8; 32]48(sp_runtimegenericdigestDigestlogs<<Vec<DigestItem><@@(sp_runtimegenericdigest(DigestItem(PreRuntimeDDConsensusEngineId4Vec<u8>$ConsensusDDConsensusEngineId4Vec<u8>SealDDConsensusEngineId4Vec<u8>Other4Vec<u8>dRuntimeEnvironmentUpdatedDHLL0frame_system,EventRecordEPT0phase)PhaseeventPEtopics�Vec<T>P@polkadot_runtime0RuntimeEvent�SystemTpframe_system::Event<Runtime>$Scheduler|�pallet_scheduler::Event<Runtime> Preimage�|pallet_preimage::Event<Runtime>
Indices�xpallet_indices::Event<Runtime> Balances�|pallet_balances::Event<Runtime>HTransactionPayment��pallet_transaction_payment::Event<Runtime> Staking�xpallet_staking::Event<Runtime> Offences�Xpallet_offences::EventSession�

how can i decode the metadata.metadata so that it is parsable?


Here is also the substrate stack exchange issue.

decentration commented 2 months ago

@jacogr can you help on this issue?

trying to know the way to parse "Runtime Calls"