Call.injectMetadata (align with Events) (#1462)

* Call.injectMetadata (align with Events)

* Fix interface builds

* Fix md builds

* Fixup tests with injection
This commit is contained in:
Jaco Greeff
2019-10-10 11:05:01 +02:00
committed by GitHub
parent 2870513453
commit b8d4bc5c6a
27 changed files with 125 additions and 92 deletions
@@ -234,24 +234,13 @@ export default class MetadataVersioned extends Struct {
}
/**
* @description
* @description Returns the wrapped metadata as a limited calls-only (latest) version
*/
public get magicNumber (): MagicNumber {
return this.get('magicNumber') as MagicNumber;
}
/**
* @description the metadata wrapped
*/
private get metadata (): MetadataEnum {
return this.get('metadata') as MetadataEnum;
}
/**
* @description the metadata version this structure represents
*/
public get version (): number {
return this.metadata.index;
public get asCallsOnly (): MetadataVersioned {
return new MetadataVersioned({
magicNumber: this.magicNumber,
metadata: new MetadataEnum(toCallsOnly(this.asLatest), this.version)
});
}
/**
@@ -319,16 +308,6 @@ export default class MetadataVersioned extends Struct {
return this.getVersion(8, v7ToV8);
}
/**
* @description Returns the wrapped metadata as a limited calls-only (latest) version
*/
public get asCallsOnly (): MetadataVersioned {
return new MetadataVersioned({
magicNumber: this.magicNumber,
metadata: new MetadataEnum(toCallsOnly(this.asLatest), this.version)
});
}
/**
* @description Returns the wrapped values as a latest version object
*/
@@ -336,6 +315,27 @@ export default class MetadataVersioned extends Struct {
return this.asV8;
}
/**
* @description
*/
public get magicNumber (): MagicNumber {
return this.get('magicNumber') as MagicNumber;
}
/**
* @description the metadata wrapped
*/
private get metadata (): MetadataEnum {
return this.get('metadata') as MetadataEnum;
}
/**
* @description the metadata version this structure represents
*/
public get version (): number {
return this.metadata.index;
}
public getUniqTypes (throwError: boolean): string[] {
return getUniqTypes(this.asLatest, throwError);
}
+1 -3
View File
@@ -4,8 +4,6 @@
import '../../injector';
import extrinsicsFromMeta from '@polkadot/api-metadata/extrinsics/fromMetadata';
import { createTypeUnsafe } from '../../codec/create';
import Metadata from '../Metadata';
import Call from '../../primitive/Generic/Call';
@@ -57,7 +55,7 @@ export function defaultValues (rpcData: string): void {
describe('storage with default values', (): void => {
const metadata = new Metadata(rpcData);
Call.injectMethods(extrinsicsFromMeta(metadata));
Call.injectMetadata(metadata);
metadata.asLatest.modules
.filter(({ storage }): boolean => storage.isSome)
+3 -3
View File
@@ -8,9 +8,9 @@ import { VoteThreshold } from '../interfaces/elections';
import { BlockNumber } from '../interfaces/runtime';
import { CodecTo } from '../types';
import extrinsics from '@polkadot/api-metadata/extrinsics/static';
import { ClassOf } from './create';
import Metadata from '../Metadata';
import metadataStatic from '../Metadata/static';
import Call from '../primitive/Generic/Call';
import Text from '../primitive/Text';
import U32 from '../primitive/U32';
@@ -66,7 +66,7 @@ describe('Tuple', (): void => {
});
it.skip('creates properly via actual hex string', (): void => {
Call.injectMethods(extrinsics);
Call.injectMetadata(new Metadata(metadataStatic));
const test = new (Tuple.with([
ClassOf('BlockNumber'), ClassOf('Proposal'), ClassOf('VoteThreshold')
+5 -3
View File
@@ -2,11 +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 { Proposal, PropIndex } from '../interfaces/democracy';
import { Codec, CodecTo } from '../types';
import extrinsics from '@polkadot/api-metadata/extrinsics/static';
import Metadata from '../Metadata';
import metadataStatic from '../Metadata/static';
import AccountId from '../primitive/Generic/AccountId';
import Call from '../primitive/Generic/Call';
import Text from '../primitive/Text';
@@ -20,7 +22,7 @@ describe('Vec', (): void => {
beforeEach((): void => {
vector = new Vec(Text, ['1', '23', '345', '4567', new Text('56789')]);
Call.injectMethods(extrinsics);
Call.injectMetadata(new Metadata(metadataStatic));
});
it('wraps a sequence of values', (): void => {
+3 -3
View File
@@ -4,8 +4,8 @@
import './injector';
import extrinsics from '@polkadot/api-metadata/extrinsics/static';
import Metadata from './Metadata';
import metadataStatic from './Metadata/static';
import { createTypeUnsafe } from './codec/create';
import GenericCall from './primitive/Generic/Call';
import { Codec } from './types';
@@ -41,7 +41,7 @@ function testTypes (type: string, typeNames: string[]): void {
});
describe(`${type}:: default creation (empty bytes)`, (): void => {
GenericCall.injectMethods(extrinsics);
GenericCall.injectMetadata(new Metadata(metadataStatic));
typeNames.forEach((name): void => {
it(`creates an empty ${name} (from empty bytes)`, (): void => {
@@ -4,8 +4,8 @@
import '../../injector';
import extrinsics from '@polkadot/api-metadata/extrinsics/static';
import Metadata from '../../Metadata';
import metadataStatic from '../../Metadata/static';
import { createType } from '../../codec/create';
import Call from '../../primitive/Generic/Call';
import events from '../../json/SignedBlock.002.json';
@@ -15,7 +15,7 @@ import knownMehods from '../../json/SignedBlock.005.json';
describe('SignedBlock', (): void => {
beforeEach((): void => {
Call.injectMethods(extrinsics);
Call.injectMetadata(new Metadata(metadataStatic));
});
it('decodes a full block', (): void => {
@@ -2,16 +2,18 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import extrinsics from '@polkadot/api-metadata/extrinsics/static';
import '../../injector';
import { hexToU8a } from '@polkadot/util';
import Call from '../Generic/Call';
import Metadata from '../../Metadata';
import metadataStatic from '../../Metadata/static';
import Extrinsic from './Extrinsic';
describe('Extrinsic', (): void => {
beforeAll((): void => {
Call.injectMethods(extrinsics);
Call.injectMetadata(new Metadata(metadataStatic));
});
describe('V1', (): void => {
@@ -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 extrinsics from '@polkadot/api-metadata/extrinsics/static';
import { createType, GenericCall } from '@polkadot/types';
import Metadata from '../../Metadata';
import metadataStatic from '../../Metadata/static';
import SignerPayload from './SignerPayload';
describe('SignerPayload', (): void => {
@@ -22,7 +23,7 @@ describe('SignerPayload', (): void => {
};
beforeEach((): void => {
GenericCall.injectMethods(extrinsics);
GenericCall.injectMetadata(new Metadata(metadataStatic));
});
it('creates a valid JSON output', (): void => {
@@ -2,10 +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 BN from 'bn.js';
import extrinsics from '@polkadot/api-metadata/extrinsics/static';
import testingPairs from '@polkadot/keyring/testingPairs';
import Metadata from '../../../Metadata';
import metadataStatic from '../../../Metadata/static';
import Call from '../../Generic/Call';
import Extrinsic from './Extrinsic';
@@ -13,7 +17,7 @@ const keyring = testingPairs({ type: 'ed25519' }, false);
describe('ExtrinsicV2', (): void => {
beforeEach((): void => {
Call.injectMethods(extrinsics);
Call.injectMetadata(new Metadata(metadataStatic));
});
it('constructs a sane Uint8Array (default)', (): void => {
@@ -2,10 +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 BN from 'bn.js';
import extrinsics from '@polkadot/api-metadata/extrinsics/static';
import testingPairs from '@polkadot/keyring/testingPairs';
import Metadata from '../../../Metadata';
import metadataStatic from '../../../Metadata/static';
import Call from '../../Generic/Call';
import Extrinsic from './Extrinsic';
@@ -13,7 +17,7 @@ const keyring = testingPairs({ type: 'ed25519' }, false);
describe('ExtrinsicV3', (): void => {
beforeEach((): void => {
Call.injectMethods(extrinsics);
Call.injectMetadata(new Metadata(metadataStatic));
});
it('constructs a sane Uint8Array (default)', (): void => {
@@ -4,15 +4,15 @@
import '../../injector';
import extrinsics from '@polkadot/api-metadata/extrinsics/static';
import Metadata from '../../Metadata';
import metadataStatic from '../../Metadata/static';
import block00300 from '../../json/SignedBlock.003.00.json';
import Call from './Call';
import Block from './Block';
describe('Block', (): void => {
beforeEach((): void => {
Call.injectMethods(extrinsics);
Call.injectMetadata(new Metadata(metadataStatic));
});
it('has a valid toRawType', (): void => {
@@ -2,13 +2,15 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import extrinsics from '@polkadot/api-metadata/extrinsics/static';
import '../../injector';
import Metadata from '../../Metadata';
import metadataStatic from '../../Metadata/static';
import Call from './Call';
describe('Call', (): void => {
beforeEach((): void => {
Call.injectMethods(extrinsics);
Call.injectMetadata(new Metadata(metadataStatic));
});
it('handles decoding correctly (bare)', (): void => {
+9 -7
View File
@@ -3,13 +3,15 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { FunctionArgumentMetadataLatest, FunctionMetadataLatest } from '../../interfaces/metadata';
import { AnyU8a, ArgsDef, CallFunction, Codec, IMethod, ModulesWithCalls } from '../../types';
import { AnyU8a, ArgsDef, CallFunction, Codec, IMethod } from '../../types';
import extrinsicsFromMeta from '@polkadot/api-metadata/extrinsics/fromMetadata';
import { assert, isHex, isObject, isU8a, u8aToU8a } from '@polkadot/util';
import { getTypeDef, getTypeClass } from '../../codec/create';
import Struct from '../../codec/Struct';
import U8aFixed from '../../codec/U8aFixed';
import Metadata from '../../Metadata';
interface DecodeMethodInput {
args: any;
@@ -80,7 +82,7 @@ export default class Call extends Struct implements IMethod {
}
private static decodeCallViaObject (value: DecodedMethod, _meta?: FunctionMetadataLatest): DecodedMethod {
// destructure value, we only pass args/methodsIndex out
// we only pass args/methodsIndex out
const { args, callIndex } = value;
// Get the correct lookupIndex
@@ -154,10 +156,10 @@ export default class Call extends Struct implements IMethod {
}, {} as unknown as ArgsDef);
}
// This is called/injected by the API on init, allowing a snapshot of
// the available system extrinsics to be used in lookups
public static injectMethods (moduleMethods: ModulesWithCalls): void {
Object.values(moduleMethods).forEach((methods): void =>
public static injectMetadata (metadata: Metadata): void {
const extrinsics = extrinsicsFromMeta(metadata);
Object.values(extrinsics).forEach((methods): void =>
Object.values(methods).forEach((method): void => {
injected[method.callIndex.toString()] = method;
})
@@ -173,7 +175,7 @@ export default class Call extends Struct implements IMethod {
}
/**
* @description Thge argument defintions
* @description The argument definitions
*/
public get argsDef (): ArgsDef {
return Call.getArgsDef(this.meta);
@@ -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 fromMetadata from '@polkadot/api-metadata/storage/fromMetadata';
import { hexToU8a } from '@polkadot/util';
import rpcDataV3 from '@polkadot/types/Metadata/v3/static';
+4 -5
View File
@@ -2,19 +2,15 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
// NOTE When adding any types here, we need to update the coumentation links as
// NOTE When adding any types here, we need to update the documentation links as
// well - <root>/docs/SUMMARY.md as well as ../README.md
/**
* @summary Type definitions that are used in the system
*/
// NOTE We are exporting Bool as bool to match with Rust (however also using Bool internally),
// so with the renamed versions, epose both cases, allowing createType to work
export { default as bool } from './Bool';
export { default as Bytes } from './Bytes';
export { default as Data } from './Data';
export * from './Extrinsic';
export * from './Generic';
export { default as H160 } from './H160';
export { default as H256 } from './H256';
export { default as H512 } from './H512';
@@ -38,3 +34,6 @@ export { default as u64 } from './U64';
export { default as u128 } from './U128';
export { default as u256 } from './U256';
export { default as usize } from './USize';
export * from './Generic';
export * from './Extrinsic';
@@ -9,6 +9,7 @@ require('@babel/register')({
plugins: [
['module-resolver', {
alias: {
'^@polkadot/api-metadata(.*)': './packages/api-metadata/src\\1',
'^@polkadot/types(.*)': './packages/types/src\\1'
}
}]
@@ -5,7 +5,15 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('@babel/register')({
extensions: ['.js', '.ts']
extensions: ['.js', '.ts'],
plugins: [
['module-resolver', {
alias: {
'^@polkadot/api-metadata(.*)': './packages/api-metadata/src\\1',
'^@polkadot/types(.*)': './packages/types/src\\1'
}
}]
]
});
require('./interfacesTs.ts');