Cleanups for bestNumber derives (#4318)
* Cleanups for bestNumber derives * Unused imports * Re-exports of types for *.d.ts outputs
This commit is contained in:
@@ -12,28 +12,25 @@ import { decodeAddress } from '@polkadot/util-crypto';
|
||||
|
||||
import { memo } from '../util';
|
||||
|
||||
function retrieve (api: ApiInterfaceRx, address: Address | AccountId | AccountIndex | string | null | undefined): Observable<AccountId> {
|
||||
const decoded = isU8a(address)
|
||||
? address
|
||||
: decodeAddress((address || '').toString());
|
||||
|
||||
if (decoded.length > 8) {
|
||||
return of(api.registry.createType('AccountId', decoded));
|
||||
}
|
||||
|
||||
const accountIndex = api.registry.createType('AccountIndex', decoded);
|
||||
|
||||
return api.derive.accounts.indexToId(accountIndex.toString()).pipe(
|
||||
map((accountId) => assertReturn(accountId, 'Unable to retrieve accountId'))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name accountId
|
||||
* @param {(Address | AccountId | AccountIndex | string | null)} address - An accounts address in various formats.
|
||||
* @description An [[AccountId]]
|
||||
*/
|
||||
export function accountId (instanceId: string, api: ApiInterfaceRx): (address?: Address | AccountId | AccountIndex | string | null) => Observable<AccountId> {
|
||||
return memo(instanceId, (address?: Address | AccountId | AccountIndex | string | null): Observable<AccountId> =>
|
||||
retrieve(api, address));
|
||||
return memo(instanceId, (address?: Address | AccountId | AccountIndex | string | null): Observable<AccountId> => {
|
||||
const decoded = isU8a(address)
|
||||
? address
|
||||
: decodeAddress((address || '').toString());
|
||||
|
||||
if (decoded.length > 8) {
|
||||
return of(api.registry.createType('AccountId', decoded));
|
||||
}
|
||||
|
||||
const accountIndex = api.registry.createType('AccountIndex', decoded);
|
||||
|
||||
return api.derive.accounts.indexToId(accountIndex.toString()).pipe(
|
||||
map((a) => assertReturn(a, 'Unable to retrieve accountId'))
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -13,31 +13,6 @@ import { decodeAddress } from '@polkadot/util-crypto';
|
||||
|
||||
import { memo } from '../util';
|
||||
|
||||
function retrieve (api: ApiInterfaceRx, address: Address | AccountId | AccountIndex | Uint8Array | string | null | undefined): Observable<AccountIdAndIndex> {
|
||||
try {
|
||||
// yes, this can fail, don't care too much, catch will catch it
|
||||
const decoded = isU8a(address)
|
||||
? address
|
||||
: decodeAddress((address || '').toString());
|
||||
|
||||
if (decoded.length > 8) {
|
||||
const accountId = api.registry.createType('AccountId', decoded);
|
||||
|
||||
return api.derive.accounts.idToIndex(accountId).pipe(
|
||||
map((accountIndex): AccountIdAndIndex => [accountId, accountIndex])
|
||||
);
|
||||
}
|
||||
|
||||
const accountIndex = api.registry.createType('AccountIndex', decoded);
|
||||
|
||||
return api.derive.accounts.indexToId(accountIndex.toString()).pipe(
|
||||
map((accountId): AccountIdAndIndex => [accountId, accountIndex])
|
||||
);
|
||||
} catch (error) {
|
||||
return of([undefined, undefined]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @name idAndIndex
|
||||
* @param {(Address | AccountId | AccountIndex | Uint8Array | string | null)} address - An accounts address in various formats.
|
||||
@@ -52,6 +27,28 @@ function retrieve (api: ApiInterfaceRx, address: Address | AccountId | AccountIn
|
||||
* ```
|
||||
*/
|
||||
export function idAndIndex (instanceId: string, api: ApiInterfaceRx): (address?: Address | AccountId | AccountIndex | Uint8Array | string | null) => Observable<AccountIdAndIndex> {
|
||||
return memo(instanceId, (address?: Address | AccountId | AccountIndex | Uint8Array | string | null): Observable<AccountIdAndIndex> =>
|
||||
retrieve(api, address));
|
||||
return memo(instanceId, (address?: Address | AccountId | AccountIndex | Uint8Array | string | null): Observable<AccountIdAndIndex> => {
|
||||
try {
|
||||
// yes, this can fail, don't care too much, catch will catch it
|
||||
const decoded = isU8a(address)
|
||||
? address
|
||||
: decodeAddress((address || '').toString());
|
||||
|
||||
if (decoded.length > 8) {
|
||||
const accountId = api.registry.createType('AccountId', decoded);
|
||||
|
||||
return api.derive.accounts.idToIndex(accountId).pipe(
|
||||
map((accountIndex): AccountIdAndIndex => [accountId, accountIndex])
|
||||
);
|
||||
}
|
||||
|
||||
const accountIndex = api.registry.createType('AccountIndex', decoded);
|
||||
|
||||
return api.derive.accounts.indexToId(accountIndex.toString()).pipe(
|
||||
map((accountId): AccountIdAndIndex => [accountId, accountIndex])
|
||||
);
|
||||
} catch (error) {
|
||||
return of([undefined, undefined]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Observable } from 'rxjs';
|
||||
import type { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import type { BlockNumber } from '@polkadot/types/interfaces';
|
||||
|
||||
import { unwrapNumber } from './util';
|
||||
import { unwrapBlockNumber } from './util';
|
||||
|
||||
/**
|
||||
* @name bestNumber
|
||||
@@ -19,6 +15,4 @@ import { unwrapNumber } from './util';
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
export function bestNumber (instanceId: string, api: ApiInterfaceRx): () => Observable<BlockNumber> {
|
||||
return unwrapNumber(instanceId, api.derive.chain.subscribeNewHeads);
|
||||
}
|
||||
export const bestNumber = unwrapBlockNumber((api) => api.derive.chain.subscribeNewHeads());
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Observable } from 'rxjs';
|
||||
import type { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import type { BlockNumber } from '@polkadot/types/interfaces';
|
||||
|
||||
import { unwrapNumber } from './util';
|
||||
import { unwrapBlockNumber } from './util';
|
||||
|
||||
/**
|
||||
* @name bestNumberFinalized
|
||||
@@ -20,6 +16,4 @@ import { unwrapNumber } from './util';
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
export function bestNumberFinalized (instanceId: string, api: ApiInterfaceRx): () => Observable<BlockNumber> {
|
||||
return unwrapNumber(instanceId, api.rpc.chain.subscribeFinalizedHeads);
|
||||
}
|
||||
export const bestNumberFinalized = unwrapBlockNumber((api) => api.rpc.chain.subscribeFinalizedHeads());
|
||||
|
||||
@@ -2,13 +2,22 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Observable } from 'rxjs';
|
||||
import type { Option } from '@polkadot/types';
|
||||
import type { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import type { Compact } from '@polkadot/types';
|
||||
import type { BlockNumber } from '@polkadot/types/interfaces';
|
||||
|
||||
import { map } from 'rxjs';
|
||||
|
||||
import { memo } from '../util';
|
||||
|
||||
export function unwrapNumber <T extends { number: Option<BlockNumber> }> (instanceId: string, fn: () => Observable<T>): () => Observable<BlockNumber> {
|
||||
return memo(instanceId, () => fn().pipe(map((r) => r.number.unwrap())));
|
||||
// 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 { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
export type { BlockNumber } from '@polkadot/types/interfaces';
|
||||
|
||||
export function unwrapBlockNumber <T extends { number: Compact<BlockNumber> }> (fn: (api: ApiInterfaceRx) => Observable<T>): (instanceId: string, api: ApiInterfaceRx) => () => Observable<BlockNumber> {
|
||||
return (instanceId: string, api: ApiInterfaceRx) =>
|
||||
memo(instanceId, () => fn(api).pipe(
|
||||
map((r) => r.number.unwrap())
|
||||
));
|
||||
}
|
||||
|
||||
@@ -4,6 +4,9 @@
|
||||
import type { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import type { Collective } from './types';
|
||||
|
||||
// We are re-exporting these from here to ensure that *.d.ts generation is correct
|
||||
export type { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
|
||||
export function getInstance (api: ApiInterfaceRx, section: string): string {
|
||||
const instances = api.registry.getModuleInstances(api.runtimeVersion.specName.toString(), section);
|
||||
|
||||
|
||||
@@ -13,6 +13,10 @@ import { isFunction } from '@polkadot/util';
|
||||
import { memo } from '../util';
|
||||
import { withSection } from './helpers';
|
||||
|
||||
// We are re-exporting these from here to ensure that *.d.ts generation is correct
|
||||
export type { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
export type { AccountId } from '@polkadot/types/interfaces';
|
||||
|
||||
export function members (_section: Collective): (instanceId: string, api: ApiInterfaceRx) => () => Observable<AccountId[]> {
|
||||
return withSection(_section, (section, instanceId, api) =>
|
||||
memo(instanceId, (): Observable<AccountId[]> =>
|
||||
|
||||
@@ -14,6 +14,10 @@ import { isFunction } from '@polkadot/util';
|
||||
import { memo } from '../util';
|
||||
import { withSection } from './helpers';
|
||||
|
||||
// We are re-exporting these from here to ensure that *.d.ts generation is correct
|
||||
export type { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
export type { AccountId } from '@polkadot/types/interfaces';
|
||||
|
||||
export function prime (_section: Collective): (instanceId: string, api: ApiInterfaceRx) => () => Observable<AccountId | null> {
|
||||
return withSection(_section, (section, instanceId, api) =>
|
||||
memo(instanceId, (): Observable<AccountId | null> =>
|
||||
|
||||
@@ -15,6 +15,11 @@ import { isFunction } from '@polkadot/util';
|
||||
import { memo } from '../util';
|
||||
import { withSection } from './helpers';
|
||||
|
||||
// We are re-exporting these from here to ensure that *.d.ts generation is correct
|
||||
export type { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
export type { Option, u32 } from '@polkadot/types';
|
||||
export type { Hash, Proposal, Votes } from '@polkadot/types/interfaces';
|
||||
|
||||
type Result = [(Hash | Uint8Array | string)[], (Option<Proposal> | null)[], Option<Votes>[]];
|
||||
|
||||
function parse (api: ApiInterfaceRx, [hashes, proposals, votes]: Result): DeriveCollectiveProposal[] {
|
||||
|
||||
@@ -1,19 +1,6 @@
|
||||
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
|
||||
// we need these type imports to be available
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
import type { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
import type { u32 } from '@polkadot/types';
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
import type { AccountId, Hash } from '@polkadot/types/interfaces';
|
||||
|
||||
import { hasProposals as collectiveHasProposals, members as collectiveMembers, prime as collectivePrime, proposal as collectiveProposal, proposalCount as collectiveProposalCount, proposalHashes as collectiveProposalHashes, proposals as collectiveProposals } from '../collective';
|
||||
|
||||
export * from './votes';
|
||||
|
||||
@@ -20,7 +20,6 @@ type Depositors = DepositorsNew | DepositorsOld;
|
||||
type Proposals = ITuple<[PropIndex, Hash, AccountId]>[];
|
||||
type Result = [Proposals, (DeriveProposalImage | undefined)[], Depositors[]];
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
function isNewDepositors (depositors: ITuple<[Vec<AccountId>, Balance]> | ITuple<[Balance, Vec<AccountId>]>): depositors is ITuple<[Vec<AccountId>, Balance]> {
|
||||
// Detect balance...
|
||||
// eslint-disable-next-line @typescript-eslint/unbound-method
|
||||
|
||||
@@ -1,19 +1,6 @@
|
||||
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
|
||||
// we need these type imports to be available
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
import type { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
import type { u32 } from '@polkadot/types';
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
import type { AccountId, Hash } from '@polkadot/types/interfaces';
|
||||
|
||||
import { hasProposals as collectiveHasProposals, members as collectiveMembers, prime as collectivePrime, proposal as collectiveProposal, proposalCount as collectiveProposalCount, proposalHashes as collectiveProposalHashes, proposals as collectiveProposals } from '../collective';
|
||||
|
||||
export const members = collectiveMembers('membership');
|
||||
|
||||
@@ -1,19 +1,6 @@
|
||||
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
|
||||
// we need these type imports to be available
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
import type { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
import type { u32 } from '@polkadot/types';
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
import type { AccountId, Hash } from '@polkadot/types/interfaces';
|
||||
|
||||
import { hasProposals as collectiveHasProposals, members as collectiveMembers, prime as collectivePrime, proposal as collectiveProposal, proposalCount as collectiveProposalCount, proposalHashes as collectiveProposalHashes, proposals as collectiveProposals } from '../collective';
|
||||
|
||||
export const members = collectiveMembers('technicalCommittee');
|
||||
|
||||
Reference in New Issue
Block a user