Metadata v7 (#1182)
* Make a start... static is in, code copied as a start. * Get exports (more-or-less) right * Well, the v7/* tests pass... now just to tweak storage generation * Linting is happy, it doesn't mean it will work... * It compiles... ship it.
This commit is contained in:
@@ -14,7 +14,7 @@ import { stringCamelCase } from '@polkadot/util';
|
||||
* @param metadata - The metadata
|
||||
*/
|
||||
export default function fromMetadata (metadata: Metadata): Constants {
|
||||
return metadata.asV6.modules.reduce((result, moduleMetadata): Constants => {
|
||||
return metadata.asV7.modules.reduce((result, moduleMetadata): Constants => {
|
||||
if (moduleMetadata.constants.isEmpty) {
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -3,10 +3,10 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Codec } from '@polkadot/types/types';
|
||||
import { ModuleConstantMetadata as MetaV6 } from '@polkadot/types/Metadata/v6/Constants';
|
||||
import { ModuleConstantMetadata as MetaV7 } from '@polkadot/types/Metadata/v6/Constants';
|
||||
|
||||
export interface ConstantCodec extends Codec {
|
||||
meta: MetaV6;
|
||||
meta: MetaV7;
|
||||
}
|
||||
|
||||
export type ModuleConstants = Record<string, Codec>;
|
||||
|
||||
@@ -3,13 +3,13 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Extrinsic, Metadata } from '@polkadot/types';
|
||||
import json from '@polkadot/types/Metadata/v6/static';
|
||||
import json from '@polkadot/types/Metadata/v7/static';
|
||||
|
||||
import fromV6 from '.';
|
||||
import fromMetadata from '.';
|
||||
|
||||
// Use the pre-generated metadata
|
||||
const metadata = new Metadata(json);
|
||||
const newExtrinsics = fromV6(metadata);
|
||||
const newExtrinsics = fromMetadata(metadata);
|
||||
|
||||
describe('fromMetadata', (): void => {
|
||||
it('should throw if an incorrect number of args is supplied', (): void => {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { ModuleMetadata } from '@polkadot/types/Metadata/v6/Metadata';
|
||||
import { ModuleMetadata } from '@polkadot/types/Metadata/v7/Metadata';
|
||||
import { Methods, ModulesWithMethods } from '@polkadot/types/primitive/Method';
|
||||
import Metadata from '@polkadot/types/Metadata';
|
||||
import { stringCamelCase } from '@polkadot/util';
|
||||
@@ -16,7 +16,7 @@ import createUnchecked from './createUnchecked';
|
||||
* @param metadata - The metadata
|
||||
*/
|
||||
export default function fromMetadata (metadata: Metadata): ModulesWithMethods {
|
||||
return metadata.asV6.modules
|
||||
return metadata.asV7.modules
|
||||
.filter((modul): boolean => modul.calls.isSome)
|
||||
.reduce((result, modul: ModuleMetadata, sectionIndex): ModulesWithMethods => {
|
||||
const section = stringCamelCase(modul.name.toString());
|
||||
|
||||
@@ -16,22 +16,24 @@ import { storage } from './storage';
|
||||
* @param metadata - The metadata
|
||||
*/
|
||||
export default function fromMetadata (metadata: Metadata): Storage {
|
||||
return metadata.asV6.modules.reduce((result, moduleMetadata): Storage => {
|
||||
return metadata.asV7.modules.reduce((result, moduleMetadata): Storage => {
|
||||
if (moduleMetadata.storage.isNone) {
|
||||
return result;
|
||||
}
|
||||
|
||||
const { name, prefix } = moduleMetadata;
|
||||
const { name } = moduleMetadata;
|
||||
const section = stringCamelCase(name.toString());
|
||||
const unwrapped = moduleMetadata.storage.unwrap();
|
||||
const prefix = unwrapped.prefix.toString();
|
||||
|
||||
// For access, we change the index names, i.e. Balances.FreeBalance -> balances.freeBalance
|
||||
result[section] = moduleMetadata.storage.unwrap().reduce((newModule, meta): ModuleStorage => {
|
||||
result[section] = unwrapped.items.reduce((newModule, meta): ModuleStorage => {
|
||||
const method = meta.name.toString();
|
||||
|
||||
newModule[stringLowerFirst(method)] = createFunction({
|
||||
meta,
|
||||
method,
|
||||
prefix: prefix.toString(),
|
||||
prefix,
|
||||
section
|
||||
});
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ import rpcMetadataV3 from '@polkadot/types/Metadata/v3/static';
|
||||
import rpcMetadataV4 from '@polkadot/types/Metadata/v4/static';
|
||||
import rpcMetadataV5 from '@polkadot/types/Metadata/v5/static';
|
||||
import rpcMetadataV6 from '@polkadot/types/Metadata/v6/static';
|
||||
import rpcMetadataV7 from '@polkadot/types/Metadata/v7/static';
|
||||
|
||||
import Api from '.';
|
||||
|
||||
@@ -204,3 +205,9 @@ formattingTests('v6', fromMetadata(new Metadata(rpcMetadataV6)), [
|
||||
'0x1dbb0224910f42a14e7f1406b24c6fe8157296691b02a78756e01946038fffab',
|
||||
'0xc7879f4faa637a90d782070a3cb6be99a9fb0316e19a0454ce93c4f0a34712f1'
|
||||
]);
|
||||
|
||||
formattingTests('v7', fromMetadata(new Metadata(rpcMetadataV7)), [
|
||||
'0xec8f96437274a883afcac82d01a9defeb68209cd4f2c084632813692aa5e65ad',
|
||||
'0x1dbb0224910f42a14e7f1406b24c6fe8157296691b02a78756e01946038fffab',
|
||||
'0xc7879f4faa637a90d782070a3cb6be99a9fb0316e19a0454ce93c4f0a34712f1'
|
||||
]);
|
||||
|
||||
@@ -12,7 +12,7 @@ import interfaces from '@polkadot/jsonrpc';
|
||||
import testKeyring from '@polkadot/keyring/testing';
|
||||
import storage from '@polkadot/api-metadata/storage/static';
|
||||
import { Codec } from '@polkadot/types/types';
|
||||
import rpcMetadataV6 from '@polkadot/types/Metadata/v6/static';
|
||||
import rpcMetadataV7 from '@polkadot/types/Metadata/v7/static';
|
||||
import { Header, RuntimeVersion } from '@polkadot/types';
|
||||
import { bnToU8a, logger, u8aToHex } from '@polkadot/util';
|
||||
import { randomAsU8a } from '@polkadot/util-crypto';
|
||||
@@ -55,7 +55,7 @@ export default class Mock implements ProviderInterface {
|
||||
);
|
||||
},
|
||||
system_chain: (): string => 'mockChain',
|
||||
state_getMetadata: (): string => rpcMetadataV6,
|
||||
state_getMetadata: (): string => rpcMetadataV7,
|
||||
system_name: (): string => 'mockClient',
|
||||
system_version: (): string => '9.8.7'
|
||||
};
|
||||
|
||||
@@ -14,12 +14,14 @@ import MetadataV3 from './v3';
|
||||
import MetadataV4 from './v4';
|
||||
import MetadataV5 from './v5';
|
||||
import MetadataV6 from './v6';
|
||||
import MetadataV7 from './v7';
|
||||
import v0ToV1 from './v0/toV1';
|
||||
import v1ToV2 from './v1/toV2';
|
||||
import v2ToV3 from './v2/toV3';
|
||||
import v3ToV4 from './v3/toV4';
|
||||
import v4ToV5 from './v4/toV5';
|
||||
import v5ToV6 from './v5/toV6';
|
||||
import v6ToV7 from './v6/toV7';
|
||||
|
||||
class MetadataEnum extends Enum {
|
||||
public constructor (value?: any) {
|
||||
@@ -30,7 +32,8 @@ class MetadataEnum extends Enum {
|
||||
MetadataV3, // once rolled-out, can replace this with MetadataDeprecated
|
||||
MetadataV4, // once rolled-out, can replace this with MetadataDeprecated
|
||||
MetadataV5, // once rolled-out, can replace this with MetadataDeprecated
|
||||
MetadataV6
|
||||
MetadataV6, // once rolled-out, can replace this with MetadataDeprecated
|
||||
MetadataV7
|
||||
}, value);
|
||||
}
|
||||
|
||||
@@ -97,6 +100,15 @@ class MetadataEnum extends Enum {
|
||||
return this.value as MetadataV6;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns the wrapped values as a V7 object
|
||||
*/
|
||||
public get asV7 (): MetadataV7 {
|
||||
assert(this.isV7, `Cannot convert '${this.type}' via asV7`);
|
||||
|
||||
return this.value as MetadataV7;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description `true` if Deprecated
|
||||
*/
|
||||
@@ -153,6 +165,13 @@ class MetadataEnum extends Enum {
|
||||
return this.type === 'MetadataV6';
|
||||
}
|
||||
|
||||
/**
|
||||
* @description `true` if V7
|
||||
*/
|
||||
public get isV7 (): boolean {
|
||||
return this.type === 'MetadataV7';
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The version this metadata represents
|
||||
*/
|
||||
@@ -179,6 +198,8 @@ export default class MetadataVersioned extends Struct {
|
||||
|
||||
private _convertedV6?: MetadataV6;
|
||||
|
||||
private _convertedV7?: MetadataV7;
|
||||
|
||||
public constructor (value?: any) {
|
||||
super({
|
||||
magicNumber: MagicNumber,
|
||||
@@ -318,7 +339,24 @@ export default class MetadataVersioned extends Struct {
|
||||
return this._convertedV6;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns the wrapped values as a V7 object
|
||||
*/
|
||||
public get asV7 (): MetadataV7 {
|
||||
assert(this.metadata.version <= 7, `Cannot convert metadata from v${this.metadata.version} to v7`);
|
||||
|
||||
if (this.metadata.version === 7) {
|
||||
return this.metadata.asV7;
|
||||
}
|
||||
|
||||
if (isUndefined(this._convertedV7)) {
|
||||
this._convertedV7 = v6ToV7(this.asV6);
|
||||
}
|
||||
|
||||
return this._convertedV7;
|
||||
}
|
||||
|
||||
public getUniqTypes (throwError: boolean): string[] {
|
||||
return this.asV6.getUniqTypes(throwError);
|
||||
return this.asV7.getUniqTypes(throwError);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,6 @@
|
||||
// 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 './v6/static';
|
||||
import metadata from './v7/static';
|
||||
|
||||
export default metadata;
|
||||
|
||||
@@ -34,19 +34,19 @@ export function decodeLatestSubstrate<Modules extends Codec> (
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a `version`, MetadataV6 and MetadataV{version} should output the same
|
||||
* Given a `version`, MetadataV7 and MetadataV{version} should output the same
|
||||
* unique types.
|
||||
*/
|
||||
export function toV6<Modules extends Codec> (version: number, rpcData: string): void {
|
||||
it('converts to V6', (): void => {
|
||||
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 metadataV6 = new Metadata(rpcData).asV6;
|
||||
const metadataV7 = new Metadata(rpcData).asV7;
|
||||
|
||||
expect(
|
||||
(metadata as unknown as MetadataInterface<Modules>).getUniqTypes(true)
|
||||
).toEqual(metadataV6.getUniqTypes(true));
|
||||
).toEqual(metadataV7.getUniqTypes(true));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -61,11 +61,11 @@ export function defaultValues (rpcData: string): void {
|
||||
|
||||
Method.injectMethods(extrinsicsFromMeta(metadata));
|
||||
|
||||
metadata.asV6.modules
|
||||
metadata.asV7.modules
|
||||
.filter(({ storage }): boolean => storage.isSome)
|
||||
.forEach((mod): void => {
|
||||
mod.storage.unwrap().forEach(({ fallback, name, type }): void => {
|
||||
it(`creates default types for ${mod.prefix}.${name}, type ${type}`, (): void => {
|
||||
mod.storage.unwrap().items.forEach(({ fallback, name, type }): void => {
|
||||
it(`creates default types for ${mod.name}.${name}, type ${type}`, (): void => {
|
||||
expect(
|
||||
(): Codec => createType(type.toString(), fallback)
|
||||
).not.toThrow();
|
||||
|
||||
@@ -6,7 +6,7 @@ import MetadataV0 from './';
|
||||
import Metadata from '../';
|
||||
import latestSubstrateV0 from './latest.substrate.v0.json';
|
||||
import rpcData from './static';
|
||||
import { defaultValues, toV6 } from '../util/testUtil';
|
||||
import { defaultValues, toV7 } from '../util/testUtil';
|
||||
|
||||
describe('Metadata', (): void => {
|
||||
it('works with fallback', (): void => {
|
||||
@@ -27,7 +27,7 @@ describe('Metadata', (): void => {
|
||||
expect(json).toEqual(latestSubstrateV0);
|
||||
});
|
||||
|
||||
toV6(0, rpcData);
|
||||
toV7(0, rpcData);
|
||||
|
||||
defaultValues(rpcData);
|
||||
});
|
||||
|
||||
@@ -4,12 +4,12 @@
|
||||
|
||||
import latestSubstrate from './latest.substrate.v1.json';
|
||||
import rpcData from './static';
|
||||
import { decodeLatestSubstrate, defaultValues, toV6 } from '../util/testUtil';
|
||||
import { decodeLatestSubstrate, defaultValues, toV7 } from '../util/testUtil';
|
||||
|
||||
describe('MetadataV1', (): void => {
|
||||
decodeLatestSubstrate(1, rpcData, latestSubstrate);
|
||||
|
||||
toV6(1, rpcData);
|
||||
toV7(1, rpcData);
|
||||
|
||||
defaultValues(rpcData);
|
||||
});
|
||||
|
||||
@@ -4,12 +4,12 @@
|
||||
|
||||
import latestSubstrate from './latest.substrate.v2.json';
|
||||
import rpcData from './static';
|
||||
import { decodeLatestSubstrate, defaultValues, toV6 } from '../util/testUtil';
|
||||
import { decodeLatestSubstrate, defaultValues, toV7 } from '../util/testUtil';
|
||||
|
||||
describe('MetadataV2', (): void => {
|
||||
decodeLatestSubstrate(2, rpcData, latestSubstrate);
|
||||
|
||||
toV6(2, rpcData);
|
||||
toV7(2, rpcData);
|
||||
|
||||
defaultValues(rpcData);
|
||||
});
|
||||
|
||||
@@ -4,12 +4,12 @@
|
||||
|
||||
import latestSubstrate from './latest.substrate.v3.json';
|
||||
import rpcData from './static';
|
||||
import { decodeLatestSubstrate, defaultValues, toV6 } from '../util/testUtil';
|
||||
import { decodeLatestSubstrate, defaultValues, toV7 } from '../util/testUtil';
|
||||
|
||||
describe('MetadataV3', (): void => {
|
||||
decodeLatestSubstrate(3, rpcData, latestSubstrate);
|
||||
|
||||
toV6(3, rpcData);
|
||||
toV7(3, rpcData);
|
||||
|
||||
defaultValues(rpcData);
|
||||
});
|
||||
|
||||
@@ -4,12 +4,12 @@
|
||||
|
||||
import latestSubstrate from './latest.substrate.v4.json';
|
||||
import rpcData from './static';
|
||||
import { decodeLatestSubstrate, defaultValues, toV6 } from '../util/testUtil';
|
||||
import { decodeLatestSubstrate, defaultValues, toV7 } from '../util/testUtil';
|
||||
|
||||
describe('MetadataV4', (): void => {
|
||||
decodeLatestSubstrate(4, rpcData, latestSubstrate);
|
||||
|
||||
toV6(4, rpcData);
|
||||
toV7(4, rpcData);
|
||||
|
||||
defaultValues(rpcData);
|
||||
});
|
||||
|
||||
@@ -6,12 +6,12 @@ import polkadotJson from './latest.polkadot.v5.json';
|
||||
import substrateJson from './latest.substrate.v5.json';
|
||||
import polkadotData from './static.polkadot';
|
||||
import substrateData from './static';
|
||||
import { decodeLatestSubstrate, defaultValues, toV6 } from '../util/testUtil';
|
||||
import { decodeLatestSubstrate, defaultValues, toV7 } from '../util/testUtil';
|
||||
|
||||
describe('MetadataV5 (substrate)', (): void => {
|
||||
decodeLatestSubstrate(5, substrateData, substrateJson);
|
||||
|
||||
toV6(5, substrateData);
|
||||
toV7(5, substrateData);
|
||||
|
||||
defaultValues(substrateData);
|
||||
});
|
||||
@@ -19,7 +19,7 @@ describe('MetadataV5 (substrate)', (): void => {
|
||||
describe('MetadataV5 (polkadot)', (): void => {
|
||||
decodeLatestSubstrate(5, polkadotData, polkadotJson);
|
||||
|
||||
toV6(5, polkadotData);
|
||||
toV7(5, polkadotData);
|
||||
|
||||
defaultValues(polkadotData);
|
||||
});
|
||||
|
||||
@@ -6,12 +6,12 @@ import polkadotJson from './latest.polkadot.v6.json';
|
||||
import substrateJson from './latest.substrate.v6.json';
|
||||
import polkadotData from './static.polkadot';
|
||||
import substrateData from './static';
|
||||
import { decodeLatestSubstrate, defaultValues, toV6 } from '../util/testUtil';
|
||||
import { decodeLatestSubstrate, defaultValues, toV7 } from '../util/testUtil';
|
||||
|
||||
describe('MetadataV6 (substrate)', (): void => {
|
||||
decodeLatestSubstrate(6, substrateData, substrateJson);
|
||||
|
||||
toV6(6, substrateData);
|
||||
toV7(6, substrateData);
|
||||
|
||||
defaultValues(substrateData);
|
||||
});
|
||||
@@ -19,7 +19,7 @@ describe('MetadataV6 (substrate)', (): void => {
|
||||
describe('MetadataV6 (polkadot)', (): void => {
|
||||
decodeLatestSubstrate(6, polkadotData, polkadotJson);
|
||||
|
||||
toV6(6, polkadotData);
|
||||
toV7(6, polkadotData);
|
||||
|
||||
defaultValues(polkadotData);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
// 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 MetadataV6 from './Metadata';
|
||||
import MetadataV7 from '../v7';
|
||||
import { StorageMetadata as StorageV7 } from '../v7/Storage';
|
||||
|
||||
/**
|
||||
* Convert from MetadataV6 to MetadataV7
|
||||
*/
|
||||
export default function toV7 ({ modules }: MetadataV6): MetadataV7 {
|
||||
return new MetadataV7({
|
||||
// FIXME, this needs typing, not any
|
||||
modules: modules.map(({ calls, constants, events, name, prefix, storage }): any => {
|
||||
return {
|
||||
name,
|
||||
storage: storage.isSome
|
||||
? new StorageV7({
|
||||
prefix,
|
||||
items: storage.unwrap()
|
||||
})
|
||||
: null,
|
||||
calls,
|
||||
events,
|
||||
constants
|
||||
};
|
||||
})
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// 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.
|
||||
|
||||
// Re-export classes that haven't changed between V6 & V7
|
||||
export {
|
||||
FunctionArgumentMetadata,
|
||||
FunctionMetadata
|
||||
} from '../v6/Calls';
|
||||
@@ -0,0 +1,8 @@
|
||||
// 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.
|
||||
|
||||
// Re-export classes that haven't changed between V6 & V7
|
||||
export {
|
||||
ModuleConstantMetadata
|
||||
} from '../v6/Constants';
|
||||
@@ -0,0 +1,8 @@
|
||||
// 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.
|
||||
|
||||
// Re-export classes that haven't changed between V6 & V7
|
||||
export {
|
||||
EventMetadata
|
||||
} from '../v6/Events';
|
||||
@@ -0,0 +1,15 @@
|
||||
// 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 substrateJson from './latest.substrate.v7.json';
|
||||
import substrateData from './static';
|
||||
import { decodeLatestSubstrate, defaultValues, toV7 } from '../util/testUtil';
|
||||
|
||||
describe('MetadataV7 (substrate)', (): void => {
|
||||
decodeLatestSubstrate(7, substrateData, substrateJson);
|
||||
|
||||
toV7(7, substrateData);
|
||||
|
||||
defaultValues(substrateData);
|
||||
});
|
||||
@@ -0,0 +1,150 @@
|
||||
// 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 { MetadataInterface } from '../types';
|
||||
|
||||
import Option from '../../codec/Option';
|
||||
import Struct from '../../codec/Struct';
|
||||
import Vector from '../../codec/Vector';
|
||||
import Text from '../../primitive/Text';
|
||||
import { flattenUniq, validateTypes } from '../util';
|
||||
|
||||
import { FunctionMetadata } from './Calls';
|
||||
import { ModuleConstantMetadata } from './Constants';
|
||||
import { EventMetadata } from './Events';
|
||||
import { StorageMetadata } from './Storage';
|
||||
|
||||
/**
|
||||
* @name ModuleMetadata
|
||||
* @description
|
||||
* The definition of a module in the system
|
||||
*/
|
||||
export class ModuleMetadata extends Struct {
|
||||
public constructor (value?: any) {
|
||||
super({
|
||||
name: Text,
|
||||
storage: Option.with(StorageMetadata),
|
||||
calls: Option.with(Vector.with(FunctionMetadata)),
|
||||
events: Option.with(Vector.with(EventMetadata)),
|
||||
constants: Vector.with(ModuleConstantMetadata)
|
||||
}, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description the module calls
|
||||
*/
|
||||
public get calls (): Option<Vector<FunctionMetadata>> {
|
||||
return this.get('calls') as Option<Vector<FunctionMetadata>>;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description the module constants
|
||||
*/
|
||||
public get constants (): Vector<ModuleConstantMetadata> {
|
||||
return this.get('constants') as Vector<ModuleConstantMetadata>;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description the module events
|
||||
*/
|
||||
public get events (): Option<Vector<EventMetadata>> {
|
||||
return this.get('events') as Option<Vector<EventMetadata>>;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description the module name
|
||||
*/
|
||||
public get name (): Text {
|
||||
return this.get('name') as Text;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description the module prefix
|
||||
*/
|
||||
public get prefix (): Text {
|
||||
return this.get('prefix') as Text;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description the associated module storage
|
||||
*/
|
||||
public get storage (): Option<StorageMetadata> {
|
||||
return this.get('storage') as Option<StorageMetadata>;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @name MetadataV7
|
||||
* @description
|
||||
* The runtime metadata as a decoded structure
|
||||
*/
|
||||
export default class MetadataV7 extends Struct implements MetadataInterface<ModuleMetadata> {
|
||||
public constructor (value?: any) {
|
||||
super({
|
||||
modules: Vector.with(ModuleMetadata)
|
||||
}, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The associated modules for this structure
|
||||
*/
|
||||
public get modules (): Vector<ModuleMetadata> {
|
||||
return this.get('modules') as Vector<ModuleMetadata>;
|
||||
}
|
||||
|
||||
private get callNames (): string[][][] {
|
||||
return this.modules.map((mod): string[][] =>
|
||||
mod.calls.isNone
|
||||
? []
|
||||
: mod.calls.unwrap().map((fn): string[] =>
|
||||
fn.args.map((arg): string => arg.type.toString())
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private get constantNames (): string[][] {
|
||||
return this.modules.map((mod): string[] =>
|
||||
mod.constants.map((c): string =>
|
||||
c.type.toString()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private get eventNames (): string[][][] {
|
||||
return this.modules.map((mod): string[][] =>
|
||||
mod.events.isNone
|
||||
? []
|
||||
: mod.events.unwrap().map((event): string[] =>
|
||||
event.args.map((arg): string => arg.toString())
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private get storageNames (): string[][][] {
|
||||
return this.modules.map((mod): string[][] =>
|
||||
mod.storage.isNone
|
||||
? []
|
||||
: mod.storage.unwrap().items.map((fn): string[] => {
|
||||
if (fn.type.isMap) {
|
||||
return [fn.type.asMap.key.toString(), fn.type.asMap.value.toString()];
|
||||
} else if (fn.type.isDoubleMap) {
|
||||
return [fn.type.asDoubleMap.key1.toString(), fn.type.asDoubleMap.key2.toString(), fn.type.asDoubleMap.value.toString()];
|
||||
} else {
|
||||
return [fn.type.asType.toString()];
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Helper to retrieve a list of all type that are found, sorted and de-deuplicated
|
||||
*/
|
||||
public getUniqTypes (throwError: boolean): string[] {
|
||||
const types = flattenUniq([this.callNames, this.constantNames, this.eventNames, this.storageNames]);
|
||||
|
||||
validateTypes(types, throwError);
|
||||
|
||||
return types;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
// 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 Struct from '../../codec/Struct';
|
||||
import Vector from '../../codec/Vector';
|
||||
import Text from '../../primitive/Text';
|
||||
|
||||
import { DoubleMapType, MapType, PlainType, StorageEntryMetadata, StorageEntryMetadataValue, StorageEntryModifier, StorageEntryType } from '../v6/Storage';
|
||||
|
||||
// Re-export classes that haven't changed between V6 and V7
|
||||
export {
|
||||
MapType,
|
||||
PlainType,
|
||||
DoubleMapType,
|
||||
StorageEntryMetadata,
|
||||
StorageEntryMetadataValue,
|
||||
StorageEntryModifier,
|
||||
StorageEntryType
|
||||
};
|
||||
|
||||
export class StorageMetadata extends Struct {
|
||||
public constructor (value?: any) {
|
||||
super({
|
||||
prefix: Text,
|
||||
// NOTE renamed to items from entries (since Struct already has entries from Map)
|
||||
items: Vector.with(StorageEntryMetadata)
|
||||
}, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description the storage entries
|
||||
*/
|
||||
public get items (): Vector<StorageEntryMetadata> {
|
||||
return this.get('items') as Vector<StorageEntryMetadata>;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description the prefix for this module
|
||||
*/
|
||||
public get prefix (): Text {
|
||||
return this.get('prefix') as Text;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// 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 MetadataV7, { ModuleMetadata as ModuleMetadataV7 } from './Metadata';
|
||||
|
||||
export default MetadataV7;
|
||||
|
||||
export {
|
||||
ModuleMetadataV7
|
||||
};
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
@@ -11,7 +11,7 @@ import Tuple from '../codec/Tuple';
|
||||
import U8aFixed from '../codec/U8aFixed';
|
||||
import { TypeDef, getTypeClass, getTypeDef } from '../codec/createType';
|
||||
import Metadata from '../Metadata';
|
||||
import { EventMetadata as EventMetadataV6 } from '../Metadata/v6/Events';
|
||||
import { EventMetadata as EventMetadataV7 } from '../Metadata/v7/Events';
|
||||
import Null from './Null';
|
||||
import U32 from './U32';
|
||||
import Unconstructable from './Unconstructable';
|
||||
@@ -32,7 +32,7 @@ export class EventIndex extends U32 {
|
||||
* Wrapper for the actual data that forms part of an [[Event]]
|
||||
*/
|
||||
export class EventData extends Tuple {
|
||||
private _meta: EventMetadataV6;
|
||||
private _meta: EventMetadataV7;
|
||||
|
||||
private _method: string;
|
||||
|
||||
@@ -40,7 +40,7 @@ export class EventData extends Tuple {
|
||||
|
||||
private _typeDef: TypeDef[];
|
||||
|
||||
public constructor (Types: Constructor[], value: Uint8Array, typeDef: TypeDef[], meta: EventMetadataV6, section: string, method: string) {
|
||||
public constructor (Types: Constructor[], value: Uint8Array, typeDef: TypeDef[], meta: EventMetadataV7, section: string, method: string) {
|
||||
super(Types, value);
|
||||
|
||||
this._meta = meta;
|
||||
@@ -52,7 +52,7 @@ export class EventData extends Tuple {
|
||||
/**
|
||||
* @description The wrapped [[EventMetadata]]
|
||||
*/
|
||||
public get meta (): EventMetadataV6 {
|
||||
public get meta (): EventMetadataV7 {
|
||||
return this._meta;
|
||||
}
|
||||
|
||||
@@ -132,7 +132,7 @@ export default class Event extends Struct {
|
||||
// This is called/injected by the API on init, allowing a snapshot of
|
||||
// the available system events to be used in lookups
|
||||
public static injectMetadata (metadata: Metadata): void {
|
||||
metadata.asV6.modules
|
||||
metadata.asV7.modules
|
||||
.filter((section): boolean => section.events.isSome)
|
||||
.forEach((section, sectionIndex): void => {
|
||||
const sectionName = stringCamelCase(section.name.toString());
|
||||
@@ -169,7 +169,7 @@ export default class Event extends Struct {
|
||||
/**
|
||||
* @description The [[EventMetadata]] with the documentation
|
||||
*/
|
||||
public get meta (): EventMetadataV6 {
|
||||
public get meta (): EventMetadataV7 {
|
||||
return this.data.meta;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ import { blake2AsU8a } from '@polkadot/util-crypto';
|
||||
import { getTypeDef, getTypeClass } from '../codec/createType';
|
||||
import Struct from '../codec/Struct';
|
||||
import U8aFixed from '../codec/U8aFixed';
|
||||
import { FunctionMetadata as FunctionMetadataV6, FunctionArgumentMetadata } from '../Metadata/v6/Calls';
|
||||
import { FunctionMetadata as FunctionMetadataV7, FunctionArgumentMetadata } from '../Metadata/v7/Calls';
|
||||
import Hash from './Hash';
|
||||
|
||||
interface DecodeMethodInput {
|
||||
@@ -20,13 +20,13 @@ interface DecodeMethodInput {
|
||||
|
||||
interface DecodedMethod extends DecodeMethodInput {
|
||||
argsDef: ArgsDef;
|
||||
meta: FunctionMetadataV6;
|
||||
meta: FunctionMetadataV7;
|
||||
}
|
||||
|
||||
export interface MethodFunction {
|
||||
(...args: any[]): Method;
|
||||
callIndex: Uint8Array;
|
||||
meta: FunctionMetadataV6;
|
||||
meta: FunctionMetadataV7;
|
||||
method: string;
|
||||
section: string;
|
||||
toJSON: () => any;
|
||||
@@ -65,9 +65,9 @@ export class MethodIndex extends U8aFixed {
|
||||
* {@link https://github.com/paritytech/wiki/blob/master/Extrinsic.md#the-extrinsic-format-for-node}.
|
||||
*/
|
||||
export default class Method extends Struct implements IMethod {
|
||||
protected _meta: FunctionMetadataV6;
|
||||
protected _meta: FunctionMetadataV7;
|
||||
|
||||
public constructor (value: any, meta?: FunctionMetadataV6) {
|
||||
public constructor (value: any, meta?: FunctionMetadataV7) {
|
||||
const decoded = Method.decodeMethod(value, meta);
|
||||
|
||||
super({
|
||||
@@ -88,7 +88,7 @@ export default class Method extends Struct implements IMethod {
|
||||
* @param _meta - Metadata to use, so that `injectMethods` lookup is not
|
||||
* necessary.
|
||||
*/
|
||||
private static decodeMethod (value: DecodedMethod | Uint8Array | string = new Uint8Array(), _meta?: FunctionMetadataV6): DecodedMethod {
|
||||
private static decodeMethod (value: DecodedMethod | Uint8Array | string = new Uint8Array(), _meta?: FunctionMetadataV7): DecodedMethod {
|
||||
if (isHex(value)) {
|
||||
return Method.decodeMethod(hexToU8a(value), _meta);
|
||||
} else if (isU8a(value)) {
|
||||
@@ -128,7 +128,7 @@ export default class Method extends Struct implements IMethod {
|
||||
}
|
||||
|
||||
// If the extrinsic function has an argument of type `Origin`, we ignore it
|
||||
public static filterOrigin (meta?: FunctionMetadataV6): FunctionArgumentMetadata[] {
|
||||
public static filterOrigin (meta?: FunctionMetadataV7): FunctionArgumentMetadata[] {
|
||||
// FIXME should be `arg.type !== Origin`, but doesn't work...
|
||||
return meta
|
||||
? meta.args.filter(({ type }): boolean =>
|
||||
@@ -156,7 +156,7 @@ export default class Method extends Struct implements IMethod {
|
||||
*
|
||||
* @param meta - The function metadata used to get the definition.
|
||||
*/
|
||||
private static getArgsDef (meta: FunctionMetadataV6): ArgsDef {
|
||||
private static getArgsDef (meta: FunctionMetadataV7): ArgsDef {
|
||||
return Method.filterOrigin(meta).reduce((result, { name, type }): ArgsDef => {
|
||||
const Type = getTypeClass(
|
||||
getTypeDef(type)
|
||||
@@ -227,7 +227,7 @@ export default class Method extends Struct implements IMethod {
|
||||
/**
|
||||
* @description The [[FunctionMetadata]]
|
||||
*/
|
||||
public get meta (): FunctionMetadataV6 {
|
||||
public get meta (): FunctionMetadataV7 {
|
||||
return this._meta;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import rpcDataV3 from '@polkadot/types/Metadata/v3/static';
|
||||
import rpcDataV4 from '@polkadot/types/Metadata/v4/static';
|
||||
import rpcDataV5 from '@polkadot/types/Metadata/v5/static';
|
||||
import rpcDataV6 from '@polkadot/types/Metadata/v6/static';
|
||||
import rpcDataV7 from '@polkadot/types/Metadata/v7/static';
|
||||
|
||||
import Metadata from '../Metadata';
|
||||
import StorageKey from './StorageKey';
|
||||
@@ -152,4 +153,32 @@ describe('StorageKey', (): void => {
|
||||
).toEqual(Uint8Array.from([1, 1, 48, 7, 201, 255, 112, 39, 246, 89, 0, 171, 205, 252, 164, 253, 177, 7, 234, 212, 126, 42, 158, 53, 88, 224, 27, 105, 27, 15, 74, 95, 133, 24, 212, 135, 50, 102, 20, 240, 102, 65, 99, 8, 191, 106, 164, 229, 4, 29, 25, 73, 146, 142, 75, 38, 237, 233, 142, 60, 235, 179, 106, 59, 23, 38]));
|
||||
});
|
||||
});
|
||||
|
||||
describe('with MetadataV7', (): void => {
|
||||
const storage = fromMetadata(new Metadata(rpcDataV7));
|
||||
|
||||
it(`should correctly get the EventTopics double map storage key (hex)`, (): void => {
|
||||
expect(
|
||||
new StorageKey([
|
||||
storage
|
||||
.system
|
||||
.eventTopics,
|
||||
['any', [1, 2, 3]]
|
||||
])
|
||||
.toHex()
|
||||
).toBe('0x3007c9ff7027f65900abcdfca4fdb107ead47e2a9e3558e01b691b0f4a5f8518d487326614f066416308bf6aa4e5041d1949928e4b26ede98e3cebb36a3b1726');
|
||||
});
|
||||
|
||||
it(`should correctly get the EventTopics double map storage key (u8a)`, (): void => {
|
||||
expect(
|
||||
new StorageKey([
|
||||
storage
|
||||
.system
|
||||
.eventTopics,
|
||||
['any', [1, 2, 3]]
|
||||
])
|
||||
.toU8a()
|
||||
).toEqual(Uint8Array.from([1, 1, 48, 7, 201, 255, 112, 39, 246, 89, 0, 171, 205, 252, 164, 253, 177, 7, 234, 212, 126, 42, 158, 53, 88, 224, 27, 105, 27, 15, 74, 95, 133, 24, 212, 135, 50, 102, 20, 240, 102, 65, 99, 8, 191, 106, 164, 229, 4, 29, 25, 73, 146, 142, 75, 38, 237, 233, 142, 60, 235, 179, 106, 59, 23, 38]));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -4,14 +4,14 @@
|
||||
|
||||
import { assert, isFunction, isString, isU8a } from '@polkadot/util';
|
||||
|
||||
import { StorageEntryMetadata as MetaV6 } from '../Metadata/v6/Storage';
|
||||
import { StorageEntryMetadata as MetaV7 } from '../Metadata/v7/Storage';
|
||||
import { AnyU8a } from '../types';
|
||||
import Bytes from './Bytes';
|
||||
|
||||
export interface StorageEntry {
|
||||
(arg?: any): Uint8Array;
|
||||
headKey?: Uint8Array;
|
||||
meta: MetaV6;
|
||||
meta: MetaV7;
|
||||
method: string;
|
||||
prefix: string;
|
||||
section: string;
|
||||
@@ -36,7 +36,7 @@ interface StorageKeyExtra {
|
||||
* constructed by passing in a raw key or a StorageEntry with (optional) arguments.
|
||||
*/
|
||||
export default class StorageKey extends Bytes {
|
||||
private _meta?: MetaV6;
|
||||
private _meta?: MetaV7;
|
||||
|
||||
private _method?: string;
|
||||
|
||||
@@ -88,7 +88,7 @@ export default class StorageKey extends Bytes {
|
||||
throw new Error(`Unable to convert input ${value} to StorageKey`);
|
||||
}
|
||||
|
||||
public static getMeta (value: StorageKey | StorageEntry | [StorageEntry, any]): MetaV6 | undefined {
|
||||
public static getMeta (value: StorageKey | StorageEntry | [StorageEntry, any]): MetaV7 | undefined {
|
||||
if (value instanceof StorageKey) {
|
||||
return value.meta;
|
||||
} else if (isFunction(value)) {
|
||||
@@ -119,7 +119,7 @@ export default class StorageKey extends Bytes {
|
||||
/**
|
||||
* @description The metadata or `undefined` when not available
|
||||
*/
|
||||
public get meta (): MetaV6 | undefined {
|
||||
public get meta (): MetaV7 | undefined {
|
||||
return this._meta;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import interfaces from '../../../type-jsonrpc/src';
|
||||
import Method from '../primitive/Method';
|
||||
import Metadata from '../Metadata';
|
||||
import rpcdata from '../Metadata/static';
|
||||
import MetadataV6, { ModuleMetadataV6 } from '../Metadata/v6';
|
||||
import MetadataV7, { ModuleMetadataV7 } from '../Metadata/v7';
|
||||
|
||||
const ANCHOR_TOP = '';
|
||||
const LINK_BACK_TO_TOP = '';
|
||||
@@ -68,7 +68,7 @@ function sortByName<T extends { name: any }> (a: T, b: T): number {
|
||||
return nameA.localeCompare(nameB);
|
||||
}
|
||||
|
||||
function addConstants (metadata: MetadataV6): string {
|
||||
function addConstants (metadata: MetadataV7): string {
|
||||
const renderHeading = `## ${ANCHOR_TOP}Constants${DESC_CONSTANTS}`;
|
||||
const orderedSections = metadata.modules.sort(sortByName);
|
||||
let renderAnchors = '';
|
||||
@@ -98,7 +98,7 @@ function addConstants (metadata: MetadataV6): string {
|
||||
return renderHeading + renderAnchors + sections;
|
||||
}
|
||||
|
||||
function addEvents (metadata: MetadataV6): string {
|
||||
function addEvents (metadata: MetadataV7): string {
|
||||
const renderHeading = `## ${ANCHOR_TOP}Events${DESC_EVENTS}`;
|
||||
const orderedSections = metadata.modules.sort(sortByName);
|
||||
let renderAnchors = '';
|
||||
@@ -129,9 +129,9 @@ function addEvents (metadata: MetadataV6): string {
|
||||
return renderHeading + renderAnchors + sections;
|
||||
}
|
||||
|
||||
function addExtrinsics (metadata: MetadataV6): string {
|
||||
function addExtrinsics (metadata: MetadataV7): string {
|
||||
const renderHeading = `## ${ANCHOR_TOP}Extrinsics${DESC_EXTRINSICS}`;
|
||||
const orderedSections = metadata.modules.map((i): ModuleMetadataV6 => i).sort(sortByName);
|
||||
const orderedSections = metadata.modules.map((i): ModuleMetadataV7 => i).sort(sortByName);
|
||||
let renderAnchors = '';
|
||||
const sections = orderedSections.reduce((md, meta): string => {
|
||||
if (meta.calls.isNone || !meta.calls.unwrap().length) {
|
||||
@@ -160,7 +160,7 @@ function addExtrinsics (metadata: MetadataV6): string {
|
||||
return renderHeading + renderAnchors + sections;
|
||||
}
|
||||
|
||||
function addStorage (metadata: MetadataV6): string {
|
||||
function addStorage (metadata: MetadataV7): string {
|
||||
const renderHeading = `## ${ANCHOR_TOP}Storage${DESC_STORAGE}`;
|
||||
const orderedSections = metadata.modules.sort(sortByName);
|
||||
let renderAnchors = '';
|
||||
@@ -174,7 +174,7 @@ function addStorage (metadata: MetadataV6): string {
|
||||
renderAnchors += sectionLink(sectionName);
|
||||
|
||||
const renderSection = generateSectionHeader(md, sectionName);
|
||||
const orderedMethods = moduleMetadata.storage.unwrap().sort(sortByName);
|
||||
const orderedMethods = moduleMetadata.storage.unwrap().items.sort(sortByName);
|
||||
|
||||
return orderedMethods.reduce((md, func): string => {
|
||||
const methodName = stringLowerFirst(func.name.toString());
|
||||
@@ -212,26 +212,26 @@ function writeToRpcMd (): void {
|
||||
writeFile('docs/METHODS_RPC.md', addRpc());
|
||||
}
|
||||
|
||||
function writeToConstantsMd (metadata: MetadataV6): void {
|
||||
function writeToConstantsMd (metadata: MetadataV7): void {
|
||||
writeFile('docs/METHODS_CONSTANTS.md', addConstants(metadata));
|
||||
}
|
||||
|
||||
function writeToStorageMd (metadata: MetadataV6): void {
|
||||
function writeToStorageMd (metadata: MetadataV7): void {
|
||||
const options = { flags: 'r', encoding: 'utf8' };
|
||||
const data = fs.readFileSync('packages/types/src/scripts/METHODS_STORAGE_SUBSTRATE.md', options);
|
||||
|
||||
writeFile('docs/METHODS_STORAGE.md', addStorage(metadata), data);
|
||||
}
|
||||
|
||||
function writeToExtrinsicsMd (metadata: MetadataV6): void {
|
||||
function writeToExtrinsicsMd (metadata: MetadataV7): void {
|
||||
writeFile('docs/METHODS_EXTRINSICS.md', addExtrinsics(metadata));
|
||||
}
|
||||
|
||||
function writeToEventsMd (metadata: MetadataV6): void {
|
||||
function writeToEventsMd (metadata: MetadataV7): void {
|
||||
writeFile('docs/METHODS_EVENTS.md', addEvents(metadata));
|
||||
}
|
||||
|
||||
const metadata = new Metadata(rpcdata).asV6;
|
||||
const metadata = new Metadata(rpcdata).asV7;
|
||||
|
||||
writeToRpcMd();
|
||||
writeToConstantsMd(metadata);
|
||||
|
||||
Reference in New Issue
Block a user