Skip to content

Commit 63a55d2

Browse files
authored
Cleanups for bestNumber derives (#4318)
* Cleanups for bestNumber derives * Unused imports * Re-exports of types for *.d.ts outputs
1 parent c32953b commit 63a55d2

File tree

14 files changed

+73
-105
lines changed

14 files changed

+73
-105
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ Changes:
66

77
- Add support for contracts V2 ABI metadata
88
- `CheckNonZeroSender` signed extension support
9-
- Generated TS Enum interfaces now expose the `type` (with allowed values)
109
- Remove old-style capabilities detection (unneeded with metadata v14+)
10+
- Generated TS Enum interfaces now expose the `type` (with allowed values)
11+
- Split local & package imports in generated TS interfaces
1112
- Add Kusama 9130 upgrade block
1213
- Internal maintainability cleanups
1314

packages/api-derive/src/accounts/accountId.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,25 @@ import { decodeAddress } from '@polkadot/util-crypto';
1212

1313
import { memo } from '../util';
1414

15-
function retrieve (api: ApiInterfaceRx, address: Address | AccountId | AccountIndex | string | null | undefined): Observable<AccountId> {
16-
const decoded = isU8a(address)
17-
? address
18-
: decodeAddress((address || '').toString());
19-
20-
if (decoded.length > 8) {
21-
return of(api.registry.createType('AccountId', decoded));
22-
}
23-
24-
const accountIndex = api.registry.createType('AccountIndex', decoded);
25-
26-
return api.derive.accounts.indexToId(accountIndex.toString()).pipe(
27-
map((accountId) => assertReturn(accountId, 'Unable to retrieve accountId'))
28-
);
29-
}
30-
3115
/**
3216
* @name accountId
3317
* @param {(Address | AccountId | AccountIndex | string | null)} address - An accounts address in various formats.
3418
* @description An [[AccountId]]
3519
*/
3620
export function accountId (instanceId: string, api: ApiInterfaceRx): (address?: Address | AccountId | AccountIndex | string | null) => Observable<AccountId> {
37-
return memo(instanceId, (address?: Address | AccountId | AccountIndex | string | null): Observable<AccountId> =>
38-
retrieve(api, address));
21+
return memo(instanceId, (address?: Address | AccountId | AccountIndex | string | null): Observable<AccountId> => {
22+
const decoded = isU8a(address)
23+
? address
24+
: decodeAddress((address || '').toString());
25+
26+
if (decoded.length > 8) {
27+
return of(api.registry.createType('AccountId', decoded));
28+
}
29+
30+
const accountIndex = api.registry.createType('AccountIndex', decoded);
31+
32+
return api.derive.accounts.indexToId(accountIndex.toString()).pipe(
33+
map((a) => assertReturn(a, 'Unable to retrieve accountId'))
34+
);
35+
});
3936
}

packages/api-derive/src/accounts/idAndIndex.ts

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,6 @@ import { decodeAddress } from '@polkadot/util-crypto';
1313

1414
import { memo } from '../util';
1515

16-
function retrieve (api: ApiInterfaceRx, address: Address | AccountId | AccountIndex | Uint8Array | string | null | undefined): Observable<AccountIdAndIndex> {
17-
try {
18-
// yes, this can fail, don't care too much, catch will catch it
19-
const decoded = isU8a(address)
20-
? address
21-
: decodeAddress((address || '').toString());
22-
23-
if (decoded.length > 8) {
24-
const accountId = api.registry.createType('AccountId', decoded);
25-
26-
return api.derive.accounts.idToIndex(accountId).pipe(
27-
map((accountIndex): AccountIdAndIndex => [accountId, accountIndex])
28-
);
29-
}
30-
31-
const accountIndex = api.registry.createType('AccountIndex', decoded);
32-
33-
return api.derive.accounts.indexToId(accountIndex.toString()).pipe(
34-
map((accountId): AccountIdAndIndex => [accountId, accountIndex])
35-
);
36-
} catch (error) {
37-
return of([undefined, undefined]);
38-
}
39-
}
40-
4116
/**
4217
* @name idAndIndex
4318
* @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
5227
* ```
5328
*/
5429
export function idAndIndex (instanceId: string, api: ApiInterfaceRx): (address?: Address | AccountId | AccountIndex | Uint8Array | string | null) => Observable<AccountIdAndIndex> {
55-
return memo(instanceId, (address?: Address | AccountId | AccountIndex | Uint8Array | string | null): Observable<AccountIdAndIndex> =>
56-
retrieve(api, address));
30+
return memo(instanceId, (address?: Address | AccountId | AccountIndex | Uint8Array | string | null): Observable<AccountIdAndIndex> => {
31+
try {
32+
// yes, this can fail, don't care too much, catch will catch it
33+
const decoded = isU8a(address)
34+
? address
35+
: decodeAddress((address || '').toString());
36+
37+
if (decoded.length > 8) {
38+
const accountId = api.registry.createType('AccountId', decoded);
39+
40+
return api.derive.accounts.idToIndex(accountId).pipe(
41+
map((accountIndex): AccountIdAndIndex => [accountId, accountIndex])
42+
);
43+
}
44+
45+
const accountIndex = api.registry.createType('AccountIndex', decoded);
46+
47+
return api.derive.accounts.indexToId(accountIndex.toString()).pipe(
48+
map((accountId): AccountIdAndIndex => [accountId, accountIndex])
49+
);
50+
} catch (error) {
51+
return of([undefined, undefined]);
52+
}
53+
});
5754
}
Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
import type { Observable } from 'rxjs';
5-
import type { ApiInterfaceRx } from '@polkadot/api/types';
6-
import type { BlockNumber } from '@polkadot/types/interfaces';
7-
8-
import { unwrapNumber } from './util';
4+
import { unwrapBlockNumber } from './util';
95

106
/**
117
* @name bestNumber
@@ -19,6 +15,4 @@ import { unwrapNumber } from './util';
1915
* });
2016
* ```
2117
*/
22-
export function bestNumber (instanceId: string, api: ApiInterfaceRx): () => Observable<BlockNumber> {
23-
return unwrapNumber(instanceId, api.derive.chain.subscribeNewHeads);
24-
}
18+
export const bestNumber = unwrapBlockNumber((api) => api.derive.chain.subscribeNewHeads());
Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
import type { Observable } from 'rxjs';
5-
import type { ApiInterfaceRx } from '@polkadot/api/types';
6-
import type { BlockNumber } from '@polkadot/types/interfaces';
7-
8-
import { unwrapNumber } from './util';
4+
import { unwrapBlockNumber } from './util';
95

106
/**
117
* @name bestNumberFinalized
@@ -20,6 +16,4 @@ import { unwrapNumber } from './util';
2016
* });
2117
* ```
2218
*/
23-
export function bestNumberFinalized (instanceId: string, api: ApiInterfaceRx): () => Observable<BlockNumber> {
24-
return unwrapNumber(instanceId, api.rpc.chain.subscribeFinalizedHeads);
25-
}
19+
export const bestNumberFinalized = unwrapBlockNumber((api) => api.rpc.chain.subscribeFinalizedHeads());

packages/api-derive/src/chain/util.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
import type { Observable } from 'rxjs';
5-
import type { Option } from '@polkadot/types';
5+
import type { ApiInterfaceRx } from '@polkadot/api/types';
6+
import type { Compact } from '@polkadot/types';
67
import type { BlockNumber } from '@polkadot/types/interfaces';
78

89
import { map } from 'rxjs';
910

1011
import { memo } from '../util';
1112

12-
export function unwrapNumber <T extends { number: Option<BlockNumber> }> (instanceId: string, fn: () => Observable<T>): () => Observable<BlockNumber> {
13-
return memo(instanceId, () => fn().pipe(map((r) => r.number.unwrap())));
13+
// re-export these - since these needs to be resolvable from api-derive, i.e. without this
14+
// we would emit code with ../<somewhere>/src embedded in the *.d.ts files
15+
export type { ApiInterfaceRx } from '@polkadot/api/types';
16+
export type { BlockNumber } from '@polkadot/types/interfaces';
17+
18+
export function unwrapBlockNumber <T extends { number: Compact<BlockNumber> }> (fn: (api: ApiInterfaceRx) => Observable<T>): (instanceId: string, api: ApiInterfaceRx) => () => Observable<BlockNumber> {
19+
return (instanceId: string, api: ApiInterfaceRx) =>
20+
memo(instanceId, () => fn(api).pipe(
21+
map((r) => r.number.unwrap())
22+
));
1423
}

packages/api-derive/src/collective/helpers.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import type { ApiInterfaceRx } from '@polkadot/api/types';
55
import type { Collective } from './types';
66

7+
// We are re-exporting these from here to ensure that *.d.ts generation is correct
8+
export type { ApiInterfaceRx } from '@polkadot/api/types';
9+
710
export function getInstance (api: ApiInterfaceRx, section: string): string {
811
const instances = api.registry.getModuleInstances(api.runtimeVersion.specName.toString(), section);
912

packages/api-derive/src/collective/members.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import { isFunction } from '@polkadot/util';
1313
import { memo } from '../util';
1414
import { withSection } from './helpers';
1515

16+
// We are re-exporting these from here to ensure that *.d.ts generation is correct
17+
export type { ApiInterfaceRx } from '@polkadot/api/types';
18+
export type { AccountId } from '@polkadot/types/interfaces';
19+
1620
export function members (_section: Collective): (instanceId: string, api: ApiInterfaceRx) => () => Observable<AccountId[]> {
1721
return withSection(_section, (section, instanceId, api) =>
1822
memo(instanceId, (): Observable<AccountId[]> =>

packages/api-derive/src/collective/prime.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import { isFunction } from '@polkadot/util';
1414
import { memo } from '../util';
1515
import { withSection } from './helpers';
1616

17+
// We are re-exporting these from here to ensure that *.d.ts generation is correct
18+
export type { ApiInterfaceRx } from '@polkadot/api/types';
19+
export type { AccountId } from '@polkadot/types/interfaces';
20+
1721
export function prime (_section: Collective): (instanceId: string, api: ApiInterfaceRx) => () => Observable<AccountId | null> {
1822
return withSection(_section, (section, instanceId, api) =>
1923
memo(instanceId, (): Observable<AccountId | null> =>

packages/api-derive/src/collective/proposals.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ import { isFunction } from '@polkadot/util';
1515
import { memo } from '../util';
1616
import { withSection } from './helpers';
1717

18+
// We are re-exporting these from here to ensure that *.d.ts generation is correct
19+
export type { ApiInterfaceRx } from '@polkadot/api/types';
20+
export type { Option, u32 } from '@polkadot/types';
21+
export type { Hash, Proposal, Votes } from '@polkadot/types/interfaces';
22+
1823
type Result = [(Hash | Uint8Array | string)[], (Option<Proposal> | null)[], Option<Votes>[]];
1924

2025
function parse (api: ApiInterfaceRx, [hashes, proposals, votes]: Result): DeriveCollectiveProposal[] {

0 commit comments

Comments
 (0)