From affa6ab1bd19b7819766fd8015f3c72ecf975d9c Mon Sep 17 00:00:00 2001 From: Jaco Date: Wed, 6 Oct 2021 08:53:50 +0300 Subject: [PATCH] Mark api.query.
..at as deprecated (#4023) * Use api.at internally for sizeAt/entriesAt/keysAt * Remove debug logs * Mark api.query.
..at as deprecated * formatting * @deprecated formatting * commenting --- packages/api-derive/src/chain/getBlock.ts | 23 ++++++++-------- packages/api-derive/src/chain/getHeader.ts | 27 +++++++++---------- .../src/chain/subscribeNewBlocks.ts | 21 ++++++++------- packages/api-derive/src/tx/events.ts | 22 +++++++-------- packages/api/src/base/Decorate.ts | 10 +++---- packages/api/src/checkTypes.manual.ts | 8 +++--- packages/api/src/types/index.ts | 2 ++ packages/api/src/types/storage.ts | 16 ++++++++++- 8 files changed, 70 insertions(+), 59 deletions(-) diff --git a/packages/api-derive/src/chain/getBlock.ts b/packages/api-derive/src/chain/getBlock.ts index 9872f6b08fcb..b45f3dcfa5f8 100644 --- a/packages/api-derive/src/chain/getBlock.ts +++ b/packages/api-derive/src/chain/getBlock.ts @@ -24,20 +24,19 @@ import { memo } from '../util'; * ``` */ export function getBlock (instanceId: string, api: ApiInterfaceRx): (hash: Uint8Array | string) => Observable { - return memo(instanceId, (hash: Uint8Array | string): Observable => - // we get the block first, setting up the registry - api.rpc.chain.getBlock(hash).pipe( - switchMap((signedBlock) => + return memo(instanceId, (blockHash: Uint8Array | string): Observable => + api.queryAt(blockHash).pipe( + switchMap((queryAt) => combineLatest([ - api.query.system.events.at(hash), - api.query.session - ? api.query.session.validators.at(hash) + api.rpc.chain.getBlock(blockHash), + queryAt.system.events(), + queryAt.session + ? queryAt.session.validators() : of([]) - ]).pipe( - map(([events, validators]) => - createSignedBlockExtended(api.registry, signedBlock, events, validators) - ) - ) + ]) + ), + map(([signedBlock, events, validators]) => + createSignedBlockExtended(api.registry, signedBlock, events, validators) ), catchError((): Observable => // where rpc.chain.getHeader throws, we will land here - it can happen that diff --git a/packages/api-derive/src/chain/getHeader.ts b/packages/api-derive/src/chain/getHeader.ts index 9bd705e6e039..c53420954ead 100644 --- a/packages/api-derive/src/chain/getHeader.ts +++ b/packages/api-derive/src/chain/getHeader.ts @@ -6,17 +6,11 @@ import type { ApiInterfaceRx } from '@polkadot/api/types'; import type { AccountId } from '@polkadot/types/interfaces'; import type { HeaderExtended } from '../type/types'; -import { catchError, map, of, switchMap } from 'rxjs'; +import { catchError, combineLatest, map, of, switchMap } from 'rxjs'; import { createHeaderExtended } from '../type'; import { memo } from '../util'; -function _getValidators (api: ApiInterfaceRx, hash: Uint8Array | string): Observable { - return api.query.session - ? api.query.session.validators.at(hash) - : of([] as AccountId[]); -} - /** * @name getHeader * @param {( Uint8Array | string )} hash - A block hash as U8 array or string. @@ -33,14 +27,17 @@ function _getValidators (api: ApiInterfaceRx, hash: Uint8Array | string): Observ */ export function getHeader (instanceId: string, api: ApiInterfaceRx): (hash: Uint8Array | string) => Observable { return memo(instanceId, (hash: Uint8Array | string): Observable => - // we get the header first, setting up the registry - api.rpc.chain.getHeader(hash).pipe( - switchMap((header) => - _getValidators(api, hash).pipe( - map((validators) => - createHeaderExtended(header.registry, header, validators) - ) - ) + api.queryAt(hash).pipe( + switchMap((queryAt) => + combineLatest([ + api.rpc.chain.getHeader(hash), + queryAt.session + ? queryAt.session.validators() + : of([] as AccountId[]) + ]) + ), + map(([header, validators]) => + createHeaderExtended(header.registry, header, validators) ), catchError((): Observable => // where rpc.chain.getHeader throws, we will land here - it can happen that diff --git a/packages/api-derive/src/chain/subscribeNewBlocks.ts b/packages/api-derive/src/chain/subscribeNewBlocks.ts index 6d63251581e4..7a374a7fc64d 100644 --- a/packages/api-derive/src/chain/subscribeNewBlocks.ts +++ b/packages/api-derive/src/chain/subscribeNewBlocks.ts @@ -5,7 +5,7 @@ import type { Observable } from 'rxjs'; import type { ApiInterfaceRx } from '@polkadot/api/types'; import type { SignedBlockExtended } from '../type/types'; -import { map, switchMap } from 'rxjs'; +import { combineLatest, map, of, switchMap } from 'rxjs'; import { createSignedBlockExtended } from '../type'; import { memo } from '../util'; @@ -21,16 +21,19 @@ export function subscribeNewBlocks (instanceId: string, api: ApiInterfaceRx): () const blockHash = header.createdAtHash || header.hash; // we get the block first, setting up the registry - return api.rpc.chain.getBlock(blockHash).pipe( - switchMap((block) => - api.query.system.events.at(blockHash).pipe( - map((events) => - createSignedBlockExtended(block.registry, block, events, header.validators) - ) - ) + return api.queryAt(blockHash).pipe( + switchMap((queryAt) => + combineLatest([ + of(header), + api.rpc.chain.getBlock(blockHash), + queryAt.system.events() + ]) ) ); - }) + }), + map(([header, block, events]) => + createSignedBlockExtended(block.registry, block, events, header.validators) + ) ) ); } diff --git a/packages/api-derive/src/tx/events.ts b/packages/api-derive/src/tx/events.ts index d64df5b4269c..98a1d7e61e23 100644 --- a/packages/api-derive/src/tx/events.ts +++ b/packages/api-derive/src/tx/events.ts @@ -5,7 +5,7 @@ import type { Observable } from 'rxjs'; import type { ApiInterfaceRx } from '@polkadot/api/types'; import type { EventRecord, Hash, SignedBlock } from '@polkadot/types/interfaces'; -import { map, switchMap } from 'rxjs'; +import { combineLatest, map, switchMap } from 'rxjs'; import { memo } from '../util'; @@ -15,17 +15,15 @@ interface Result { } export function events (instanceId: string, api: ApiInterfaceRx): (at: Hash) => Observable { - return memo(instanceId, (at: Hash) => - // we get the block first, setting up the registry - api.rpc.chain.getBlock(at).pipe( - switchMap((block) => - api.query.system.events.at(at).pipe( - map((events): Result => ({ - block, - events - })) - ) - ) + return memo(instanceId, (blockHash: Hash) => + api.queryAt(blockHash).pipe( + switchMap((queryAt) => + combineLatest([ + api.rpc.chain.getBlock(blockHash), + queryAt.system.events() + ]) + ), + map(([block, events]): Result => ({ block, events })) ) ); } diff --git a/packages/api/src/base/Decorate.ts b/packages/api/src/base/Decorate.ts index 799419a29833..775fffcf56c6 100644 --- a/packages/api/src/base/Decorate.ts +++ b/packages/api/src/base/Decorate.ts @@ -138,6 +138,8 @@ export abstract class Decorate extends Events { this.#instanceId = `${++instanceCounter}`; this.#registry = options.source?.registry || options.registry || new TypeRegistry(); + this._rx.queryAt = (blockHash: Uint8Array | string) => + from(this.at(blockHash)).pipe(map((a) => a.rx.query)); this._rx.registry = this.#registry; const thisProvider = options.source @@ -453,8 +455,7 @@ export abstract class Decorate extends Events { decorated.at = decorateMethod((blockHash: Hash, ...args: unknown[]): Observable => getQueryAt(blockHash).pipe( - switchMap((q) => q(...args)) - )); + switchMap((q) => q(...args)))); decorated.hash = decorateMethod((...args: unknown[]): Observable => this._rpcCore.state.getStorageHash(getArgs(args))); @@ -478,10 +479,7 @@ export abstract class Decorate extends Events { decorated.sizeAt = decorateMethod((blockHash: Hash | Uint8Array | string, ...args: unknown[]): Observable => getQueryAt(blockHash).pipe( switchMap((q) => - this._rpcCore.state.getStorageSize(getArgs(args, q.creator.meta.registry), blockHash) - ) - ) - ); + this._rpcCore.state.getStorageSize(getArgs(args, q.creator.meta.registry), blockHash)))); // .keys() & .entries() only available on map types if (creator.iterKey && creator.meta.type.isMap) { diff --git a/packages/api/src/checkTypes.manual.ts b/packages/api/src/checkTypes.manual.ts index e8ae4da50ace..378270ba23f4 100644 --- a/packages/api/src/checkTypes.manual.ts +++ b/packages/api/src/checkTypes.manual.ts @@ -80,11 +80,11 @@ async function query (api: ApiPromise, pairs: TestKeyringMap): Promise { const bal2 = await api.query.balances.totalIssuance('WRONG_ARG'); // bal2 is Codec (wrong args) const override = await api.query.balances.totalIssuance
(); // override is still available const oldBal = await api.query.balances.totalIssuance.at('abcd'); - // It's hard to correctly type .multi. Expected: `Balance[]`, actual: Codec[]. - // In the meantime, we can case with `` (this is not available on recent chains) - const multi = await api.query.balances.freeBalance.multi([pairs.alice.address, pairs.bob.address]); + // For older queries we can cast with `` (newer chain have multi typed) + const multia = await api.query.balances.freeBalance.multi([pairs.alice.address, pairs.bob.address]); + const multib = await api.query.system.account.multi([pairs.alice.address, pairs.bob.address]); - console.log('query types:', bar, bal, bal2, override, oldBal, multi); + console.log('query types:', bar, bal, bal2, override, oldBal, multia, multib); // check multi for unsub const multiUnsub = await api.queryMulti([ diff --git a/packages/api/src/types/index.ts b/packages/api/src/types/index.ts index bc802c4dc831..eb9f010ae7b4 100644 --- a/packages/api/src/types/index.ts +++ b/packages/api/src/types/index.ts @@ -4,6 +4,7 @@ // Augment the modules import '@polkadot/api/augment'; +import type { Observable } from 'rxjs'; import type { DeriveCustom, ExactDerive } from '@polkadot/api-derive'; import type { RpcInterface } from '@polkadot/rpc-core/types'; import type { ProviderInterface, ProviderInterfaceEmitted } from '@polkadot/rpc-provider/types'; @@ -94,6 +95,7 @@ export interface ApiInterfaceRx { runtimeMetadata: Metadata; runtimeVersion: RuntimeVersion; query: QueryableStorage<'rxjs'>; + queryAt: (blockHash: Uint8Array | string) => Observable>; queryMulti: QueryableStorageMulti<'rxjs'>; rpc: DecoratedRpc<'rxjs', RpcInterface>; tx: SubmittableExtrinsics<'rxjs'>; diff --git a/packages/api/src/types/storage.ts b/packages/api/src/types/storage.ts index c0ee653688a1..9c70d3bb8faf 100644 --- a/packages/api/src/types/storage.ts +++ b/packages/api/src/types/storage.ts @@ -59,14 +59,28 @@ export interface QueryableStorageAt extends AugmentedQ } export interface StorageEntryBase extends StorageEntryBaseAt { + /** + * @deprecated Use api.at() + */ at: >(hash: Hash | Uint8Array | string, ...args: Parameters) => PromiseOrObs; creator: StorageEntry; + /** + * @deprecated Use api.at() + */ entriesAt: , K extends AnyTuple = A>(hash: Hash | Uint8Array | string, ...args: DropLast>) => PromiseOrObs, T][]>; entriesPaged: , K extends AnyTuple = A>(opts: PaginationOptions[0]>) => PromiseOrObs, T][]>; + /** + * @deprecated Use api.at() + */ keysAt: (hash: Hash | Uint8Array | string, ...args: DropLast>) => PromiseOrObs[]>; keysPaged: (opts: PaginationOptions[0]>) => PromiseOrObs[]>; - // @deprecated The underlying RPC this been marked unsafe and is generally not exposed + /** + * @deprecated The underlying RPC this been marked unsafe and is generally not exposed + */ range: >([from, to]: [Hash | Uint8Array | string, Hash | Uint8Array | string | undefined] | [Hash | Uint8Array | string], ...args: Parameters) => PromiseOrObs; + /** + * @deprecated Use api.at() + */ sizeAt: (hash: Hash | Uint8Array | string, ...args: Parameters) => PromiseOrObs; multi: ApiType extends 'rxjs' ? StorageEntryObservableMulti>