Add api.{errors, events, query, tx}.<method>.<section>.is(...) (#2984)
* api.{event, error}.<section>.is<Type>
* Full generation, with checkTypes
* .is
* Extrinsic with arg type definition
* decorate is
* Call is
* Remove hasOrigin
* Renames
* Query is
* Fix extrinsic is
* Backwards compatibility for generated types
* event with optional AnyTuple
* Cleanups
* Cleanups
* Adjust
* Cleanup
This commit is contained in:
@@ -20,7 +20,7 @@ function init (meta: string): [Constants, TypeRegistry] {
|
||||
return [decorateConstants(registry, metadata.asLatest), registry];
|
||||
}
|
||||
|
||||
describe('fromMetadata', (): void => {
|
||||
describe('decorateConstants', (): void => {
|
||||
it('should return constants with the correct type and value', (): void => {
|
||||
const [consts, registry] = init(rpcMetadata);
|
||||
|
||||
|
||||
@@ -9,21 +9,19 @@ import { hexToU8a, stringCamelCase } from '@polkadot/util';
|
||||
|
||||
/** @internal */
|
||||
export function decorateConstants (registry: Registry, { modules }: MetadataLatest): Constants {
|
||||
return modules.reduce((result: Constants, moduleMetadata): Constants => {
|
||||
if (moduleMetadata.constants.isEmpty) {
|
||||
return modules.reduce((result: Constants, { constants, name }): Constants => {
|
||||
if (constants.isEmpty) {
|
||||
return result;
|
||||
}
|
||||
|
||||
const { name } = moduleMetadata;
|
||||
|
||||
// For access, we change the index names, i.e. Democracy.EnactmentPeriod -> democracy.enactmentPeriod
|
||||
result[stringCamelCase(name)] = moduleMetadata.constants.reduce((newModule: ModuleConstants, meta): ModuleConstants => {
|
||||
result[stringCamelCase(name)] = constants.reduce((newModule: ModuleConstants, meta): ModuleConstants => {
|
||||
// convert to the natural type as received
|
||||
const type = meta.type.toString();
|
||||
const codec = registry.createType(type as 'Raw', hexToU8a(meta.value.toHex())) as unknown as ConstantCodec;
|
||||
const codec = registry.createType(type as 'Raw', hexToU8a(meta.value.toHex())) as unknown;
|
||||
|
||||
codec.meta = meta;
|
||||
newModule[stringCamelCase(meta.name)] = codec;
|
||||
(codec as Record<string, unknown>).meta = meta;
|
||||
newModule[stringCamelCase(meta.name)] = codec as ConstantCodec;
|
||||
|
||||
return newModule;
|
||||
}, {} as ModuleConstants);
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
// Copyright 2017-2020 @polkadot/metadata authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { DispatchError, MetadataLatest } from '@polkadot/types/interfaces';
|
||||
import type { Registry } from '@polkadot/types/types';
|
||||
import type { Errors, ModuleErrors } from '../types';
|
||||
|
||||
import { stringCamelCase } from '@polkadot/util';
|
||||
|
||||
function isError (dispatchError: DispatchError, sectionIndex: number, errorIndex: number): boolean {
|
||||
if (!dispatchError.isModule) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const { error, index } = dispatchError.asModule;
|
||||
|
||||
return index.eq(sectionIndex) && error.eq(errorIndex);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function decorateErrors (_: Registry, { modules }: MetadataLatest, metaVersion: number): Errors {
|
||||
return modules.reduce((result: Errors, { errors, index, name }, _sectionIndex): Errors => {
|
||||
if (!errors.length) {
|
||||
return result;
|
||||
}
|
||||
|
||||
const sectionIndex = metaVersion === 12 ? index.toNumber() : _sectionIndex;
|
||||
|
||||
result[stringCamelCase(name)] = errors.reduce((newModule: ModuleErrors, meta, errorIndex): ModuleErrors => {
|
||||
// we don't camelCase the error name
|
||||
newModule[meta.name.toString()] = {
|
||||
is: (dispatchError: DispatchError): boolean =>
|
||||
isError(dispatchError, sectionIndex, errorIndex),
|
||||
meta
|
||||
};
|
||||
|
||||
return newModule;
|
||||
}, {} as ModuleErrors);
|
||||
|
||||
return result;
|
||||
}, {} as Errors);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// Copyright 2017-2020 @polkadot/metadata authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { MetadataLatest } from '@polkadot/types/interfaces';
|
||||
import type { AnyTuple, IEventRecord, Registry } from '@polkadot/types/types';
|
||||
import type { Events, ModuleEvents } from '../types';
|
||||
|
||||
import { stringCamelCase } from '@polkadot/util';
|
||||
|
||||
function isEvent <T extends AnyTuple> (eventRecord: IEventRecord<AnyTuple>, sectionIndex: number, eventIndex: number): eventRecord is IEventRecord<T> {
|
||||
return eventRecord.event.index[0] === sectionIndex && eventRecord.event.index[0] === eventIndex;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function decorateEvents (_: Registry, { modules }: MetadataLatest, metaVersion: number): Events {
|
||||
return modules.reduce((result: Events, { events, index, name }, _sectionIndex): Events => {
|
||||
if (!events.isSome) {
|
||||
return result;
|
||||
}
|
||||
|
||||
const sectionIndex = metaVersion === 12 ? index.toNumber() : _sectionIndex;
|
||||
|
||||
result[stringCamelCase(name)] = events.unwrap().reduce((newModule: ModuleEvents, meta, eventIndex): ModuleEvents => {
|
||||
// we don't camelCase the event name
|
||||
newModule[meta.name.toString()] = {
|
||||
is: <T extends AnyTuple> (eventRecord: IEventRecord<AnyTuple>): eventRecord is IEventRecord<T> =>
|
||||
isEvent(eventRecord, sectionIndex, eventIndex),
|
||||
meta
|
||||
};
|
||||
|
||||
return newModule;
|
||||
}, {} as ModuleEvents);
|
||||
|
||||
return result;
|
||||
}, {} as Events);
|
||||
}
|
||||
@@ -2,10 +2,14 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Call, FunctionMetadataLatest } from '@polkadot/types/interfaces';
|
||||
import type { AnyJson, CallFunction, Registry } from '@polkadot/types/types';
|
||||
import type { AnyJson, AnyTuple, CallFunction, IMethod, Registry } from '@polkadot/types/types';
|
||||
|
||||
import { assert, stringCamelCase } from '@polkadot/util';
|
||||
|
||||
function isTx <A extends AnyTuple> (tx: IMethod<AnyTuple>, callIndex: Uint8Array): tx is IMethod<A> {
|
||||
return tx.callIndex[0] === callIndex[0] && tx.callIndex[1] === callIndex[1];
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function createUnchecked (registry: Registry, section: string, callIndex: Uint8Array, callMetadata: FunctionMetadataLatest): CallFunction {
|
||||
const expectedArgs = callMetadata.args;
|
||||
@@ -20,6 +24,9 @@ export function createUnchecked (registry: Registry, section: string, callIndex:
|
||||
}, callMetadata);
|
||||
};
|
||||
|
||||
extrinsicFn.is = <A extends AnyTuple> (tx: IMethod<AnyTuple>): tx is IMethod<A> =>
|
||||
isTx(tx, callIndex);
|
||||
|
||||
extrinsicFn.callIndex = callIndex;
|
||||
extrinsicFn.meta = callMetadata;
|
||||
extrinsicFn.method = funcName;
|
||||
|
||||
@@ -16,7 +16,7 @@ const metadata = new Metadata(registry, metadataStatic);
|
||||
|
||||
registry.setMetadata(metadata);
|
||||
|
||||
const extrinsics = decorateExtrinsics(registry, metadata.asLatest);
|
||||
const extrinsics = decorateExtrinsics(registry, metadata.asLatest, metadata.version);
|
||||
|
||||
describe('extrinsics', (): void => {
|
||||
it('encodes an actual transfer (actual data)', (): void => {
|
||||
@@ -53,7 +53,7 @@ describe('extrinsics', (): void => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('fromMetadata', (): void => {
|
||||
describe('decorateExtrinsics', (): void => {
|
||||
it('should throw if an incorrect number of args is supplied', (): void => {
|
||||
expect(() => extrinsics.balances.setBalance()).toThrowError(/expects 3 arguments/);
|
||||
});
|
||||
@@ -78,4 +78,11 @@ describe('fromMetadata', (): void => {
|
||||
])
|
||||
);
|
||||
});
|
||||
|
||||
it('has working .is', (): void => {
|
||||
const tx = extrinsics.balances.setBalance('5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF', 2, 3);
|
||||
|
||||
expect(extrinsics.balances.setBalance.is(tx)).toBe(true);
|
||||
expect(extrinsics.balances.transfer.is(tx)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -10,13 +10,11 @@ import { stringCamelCase } from '@polkadot/util';
|
||||
import { createUnchecked } from './createUnchecked';
|
||||
|
||||
/** @internal */
|
||||
export function decorateExtrinsics (registry: Registry, { modules }: MetadataLatest): Extrinsics {
|
||||
const isIndexed = modules.some(({ index }) => !index.eqn(255));
|
||||
|
||||
export function decorateExtrinsics (registry: Registry, { modules }: MetadataLatest, metaVersion: number): Extrinsics {
|
||||
return modules
|
||||
.filter(({ calls }) => calls.isSome)
|
||||
.reduce((result: Extrinsics, { calls, index, name }, _sectionIndex): Extrinsics => {
|
||||
const sectionIndex = isIndexed
|
||||
const sectionIndex = metaVersion === 12
|
||||
? index.toNumber()
|
||||
: _sectionIndex;
|
||||
const section = stringCamelCase(name);
|
||||
|
||||
@@ -27,7 +27,7 @@ describe('Decorated', () => {
|
||||
});
|
||||
|
||||
it('should return properly-encoded transactions', (): void => {
|
||||
const tx = decorateExtrinsics(registry, metadata.asLatest);
|
||||
const tx = decorateExtrinsics(registry, metadata.asLatest, metadata.version);
|
||||
|
||||
expect(
|
||||
registry.createType('Extrinsic', tx.timestamp.set([10101])).toU8a()
|
||||
|
||||
@@ -8,6 +8,8 @@ import { assert } from '@polkadot/util';
|
||||
|
||||
import { Metadata } from '../Metadata';
|
||||
import { decorateConstants } from './constants';
|
||||
import { decorateErrors } from './errors';
|
||||
import { decorateEvents } from './events';
|
||||
import { decorateExtrinsics } from './extrinsics';
|
||||
import { decorateStorage } from './storage';
|
||||
|
||||
@@ -21,8 +23,10 @@ export function expandMetadata (registry: Registry, metadata: Metadata): Decorat
|
||||
|
||||
return {
|
||||
consts: decorateConstants(registry, latest),
|
||||
errors: decorateErrors(registry, latest, metadata.version),
|
||||
events: decorateEvents(registry, latest, metadata.version),
|
||||
query: decorateStorage(registry, latest, metadata.version),
|
||||
tx: decorateExtrinsics(registry, latest)
|
||||
tx: decorateExtrinsics(registry, latest, metadata.version)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ import { decorateStorage } from '..';
|
||||
|
||||
const keyring = createTestPairs({ type: 'ed25519' });
|
||||
|
||||
describe('fromMetadata', (): void => {
|
||||
describe('decorateStorage', (): void => {
|
||||
describe('latest', (): void => {
|
||||
const registry = new TypeRegistry();
|
||||
const metadata = new Metadata(registry, rpcMetadata);
|
||||
|
||||
@@ -1,15 +1,31 @@
|
||||
// Copyright 2017-2020 @polkadot/metadata authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { ModuleConstantMetadataLatest } from '@polkadot/types/interfaces';
|
||||
import type { DispatchError, ErrorMetadataLatest, EventMetadataLatest, ModuleConstantMetadataLatest } from '@polkadot/types/interfaces';
|
||||
import type { StorageEntry } from '@polkadot/types/primitive/types';
|
||||
import type { CallFunction, Codec } from '@polkadot/types/types';
|
||||
import type { AnyTuple, CallFunction, Codec, IEventRecord } from '@polkadot/types/types';
|
||||
|
||||
export interface ConstantCodec extends Codec {
|
||||
meta: ModuleConstantMetadataLatest;
|
||||
readonly meta: ModuleConstantMetadataLatest;
|
||||
}
|
||||
|
||||
export type ModuleConstants = Record<string, ConstantCodec>
|
||||
export interface IsError {
|
||||
readonly meta: ErrorMetadataLatest;
|
||||
|
||||
is: (dispatchError: DispatchError) => boolean;
|
||||
}
|
||||
|
||||
export interface IsEvent <T extends AnyTuple> {
|
||||
readonly meta: EventMetadataLatest;
|
||||
|
||||
is: (record: IEventRecord<AnyTuple>) => record is IEventRecord<T>;
|
||||
}
|
||||
|
||||
export type ModuleConstants = Record<string, ConstantCodec>;
|
||||
|
||||
export type ModuleErrors = Record<string, IsError>;
|
||||
|
||||
export type ModuleEvents = Record<string, IsEvent<AnyTuple>>;
|
||||
|
||||
export type ModuleExtrinsics = Record<string, CallFunction>;
|
||||
|
||||
@@ -17,12 +33,18 @@ export type ModuleStorage = Record<string, StorageEntry>
|
||||
|
||||
export type Constants = Record<string, ModuleConstants>;
|
||||
|
||||
export type Errors = Record<string, ModuleErrors>;
|
||||
|
||||
export type Events = Record<string, ModuleEvents>;
|
||||
|
||||
export type Extrinsics = Record<string, ModuleExtrinsics>
|
||||
|
||||
export type Storage = Record<string, ModuleStorage>;
|
||||
|
||||
export interface DecoratedMeta {
|
||||
consts: Constants;
|
||||
query: Storage;
|
||||
tx: Extrinsics
|
||||
readonly consts: Constants;
|
||||
readonly errors: Errors;
|
||||
readonly events: Events;
|
||||
readonly query: Storage;
|
||||
readonly tx: Extrinsics
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user