npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@polkadot-api/metadata-builders

v0.10.0

Published

This package has multiple functions that read a metadata object, denormalizes it, and builds other structures needed for different processes

Downloads

310,341

Readme

@polkadot-api/metadata-builders

This package has multiple functions that read a metadata object, denormalizes it, and builds other structures needed for different processes

## getLookupFn

interface MetadataLookup {
  (id: number): LookupEntry
  metadata: V14 | V15
}

function getLookupFn(metadata: Metadata): MetadataLookup

Given the a metadata, returns a function that will give the LookupEntry for an id along. The function also has access to the original metadata, as it's usually needed to work with the actual lookup.

The LookupEntry is a denormalized data structure for one entry in the metadata. It also "shortcuts" type references when those are pointers (composites or tuples of length 1). Essentially, it's a union of each of the different types that can be found in the lookup, mostly equivalent to something like:

type TerminalVar =
  | PrimitiveVar // u8, str, char, i128, etc.
  | CompactVar
  | BitSequenceVar
  | AccountId32

type ComposedVar =
  | TupleVar
  | StructVar
  | SequenceVar
  | ArrayVar
  | OptionVar
  | ResultVar
  | EnumVar

type LookupEntry = TerminalVar | ComposedVar

Where, for instance, a StructVar is of the shape

type StructVar = {
  type: "struct"
  value: Record<string, LookupEntry>
}

It's useful to get types referenced by storage calls, etc.

getDynamicBuilder

function getDynamicBuilder(metadataLookup: MetadataLookup): {
  buildDefinition: (id: number) => Codec
  buildConstant: (pallet: string, name: string) => Codec
  buildEvent: (pallet: string, name: string) => VariantEntry
  buildError: (pallet: string, name: string) => VariantEntry
  buildCall: (pallet: string, name: string) => VariantEntry
  buildStorage: (pallet: string, entry: string) => StorageEntry
  buildRuntimeCall: (api: string, method: string) => RuntimeEntry
}

Generates all the codecs needed to SCALE encode or decode the data for any interaction with the chain.

buildDefinition returns the codec for the type identified by the parameter id

buildConstant returns the codec for the requested constant (equivalent as calling buildDefinition with the type id of that constant)

buildEvent, buildError and buildCall return an object with the codec, and the indices of the pallet and entry within the metadata:

interface VariantEntry {
  location: [number, number] // [palletIdx, entryIdx],
  codec: Codec
}

buildStorage creates all the encoders/decoders needed to encode a storage call and decode its result:

interface StorageEntry {
  // Encodes the arguments of the storage call.
  enc: (...args: any[]) => string
  // Decodes the result from the storage call.
  dec: (value: string) => any
  // Decodes the arguments of the storage call
  keyDecoder: (value: string) => any[]
  // Expected number of arguments
  len: number
  // Decoded fallback value as defined in the metadata entry
  fallback: unknown
}

Similarly, buildRuntimeCall returns the codecs for both encoding the arguments of the runtime call, and the codec for decoding the result

interface RuntimeEntry {
  args: Codec<any[]>
  value: Codec<any>
}

getChecksumBuilder

function getChecksumBuilder(metadataLookup: MetadataLookup): {
  buildDefinition: (id: number) => string | null
  buildRuntimeCall: (api: string, method: string) => string | null
  buildStorage: (pallet: string, entry: string) => string | null
  buildCall: (pallet: string, name: string) => string | null
  buildEvent: (pallet: string, name: string) => string | null
  buildError: (pallet: string, name: string) => string | null
  buildConstant: (pallet: string, constantName: string) => string | null
}

Generates the checksums for the different components defined in the metadata.

buildDefinition builds the checksum of one of the types in the lookup. The rest of the methods build the checksum for each of the interfaces of the chain.