Skip to content

Commit 248561c

Browse files
authored
Cleanups with DRY-er code (#4312)
* Cleanups with DRY-er code * dedupe * lint fixes
1 parent d4a9b43 commit 248561c

File tree

13 files changed

+50
-59
lines changed

13 files changed

+50
-59
lines changed

packages/api/src/submittable/createClass.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import type { Observable, OperatorFunction } from 'rxjs';
77
import type { Address, ApplyExtrinsicResult, Call, Extrinsic, ExtrinsicEra, ExtrinsicStatus, Hash, Header, Index, RuntimeDispatchInfo } from '@polkadot/types/interfaces';
88
import type { Callback, Codec, Constructor, IKeyringPair, ISubmittableResult, Registry, SignatureOptions } from '@polkadot/types/types';
9-
import type { ApiInterfaceRx, ApiTypes, SignerResult } from '../types';
10-
import type { AddressOrPair, SignerOptions, SubmittableDryRunResult, SubmittableExtrinsic, SubmittablePaymentResult, SubmittableResultResult, SubmittableResultSubscription, SubmittableThis } from './types';
9+
import type { ApiInterfaceRx, ApiTypes, PromiseOrObs, SignerResult } from '../types';
10+
import type { AddressOrPair, SignerOptions, SubmittableDryRunResult, SubmittableExtrinsic, SubmittablePaymentResult, SubmittableResultResult, SubmittableResultSubscription } from './types';
1111

1212
import { catchError, first, map, mapTo, mergeMap, of, switchMap, tap } from 'rxjs';
1313

@@ -168,7 +168,7 @@ export function createClass <ApiType extends ApiTypes> ({ api, apiType, decorate
168168
/**
169169
* @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.
170170
*/
171-
public signAsync (account: AddressOrPair, partialOptions?: Partial<SignerOptions>): SubmittableThis<ApiType, this> {
171+
public signAsync (account: AddressOrPair, partialOptions?: Partial<SignerOptions>): PromiseOrObs<ApiType, this> {
172172
// eslint-disable-next-line @typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-call
173173
return decorateMethod(
174174
(): Observable<this> =>

packages/api/src/submittable/types.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import type { Observable } from 'rxjs';
55
import type { AccountId, Address, ApplyExtrinsicResult, DispatchError, DispatchInfo, EventRecord, Extrinsic, ExtrinsicStatus, Hash, RuntimeDispatchInfo } from '@polkadot/types/interfaces';
66
import type { AnyNumber, Callback, Codec, IExtrinsicEra, IKeyringPair, ISubmittableResult, Signer } from '@polkadot/types/types';
7-
import type { ApiTypes } from '../types';
7+
import type { ApiTypes, PromiseOrObs } from '../types';
88

99
export interface SubmittableResultValue {
1010
dispatchError?: DispatchError;
@@ -34,11 +34,6 @@ export type SubmittableResultSubscription<ApiType extends ApiTypes, R extends IS
3434
? Observable<R>
3535
: Promise<() => void>;
3636

37-
export type SubmittableThis<ApiType extends ApiTypes, THIS> =
38-
ApiType extends 'rxjs'
39-
? Observable<THIS>
40-
: Promise<THIS>;
41-
4237
export interface SignerOptions {
4338
blockHash: Uint8Array | string;
4439
era?: IExtrinsicEra | number;
@@ -64,7 +59,7 @@ export interface SubmittableExtrinsic<ApiType extends ApiTypes, R extends ISubmi
6459
*/
6560
sign (account: IKeyringPair, _options?: Partial<SignerOptions>): this;
6661

67-
signAsync (account: AddressOrPair, _options?: Partial<SignerOptions>): SubmittableThis<ApiType, this>;
62+
signAsync (account: AddressOrPair, _options?: Partial<SignerOptions>): PromiseOrObs<ApiType, this>;
6863

6964
signAndSend (account: AddressOrPair, options?: Partial<SignerOptions>): SubmittableResultResult<ApiType, R>;
7065

packages/api/src/types/base.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@ export type VoidFn = () => void;
1717

1818
export type UnsubscribePromise = Promise<VoidFn>;
1919

20-
// FIXME The day TS has higher-kinded types, we can remove this hardcoded stuff
21-
export type PromiseOrObs<ApiType extends ApiTypes, T> = ApiType extends 'rxjs'
22-
? Observable<T>
23-
: Promise<T>;
20+
export type PromiseOrObs<ApiType extends ApiTypes, T> =
21+
ApiType extends 'rxjs'
22+
? Observable<T>
23+
: Promise<T>;
24+
25+
export type MethodResult<ApiType extends ApiTypes, F extends AnyFunction> =
26+
ApiType extends 'rxjs'
27+
? RxResult<F>
28+
: PromiseResult<F>;
2429

2530
// Here are the return types of these parts of the api:
2631
// - api.query.*.*: no exact typings
@@ -39,15 +44,9 @@ export interface PromiseResult<F extends AnyFunction> {
3944
(...args: Parameters<F>): Promise<ObsInnerType<ReturnType<F>>>;
4045
(...args: Push<Parameters<F>, Callback<ObsInnerType<ReturnType<F>>>>): UnsubscribePromise;
4146
<T extends Codec | Codec[]>(...args: Parameters<F>): Promise<T>;
42-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
4347
<T extends Codec | Codec[]>(...args: Push<Parameters<F>, Callback<T>>): UnsubscribePromise;
4448
}
4549

46-
// FIXME The day TS has higher-kinded types, we can remove this hardcoded stuff
47-
export type MethodResult<ApiType extends ApiTypes, F extends AnyFunction> = ApiType extends 'rxjs'
48-
? RxResult<F>
49-
: PromiseResult<F>;
50-
5150
// In the abstract `decorateMethod` in Base.ts, we can also pass in some meta-
5251
// information. This describes it.
5352
export interface DecorateMethodOptions {

packages/api/src/types/rpc.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ export type RpcMethodResult<ApiType extends ApiTypes, F extends AnyFunction> = A
2222
: PromiseRpcResult<F>;
2323

2424
export type DecoratedRpcSection<ApiType extends ApiTypes, Section> = {
25-
[Method in keyof Section]: Section[Method] extends AnyFunction
26-
? RpcMethodResult<ApiType, Section[Method]>
25+
[M in keyof Section]: Section[M] extends AnyFunction
26+
? RpcMethodResult<ApiType, Section[M]>
2727
: never
2828
}
2929

3030
export type DecoratedRpc<ApiType extends ApiTypes, AllSections> = {
31-
[Section in keyof AllSections]: DecoratedRpcSection<ApiType, AllSections[Section]>
31+
[S in keyof AllSections]: DecoratedRpcSection<ApiType, AllSections[S]>
3232
}
3333

3434
export type AugmentedRpc<F extends AnyFunction> = F & {

packages/api/src/types/storage.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,12 @@ export type GenericStorageEntryFunction = (...args: unknown[]) => Observable<Cod
4343

4444
export type QueryableStorageEntry<ApiType extends ApiTypes, A extends AnyTuple = AnyTuple> =
4545
ApiType extends 'rxjs'
46-
// eslint-disable-next-line no-use-before-define
4746
? AugmentedQuery<'rxjs', GenericStorageEntryFunction, A>
48-
// eslint-disable-next-line no-use-before-define
4947
: AugmentedQuery<'promise', GenericStorageEntryFunction, A> & StorageEntryPromiseOverloads;
5048

5149
export type QueryableStorageEntryAt<ApiType extends ApiTypes, A extends AnyTuple = AnyTuple> =
5250
ApiType extends 'rxjs'
53-
// eslint-disable-next-line no-use-before-define
5451
? AugmentedQueryAt<'rxjs', GenericStorageEntryFunction, A>
55-
// eslint-disable-next-line no-use-before-define
5652
: AugmentedQueryAt<'promise', GenericStorageEntryFunction, A> & StorageEntryPromiseOverloadsAt;
5753

5854
export interface QueryableStorageAt<ApiType extends ApiTypes> extends AugmentedQueries<ApiType> {

packages/api/src/util/decorate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ type AnyDerive = Record<string, AnyDeriveSection>;
1313

1414
// Exact typings for a particular section `api.derive.section.*`
1515
type DeriveSection<ApiType extends ApiTypes, Section extends AnyDeriveSection> = {
16-
[MethodName in keyof Section]: MethodResult<ApiType, Section[MethodName]>
16+
[M in keyof Section]: MethodResult<ApiType, Section[M]>
1717
};
1818

1919
// Exact typings for all sections `api.derive.*.*`
2020
export type DeriveAllSections<ApiType extends ApiTypes, AllSections extends AnyDerive> = {
21-
[SectionName in keyof AllSections]: DeriveSection<ApiType, AllSections[SectionName]>
21+
[S in keyof AllSections]: DeriveSection<ApiType, AllSections[S]>
2222
};
2323

2424
/**

packages/api/src/util/filterEvents.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function filterEvents (extHash: H256, { block: { extrinsics, header } }:
1919
// if we do get the block after finalized, it _should_ be there
2020
// only warn on filtering with isInBlock (finalization finalizes after)
2121
if (status.isInBlock) {
22-
const allHashes = extrinsics.map((ext): string => ext.hash.toHex());
22+
const allHashes = extrinsics.map((x) => x.hash.toHex());
2323

2424
l.warn(`block ${header.hash.toHex()}: Unable to find extrinsic ${extHash.toHex()} inside ${allHashes.join(', ')}`);
2525
}

packages/api/src/util/validate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function sig ({ lookup }: Registry, { method, section }: StorageEntry, args: SiL
1414
// sets up the arguments in the form of [creator, args] ready to be used in a storage
1515
// call. Additionally, it verifies that the correct number of arguments have been passed
1616
export function extractStorageArgs (registry: Registry, creator: StorageEntry, _args: unknown[]): [StorageEntry, unknown[]] {
17-
const args = _args.filter((arg) => !isUndefined(arg));
17+
const args = _args.filter((a) => !isUndefined(a));
1818

1919
if (creator.meta.type.isPlain) {
2020
assert(args.length === 0, () => `${sig(registry, creator, [])} does not take any arguments, ${args.length} found`);

packages/types-known/src/chain/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import type { OverrideVersionedType } from '@polkadot/types/types';
55

66
// Type overrides based on specific nodes
7-
const typesChain: Record<string, OverrideVersionedType[]> = {
8-
};
7+
const typesChain: Record<string, OverrideVersionedType[]> = {};
98

109
export default typesChain;

packages/types-known/src/knownOrigins.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// We want predictive ordering (manually managed)
55
/* eslint-disable sort-keys */
66

7-
// FIXME: Need some sort of solution for specifying these
87
// Since we don't have insight into the origin specification, we can only define what we know about
98
// in a pure Substrate/Polkadot implementation, any other custom origins won't be handled at all
109
export const knownOrigins: Record<string, string> = {

0 commit comments

Comments
 (0)