Adjust blockNumber extract (w & w/o Compact) (#5066)

* Adjust blockNumber extract (w & w/o Compact)

* Adjust unused test file
This commit is contained in:
Jaco
2022-07-13 16:59:39 +03:00
committed by GitHub
parent 998f88be05
commit 60384ad850
7 changed files with 31 additions and 13 deletions
+3 -3
View File
@@ -3,7 +3,7 @@
import type { DeriveApi } from '../types';
import { unwrapBlockNumber } from './util';
import { createBlockNumberDerive } from './util';
/**
* @name bestNumber
@@ -17,7 +17,7 @@ import { unwrapBlockNumber } from './util';
* });
* ```
*/
export const bestNumber = unwrapBlockNumber(
export const bestNumber = createBlockNumberDerive(
(api: DeriveApi) =>
api.derive.chain.subscribeNewHeads()
api.rpc.chain.subscribeNewHeads()
);
@@ -3,7 +3,7 @@
import type { DeriveApi } from '../types';
import { unwrapBlockNumber } from './util';
import { createBlockNumberDerive } from './util';
/**
* @name bestNumberFinalized
@@ -18,7 +18,7 @@ import { unwrapBlockNumber } from './util';
* });
* ```
*/
export const bestNumberFinalized = unwrapBlockNumber(
export const bestNumberFinalized = createBlockNumberDerive(
(api: DeriveApi) =>
api.rpc.chain.subscribeFinalizedHeads()
);
+7 -5
View File
@@ -10,17 +10,19 @@ import type { DeriveApi } from '../types';
import { combineLatest, map, of } from 'rxjs';
import { memo } from '../util';
import { memo, unwrapBlockNumber } from '../util';
// re-export these - since these needs to be resolvable from api-derive, i.e. without this
// we would emit code with ../<somewhere>/src embedded in the *.d.ts files
export type { BlockNumber } from '@polkadot/types/interfaces';
export function unwrapBlockNumber <T extends { number: Compact<BlockNumber> }> (fn: (api: DeriveApi) => Observable<T>): (instanceId: string, api: DeriveApi) => () => Observable<BlockNumber> {
export function createBlockNumberDerive <T extends { number: Compact<BlockNumber> | BlockNumber }> (fn: (api: DeriveApi) => Observable<T>): (instanceId: string, api: DeriveApi) => () => Observable<BlockNumber> {
return (instanceId: string, api: DeriveApi) =>
memo(instanceId, () => fn(api).pipe(
map((r) => r.number.unwrap())
));
memo(instanceId, () =>
fn(api).pipe(
map(unwrapBlockNumber)
)
);
}
export function getAuthorDetails (header: Header, queryAt: QueryableStorage<'rxjs'>): Observable<[Header, Vec<AccountId> | null, AccountId | null]> {
+2 -1
View File
@@ -10,6 +10,7 @@ import { combineLatest, map, of, switchMap } from 'rxjs';
import { isNumber, isUndefined } from '@polkadot/util';
import { unwrapBlockNumber } from '../util';
import { FALLBACK_MAX_HASH_COUNT, FALLBACK_PERIOD, MAX_FINALITY_LAG, MORTAL_PERIOD } from './constants';
interface Result {
@@ -51,7 +52,7 @@ function signingHeader (api: DeriveApi): Observable<Header> {
]).pipe(
map(([current, finalized]) =>
// determine the hash to use, current when lag > max, else finalized
current.number.unwrap().sub(finalized.number.unwrap()).gt(MAX_FINALITY_LAG)
unwrapBlockNumber(current).sub(unwrapBlockNumber(finalized)).gt(MAX_FINALITY_LAG)
? current
: finalized
)
@@ -0,0 +1,15 @@
// Copyright 2017-2022 @polkadot/api-derive authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Compact } from '@polkadot/types';
import type { BlockNumber } from '@polkadot/types/interfaces';
import { isCompact } from '@polkadot/util';
type CompatHeader = { number: Compact<BlockNumber> | BlockNumber };
export function unwrapBlockNumber (hdr: CompatHeader): BlockNumber {
return isCompact<BlockNumber>(hdr.number)
? hdr.number.unwrap()
: hdr.number;
}
+1
View File
@@ -4,6 +4,7 @@
export { drr, memo } from '@polkadot/rpc-core';
export * from './approvalFlagsToBools';
export * from './blockNumber';
export * from './cache';
export * from './cacheImpl';
export * from './first';
@@ -7,7 +7,6 @@ import type { Enum, Struct } from '@polkadot/types';
import type { Option } from '@polkadot/types/codec';
import type { AccountId, AccountIndex, AccountInfo, Address, Balance, BlockNumber, Index } from '@polkadot/types/interfaces';
import { jest } from '@jest/globals';
import { combineLatest, map, of, switchMap } from 'rxjs';
import { ApiPromise, WsProvider } from '@polkadot/api';
@@ -223,7 +222,7 @@ function createApi (): Promise<ApiPromise> {
jest.setTimeout(30000);
const provider = new WsProvider('wss://tge.equilibrium.io');
return new ApiPromise({ provider, typesBundle }).isReady;
return ApiPromise.create({ provider, typesBundle });
}
describe.skip('equilibrium api', () => {