How to use the @polkadot/types.Metadata 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 / metadata / src / Decorated / consts / fromMetadata / fromMetadata.spec.ts View on Github external
// Copyright 2017-2019 @polkadot/metadata authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

import { ClassOf, Metadata, TypeRegistry } from '@polkadot/types';

import rpcMetadata from '../../../Metadata/static';
import fromMetadata from '../fromMetadata';

// Use the pre-generated metadata
const registry = new TypeRegistry();
const metadata = new Metadata(registry, rpcMetadata);
const consts = fromMetadata(registry, metadata);

describe('fromMetadata', (): void => {
  it('should return constants with the correct type and value', (): void => {
    expect(consts.democracy.cooloffPeriod).toBeInstanceOf(ClassOf(registry, 'BlockNumber'));
    // 3 second blocks, 28 days
    expect(consts.democracy.cooloffPeriod.toNumber()).toEqual(28 * 24 * 60 * (60 / 3));
  });

  it('correctly handles bytes', (): void => {
    // 0x34 removes as the length prefix removed
    expect(consts.session.dedupKeyPrefix.toHex()).toEqual('0x3a73657373696f6e3a6b657973');
  });
});
github polkadot-js / api / packages / type-storage / src / fromMetadata / v0 / v0.spec.ts View on Github external
// Copyright 2017-2019 @polkadot/storage authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

import { u8aToHex } from '@polkadot/util';
import { Metadata } from '@polkadot/types';
import json from '@polkadot/types/Metadata/v0/static';

import fromv0 from '.';

// Use the pre-generated metadata
const metadata = new Metadata(json).asV0;
const newStorage = fromv0(metadata);

describe('fromv0', () => {
  it('should throw if the storage function expects an argument', () => {
    expect(() => newStorage.balances.freeBalance()).toThrowError(/expects one argument/);
  });

  it('should return a value if the storage function does not expect an argument', () => {
    expect(() => newStorage.timestamp.now()).not.toThrow();
  });

  it('should return the correct length-prefixed storage key', () => {
    expect(
      u8aToHex(
        newStorage.balances.freeBalance('5DkQbYAExs3M2sZgT1Ec3mKfZnAQCL4Dt9beTCknkCUn5jzo')
      )
github polkadot-js / api / packages / metadata / src / Decorated / extrinsics / fromMetadata / fromMetadata.spec.ts View on Github external
// Copyright 2017-2019 @polkadot/metadata authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

import { createType, Metadata, TypeRegistry } from '@polkadot/types';

import json from '../../../Metadata/static';
import fromMetadata from '.';

// Use the pre-generated metadata
const registry = new TypeRegistry();
const metadata = new Metadata(registry, json);
const newExtrinsics = fromMetadata(registry, metadata);

describe('fromMetadata', (): void => {
  it('should throw if an incorrect number of args is supplied', (): void => {
    expect((): any => newExtrinsics.balances.setBalance()).toThrowError(/expects 3 arguments/);
  });

  it('should return a value if the storage function does not expect an argument', (): void => {
    expect((): any => newExtrinsics.balances.setBalance('5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF', 2, 3)).not.toThrow();
  });

  it('should return properly-encoded transactions', (): void => {
    expect(
      createType(registry, 'Extrinsic', newExtrinsics.timestamp.set([10101])).toU8a()
    ).toEqual(
      new Uint8Array([
github polkadot-js / api / packages / type-extrinsics / src / fromMetadata / v0 / fromMetadata.spec.ts View on Github external
// Copyright 2017-2019 @polkadot/extrinsics authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

import { Extrinsic, Metadata } from '@polkadot/types';
import json from '@polkadot/types/Metadata/v0/static';

import fromV0 from '.';

// Use the pre-generated metadata
const metadata = new Metadata(json).asV0;
const newExtrinsics = fromV0(metadata);

describe('fromMetadata', () => {
  it('should throw if an incorrect number of args is supplied', () => {
    expect(() => newExtrinsics.balances.setBalance()).toThrowError(/expects 3 arguments/);
  });

  it('should return a value if the storage function does not expect an argument', () => {
    expect(() => newExtrinsics.balances.setBalance('5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF', 2, 3)).not.toThrow();
  });

  it('should return properly-encoded transactions', () => {
    expect(
      new Extrinsic(newExtrinsics.timestamp.set([10101])).toU8a()
    ).toEqual(
      new Uint8Array([
github polkadot-js / api / packages / metadata / src / Decorated / extrinsics / index.spec.ts View on Github external
// Copyright 2017-2019 @polkadot/metadata authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

import BN from 'bn.js';
import testingPairs from '@polkadot/keyring/testingPairs';
import { createType, Metadata, TypeRegistry } from '@polkadot/types';

import metadataStatic from '../../Metadata/static';
import fromMetadata from './fromMetadata';

const keyring = testingPairs({ type: 'ed25519' }, false);
const registry = new TypeRegistry();
const metadata = new Metadata(registry, metadataStatic);
const extrinsics = fromMetadata(registry, metadata);

describe('extrinsics', (): void => {
  it('encodes an actual transfer (actual data)', (): void => {
    expect(
      createType(registry, 'Extrinsic',
        extrinsics.balances.transfer(keyring.bob.publicKey, 6969)
      ).sign(keyring.alice, {
        blockHash: '0xec7afaf1cca720ce88c1d1b689d81f0583cc15a97d621cf046dd9abf605ef22f',
        genesisHash: '0xdcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b',
        nonce: 0,
        runtimeVersion: {
          apis: [],
          authoringVersion: new BN(123),
          implName: 'test',
          implVersion: new BN(123),
github polkadot-js / api / packages / type-extrinsics / src / static.ts View on Github external
// Copyright 2017-2019 @polkadot/extrinsics authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

import { Metadata } from '@polkadot/types';
import metadataRpc from '@polkadot/types/Metadata/v0/static';
import { ModulesWithMethods } from '@polkadot/types/primitive/Method';

import fromMetadata from './fromMetadata';

const staticMetadata: ModulesWithMethods = fromMetadata(
  new Metadata(metadataRpc)
);
export default staticMetadata;
github polkadot-js / api / packages / api-metadata / src / consts / static.ts View on Github external
// Copyright 2017-2019 @polkadot/api-metadata authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

import { Metadata } from '@polkadot/types';
import metadataRpc from '@polkadot/types/Metadata/static';

import fromMetadata from './fromMetadata';

export default fromMetadata(
  new Metadata(metadataRpc)
);
github polkadot-js / api / packages / api-metadata / src / storage / static.ts View on Github external
// Copyright 2017-2019 @polkadot/api-metadata authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

import { Metadata } from '@polkadot/types';
import metadataRpc from '@polkadot/types/Metadata/static';

import fromMetadata from './fromMetadata';

export default fromMetadata(
  new Metadata(metadataRpc)
);
github polkadot-js / extension / packages / extension-chains / src / index.ts View on Github external
[alexander, edgeware, kusamaCC3].map(({ chain, genesisHash, icon, metaCalls, specVersion, ss58Format, tokenDecimals, tokenSymbol, types }): [string, Chain] => {
    let metadata: Metadata | undefined;
    const registry = new TypeRegistry();

    registry.register(types || {});

    if (metaCalls) {
      metadata = new Metadata(registry, Buffer.from(metaCalls, 'base64'));
    }

    return [genesisHash, {
      genesisHash,
      hasMetadata: !!metadata,
      icon,
      name: chain,
      registry,
      specVersion,
      ss58Format,
      tokenDecimals,
      tokenSymbol
    }];
  })
);