Remove injectDefinitions import, prefer injector (#1195)

* Convert RPC classes to primitive classes or /srml types

* Lint passes... lokking at tests next.

* Tests pass.

* Types with basic docs (lots of work here)

* Update StorageKey.spec.ts

* Add injector to metadata doc generation

* Fix import

* Remove injectDefinitions import, prefer injector
This commit is contained in:
Jaco Greeff
2019-07-26 11:12:17 +02:00
committed by GitHub
parent 478f54c703
commit 700eb7e1f4
34 changed files with 80 additions and 169 deletions
+2 -7
View File
@@ -2,8 +2,9 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../../injector';
import extrinsicsFromMeta from '@polkadot/api-metadata/extrinsics/fromMetadata';
import { injectDefinitions } from '@polkadot/types/srml';
import createType from '../../codec/createType';
import Metadata from '../Metadata';
@@ -21,8 +22,6 @@ export function decodeLatestSubstrate<Modules extends Codec> (
latestSubstrate: object
): void {
it('decodes latest substrate properly', (): void => {
injectDefinitions();
const metadata = new Metadata(rpcData);
console.error(JSON.stringify(metadata.toJSON()));
@@ -39,8 +38,6 @@ export function decodeLatestSubstrate<Modules extends Codec> (
*/
export function toV7<Modules extends Codec> (version: number, rpcData: string): void {
it('converts to V7', (): void => {
injectDefinitions();
const metadata = new Metadata(rpcData)[`asV${version}` as keyof Metadata];
const metadataV7 = new Metadata(rpcData).asV7;
@@ -55,8 +52,6 @@ export function toV7<Modules extends Codec> (version: number, rpcData: string):
*/
export function defaultValues (rpcData: string): void {
describe('storage with default values', (): void => {
injectDefinitions();
const metadata = new Metadata(rpcData);
Method.injectMethods(extrinsicsFromMeta(metadata));
+2 -5
View File
@@ -2,6 +2,8 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../injector';
import { Balance, BlockNumber } from '../srml/runtime/types';
import BN from 'bn.js';
@@ -9,15 +11,10 @@ import BN from 'bn.js';
import createType, { ClassOf } from '../codec/createType';
import Moment from '../primitive/Moment';
import U32 from '../primitive/U32';
import { injectDefinitions } from '../srml';
import Compact from './Compact';
import UInt from './UInt';
describe('Compact', (): void => {
beforeEach((): void => {
injectDefinitions();
});
describe('encodeU8a', (): void => {
it('encodes short u8', (): void => {
expect(
+2 -5
View File
@@ -2,17 +2,14 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../injector';
import Enum from './Enum';
import Null from '../primitive/Null';
import Text from '../primitive/Text';
import U32 from '../primitive/U32';
import { injectDefinitions } from '../srml';
describe('Enum', (): void => {
beforeEach((): void => {
injectDefinitions();
});
describe('typed enum (previously EnumType)', (): void => {
it('provides a clean toString() (value)', (): void => {
expect(
+2 -5
View File
@@ -2,7 +2,8 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { injectDefinitions } from '../srml';
import '../injector';
import Option from './Option';
import Bytes from '../primitive/Bytes';
import U32 from '../primitive/U32';
@@ -24,10 +25,6 @@ const testEncode = (to: string, expected: any): void =>
});
describe('Option', (): void => {
beforeEach((): void => {
injectDefinitions();
});
it('converts undefined/null to empty', (): void => {
expect(new Option(Text, undefined).isNone).toBe(true);
expect(new Option(Text, null).isNone).toBe(true);
+2 -5
View File
@@ -2,20 +2,17 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../injector';
import { ClassOf } from '../codec/createType';
import AccountId from '../primitive/AccountId';
import Text from '../primitive/Text';
import U32 from '../primitive/U32';
import { injectDefinitions } from '../srml';
import { CodecTo } from '../types';
import Struct from './Struct';
import Vec from './Vec';
describe('Struct', (): void => {
beforeEach((): void => {
injectDefinitions();
});
describe('decoding', (): void => {
const testDecode = (type: string, input: any): void =>
it(`can decode from ${type}`, (): void => {
+2 -2
View File
@@ -2,6 +2,8 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../injector';
import { VoteThreshold } from '../srml/elections/types';
import { BlockNumber } from '../srml/runtime/types';
import { CodecTo } from '../types';
@@ -12,14 +14,12 @@ import { ClassOf } from '../codec/createType';
import Method from '../primitive/Method';
import Text from '../primitive/Text';
import U32 from '../primitive/U32';
import { injectDefinitions } from '../srml';
import Tuple from './Tuple';
describe('Tuple', (): void => {
let tuple: Tuple;
beforeEach((): void => {
injectDefinitions();
tuple = new Tuple(
[Text, U32],
['bazzing', 69]
+2 -9
View File
@@ -2,11 +2,12 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../injector';
import { Codec, Constructor } from '../types';
import createType, { TypeDef, TypeDefInfo, createClass, getTypeClass, getTypeDef, typeSplit, ClassOf } from './createType';
import Text from '../primitive/Text';
import { injectDefinitions } from '../srml';
import CodecSet from './Set';
describe('typeSplit', (): void => {
@@ -214,10 +215,6 @@ describe('getTypeClass', (): void => {
});
describe('createClass', (): void => {
beforeAll((): void => {
injectDefinitions();
});
it('should memoize from strings', (): void => {
const a = createClass('BabeWeight');
const b = createClass('BabeWeight');
@@ -300,10 +297,6 @@ describe('createType', (): void => {
});
describe('instanceof', (): void => {
beforeAll((): void => {
injectDefinitions();
});
it('instanceof should work (primitive type)', (): void => {
const value = createType('Balance', 1234);
+2 -5
View File
@@ -2,20 +2,17 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import './injector';
import extrinsics from '@polkadot/api-metadata/extrinsics/static';
import { Codec, Constructor } from './types';
import * as Classes from './index.types';
import { injectDefinitions } from './srml';
const Types = Classes as Record<string, Constructor>;
const UNCONSTRUCTABLE = ['origin', 'usize', 'vote'];
describe('types', (): void => {
beforeEach((): void => {
injectDefinitions();
});
describe('default creation', (): void => {
Object.keys(Types).forEach((name): void => {
it(`creates an empty ${name}`, (): void => {
+17 -1
View File
@@ -2,6 +2,22 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { injectDefinitions } from './srml';
import { ClassOf } from './codec/createType';
import getTypeRegistry from './codec/typeRegistry';
import * as srmlTypes from './srml/definitions';
/**
* @description A utility method that injects all the srml definitions into the type registry
*/
export function injectDefinitions (): void {
const registry = getTypeRegistry();
Object.values(srmlTypes).forEach(({ types }): void =>
registry.register(types)
);
// Here we setup the fallbacks
ClassOf('EventRecord').Fallback = ClassOf('EventRecord0to76');
}
injectDefinitions();
@@ -2,20 +2,17 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../injector';
import { setAddressPrefix } from '@polkadot/util-crypto';
import createType from '../codec/createType';
import U8a from '../codec/U8a';
import Vec from '../codec/Vec';
import jsonVec from '../json/AccountIdVec.001.json';
import { injectDefinitions } from '../srml';
import AccountId from './AccountId';
describe('AccountId', (): void => {
beforeEach((): void => {
injectDefinitions();
});
describe('defaults', (): void => {
const id = new AccountId();
+2 -2
View File
@@ -2,16 +2,16 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../injector';
import extrinsics from '@polkadot/api-metadata/extrinsics/static';
import block00300 from '../json/SignedBlock.003.00.json';
import { injectDefinitions } from '../srml';
import Method from './Method';
import Block from './Block';
describe('Block', (): void => {
beforeEach((): void => {
injectDefinitions();
Method.injectMethods(extrinsics);
});
+2 -5
View File
@@ -2,15 +2,12 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../injector';
import createType from '../codec/createType';
import { injectDefinitions } from '../srml';
import Bytes from './Bytes';
describe('Bytes', (): void => {
beforeEach((): void => {
injectDefinitions();
});
const CODE = new Uint8Array([0x14, 0x3a, 0x63, 0x6f, 0x64, 0x65]);
describe('construction', (): void => {
+2 -5
View File
@@ -2,17 +2,14 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { injectDefinitions } from '../srml';
import '../injector';
import json3 from '../json/Header.003.json';
import block1 from '../json/SignedBlock.003.00.json';
import ConsensusEngineId, { CID_AURA, CID_BABE, CID_GRPA } from './ConsensusEngineId';
import Digest from './Digest';
describe('Digest', (): void => {
beforeEach((): void => {
injectDefinitions();
});
it('decodes logs with consensus', (): void => {
const digest = new Digest(json3.result.digest);
@@ -2,14 +2,11 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { injectDefinitions } from '../../srml';
import '../../injector';
import ExtrinsicEra from './ExtrinsicEra';
describe('ExtrinsicEra', (): void => {
beforeEach((): void => {
injectDefinitions();
});
it('decodes an Extrinsic Era with immortal', (): void => {
const extrinsicEra = new ExtrinsicEra(new Uint8Array([0]));
@@ -2,14 +2,11 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { injectDefinitions } from '../../../srml';
import '../../../injector';
import ExtrinsicExtra from './ExtrinsicExtra';
describe('ExtrinsicExtraV2', (): void => {
beforeEach((): void => {
injectDefinitions();
});
it('encodes to a sane Uint8Array (default construction)', (): void => {
expect(
new ExtrinsicExtra().toU8a()
@@ -2,14 +2,11 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { injectDefinitions } from '../../../srml';
import '../../../injector';
import ExtrinsicSignature from './ExtrinsicSignature';
describe('ExtrinsicSignatureV2', (): void => {
beforeEach((): void => {
injectDefinitions();
});
it('encodes to a sane Uint8Array', (): void => {
const u8a = new Uint8Array([
// signer as an AccountIndex
+2 -5
View File
@@ -2,9 +2,10 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../injector';
import BN from 'bn.js';
import { injectDefinitions } from '../srml';
import json1 from '../json/Header.001.json';
import json2 from '../json/Header.002.json';
import json3 from '../json/Header.003.json';
@@ -13,10 +14,6 @@ import block00301 from '../json/SignedBlock.003.01.json';
import Header from './Header';
describe('Header', (): void => {
beforeEach((): void => {
injectDefinitions();
});
it('decodes an actual JSON response', (): void => {
const header = new Header(json1.result);
+2 -5
View File
@@ -2,14 +2,11 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { injectDefinitions } from '../srml';
import '../injector';
import Vote from './Vote';
describe('Vote', (): void => {
beforeEach((): void => {
injectDefinitions();
});
it('constructs via boolean true', (): void => {
expect(new Vote(true).toU8a()).toEqual(new Uint8Array([128]));
expect(new Vote(true).isAye).toBe(true);
-21
View File
@@ -1,21 +0,0 @@
// 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 { ClassOf } from '../codec/createType';
import getTypeRegistry from '../codec/typeRegistry';
import * as srmlTypes from './definitions';
/**
* @description A utility method that injects all the srml definitions into the type registry
*/
export function injectDefinitions (): void {
const registry = getTypeRegistry();
Object.values(srmlTypes).forEach(({ types }): void =>
registry.register(types)
);
// Here we setup the fallbacks
ClassOf('EventRecord').Fallback = ClassOf('EventRecord0to76');
}
@@ -2,14 +2,11 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../../injector';
import createType from '../../codec/createType';
import { injectDefinitions } from '..';
describe('ChainProperties', (): void => {
beforeEach((): void => {
injectDefinitions();
});
it('decodes from a null value', (): void => {
expect(
[...createType('ChainProperties', null).entries()]
@@ -2,18 +2,17 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../../injector';
import { ExtrinsicStatus } from './types';
import createType from '../../codec/createType';
import rpc from '../../json/ExtrinsicStatus.001.json';
import { injectDefinitions } from '..';
describe('ExtrinsicStatus', (): void => {
let status: ExtrinsicStatus;
beforeEach((): void => {
injectDefinitions();
status = createType('ExtrinsicStatus', rpc.params.result);
});
@@ -2,14 +2,11 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../../injector';
import createType from '../../codec/createType';
import { injectDefinitions } from '..';
describe('KeyValueOption', (): void => {
beforeEach((): void => {
injectDefinitions();
});
it('exposes the properties for key/value', (): void => {
const [key, value] = createType('KeyValueOption', [
'0x11223344'
@@ -2,17 +2,17 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../../injector';
import { RuntimeVersion } from './types';
import createType from '../../codec/createType';
import rpc from '../../json/RuntimeVersion.002.json';
import { injectDefinitions } from '..';
describe('RuntimeVersion', (): void => {
let version: RuntimeVersion;
beforeEach((): void => {
injectDefinitions();
version = createType('RuntimeVersion', rpc.result);
});
@@ -2,6 +2,8 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../../injector';
import extrinsics from '@polkadot/api-metadata/extrinsics/static';
import createType from '../../codec/createType';
@@ -9,11 +11,9 @@ import Method from '../../primitive/Method';
import events from '../../json/SignedBlock.002.json';
import immortalTxs from '../../json/SignedBlock.004.immortal.json';
import mortalTxs from '../../json/SignedBlock.004.mortal.json';
import { injectDefinitions } from '..';
describe('SignedBlock', (): void => {
beforeEach((): void => {
injectDefinitions();
Method.injectMethods(extrinsics);
});
@@ -2,16 +2,13 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../../injector';
import { createType } from '@polkadot/types';
import json from '../../json/StorageChangeSet.001.json';
import { injectDefinitions } from '../';
describe('StorageChangeSet', (): void => {
beforeEach((): void => {
injectDefinitions();
});
describe('construction', (): void => {
const set = createType('StorageChangeSet', {
block: '0x1234',
@@ -2,16 +2,13 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../../injector';
import { KeyValue } from './types';
import createType from '../../codec/createType';
import { injectDefinitions } from '..';
describe('KeyValue', (): void => {
beforeEach((): void => {
injectDefinitions();
});
it('decodes KeyValue from u8a', (): void => {
expect(
createType('KeyValue', Uint8Array.from([
@@ -2,23 +2,20 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../../injector';
import { EventRecord } from './types';
import createType from '../../codec/createType';
import Vec from '../../codec/Vec';
import json1 from '../../json/EventRecord.001.json';
import json3 from '../../json/EventRecord.003.json';
import { injectDefinitions } from '..';
import Event from '../../primitive/Event';
import Metadata from '../../Metadata';
import metadataV0 from '../../Metadata/v0/static';
import metadata from '../../Metadata/static';
import Event from '../../primitive/Event';
describe('EventRecord', (): void => {
beforeEach((): void => {
injectDefinitions();
});
describe('EventRecord_0_76', (): void => {
beforeEach((): void => {
Event.injectMetadata(
@@ -36,8 +33,6 @@ describe('EventRecord', (): void => {
describe('EventRecord (current)', (): void => {
beforeEach((): void => {
injectDefinitions();
Event.injectMetadata(
new Metadata(metadata)
);