How to use the @polkadot/metadata/Metadata function in @polkadot/metadata

To help you get started, we’ve selected a few @polkadot/metadata 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 / types / src / codec / Tuple.spec.ts View on Github external
it('creates properly via actual hex string', (): void => {
    // eslint-disable-next-line no-new
    new Metadata(registry, rpcMetadata);

    const test = new (Tuple.with([
      ClassOf(registry, 'BlockNumber'), ClassOf(registry, 'VoteThreshold')
    ]
    ))(registry, '0x6219000001');

    expect((test[0] as BlockNumber).toNumber()).toEqual(6498);
    expect((test[1] as VoteThreshold).toNumber()).toEqual(1);
  });
github polkadot-js / api / packages / types / src / primitive / Extrinsic / Extrinsic.spec.ts View on Github external
// Copyright 2017-2019 @polkadot/types 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/metadata/Metadata';
import rpcMetadata from '@polkadot/metadata/Metadata/static';
import { hexToU8a } from '@polkadot/util';

import { TypeRegistry } from '../../codec';
import Extrinsic from './Extrinsic';

const registry = new TypeRegistry();

// eslint-disable-next-line no-new
new Metadata(registry, rpcMetadata);

describe('Extrinsic', (): void => {
  describe('V1', (): void => {
    it('decodes an actual transaction (length prefix)', (): void => {
      const extrinsic = new Extrinsic(
        registry,
        '0x' +
        '2502' +
        '81' +
        'ff' +
        'bfc823aa75c30058eeec21abe2c2d6b7247418a4af89d67a2084c2ac864da080' +
        'c0aa4df3b4926c3cd78bbdced31d8bdccb8604b779b71b90e58b2848df4a9ad6' +
        'b0aa1aae6be7a05c9413a172b0325e4d214e5ff2b25098028b30f1a50be9c90e' +
        '0c' + // nonce
        '00' + // era
        '0600' + // balances.transfer
github polkadot-js / api / packages / types / src / primitive / Extrinsic / SignerPayload.spec.ts View on Github external
// Copyright 2017-2019 @polkadot/api 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 } from '@polkadot/types';

import Metadata from '@polkadot/metadata/Metadata';
import rpcMetadata from '@polkadot/metadata/Metadata/static';

import { TypeRegistry } from '../../codec';
import SignerPayload from './SignerPayload';

const registry = new TypeRegistry();

// eslint-disable-next-line no-new
new Metadata(registry, rpcMetadata);

describe('SignerPayload', (): void => {
  const TEST = {
    address: '5DTestUPts3kjeXSTMyerHihn1uwMfLj8vU8sqF7qYrFabHE',
    blockHash: '0xde8f69eeb5e065e18c6950ff708d7e551f68dc9bf59a07c52367c0280f805ec7',
    blockNumber: '0x00231d30',
    era: '0x0703',
    genesisHash: '0xdcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b',
    method: '0x0600ffd7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9e56c',
    nonce: '0x00001234',
    specVersion: '0x00000000',
    tip: '0x00000000000000000000000000005678',
    version: 3
  };

  it('creates a valid JSON output', (): void => {
github polkadot-js / api / packages / types / src / index.spec.ts View on Github external
// NOTE This is not a shortcut to implementing types incorrectly. This is here
// specifically for the types that _should_ throw in the constrtuctor, i.e
// `usize` is not allowed (runtime incompat) and `origin` is not passed through
// to any calls. All other types _must_ pass and allow for empty defaults
const UNCONSTRUCTABLE = [
  'ExtrinsicPayloadUnknown', 'GenericExtrinsicPayloadUnknown',
  'ExtrinsicUnknown', 'GenericExtrinsicUnknown',
  'GenericOrigin', 'Origin',
  'usize'
].map((v): string => v.toLowerCase());

const registry = new TypeRegistry();

// eslint-disable-next-line no-new
new Metadata(registry, metadataStatic);

function testTypes (type: string, typeNames: string[]): void {
  describe(type, (): void => {
    describe(`${type}:: default creation`, (): void => {
      typeNames.forEach((name): void => {
        it(`creates an empty ${name}`, (): void => {
          const constructFn = (): Codec =>
            createTypeUnsafe(registry, name);

          if (UNCONSTRUCTABLE.includes(name.toLowerCase())) {
            expect(constructFn).toThrow();
          } else {
            expect(constructFn).not.toThrow();
          }
        });
      });
github polkadot-js / api / packages / types / src / primitive / Extrinsic / v3 / Extrinsic.spec.ts View on Github external
import BN from 'bn.js';
import Decorated from '@polkadot/metadata/Decorated';
import Metadata from '@polkadot/metadata/Metadata';
import rpcMetadata from '@polkadot/metadata/Metadata/static';
import testingPairs from '@polkadot/keyring/testingPairs';

import { TypeRegistry } from '../../../codec';
import Extrinsic from './Extrinsic';

const registry = new TypeRegistry();
const decorated = new Decorated(registry, rpcMetadata);
const keyring = testingPairs({ type: 'ed25519' }, false);

// eslint-disable-next-line no-new
new Metadata(registry, rpcMetadata);

describe('ExtrinsicV3', (): void => {
  it('constructs a sane Uint8Array (default)', (): void => {
    expect(
      new Extrinsic(registry).toU8a()
    ).toEqual(new Uint8Array([0, 0]));
  });

  it('creates a unsigned extrinsic', (): void => {
    expect(
      new Extrinsic(
        registry,
        decorated.tx.balances.transfer(keyring.bob.publicKey, 6969)
      ).toHex()
    ).toEqual(
      '0x' +
github polkadot-js / api / packages / types / src / primitive / Extrinsic / v2 / Extrinsic.spec.ts View on Github external
import BN from 'bn.js';
import Decorated from '@polkadot/metadata/Decorated';
import Metadata from '@polkadot/metadata/Metadata';
import rpcMetadata from '@polkadot/metadata/Metadata/static';
import testingPairs from '@polkadot/keyring/testingPairs';

import { TypeRegistry } from '../../../codec';
import Extrinsic from './Extrinsic';

const registry = new TypeRegistry();
const decorated = new Decorated(registry, rpcMetadata);
const keyring = testingPairs({ type: 'ed25519' }, false);

// eslint-disable-next-line no-new
new Metadata(registry, rpcMetadata);

describe('ExtrinsicV2', (): void => {
  it('constructs a sane Uint8Array (default)', (): void => {
    expect(
      new Extrinsic(registry).toU8a()
    ).toEqual(new Uint8Array([0, 0]));
  });

  it('creates a unsigned extrinsic', (): void => {
    expect(
      new Extrinsic(
        registry,
        decorated.tx.balances.transfer(keyring.bob.publicKey, 6969)
      ).toHex()
    ).toEqual(
      '0x' +
github polkadot-js / api / packages / types / src / interfaces / runtime / SignedBlock.spec.ts View on Github external
// 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/metadata/Metadata';
import metadataStatic from '@polkadot/metadata/Metadata/static';

import { createType, TypeRegistry } from '../../codec/create';
import events from '../../json/SignedBlock.002.json';
import immortalTxs from '../../json/SignedBlock.004.immortal.json';
import mortalTxs from '../../json/SignedBlock.004.mortal.json';
import knownMehods from '../../json/SignedBlock.005.json';

const registry = new TypeRegistry();

// eslint-disable-next-line no-new
new Metadata(registry, metadataStatic);

describe('SignedBlock', (): void => {
  it('decodes a full block', (): void => {
    const s = createType(registry, 'SignedBlock', events.result);

    expect(
      s.block.header.stateRoot.toString()
    ).toEqual('0x24bab7fe0fee187a57bd0029ba151e81a0bae0fea96ad9f85c579c1ba9d88948');
    expect(s.block.header.digest.logs.length).toBe(1);
    expect(s.block.header.digest.logs[0].type).toBe('AuthoritiesChange');
    expect(s.block.header.digest.logs[0].value.toJSON()).toEqual([
      '5EyrU8k6worwXWfW2LajRvS7XF6eta286tMpHeHUwMPpzUwT',
      '5Dpq7rU6KAZVdRtcegvpCoeJtZzFPzieRkRz4xajRAiMRxCd',
      '5CCtQPAYtuDXt6H4WMrn5nFNk4gemkVRD3k1r1mA7YiZLPP5',
      '5F2ABwFm3fUhWMZT7Zeu7UjCr1dndjJD7qBeHChJhiJC99UH',
      '5CA1n5DSpE46Q9Y7NaqD39T9X7S2dRtscsPvrv2dWuYhFg5U',
github polkadot-js / api / packages / types / src / codec / Vec.spec.ts View on Github external
import { PropIndex } from '../interfaces/democracy';
import { Codec, CodecTo } from '../types';

import Metadata from '@polkadot/metadata/Metadata';
import rpcMetadata from '@polkadot/metadata/Metadata/static';

import AccountId from '../primitive/Generic/AccountId';
import Text from '../primitive/Text';
import { createTypeUnsafe, TypeRegistry } from './create';
import Vec from './Vec';
import Tuple from './Tuple';

const registry = new TypeRegistry();

// eslint-disable-next-line no-new
new Metadata(registry, rpcMetadata);

describe('Vec', (): void => {
  let vector: Vec;

  beforeEach((): void => {
    vector = new Vec(registry, Text, ['1', '23', '345', '4567', new Text(registry, '56789')]);
  });

  it('wraps a sequence of values', (): void => {
    expect(vector.length).toEqual(5); // eslint-disable-line
  });

  it('has a sane representation for toString', (): void => {
    expect(vector.toString()).toEqual('[1, 23, 345, 4567, 56789]');
  });
github polkadot-js / api / packages / types / src / interfaces / system / EventRecord.spec.ts View on Github external
beforeEach((): void => {
      // eslint-disable-next-line no-new
      new Metadata(registry, rpcMetadata);
    });
github polkadot-js / api / packages / types / src / interfaces / system / EventRecord.spec.ts View on Github external
beforeEach((): void => {
      // eslint-disable-next-line no-new
      new Metadata(registry, rpcMetadataV0);
    });

@polkadot/metadata

Helpers to extract information from runtime metadata

Apache-2.0
Latest version published 3 years ago

Package Health Score

70 / 100
Full package analysis

Similar packages