Use asLatest in metadata extraction (#2800)
* User asLatest in metadata extraction * Cleanup Decorated, bump * linting * cleanup decorate * Remove registry arg from Decorated
This commit is contained in:
@@ -29,11 +29,11 @@
|
||||
"@babel/runtime": "^7.12.1",
|
||||
"@polkadot/types": "^2.4.2-9",
|
||||
"@polkadot/types-known": "^2.4.2-9",
|
||||
"@polkadot/util": "^3.6.2-7",
|
||||
"@polkadot/util-crypto": "^3.6.2-7",
|
||||
"@polkadot/util": "^3.6.2-8",
|
||||
"@polkadot/util-crypto": "^3.6.2-8",
|
||||
"bn.js": "^5.1.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/keyring": "^3.6.2-7"
|
||||
"@polkadot/keyring": "^3.6.2-8"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ const metadata = new Metadata(registry, json);
|
||||
|
||||
registry.setMetadata(metadata);
|
||||
|
||||
const decorated = new Decorated(registry, metadata);
|
||||
const decorated = new Decorated(metadata);
|
||||
|
||||
describe('Decorated', () => {
|
||||
it('should correctly get Alice\'s nonce storage key (u8a)', (): void => {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2020 @polkadot/metadata authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { ModulesWithCalls, Registry } from '@polkadot/types/types';
|
||||
import { ModulesWithCalls } from '@polkadot/types/types';
|
||||
import { Constants, Storage } from './types';
|
||||
|
||||
import { assert } from '@polkadot/util';
|
||||
@@ -19,23 +19,18 @@ import storageFromMeta from './storage/fromMetadata';
|
||||
export default class Decorated {
|
||||
public readonly consts: Constants;
|
||||
|
||||
public readonly metadata: Metadata;
|
||||
|
||||
public readonly registry: Registry;
|
||||
|
||||
public readonly query: Storage;
|
||||
|
||||
public readonly tx: ModulesWithCalls;
|
||||
|
||||
constructor (registry: Registry, value: Metadata) {
|
||||
constructor (value: Metadata) {
|
||||
assert(value instanceof Metadata, 'You need to pass a valid Metadata instance to Decorated');
|
||||
|
||||
this.registry = registry;
|
||||
this.metadata = value;
|
||||
const latest = value.asLatest;
|
||||
|
||||
// decoration
|
||||
this.tx = extrinsicsFromMeta(registry, this.metadata);
|
||||
this.query = storageFromMeta(registry, this.metadata);
|
||||
this.consts = constantsFromMeta(registry, this.metadata);
|
||||
this.tx = extrinsicsFromMeta(value.registry, latest);
|
||||
this.query = storageFromMeta(value.registry, latest);
|
||||
this.consts = constantsFromMeta(value.registry, latest);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ function init (meta: string): [Constants, TypeRegistry] {
|
||||
|
||||
registry.setMetadata(metadata);
|
||||
|
||||
return [fromMetadata(registry, metadata), registry];
|
||||
return [fromMetadata(registry, metadata.asLatest), registry];
|
||||
}
|
||||
|
||||
describe('fromMetadata', (): void => {
|
||||
|
||||
@@ -1,17 +1,16 @@
|
||||
// Copyright 2017-2020 @polkadot/metadata authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { MetadataLatest } from '@polkadot/types/interfaces';
|
||||
import { Registry } from '@polkadot/types/types';
|
||||
import { Constants, ConstantCodec, ModuleConstants } from '../../types';
|
||||
|
||||
import { createTypeUnsafe } from '@polkadot/types/create';
|
||||
import { hexToU8a, stringCamelCase } from '@polkadot/util';
|
||||
|
||||
import Metadata from '../../../Metadata';
|
||||
|
||||
/** @internal */
|
||||
export default function fromMetadata (registry: Registry, metadata: Metadata): Constants {
|
||||
return metadata.asLatest.modules.reduce((result: Constants, moduleMetadata): Constants => {
|
||||
export default function fromMetadata (registry: Registry, { modules }: MetadataLatest): Constants {
|
||||
return modules.reduce((result: Constants, moduleMetadata): Constants => {
|
||||
if (moduleMetadata.constants.isEmpty) {
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ const metadata = new Metadata(registry, json);
|
||||
|
||||
registry.setMetadata(metadata);
|
||||
|
||||
const newExtrinsics = fromMetadata(registry, metadata);
|
||||
const newExtrinsics = fromMetadata(registry, metadata.asLatest);
|
||||
|
||||
describe('fromMetadata', (): void => {
|
||||
it('should throw if an incorrect number of args is supplied', (): void => {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2020 @polkadot/metadata authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { Calls, ModulesWithCalls, Registry, RegistryMetadata, RegistryMetadataModule } from '@polkadot/types/types';
|
||||
import { Calls, ModulesWithCalls, Registry, RegistryMetadataLatest, RegistryMetadataModule } from '@polkadot/types/types';
|
||||
|
||||
import { stringCamelCase } from '@polkadot/util';
|
||||
|
||||
@@ -9,8 +9,7 @@ import extrinsics from '../';
|
||||
import createUnchecked from './createUnchecked';
|
||||
|
||||
/** @internal */
|
||||
export default function fromMetadata (registry: Registry, metadata: RegistryMetadata): ModulesWithCalls {
|
||||
const modules = metadata.asLatest.modules;
|
||||
export default function fromMetadata (registry: Registry, { modules }: RegistryMetadataLatest): ModulesWithCalls {
|
||||
const isIndexed = modules.some(({ index }) => !index.eqn(255));
|
||||
|
||||
return modules
|
||||
|
||||
@@ -14,7 +14,7 @@ const metadata = new Metadata(registry, metadataStatic);
|
||||
|
||||
registry.setMetadata(metadata);
|
||||
|
||||
const extrinsics = fromMetadata(registry, metadata);
|
||||
const extrinsics = fromMetadata(registry, metadata.asLatest);
|
||||
|
||||
describe('extrinsics', (): void => {
|
||||
it('encodes an actual transfer (actual data)', (): void => {
|
||||
|
||||
@@ -27,7 +27,7 @@ describe('createFunction', (): void => {
|
||||
method: 'NextKeys',
|
||||
prefix: 'Session',
|
||||
section: 'session'
|
||||
}, { metaVersion: 9 });
|
||||
}, {});
|
||||
|
||||
expect(
|
||||
u8aToHex(
|
||||
|
||||
@@ -15,7 +15,6 @@ import getHasher, { HasherFunction } from './getHasher';
|
||||
|
||||
export interface CreateItemOptions {
|
||||
key?: string;
|
||||
metaVersion: number;
|
||||
skipHashing?: boolean; // We don't hash the keys defined in ./substrate.ts
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ describe('fromMetadata', (): void => {
|
||||
|
||||
registry.setMetadata(metadata);
|
||||
|
||||
const decorated = new Decorated(registry, metadata);
|
||||
const decorated = new Decorated(metadata);
|
||||
|
||||
it('should throw if the storage function expects an argument', (): void => {
|
||||
expect((): any => decorated.query.balances.account()).toThrowError(/requires one argument/);
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
// Copyright 2017-2020 @polkadot/metadata authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { MetadataLatest } from '@polkadot/types/interfaces';
|
||||
import { Registry } from '@polkadot/types/types';
|
||||
import { ModuleStorage, Storage } from '../../types';
|
||||
|
||||
import { stringCamelCase, stringLowerFirst } from '@polkadot/util';
|
||||
|
||||
import Metadata from '../../../Metadata';
|
||||
import createFunction from './createFunction';
|
||||
import getStorage from './storage';
|
||||
|
||||
/** @internal */
|
||||
export default function fromMetadata (registry: Registry, metadata: Metadata): Storage {
|
||||
return metadata.asLatest.modules.reduce((result, moduleMetadata): Storage => {
|
||||
export default function fromMetadata (registry: Registry, { modules }: MetadataLatest): Storage {
|
||||
return modules.reduce((result, moduleMetadata): Storage => {
|
||||
if (moduleMetadata.storage.isNone) {
|
||||
return result;
|
||||
}
|
||||
@@ -31,11 +31,11 @@ export default function fromMetadata (registry: Registry, metadata: Metadata): S
|
||||
method,
|
||||
prefix,
|
||||
section
|
||||
}, { metaVersion: metadata.version });
|
||||
}, {});
|
||||
|
||||
return newModule;
|
||||
}, {} as ModuleStorage);
|
||||
|
||||
return result;
|
||||
}, { ...getStorage(registry, metadata.version) });
|
||||
}, { ...getStorage(registry) });
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import getStorage from './storage';
|
||||
|
||||
describe('storage', (): void => {
|
||||
const registry = new TypeRegistry();
|
||||
const storage = getStorage(registry, 8);
|
||||
const storage = getStorage(registry);
|
||||
|
||||
it('should return well known keys', (): void => {
|
||||
expect(typeof storage.substrate).toBe('object');
|
||||
|
||||
@@ -8,12 +8,12 @@ import { Storage } from '../../types';
|
||||
import * as substrate from './substrate';
|
||||
|
||||
/** @internal */
|
||||
export default function getStorage (registry: Registry, metaVersion: number): Storage {
|
||||
export default function getStorage (registry: Registry): Storage {
|
||||
return {
|
||||
substrate: Object
|
||||
.entries(substrate)
|
||||
.reduce((storage: Record<string, StorageEntry>, [key, fn]): Record<string, StorageEntry> => {
|
||||
(storage as Record<string, unknown>)[key] = fn(registry, metaVersion);
|
||||
(storage as Record<string, unknown>)[key] = fn(registry);
|
||||
|
||||
return storage;
|
||||
}, {})
|
||||
|
||||
@@ -15,8 +15,8 @@ interface SubstrateMetadata {
|
||||
|
||||
// Small helper function to factorize code on this page.
|
||||
/** @internal */
|
||||
function createRuntimeFunction (method: string, key: string, { documentation, type }: SubstrateMetadata): (registry: Registry, metaVersion: number) => StorageEntry {
|
||||
return (registry: Registry, metaVersion: number): StorageEntry =>
|
||||
function createRuntimeFunction (method: string, key: string, { documentation, type }: SubstrateMetadata): (registry: Registry) => StorageEntry {
|
||||
return (registry: Registry): StorageEntry =>
|
||||
createFunction(registry, {
|
||||
meta: {
|
||||
documentation: registry.createType('Vec<Text>', [documentation]),
|
||||
@@ -27,7 +27,7 @@ function createRuntimeFunction (method: string, key: string, { documentation, ty
|
||||
method,
|
||||
prefix: 'Substrate',
|
||||
section: 'substrate'
|
||||
}, { key, metaVersion, skipHashing: true });
|
||||
}, { key, skipHashing: true });
|
||||
}
|
||||
|
||||
export const code = createRuntimeFunction('code', ':code', {
|
||||
|
||||
Reference in New Issue
Block a user