Add additional isCodec checks to error/event .is (#4840)
This commit is contained in:
@@ -7,7 +7,7 @@ import type { DispatchErrorModule, DispatchErrorModuleU8a, MetadataLatest, SiFie
|
||||
import type { PortableRegistry } from '../../../metadata';
|
||||
import type { Errors, IsError } from '../types';
|
||||
|
||||
import { isU8a, lazyMethod, objectSpread, stringCamelCase } from '@polkadot/util';
|
||||
import { isCodec, isU8a, lazyMethod, objectSpread, stringCamelCase } from '@polkadot/util';
|
||||
|
||||
import { lazyVariants } from '../../../create/lazy';
|
||||
import { objectNameToString } from '../util';
|
||||
@@ -39,11 +39,14 @@ export function decorateErrors (registry: Registry, { lookup, pallets }: Metadat
|
||||
|
||||
lazyMethod(result, stringCamelCase(name), () =>
|
||||
lazyVariants(lookup, errors.unwrap(), objectNameToString, (variant: SiVariant): IsError => ({
|
||||
is: ({ error, index }: DispatchErrorModule | DispatchErrorModuleU8a) =>
|
||||
index.eq(sectionIndex) && (
|
||||
isU8a(error)
|
||||
? error[0] === variant.index.toNumber()
|
||||
: error.eq(variant.index)
|
||||
// We sprinkle in isCodec & isU8a to ensure we are dealing with the correct objects
|
||||
is: (errorMod: DispatchErrorModule | DispatchErrorModuleU8a) =>
|
||||
isCodec(errorMod) &&
|
||||
isCodec(errorMod.index) &&
|
||||
errorMod.index.eq(sectionIndex) && (
|
||||
isU8a(errorMod.error)
|
||||
? errorMod.error[0] === variant.index.toNumber()
|
||||
: isCodec(errorMod.error) && errorMod.error.eq(variant.index)
|
||||
),
|
||||
meta: registry.createTypeUnsafe('ErrorMetadataLatest', [variantToMeta(lookup, variant)])
|
||||
}))
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
// Copyright 2017-2022 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { AnyTuple } from '@polkadot/types-codec/types';
|
||||
import type { IEvent } from '../../../types';
|
||||
|
||||
import rpcMetadata from '@polkadot/types-support/metadata/static-substrate';
|
||||
|
||||
import { TypeRegistry } from '../../../create';
|
||||
@@ -42,7 +39,7 @@ describe('decorateEvents', (): void => {
|
||||
|
||||
it('should check against a specific error', (): void => {
|
||||
expect(
|
||||
events.system.ExtrinsicSuccess.is({ index: [0, 0] } as unknown as IEvent<AnyTuple>)
|
||||
events.system.ExtrinsicSuccess.is(registry.createType('Event', { index: [0, 0] }))
|
||||
).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,7 +6,7 @@ import type { MetadataLatest, PalletMetadataLatest, SiVariant } from '../../../i
|
||||
import type { IEvent } from '../../../types';
|
||||
import type { Events, IsEvent } from '../types';
|
||||
|
||||
import { lazyMethod, stringCamelCase } from '@polkadot/util';
|
||||
import { isCodec, isU8a, lazyMethod, stringCamelCase } from '@polkadot/util';
|
||||
|
||||
import { lazyVariants } from '../../../create/lazy';
|
||||
import { variantToMeta } from '../errors';
|
||||
@@ -27,7 +27,10 @@ export function decorateEvents (registry: Registry, { lookup, pallets }: Metadat
|
||||
|
||||
lazyMethod(result, stringCamelCase(name), () =>
|
||||
lazyVariants(lookup, events.unwrap(), objectNameToString, (variant: SiVariant): IsEvent<AnyTuple> => ({
|
||||
// We sprinkle in isCodec & isU8a to ensure we are dealing with the correct objects
|
||||
is: <T extends AnyTuple> (eventRecord: IEvent<AnyTuple>): eventRecord is IEvent<T> =>
|
||||
isCodec(eventRecord) &&
|
||||
isU8a(eventRecord.index) &&
|
||||
sectionIndex === eventRecord.index[0] &&
|
||||
variant.index.eq(eventRecord.index[1]),
|
||||
meta: registry.createTypeUnsafe('EventMetadataLatest', [variantToMeta(lookup, variant)])
|
||||
|
||||
Reference in New Issue
Block a user