Cleanups with DRY-er code (#4312)
* Cleanups with DRY-er code * dedupe * lint fixes
This commit is contained in:
@@ -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 <ApiType extends ApiTypes> ({ 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<SignerOptions>): SubmittableThis<ApiType, this> {
|
||||
public signAsync (account: AddressOrPair, partialOptions?: Partial<SignerOptions>): PromiseOrObs<ApiType, this> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-call
|
||||
return decorateMethod(
|
||||
(): Observable<this> =>
|
||||
|
||||
@@ -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<ApiType extends ApiTypes, R extends IS
|
||||
? Observable<R>
|
||||
: Promise<() => void>;
|
||||
|
||||
export type SubmittableThis<ApiType extends ApiTypes, THIS> =
|
||||
ApiType extends 'rxjs'
|
||||
? Observable<THIS>
|
||||
: Promise<THIS>;
|
||||
|
||||
export interface SignerOptions {
|
||||
blockHash: Uint8Array | string;
|
||||
era?: IExtrinsicEra | number;
|
||||
@@ -64,7 +59,7 @@ export interface SubmittableExtrinsic<ApiType extends ApiTypes, R extends ISubmi
|
||||
*/
|
||||
sign (account: IKeyringPair, _options?: Partial<SignerOptions>): this;
|
||||
|
||||
signAsync (account: AddressOrPair, _options?: Partial<SignerOptions>): SubmittableThis<ApiType, this>;
|
||||
signAsync (account: AddressOrPair, _options?: Partial<SignerOptions>): PromiseOrObs<ApiType, this>;
|
||||
|
||||
signAndSend (account: AddressOrPair, options?: Partial<SignerOptions>): SubmittableResultResult<ApiType, R>;
|
||||
|
||||
|
||||
@@ -17,10 +17,15 @@ export type VoidFn = () => void;
|
||||
|
||||
export type UnsubscribePromise = Promise<VoidFn>;
|
||||
|
||||
// FIXME The day TS has higher-kinded types, we can remove this hardcoded stuff
|
||||
export type PromiseOrObs<ApiType extends ApiTypes, T> = ApiType extends 'rxjs'
|
||||
? Observable<T>
|
||||
: Promise<T>;
|
||||
export type PromiseOrObs<ApiType extends ApiTypes, T> =
|
||||
ApiType extends 'rxjs'
|
||||
? Observable<T>
|
||||
: Promise<T>;
|
||||
|
||||
export type MethodResult<ApiType extends ApiTypes, F extends AnyFunction> =
|
||||
ApiType extends 'rxjs'
|
||||
? RxResult<F>
|
||||
: PromiseResult<F>;
|
||||
|
||||
// Here are the return types of these parts of the api:
|
||||
// - api.query.*.*: no exact typings
|
||||
@@ -39,15 +44,9 @@ export interface PromiseResult<F extends AnyFunction> {
|
||||
(...args: Parameters<F>): Promise<ObsInnerType<ReturnType<F>>>;
|
||||
(...args: Push<Parameters<F>, Callback<ObsInnerType<ReturnType<F>>>>): UnsubscribePromise;
|
||||
<T extends Codec | Codec[]>(...args: Parameters<F>): Promise<T>;
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
<T extends Codec | Codec[]>(...args: Push<Parameters<F>, Callback<T>>): UnsubscribePromise;
|
||||
}
|
||||
|
||||
// FIXME The day TS has higher-kinded types, we can remove this hardcoded stuff
|
||||
export type MethodResult<ApiType extends ApiTypes, F extends AnyFunction> = ApiType extends 'rxjs'
|
||||
? RxResult<F>
|
||||
: PromiseResult<F>;
|
||||
|
||||
// In the abstract `decorateMethod` in Base.ts, we can also pass in some meta-
|
||||
// information. This describes it.
|
||||
export interface DecorateMethodOptions {
|
||||
|
||||
@@ -22,13 +22,13 @@ export type RpcMethodResult<ApiType extends ApiTypes, F extends AnyFunction> = A
|
||||
: PromiseRpcResult<F>;
|
||||
|
||||
export type DecoratedRpcSection<ApiType extends ApiTypes, Section> = {
|
||||
[Method in keyof Section]: Section[Method] extends AnyFunction
|
||||
? RpcMethodResult<ApiType, Section[Method]>
|
||||
[M in keyof Section]: Section[M] extends AnyFunction
|
||||
? RpcMethodResult<ApiType, Section[M]>
|
||||
: never
|
||||
}
|
||||
|
||||
export type DecoratedRpc<ApiType extends ApiTypes, AllSections> = {
|
||||
[Section in keyof AllSections]: DecoratedRpcSection<ApiType, AllSections[Section]>
|
||||
[S in keyof AllSections]: DecoratedRpcSection<ApiType, AllSections[S]>
|
||||
}
|
||||
|
||||
export type AugmentedRpc<F extends AnyFunction> = F & {
|
||||
|
||||
@@ -43,16 +43,12 @@ export type GenericStorageEntryFunction = (...args: unknown[]) => Observable<Cod
|
||||
|
||||
export type QueryableStorageEntry<ApiType extends ApiTypes, A extends AnyTuple = AnyTuple> =
|
||||
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 ApiTypes, A extends AnyTuple = AnyTuple> =
|
||||
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<ApiType extends ApiTypes> extends AugmentedQueries<ApiType> {
|
||||
|
||||
@@ -13,12 +13,12 @@ type AnyDerive = Record<string, AnyDeriveSection>;
|
||||
|
||||
// Exact typings for a particular section `api.derive.section.*`
|
||||
type DeriveSection<ApiType extends ApiTypes, Section extends AnyDeriveSection> = {
|
||||
[MethodName in keyof Section]: MethodResult<ApiType, Section[MethodName]>
|
||||
[M in keyof Section]: MethodResult<ApiType, Section[M]>
|
||||
};
|
||||
|
||||
// Exact typings for all sections `api.derive.*.*`
|
||||
export type DeriveAllSections<ApiType extends ApiTypes, AllSections extends AnyDerive> = {
|
||||
[SectionName in keyof AllSections]: DeriveSection<ApiType, AllSections[SectionName]>
|
||||
[S in keyof AllSections]: DeriveSection<ApiType, AllSections[S]>
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -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(', ')}`);
|
||||
}
|
||||
|
||||
@@ -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`);
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
import type { OverrideVersionedType } from '@polkadot/types/types';
|
||||
|
||||
// Type overrides based on specific nodes
|
||||
const typesChain: Record<string, OverrideVersionedType[]> = {
|
||||
};
|
||||
const typesChain: Record<string, OverrideVersionedType[]> = {};
|
||||
|
||||
export default typesChain;
|
||||
|
||||
@@ -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<string, string> = {
|
||||
|
||||
@@ -2,3 +2,5 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import './detectPackage';
|
||||
|
||||
export { packageInfo } from './packageInfo';
|
||||
|
||||
@@ -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<Codec> {
|
||||
function createInt (Clazz: typeof Int | typeof UInt, { displayName, length }: TypeDef): Constructor<Codec> {
|
||||
assert(isNumber(length), () => `Expected bitLength information for ${displayName || Clazz.constructor.name}<bitLength>`);
|
||||
|
||||
return Clazz.with(length as UIntBitLength, displayName);
|
||||
}
|
||||
|
||||
function createHashMap (value: TypeDef, Clazz: typeof BTreeMap | typeof HashMap): Constructor<Codec> {
|
||||
function createHashMap (Clazz: typeof BTreeMap | typeof HashMap, value: TypeDef): Constructor<Codec> {
|
||||
const [keyType, valueType] = getTypeClassArray(value);
|
||||
|
||||
return Clazz.with(keyType, valueType);
|
||||
}
|
||||
|
||||
function createWithSub (Clazz: { with: (t: string) => Constructor<Codec> }, value: TypeDef): Constructor<Codec> {
|
||||
return Clazz.with(getSubType(value));
|
||||
}
|
||||
|
||||
const infoMapping: Record<TypeDefInfo, (registry: Registry, value: TypeDef) => Constructor<Codec>> = {
|
||||
[TypeDefInfo.BTreeMap]: (registry: Registry, value: TypeDef): Constructor<Codec> =>
|
||||
createHashMap(value, BTreeMap),
|
||||
createHashMap(BTreeMap, value),
|
||||
|
||||
[TypeDefInfo.BTreeSet]: (registry: Registry, value: TypeDef): Constructor<Codec> =>
|
||||
BTreeSet.with(getSubType(value)),
|
||||
createWithSub(BTreeSet, value),
|
||||
|
||||
[TypeDefInfo.Compact]: (registry: Registry, value: TypeDef): Constructor<Codec> =>
|
||||
Compact.with(getSubType(value)),
|
||||
createWithSub(Compact, value),
|
||||
|
||||
[TypeDefInfo.DoNotConstruct]: (registry: Registry, value: TypeDef): Constructor<Codec> =>
|
||||
DoNotConstruct.with(value.displayName || value.type),
|
||||
@@ -89,10 +93,10 @@ const infoMapping: Record<TypeDefInfo, (registry: Registry, value: TypeDef) => C
|
||||
},
|
||||
|
||||
[TypeDefInfo.HashMap]: (registry: Registry, value: TypeDef): Constructor<Codec> =>
|
||||
createHashMap(value, HashMap),
|
||||
createHashMap(HashMap, value),
|
||||
|
||||
[TypeDefInfo.Int]: (registry: Registry, value: TypeDef): Constructor<Codec> =>
|
||||
createInt(value, Int),
|
||||
createInt(Int, value),
|
||||
|
||||
// We have circular deps between Linkage & Struct
|
||||
[TypeDefInfo.Linkage]: (registry: Registry, value: TypeDef): Constructor<Codec> => {
|
||||
@@ -114,7 +118,7 @@ const infoMapping: Record<TypeDefInfo, (registry: Registry, value: TypeDef) => C
|
||||
Null,
|
||||
|
||||
[TypeDefInfo.Option]: (registry: Registry, value: TypeDef): Constructor<Codec> =>
|
||||
Option.with(getSubType(value)),
|
||||
createWithSub(Option, value),
|
||||
|
||||
[TypeDefInfo.Plain]: (registry: Registry, value: TypeDef): Constructor<Codec> =>
|
||||
registry.getOrUnknown(value.type),
|
||||
@@ -149,7 +153,7 @@ const infoMapping: Record<TypeDefInfo, (registry: Registry, value: TypeDef) => C
|
||||
Tuple.with(getTypeClassArray(value)),
|
||||
|
||||
[TypeDefInfo.UInt]: (registry: Registry, value: TypeDef): Constructor<Codec> =>
|
||||
createInt(value, UInt),
|
||||
createInt(UInt, value),
|
||||
|
||||
[TypeDefInfo.Vec]: (registry: Registry, { sub }: TypeDef): Constructor<Codec> => {
|
||||
assert(sub && !Array.isArray(sub), 'Expected type information for vector');
|
||||
@@ -172,7 +176,7 @@ const infoMapping: Record<TypeDefInfo, (registry: Registry, value: TypeDef) => C
|
||||
},
|
||||
|
||||
[TypeDefInfo.WrapperOpaque]: (registry: Registry, value: TypeDef): Constructor<Codec> =>
|
||||
WrapperOpaque.with(getSubType(value))
|
||||
createWithSub(WrapperOpaque, value)
|
||||
};
|
||||
|
||||
export function constructTypeClass<T extends Codec = Codec> (registry: Registry, typeDef: TypeDef): Constructor<T> {
|
||||
|
||||
@@ -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', {
|
||||
|
||||
Reference in New Issue
Block a user