Adjust for last remaining circular deps (#2971)
* Adjust for last remaining circular deps * Remove RegistryMetadata duplication * Update packages/typegen/scripts/polkadot-types-internal-interfaces.js * typo * Direct decorateExtrinsics import
This commit is contained in:
+4
-3
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2020 @polkadot/metadata authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { BlockNumber } from '@polkadot/types/interfaces';
|
||||
import type { Constants } from '../types';
|
||||
|
||||
import { TypeRegistry } from '@polkadot/types/create';
|
||||
@@ -8,7 +9,7 @@ import { TypeRegistry } from '@polkadot/types/create';
|
||||
import { Metadata } from '../../Metadata';
|
||||
import rpcMetadata from '../../static';
|
||||
import rpcMetadataV10 from '../../v10/static';
|
||||
import { constantsFromMeta } from './fromMetadata';
|
||||
import { decorateConstants } from '..';
|
||||
|
||||
function init (meta: string): [Constants, TypeRegistry] {
|
||||
const registry = new TypeRegistry();
|
||||
@@ -16,7 +17,7 @@ function init (meta: string): [Constants, TypeRegistry] {
|
||||
|
||||
registry.setMetadata(metadata);
|
||||
|
||||
return [constantsFromMeta(registry, metadata), registry];
|
||||
return [decorateConstants(registry, metadata.asLatest), registry];
|
||||
}
|
||||
|
||||
describe('fromMetadata', (): void => {
|
||||
@@ -25,7 +26,7 @@ describe('fromMetadata', (): void => {
|
||||
|
||||
expect(consts.democracy.cooloffPeriod).toBeInstanceOf(registry.createClass('BlockNumber'));
|
||||
// 3 second blocks, 28 days
|
||||
expect(consts.democracy.cooloffPeriod.toNumber()).toEqual(28 * 24 * 60 * (60 / 3));
|
||||
expect((consts.democracy.cooloffPeriod as unknown as BlockNumber).toNumber()).toEqual(28 * 24 * 60 * (60 / 3));
|
||||
});
|
||||
|
||||
// removed from session
|
||||
+3
-4
@@ -1,16 +1,15 @@
|
||||
// Copyright 2017-2020 @polkadot/metadata authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { MetadataLatest } from '@polkadot/types/interfaces';
|
||||
import type { Registry } from '@polkadot/types/types';
|
||||
import type { ConstantCodec, Constants, ModuleConstants } from '../types';
|
||||
|
||||
import { hexToU8a, stringCamelCase } from '@polkadot/util';
|
||||
|
||||
import { Metadata } from '../../Metadata';
|
||||
|
||||
/** @internal */
|
||||
export function constantsFromMeta (registry: Registry, metadata: Metadata): Constants {
|
||||
return metadata.asLatest.modules.reduce((result: Constants, moduleMetadata): Constants => {
|
||||
export function decorateConstants (registry: Registry, { modules }: MetadataLatest): Constants {
|
||||
return modules.reduce((result: Constants, moduleMetadata): Constants => {
|
||||
if (moduleMetadata.constants.isEmpty) {
|
||||
return result;
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
// Copyright 2017-2020 @polkadot/metadata authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Call } from '@polkadot/types/interfaces';
|
||||
import type { AnyJson, CallFunction, Registry, RegistryMetadataCall } from '@polkadot/types/types';
|
||||
import type { Call, FunctionMetadataLatest } from '@polkadot/types/interfaces';
|
||||
import type { AnyJson, CallFunction, Registry } from '@polkadot/types/types';
|
||||
|
||||
import { assert, stringCamelCase } from '@polkadot/util';
|
||||
|
||||
/** @internal */
|
||||
export function createUnchecked (registry: Registry, section: string, callIndex: Uint8Array, callMetadata: RegistryMetadataCall): CallFunction {
|
||||
export function createUnchecked (registry: Registry, section: string, callIndex: Uint8Array, callMetadata: FunctionMetadataLatest): CallFunction {
|
||||
const expectedArgs = callMetadata.args;
|
||||
const funcName = stringCamelCase(callMetadata.name);
|
||||
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
// Copyright 2017-2020 @polkadot/metadata authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { TypeRegistry } from '@polkadot/types/create';
|
||||
|
||||
import { Metadata } from '../../Metadata';
|
||||
import json from '../../static';
|
||||
import { extrinsicsFromMeta } from './fromMetadata';
|
||||
|
||||
// Use the pre-generated metadata
|
||||
const registry = new TypeRegistry();
|
||||
const metadata = new Metadata(registry, json);
|
||||
|
||||
registry.setMetadata(metadata);
|
||||
|
||||
const newExtrinsics = extrinsicsFromMeta(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(
|
||||
registry.createType('Extrinsic', newExtrinsics.timestamp.set([10101])).toU8a()
|
||||
).toEqual(
|
||||
new Uint8Array([
|
||||
// length (encoded)
|
||||
4 << 2,
|
||||
// version, no signature
|
||||
4,
|
||||
// index
|
||||
3, 0,
|
||||
// values, Compact<Moment>
|
||||
116
|
||||
])
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -1,34 +0,0 @@
|
||||
// Copyright 2017-2020 @polkadot/metadata authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Calls, ModulesWithCalls, Registry, RegistryMetadata, RegistryMetadataModule } from '@polkadot/types/types';
|
||||
|
||||
import { stringCamelCase } from '@polkadot/util';
|
||||
|
||||
import { createUnchecked } from './createUnchecked';
|
||||
import extrinsics from '.';
|
||||
|
||||
/** @internal */
|
||||
export function extrinsicsFromMeta (registry: Registry, metadata: RegistryMetadata): ModulesWithCalls {
|
||||
const modules = metadata.asLatest.modules;
|
||||
const isIndexed = modules.some(({ index }) => !index.eqn(255));
|
||||
|
||||
return modules
|
||||
.filter(({ calls }) => calls.isSome)
|
||||
.reduce((result, { calls, index, name }: RegistryMetadataModule, _sectionIndex): ModulesWithCalls => {
|
||||
const sectionIndex = isIndexed
|
||||
? index.toNumber()
|
||||
: _sectionIndex;
|
||||
const section = stringCamelCase(name);
|
||||
|
||||
result[section] = calls.unwrap().reduce((newModule: Calls, callMetadata, methodIndex): Calls => {
|
||||
const method = stringCamelCase(callMetadata.name);
|
||||
|
||||
newModule[method] = createUnchecked(registry, section, new Uint8Array([sectionIndex, methodIndex]), callMetadata);
|
||||
|
||||
return newModule;
|
||||
}, {});
|
||||
|
||||
return result;
|
||||
}, { ...extrinsics });
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import { TypeRegistry } from '@polkadot/types/create';
|
||||
|
||||
import { Metadata } from '../../Metadata';
|
||||
import metadataStatic from '../../static';
|
||||
import { extrinsicsFromMeta } from './fromMetadata';
|
||||
import { decorateExtrinsics } from '..';
|
||||
|
||||
const keyring = createTestPairs({ type: 'ed25519' }, false);
|
||||
const registry = new TypeRegistry();
|
||||
@@ -16,7 +16,7 @@ const metadata = new Metadata(registry, metadataStatic);
|
||||
|
||||
registry.setMetadata(metadata);
|
||||
|
||||
const extrinsics = extrinsicsFromMeta(registry, metadata);
|
||||
const extrinsics = decorateExtrinsics(registry, metadata.asLatest);
|
||||
|
||||
describe('extrinsics', (): void => {
|
||||
it('encodes an actual transfer (actual data)', (): void => {
|
||||
@@ -52,3 +52,30 @@ describe('extrinsics', (): void => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('fromMetadata', (): void => {
|
||||
it('should throw if an incorrect number of args is supplied', (): void => {
|
||||
expect(() => extrinsics.balances.setBalance()).toThrowError(/expects 3 arguments/);
|
||||
});
|
||||
|
||||
it('should return a value if the storage function does not expect an argument', (): void => {
|
||||
expect(() => extrinsics.balances.setBalance('5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF', 2, 3)).not.toThrow();
|
||||
});
|
||||
|
||||
it('should return properly-encoded transactions', (): void => {
|
||||
expect(
|
||||
registry.createType('Extrinsic', extrinsics.timestamp.set([10101])).toU8a()
|
||||
).toEqual(
|
||||
new Uint8Array([
|
||||
// length (encoded)
|
||||
4 << 2,
|
||||
// version, no signature
|
||||
4,
|
||||
// index
|
||||
3, 0,
|
||||
// values, Compact<Moment>
|
||||
116
|
||||
])
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,8 +1,32 @@
|
||||
// Copyright 2017-2020 @polkadot/metadata authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { ModulesWithCalls } from '@polkadot/types/types';
|
||||
import type { MetadataLatest } from '@polkadot/types/interfaces';
|
||||
import type { Registry } from '@polkadot/types/types';
|
||||
import type { Extrinsics, ModuleExtrinsics } from '../types';
|
||||
|
||||
const extrinsics: ModulesWithCalls = {};
|
||||
import { stringCamelCase } from '@polkadot/util';
|
||||
|
||||
export default extrinsics;
|
||||
import { createUnchecked } from './createUnchecked';
|
||||
|
||||
/** @internal */
|
||||
export function decorateExtrinsics (registry: Registry, { modules }: MetadataLatest): Extrinsics {
|
||||
const isIndexed = modules.some(({ index }) => !index.eqn(255));
|
||||
|
||||
return modules
|
||||
.filter(({ calls }) => calls.isSome)
|
||||
.reduce((result: Extrinsics, { calls, index, name }, _sectionIndex): Extrinsics => {
|
||||
const sectionIndex = isIndexed
|
||||
? index.toNumber()
|
||||
: _sectionIndex;
|
||||
const section = stringCamelCase(name);
|
||||
|
||||
result[section] = calls.unwrap().reduce((newModule: ModuleExtrinsics, callMetadata, methodIndex): ModuleExtrinsics => {
|
||||
newModule[stringCamelCase(callMetadata.name)] = createUnchecked(registry, section, new Uint8Array([sectionIndex, methodIndex]), callMetadata);
|
||||
|
||||
return newModule;
|
||||
}, {});
|
||||
|
||||
return result;
|
||||
}, {});
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import { u8aToHex } from '@polkadot/util';
|
||||
|
||||
import { Metadata } from '../Metadata';
|
||||
import json from '../static';
|
||||
import { constantsFromMeta, extrinsicsFromMeta, storageFromMeta } from './';
|
||||
import { decorateConstants, decorateExtrinsics, decorateStorage } from './';
|
||||
|
||||
const registry = new TypeRegistry();
|
||||
const metadata = new Metadata(registry, json);
|
||||
@@ -15,7 +15,7 @@ registry.setMetadata(metadata);
|
||||
|
||||
describe('Decorated', () => {
|
||||
it('should correctly get Alice\'s nonce storage key (u8a)', (): void => {
|
||||
const query = storageFromMeta(registry, metadata);
|
||||
const query = decorateStorage(registry, metadata.asLatest, metadata.version);
|
||||
|
||||
expect(
|
||||
u8aToHex(
|
||||
@@ -27,7 +27,7 @@ describe('Decorated', () => {
|
||||
});
|
||||
|
||||
it('should return properly-encoded transactions', (): void => {
|
||||
const tx = extrinsicsFromMeta(registry, metadata);
|
||||
const tx = decorateExtrinsics(registry, metadata.asLatest);
|
||||
|
||||
expect(
|
||||
registry.createType('Extrinsic', tx.timestamp.set([10101])).toU8a()
|
||||
@@ -46,7 +46,7 @@ describe('Decorated', () => {
|
||||
});
|
||||
|
||||
it('should return constants with the correct type and value', (): void => {
|
||||
const consts = constantsFromMeta(registry, metadata);
|
||||
const consts = decorateConstants(registry, metadata.asLatest);
|
||||
|
||||
expect(consts.democracy.cooloffPeriod).toBeInstanceOf(registry.createClass('BlockNumber'));
|
||||
expect(consts.democracy.cooloffPeriod.toHex()).toEqual('0x000c4e00');
|
||||
|
||||
@@ -7,21 +7,23 @@ import type { DecoratedMeta } from './types';
|
||||
import { assert } from '@polkadot/util';
|
||||
|
||||
import { Metadata } from '../Metadata';
|
||||
import { constantsFromMeta } from './consts/fromMetadata';
|
||||
import { extrinsicsFromMeta } from './extrinsics/fromMetadata';
|
||||
import { storageFromMeta } from './storage/fromMetadata';
|
||||
import { decorateConstants } from './constants';
|
||||
import { decorateExtrinsics } from './extrinsics';
|
||||
import { decorateStorage } from './storage';
|
||||
|
||||
/**
|
||||
* Expands the metadata by decoration int consts, query and tx sections
|
||||
* Expands the metadata by decoration into consts, query and tx sections
|
||||
*/
|
||||
export function expandMetadata (registry: Registry, value: Metadata): DecoratedMeta {
|
||||
assert(value instanceof Metadata, 'You need to pass a valid Metadata instance to Decorated');
|
||||
export function expandMetadata (registry: Registry, metadata: Metadata): DecoratedMeta {
|
||||
assert(metadata instanceof Metadata, 'You need to pass a valid Metadata instance to Decorated');
|
||||
|
||||
const latest = metadata.asLatest;
|
||||
|
||||
return {
|
||||
consts: constantsFromMeta(registry, value),
|
||||
query: storageFromMeta(registry, value),
|
||||
tx: extrinsicsFromMeta(registry, value)
|
||||
consts: decorateConstants(registry, latest),
|
||||
query: decorateStorage(registry, latest, metadata.version),
|
||||
tx: decorateExtrinsics(registry, latest)
|
||||
};
|
||||
}
|
||||
|
||||
export { constantsFromMeta, extrinsicsFromMeta, storageFromMeta };
|
||||
export { decorateConstants, decorateExtrinsics, decorateStorage };
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
// Copyright 2017-2020 @polkadot/metadata authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Registry } from '@polkadot/types/types';
|
||||
import type { ModuleStorage, Storage } from '../types';
|
||||
|
||||
import { stringCamelCase, stringLowerFirst } from '@polkadot/util';
|
||||
|
||||
import { Metadata } from '../../Metadata';
|
||||
import { createFunction } from './createFunction';
|
||||
import { getStorage } from './storage';
|
||||
|
||||
/** @internal */
|
||||
export function storageFromMeta (registry: Registry, metadata: Metadata): Storage {
|
||||
return metadata.asLatest.modules.reduce((result: Storage, moduleMetadata): Storage => {
|
||||
if (moduleMetadata.storage.isNone) {
|
||||
return result;
|
||||
}
|
||||
|
||||
const { name } = moduleMetadata;
|
||||
const section = stringCamelCase(name);
|
||||
const unwrapped = moduleMetadata.storage.unwrap();
|
||||
const prefix = unwrapped.prefix.toString();
|
||||
|
||||
// For access, we change the index names, i.e. System.Account -> system.account
|
||||
result[section] = unwrapped.items.reduce((newModule, meta): ModuleStorage => {
|
||||
const method = meta.name.toString();
|
||||
|
||||
newModule[stringLowerFirst(method)] = createFunction(registry, {
|
||||
meta,
|
||||
method,
|
||||
prefix,
|
||||
section
|
||||
}, { metaVersion: metadata.version });
|
||||
|
||||
return newModule;
|
||||
}, {} as ModuleStorage);
|
||||
|
||||
return result;
|
||||
}, { ...getStorage(registry, metadata.version) });
|
||||
}
|
||||
+2
-2
@@ -3,9 +3,9 @@
|
||||
|
||||
import { TypeRegistry } from '@polkadot/types/create';
|
||||
|
||||
import { getStorage } from './storage';
|
||||
import { getStorage } from './getStorage';
|
||||
|
||||
describe('storage', (): void => {
|
||||
describe('getSorage', (): void => {
|
||||
const registry = new TypeRegistry();
|
||||
const storage = getStorage(registry, 8);
|
||||
|
||||
+4
-4
@@ -7,7 +7,7 @@ import { u8aToHex } from '@polkadot/util';
|
||||
|
||||
import { Metadata } from '../../';
|
||||
import rpcMetadata from '../../static';
|
||||
import { storageFromMeta } from '../';
|
||||
import { decorateStorage } from '..';
|
||||
|
||||
const keyring = createTestPairs({ type: 'ed25519' });
|
||||
|
||||
@@ -18,14 +18,14 @@ describe('fromMetadata', (): void => {
|
||||
|
||||
registry.setMetadata(metadata);
|
||||
|
||||
const query = storageFromMeta(registry, metadata);
|
||||
const query = decorateStorage(registry, metadata.asLatest, metadata.version);
|
||||
|
||||
it('should throw if the storage function expects an argument', (): void => {
|
||||
expect((): any => query.balances.account()).toThrowError(/requires one argument/);
|
||||
expect(() => query.balances.account()).toThrowError(/requires one argument/);
|
||||
});
|
||||
|
||||
it('should return a value if the storage function does not expect an argument', (): void => {
|
||||
expect((): any => query.timestamp.now()).not.toThrow();
|
||||
expect(() => query.timestamp.now()).not.toThrow();
|
||||
});
|
||||
|
||||
it('should return the correct length-prefixed storage key', (): void => {
|
||||
@@ -1,6 +1,41 @@
|
||||
// Copyright 2017-2020 @polkadot/metadata authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { getStorage } from './storage';
|
||||
import type { MetadataLatest } from '@polkadot/types/interfaces';
|
||||
import type { Registry } from '@polkadot/types/types';
|
||||
import type { ModuleStorage, Storage } from '../types';
|
||||
|
||||
export default getStorage;
|
||||
import { stringCamelCase, stringLowerFirst } from '@polkadot/util';
|
||||
|
||||
import { createFunction } from './createFunction';
|
||||
import { getStorage } from './getStorage';
|
||||
|
||||
/** @internal */
|
||||
export function decorateStorage (registry: Registry, { modules }: MetadataLatest, metaVersion: number): Storage {
|
||||
return modules.reduce((result: Storage, moduleMetadata): Storage => {
|
||||
if (moduleMetadata.storage.isNone) {
|
||||
return result;
|
||||
}
|
||||
|
||||
const { name } = moduleMetadata;
|
||||
const section = stringCamelCase(name);
|
||||
const unwrapped = moduleMetadata.storage.unwrap();
|
||||
const prefix = unwrapped.prefix.toString();
|
||||
|
||||
// For access, we change the index names, i.e. System.Account -> system.account
|
||||
result[section] = unwrapped.items.reduce((newModule, meta): ModuleStorage => {
|
||||
const method = meta.name.toString();
|
||||
|
||||
newModule[stringLowerFirst(method)] = createFunction(registry, {
|
||||
meta,
|
||||
method,
|
||||
prefix,
|
||||
section
|
||||
}, { metaVersion });
|
||||
|
||||
return newModule;
|
||||
}, {} as ModuleStorage);
|
||||
|
||||
return result;
|
||||
}, { ...getStorage(registry, metaVersion) });
|
||||
}
|
||||
|
||||
@@ -1,27 +1,28 @@
|
||||
// Copyright 2017-2020 @polkadot/metadata authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { AugmentedConst, QueryableConsts, QueryableModuleConsts } from '@polkadot/api/types/consts';
|
||||
import type { ModuleConstantMetadataLatest } from '@polkadot/types/interfaces';
|
||||
import type { StorageEntry } from '@polkadot/types/primitive/types';
|
||||
import type { Codec, ModulesWithCalls } from '@polkadot/types/types';
|
||||
import type { CallFunction, Codec } from '@polkadot/types/types';
|
||||
|
||||
export interface ModuleStorage {
|
||||
[key: string]: StorageEntry;
|
||||
export interface ConstantCodec extends Codec {
|
||||
meta: ModuleConstantMetadataLatest;
|
||||
}
|
||||
|
||||
export type Constants = QueryableConsts<'rxjs'>;
|
||||
export type ModuleConstants = Record<string, ConstantCodec>
|
||||
|
||||
export type ConstantCodec = Codec & AugmentedConst<'rxjs'>;
|
||||
export type ModuleExtrinsics = Record<string, CallFunction>;
|
||||
|
||||
export type ModuleConstants = QueryableModuleConsts;
|
||||
export type ModuleStorage = Record<string, StorageEntry>
|
||||
|
||||
export interface Storage {
|
||||
[key: string]: ModuleStorage;
|
||||
substrate: ModuleStorage;
|
||||
}
|
||||
export type Constants = Record<string, ModuleConstants>;
|
||||
|
||||
export type Extrinsics = Record<string, ModuleExtrinsics>
|
||||
|
||||
export type Storage = Record<string, ModuleStorage>;
|
||||
|
||||
export interface DecoratedMeta {
|
||||
consts: Constants;
|
||||
query: Storage;
|
||||
tx: ModulesWithCalls
|
||||
tx: Extrinsics
|
||||
}
|
||||
|
||||
@@ -4,4 +4,4 @@
|
||||
import './detectPackage';
|
||||
|
||||
export { Metadata } from './Metadata';
|
||||
export { constantsFromMeta, expandMetadata, extrinsicsFromMeta, storageFromMeta } from './decorate';
|
||||
export { decorateConstants, decorateExtrinsics, decorateStorage, expandMetadata } from './decorate';
|
||||
|
||||
Reference in New Issue
Block a user