How to use the @polkadot/types.TypeRegistry 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 / 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(
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 / api-contract / src / MetaRegistry.spec.ts View on Github external
describe('MetaRegistry', (): void => {
  const registry = new TypeRegistry();

  describe('construction', (): void => {
    it('initializes from a contract ABI (ERC20)', (): void => {
      compare(new MetaRegistry(registry, erc20Abi), erc20Cmp);
    });

    it('initializes from a contract ABI (SharedVec)', (): void => {
      compare(new MetaRegistry(registry, sharedVecAbi), sharedVecCmp);
    });

    it('initializes from a contract ABI (Other, test001)', (): void => {
      compare(new MetaRegistry(registry, test001Abi), test001Cmp);
    });
  });
});
github polkadot-js / api / packages / api-derive / src / util / approvalFlagToBools.spec.ts View on Github external
describe('approvalFlagsToBools', (): void => {
  const registry = new TypeRegistry();

  it('translates and empty array to empty', (): void => {
    expect(
      approvalFlagsToBools([] as ApprovalFlag[])
    ).toEqual([]);
  });

  it('translates a single input', (): void => {
    expect(
      approvalFlagsToBools([
        createType(registry, 'ApprovalFlag', 0b1010)
      ])
    ).toEqual([false, true, false, true]);
  });

  it('translates multiple inputs', (): void => {
github polkadot-js / api / packages / metadata / src / Metadata / v6 / Metadata.spec.ts View on Github external
describe('MetadataV6 (substrate)', (): void => {
  const registry = new TypeRegistry();

  decodeLatestSubstrate(registry, 6, substrateData, substrateJson);

  toLatest(registry, 6, substrateData);

  defaultValues(registry, substrateData);
});
github polkadot-js / api / packages / metadata / src / Decorated / storage / fromMetadata / getHasher.spec.ts View on Github external
describe('getHasher', (): void => {
  const registry = new TypeRegistry();

  describe('Twox64Concat', (): void => {
    it('matches the foo test from Rust', (): void => {
      const hasher = getHasher(new StorageHasher(registry, 'Twox64Concat'));
      const hash = hasher('foo');
      const xxhash = xxhashAsU8a('foo', 128);

      expect([
        hash.subarray(0, 8),
        hash.subarray(8)
      ]).toEqual([
        xxhash.subarray(0, 8),
        stringToU8a('foo')
      ]);
    });
  });
github polkadot-js / api / packages / metadata / src / Metadata / v4 / Metadata.spec.ts View on Github external
describe('MetadataV4', (): void => {
  const registry = new TypeRegistry();

  decodeLatestSubstrate(registry, 4, rpcData, staticSubstrate);

  toLatest(registry, 4, rpcData);

  defaultValues(registry, rpcData);
});
github polkadot-js / api / packages / rpc-core / src / cached.spec.ts View on Github external
describe('Cached Observables', (): void => {
  const registry = new TypeRegistry();
  let rpc: Rpc;
  const keyring = testingPairs();

  beforeEach((): void => {
    rpc = new Rpc(registry, new MockProvider(registry));
  });

  it('creates a single observable for subscriptions (multiple calls)', (): void => {
    const observable1 = rpc.state.subscribeStorage([123]);
    const observable2 = rpc.state.subscribeStorage([123]);

    expect(observable2).toBe(observable1);
  });

  it('creates a single observable for subscriptions (multiple calls, no arguments)', (): void => {
    const observable1 = rpc.chain.subscribeNewHeads();
github polkadot-js / api / packages / metadata / src / Metadata / v7 / Metadata.spec.ts View on Github external
describe('MetadataV7 (polkadot)', (): void => {
  const registry = new TypeRegistry();

  decodeLatestSubstrate(registry, 7, polkadotData, polkadotJson);

  toLatest(registry, 7, polkadotData);

  defaultValues(registry, polkadotData);
});
github polkadot-js / api / packages / api-derive / src / index.spec.ts View on Github external
describe('derive', (): void => {
  const registry = new TypeRegistry();

  describe('builtin', (): void => {
    const api = new ApiRx({ provider: new MockProvider(registry), registry });

    beforeAll((done): void => {
      api.isReady.subscribe((): void => done());
    });

    testFunction(api)('accounts', 'idAndIndex', []);
    testFunction(api)('accounts', 'idToIndex', []);
    testFunction(api)('accounts', 'indexes', []);
    testFunction(api)('accounts', 'indexToId', []);

    testFunction(api)('balances', 'all', []);
    testFunction(api)('balances', 'fees', []);
    testFunction(api)('balances', 'votingBalance', []);