diff --git a/packages/api/src/submittable/createClass.ts b/packages/api/src/submittable/createClass.ts index d34f1e8d4f..04b72e9489 100644 --- a/packages/api/src/submittable/createClass.ts +++ b/packages/api/src/submittable/createClass.ts @@ -6,8 +6,8 @@ import type { Observable, OperatorFunction } from 'rxjs'; import type { Address, ApplyExtrinsicResult, Call, Extrinsic, ExtrinsicEra, ExtrinsicStatus, Hash, Header, Index, RuntimeDispatchInfo } from '@polkadot/types/interfaces'; import type { Callback, Codec, Constructor, IKeyringPair, ISubmittableResult, Registry, SignatureOptions } from '@polkadot/types/types'; -import type { ApiInterfaceRx, ApiTypes, SignerResult } from '../types'; -import type { AddressOrPair, SignerOptions, SubmittableDryRunResult, SubmittableExtrinsic, SubmittablePaymentResult, SubmittableResultResult, SubmittableResultSubscription, SubmittableThis } from './types'; +import type { ApiInterfaceRx, ApiTypes, PromiseOrObs, SignerResult } from '../types'; +import type { AddressOrPair, SignerOptions, SubmittableDryRunResult, SubmittableExtrinsic, SubmittablePaymentResult, SubmittableResultResult, SubmittableResultSubscription } from './types'; import { catchError, first, map, mapTo, mergeMap, of, switchMap, tap } from 'rxjs'; @@ -168,7 +168,7 @@ export function createClass ({ api, apiType, decorate /** * @description Signs a transaction, returning `this` to allow chaining. E.g.: `sign(...).send()`. Like `.signAndSend` this will retrieve the nonce and blockHash to send the tx with. */ - public signAsync (account: AddressOrPair, partialOptions?: Partial): SubmittableThis { + public signAsync (account: AddressOrPair, partialOptions?: Partial): PromiseOrObs { // eslint-disable-next-line @typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-call return decorateMethod( (): Observable => diff --git a/packages/api/src/submittable/types.ts b/packages/api/src/submittable/types.ts index 741bc5335d..9560537ae0 100644 --- a/packages/api/src/submittable/types.ts +++ b/packages/api/src/submittable/types.ts @@ -4,7 +4,7 @@ import type { Observable } from 'rxjs'; import type { AccountId, Address, ApplyExtrinsicResult, DispatchError, DispatchInfo, EventRecord, Extrinsic, ExtrinsicStatus, Hash, RuntimeDispatchInfo } from '@polkadot/types/interfaces'; import type { AnyNumber, Callback, Codec, IExtrinsicEra, IKeyringPair, ISubmittableResult, Signer } from '@polkadot/types/types'; -import type { ApiTypes } from '../types'; +import type { ApiTypes, PromiseOrObs } from '../types'; export interface SubmittableResultValue { dispatchError?: DispatchError; @@ -34,11 +34,6 @@ export type SubmittableResultSubscription : Promise<() => void>; -export type SubmittableThis = - ApiType extends 'rxjs' - ? Observable - : Promise; - export interface SignerOptions { blockHash: Uint8Array | string; era?: IExtrinsicEra | number; @@ -64,7 +59,7 @@ export interface SubmittableExtrinsic): this; - signAsync (account: AddressOrPair, _options?: Partial): SubmittableThis; + signAsync (account: AddressOrPair, _options?: Partial): PromiseOrObs; signAndSend (account: AddressOrPair, options?: Partial): SubmittableResultResult; diff --git a/packages/api/src/types/base.ts b/packages/api/src/types/base.ts index b2b28eb3e6..d8aa840603 100644 --- a/packages/api/src/types/base.ts +++ b/packages/api/src/types/base.ts @@ -17,10 +17,15 @@ export type VoidFn = () => void; export type UnsubscribePromise = Promise; -// FIXME The day TS has higher-kinded types, we can remove this hardcoded stuff -export type PromiseOrObs = ApiType extends 'rxjs' - ? Observable - : Promise; +export type PromiseOrObs = + ApiType extends 'rxjs' + ? Observable + : Promise; + +export type MethodResult = + ApiType extends 'rxjs' + ? RxResult + : PromiseResult; // Here are the return types of these parts of the api: // - api.query.*.*: no exact typings @@ -39,15 +44,9 @@ export interface PromiseResult { (...args: Parameters): Promise>>; (...args: Push, Callback>>>): UnsubscribePromise; (...args: Parameters): Promise; - // eslint-disable-next-line @typescript-eslint/no-unused-vars (...args: Push, Callback>): UnsubscribePromise; } -// FIXME The day TS has higher-kinded types, we can remove this hardcoded stuff -export type MethodResult = ApiType extends 'rxjs' - ? RxResult - : PromiseResult; - // In the abstract `decorateMethod` in Base.ts, we can also pass in some meta- // information. This describes it. export interface DecorateMethodOptions { diff --git a/packages/api/src/types/rpc.ts b/packages/api/src/types/rpc.ts index 626fba148b..a5162c7469 100644 --- a/packages/api/src/types/rpc.ts +++ b/packages/api/src/types/rpc.ts @@ -22,13 +22,13 @@ export type RpcMethodResult = A : PromiseRpcResult; export type DecoratedRpcSection = { - [Method in keyof Section]: Section[Method] extends AnyFunction - ? RpcMethodResult + [M in keyof Section]: Section[M] extends AnyFunction + ? RpcMethodResult : never } export type DecoratedRpc = { - [Section in keyof AllSections]: DecoratedRpcSection + [S in keyof AllSections]: DecoratedRpcSection } export type AugmentedRpc = F & { diff --git a/packages/api/src/types/storage.ts b/packages/api/src/types/storage.ts index 213816adff..8612b5b74f 100644 --- a/packages/api/src/types/storage.ts +++ b/packages/api/src/types/storage.ts @@ -43,16 +43,12 @@ export type GenericStorageEntryFunction = (...args: unknown[]) => Observable = ApiType extends 'rxjs' - // eslint-disable-next-line no-use-before-define ? AugmentedQuery<'rxjs', GenericStorageEntryFunction, A> - // eslint-disable-next-line no-use-before-define : AugmentedQuery<'promise', GenericStorageEntryFunction, A> & StorageEntryPromiseOverloads; export type QueryableStorageEntryAt = ApiType extends 'rxjs' - // eslint-disable-next-line no-use-before-define ? AugmentedQueryAt<'rxjs', GenericStorageEntryFunction, A> - // eslint-disable-next-line no-use-before-define : AugmentedQueryAt<'promise', GenericStorageEntryFunction, A> & StorageEntryPromiseOverloadsAt; export interface QueryableStorageAt extends AugmentedQueries { diff --git a/packages/api/src/util/decorate.ts b/packages/api/src/util/decorate.ts index fb4f8b5167..d92bd55ff7 100644 --- a/packages/api/src/util/decorate.ts +++ b/packages/api/src/util/decorate.ts @@ -13,12 +13,12 @@ type AnyDerive = Record; // Exact typings for a particular section `api.derive.section.*` type DeriveSection = { - [MethodName in keyof Section]: MethodResult + [M in keyof Section]: MethodResult }; // Exact typings for all sections `api.derive.*.*` export type DeriveAllSections = { - [SectionName in keyof AllSections]: DeriveSection + [S in keyof AllSections]: DeriveSection }; /** diff --git a/packages/api/src/util/filterEvents.ts b/packages/api/src/util/filterEvents.ts index eab135452e..3680ff4cc7 100644 --- a/packages/api/src/util/filterEvents.ts +++ b/packages/api/src/util/filterEvents.ts @@ -19,7 +19,7 @@ export function filterEvents (extHash: H256, { block: { extrinsics, header } }: // if we do get the block after finalized, it _should_ be there // only warn on filtering with isInBlock (finalization finalizes after) if (status.isInBlock) { - const allHashes = extrinsics.map((ext): string => ext.hash.toHex()); + const allHashes = extrinsics.map((x) => x.hash.toHex()); l.warn(`block ${header.hash.toHex()}: Unable to find extrinsic ${extHash.toHex()} inside ${allHashes.join(', ')}`); } diff --git a/packages/api/src/util/validate.ts b/packages/api/src/util/validate.ts index d5d22b7c0e..1de88edaed 100644 --- a/packages/api/src/util/validate.ts +++ b/packages/api/src/util/validate.ts @@ -14,7 +14,7 @@ function sig ({ lookup }: Registry, { method, section }: StorageEntry, args: SiL // sets up the arguments in the form of [creator, args] ready to be used in a storage // call. Additionally, it verifies that the correct number of arguments have been passed export function extractStorageArgs (registry: Registry, creator: StorageEntry, _args: unknown[]): [StorageEntry, unknown[]] { - const args = _args.filter((arg) => !isUndefined(arg)); + const args = _args.filter((a) => !isUndefined(a)); if (creator.meta.type.isPlain) { assert(args.length === 0, () => `${sig(registry, creator, [])} does not take any arguments, ${args.length} found`); diff --git a/packages/types-known/src/chain/index.ts b/packages/types-known/src/chain/index.ts index 19899c3684..2fb923c89b 100644 --- a/packages/types-known/src/chain/index.ts +++ b/packages/types-known/src/chain/index.ts @@ -4,7 +4,6 @@ import type { OverrideVersionedType } from '@polkadot/types/types'; // Type overrides based on specific nodes -const typesChain: Record = { -}; +const typesChain: Record = {}; export default typesChain; diff --git a/packages/types-known/src/knownOrigins.ts b/packages/types-known/src/knownOrigins.ts index 0c3ddffc86..077a4de0a8 100644 --- a/packages/types-known/src/knownOrigins.ts +++ b/packages/types-known/src/knownOrigins.ts @@ -4,7 +4,6 @@ // We want predictive ordering (manually managed) /* eslint-disable sort-keys */ -// FIXME: Need some sort of solution for specifying these // Since we don't have insight into the origin specification, we can only define what we know about // in a pure Substrate/Polkadot implementation, any other custom origins won't be handled at all export const knownOrigins: Record = { diff --git a/packages/types-support/src/index.ts b/packages/types-support/src/index.ts index c18aaa2414..46aa6edd33 100644 --- a/packages/types-support/src/index.ts +++ b/packages/types-support/src/index.ts @@ -2,3 +2,5 @@ // SPDX-License-Identifier: Apache-2.0 import './detectPackage'; + +export { packageInfo } from './packageInfo'; diff --git a/packages/types/src/create/createClass.ts b/packages/types/src/create/createClass.ts index 20074954f9..57dbf5fa0d 100644 --- a/packages/types/src/create/createClass.ts +++ b/packages/types/src/create/createClass.ts @@ -49,27 +49,31 @@ function getTypeClassArray (value: TypeDef): string[] { return getSubDefArray(value).map(getTypeDefType); } -function createInt ({ displayName, length }: TypeDef, Clazz: typeof Int | typeof UInt): Constructor { +function createInt (Clazz: typeof Int | typeof UInt, { displayName, length }: TypeDef): Constructor { assert(isNumber(length), () => `Expected bitLength information for ${displayName || Clazz.constructor.name}`); return Clazz.with(length as UIntBitLength, displayName); } -function createHashMap (value: TypeDef, Clazz: typeof BTreeMap | typeof HashMap): Constructor { +function createHashMap (Clazz: typeof BTreeMap | typeof HashMap, value: TypeDef): Constructor { const [keyType, valueType] = getTypeClassArray(value); return Clazz.with(keyType, valueType); } +function createWithSub (Clazz: { with: (t: string) => Constructor }, value: TypeDef): Constructor { + return Clazz.with(getSubType(value)); +} + const infoMapping: Record Constructor> = { [TypeDefInfo.BTreeMap]: (registry: Registry, value: TypeDef): Constructor => - createHashMap(value, BTreeMap), + createHashMap(BTreeMap, value), [TypeDefInfo.BTreeSet]: (registry: Registry, value: TypeDef): Constructor => - BTreeSet.with(getSubType(value)), + createWithSub(BTreeSet, value), [TypeDefInfo.Compact]: (registry: Registry, value: TypeDef): Constructor => - Compact.with(getSubType(value)), + createWithSub(Compact, value), [TypeDefInfo.DoNotConstruct]: (registry: Registry, value: TypeDef): Constructor => DoNotConstruct.with(value.displayName || value.type), @@ -89,10 +93,10 @@ const infoMapping: Record C }, [TypeDefInfo.HashMap]: (registry: Registry, value: TypeDef): Constructor => - createHashMap(value, HashMap), + createHashMap(HashMap, value), [TypeDefInfo.Int]: (registry: Registry, value: TypeDef): Constructor => - createInt(value, Int), + createInt(Int, value), // We have circular deps between Linkage & Struct [TypeDefInfo.Linkage]: (registry: Registry, value: TypeDef): Constructor => { @@ -114,7 +118,7 @@ const infoMapping: Record C Null, [TypeDefInfo.Option]: (registry: Registry, value: TypeDef): Constructor => - Option.with(getSubType(value)), + createWithSub(Option, value), [TypeDefInfo.Plain]: (registry: Registry, value: TypeDef): Constructor => registry.getOrUnknown(value.type), @@ -149,7 +153,7 @@ const infoMapping: Record C Tuple.with(getTypeClassArray(value)), [TypeDefInfo.UInt]: (registry: Registry, value: TypeDef): Constructor => - createInt(value, UInt), + createInt(UInt, value), [TypeDefInfo.Vec]: (registry: Registry, { sub }: TypeDef): Constructor => { assert(sub && !Array.isArray(sub), 'Expected type information for vector'); @@ -172,7 +176,7 @@ const infoMapping: Record C }, [TypeDefInfo.WrapperOpaque]: (registry: Registry, value: TypeDef): Constructor => - WrapperOpaque.with(getSubType(value)) + createWithSub(WrapperOpaque, value) }; export function constructTypeClass (registry: Registry, typeDef: TypeDef): Constructor { diff --git a/packages/types/src/metadata/PortableRegistry/toV1.ts b/packages/types/src/metadata/PortableRegistry/toV1.ts index 1e303ee809..ed9220e9f8 100644 --- a/packages/types/src/metadata/PortableRegistry/toV1.ts +++ b/packages/types/src/metadata/PortableRegistry/toV1.ts @@ -1,11 +1,20 @@ // Copyright 2017-2021 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { PortableType, Si0Field, Si0LookupTypeId, Si0Path, Si0Type, Si0TypeDefArray, Si0TypeDefBitSequence, Si0TypeDefComposite, Si0TypeDefPrimitive, Si0TypeDefSequence, Si0TypeDefVariant, Si1Field, Si1TypeDef } from '../../interfaces'; +import type { PortableType, Si0Field, Si0LookupTypeId, Si0Path, Si0Type, Si0TypeDefArray, Si0TypeDefBitSequence, Si0TypeDefComposite, Si0TypeDefPrimitive, Si0TypeDefSequence, Si0TypeDefVariant, Si1Field, Si1TypeDef, SiTypeDef } from '../../interfaces'; import type { Registry } from '../../types'; import { assertUnreachable } from './util'; +function convertType (key: 'Compact' | 'Sequence'): (registry: Registry, si: Si0TypeDefSequence) => SiTypeDef { + return (registry: Registry, { type }: Si0TypeDefSequence) => + registry.createType('Si1TypeDef', { + [key]: { + type: type.toNumber() + } + }); +} + function convertArray (registry: Registry, { len, type }: Si0TypeDefArray): Si1TypeDef { return registry.createType('Si1TypeDef', { Array: { @@ -24,13 +33,7 @@ function convertBitSequence (registry: Registry, { bitOrderType, bitStoreType }: }); } -function convertCompact (registry: Registry, { type }: Si0TypeDefSequence): Si1TypeDef { - return registry.createType('Si1TypeDef', { - Compact: { - type: type.toNumber() - } - }); -} +const convertCompact = convertType('Compact'); function convertComposite (registry: Registry, { fields }: Si0TypeDefComposite): Si1TypeDef { return registry.createType('Si1TypeDef', { @@ -65,13 +68,7 @@ function convertPrimitive (registry: Registry, prim: Si0TypeDefPrimitive): Si1Ty }); } -function convertSequence (registry: Registry, { type }: Si0TypeDefSequence): Si1TypeDef { - return registry.createType('Si1TypeDef', { - Sequence: { - type: type.toNumber() - } - }); -} +const convertSequence = convertType('Sequence'); function convertTuple (registry: Registry, types: Si0LookupTypeId[]): Si1TypeDef { return registry.createType('Si1TypeDef', {