How to use the @polkadot/types/codec.createType function in @polkadot/types

To help you get started, we’ve selected a few @polkadot/types examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github polkadot-js / api / packages / api / src / checkTypes.manual.ts View on Github external
function types (): void {
  // check correct types with `createType`
  const balance = createType('Balance', 2);
  const gas = createType('Gas', 2);
  const compact = createType('Compact', 2);
  // const random = createType('RandomType', 2); // This one should deliberately show a TS error

  const gasUnsafe = createTypeUnsafe('Gas', [2]);
  const overriddenUnsafe = createTypeUnsafe<header>('Gas', [2]);

  console.log(balance, gas, compact, gasUnsafe, overriddenUnsafe);
}
</header>
github polkadot-js / api / packages / metadata / src / Decorated / storage / fromMetadata / createFunction.ts View on Github external
function extendLinkedMap (registry: Registry, { meta: { documentation, name, type } }: CreateItemFn, storageFn: StorageEntry, stringKey: string, hasher: HasherFunction): StorageEntry {
  const headHash = new U8a(registry, hasher(`head of ${stringKey}`));
  const headFn: any = (): U8a =>
    headHash;

  // metadata with a fallback value using the type of the key, the normal
  // meta fallback only applies to actual entry values, create one for head
  headFn.meta = new StorageEntryMetadata(registry, {
    name,
    modifier: createType(registry, 'StorageEntryModifierLatest', 1), // required
    type: new StorageEntryType(registry, createType(registry, 'PlainTypeLatest', type.asMap.key), 0),
    fallback: createType(registry, 'Bytes', createTypeUnsafe(registry, type.asMap.key.toString()).toHex()),
    documentation
  });

  // here we pass the section/method through as well - these are not on
  // the function itself, so specify these explicitly to the constructor
  storageFn.headKey = createType(registry, 'StorageKey', headFn, {
    method: storageFn.method,
    section: `head of ${storageFn.section}`
  });

  return storageFn;
}
github polkadot-js / api / packages / metadata / src / Metadata / v3 / toV4.ts View on Github external
function toV4StorageFunction (registry: Registry, storageFn: StorageFunctionMetadataV3): StorageFunctionMetadata {
  const { documentation, fallback, modifier, name, type } = storageFn;

  // Convert the old type to the new type: there is one new field
  // called `hasher`, which we initialize to xxHash (the default in
  // v3).
  const [newType, index] = type.isPlainType
    ? [type, 0]
    : type.isMap
      ? [createType(registry, 'MapTypeV4', {
        hasher: new StorageHasher(registry, 'Twox128'),
        key: type.asMap.key,
        value: type.asMap.value,
        linked: type.asMap.linked
      }), 1]
      : [createType(registry, 'DoubleMapTypeV4', {
        hasher: new StorageHasher(registry, 'Twox128'),
        key1: type.asDoubleMap.key1,
        key2: type.asDoubleMap.key2,
        value: type.asDoubleMap.value,
        key2Hasher: type.asDoubleMap.key2Hasher
      }), 2];

  return new StorageFunctionMetadata(registry, {
    documentation,
    fallback,
    name,
    modifier,
    type: new StorageFunctionType(registry, newType, index)
  });
}
github polkadot-js / api / packages / metadata / src / Decorated / storage / fromMetadata / createFunction.ts View on Github external
const headFn: any = (): U8a =>
    headHash;

  // metadata with a fallback value using the type of the key, the normal
  // meta fallback only applies to actual entry values, create one for head
  headFn.meta = new StorageEntryMetadata(registry, {
    name,
    modifier: createType(registry, 'StorageEntryModifierLatest', 1), // required
    type: new StorageEntryType(registry, createType(registry, 'PlainTypeLatest', type.asMap.key), 0),
    fallback: createType(registry, 'Bytes', createTypeUnsafe(registry, type.asMap.key.toString()).toHex()),
    documentation
  });

  // here we pass the section/method through as well - these are not on
  // the function itself, so specify these explicitly to the constructor
  storageFn.headKey = createType(registry, 'StorageKey', headFn, {
    method: storageFn.method,
    section: `head of ${storageFn.section}`
  });

  return storageFn;
}
github polkadot-js / api / packages / metadata / src / Decorated / storage / fromMetadata / substrate.ts View on Github external
return (registry: Registry): StorageEntry =&gt;
    createFunction(registry, {
      meta: {
        documentation: createType(registry, 'Vec
github polkadot-js / api / packages / metadata / src / Decorated / storage / fromMetadata / substrate.ts View on Github external
return (registry: Registry): StorageEntry =&gt;
    createFunction(registry, {
      meta: {
        documentation: createType(registry, 'Vec
github polkadot-js / api / packages / metadata / src / Metadata / v3 / toV4.ts View on Github external
function toV4StorageFunction (registry: Registry, storageFn: StorageFunctionMetadataV3): StorageFunctionMetadata {
  const { documentation, fallback, modifier, name, type } = storageFn;

  // Convert the old type to the new type: there is one new field
  // called `hasher`, which we initialize to xxHash (the default in
  // v3).
  const [newType, index] = type.isPlainType
    ? [type, 0]
    : type.isMap
      ? [createType(registry, 'MapTypeV4', {
        hasher: new StorageHasher(registry, 'Twox128'),
        key: type.asMap.key,
        value: type.asMap.value,
        linked: type.asMap.linked
      }), 1]
      : [createType(registry, 'DoubleMapTypeV4', {
        hasher: new StorageHasher(registry, 'Twox128'),
        key1: type.asDoubleMap.key1,
        key2: type.asDoubleMap.key2,
        value: type.asDoubleMap.value,
        key2Hasher: type.asDoubleMap.key2Hasher
      }), 2];

  return new StorageFunctionMetadata(registry, {
    documentation,
    fallback,
github polkadot-js / api / packages / metadata / src / Metadata / v4 / toV5.ts View on Github external
function toV5StorageFunction (registry: Registry, storageFn: StorageFunctionMetadataV4): StorageFunctionMetadata {
  const { documentation, fallback, modifier, name, type } = storageFn;
  const [newType, index] = type.isPlainType
    ? [type, 0]
    : type.isMap
      ? [type.asMap, 1]
      : [createType(registry, 'DoubleMapTypeV5', {
        hasher: type.asDoubleMap.hasher,
        key1: type.asDoubleMap.key1,
        key2: type.asDoubleMap.key2,
        value: type.asDoubleMap.value,
        key2Hasher: toStorageHasher(registry, type.asDoubleMap.key2Hasher)
      }), 2];

  return new StorageFunctionMetadata(registry, {
    documentation,
    fallback,
    name,
    modifier,
    type: new StorageFunctionType(registry, newType, index)
  });
}