Some minor cleanups (#1966)

* Some minor cleanups

* Small additions

* Cleanup ExtrinsicEra decoding

* Minor adjustments

* Remove support for metadata v0 parsing

* Adjust refCountDelay

* refCountDelay comments

* Adjust Metadata tests

* Metadata default creation

* Funky lines

* Cleanup as AccountIdAndIndex

* Some derive returns

* Try and cleanup info extraction
This commit is contained in:
Jaco Greeff
2020-02-27 15:01:20 +01:00
committed by GitHub
parent efd6c5331d
commit 4e756704ea
28 changed files with 194 additions and 247 deletions
@@ -26,17 +26,17 @@ function retrieve (api: ApiInterfaceRx, address: Address | AccountId | AccountIn
const accountId = createType(api.registry, 'AccountId', decoded);
return api.derive.accounts.idToIndex(accountId).pipe(
map((accountIndex): AccountIdAndIndex => [accountId, accountIndex] as AccountIdAndIndex)
map((accountIndex): AccountIdAndIndex => [accountId, accountIndex])
);
}
const accountIndex = createType(api.registry, 'AccountIndex', decoded);
return api.derive.accounts.indexToId(accountIndex).pipe(
map((accountId): AccountIdAndIndex => [accountId, accountIndex] as AccountIdAndIndex)
map((accountId): AccountIdAndIndex => [accountId, accountIndex])
);
} catch (error) {
return of([undefined, undefined] as AccountIdAndIndex);
return of([undefined, undefined]);
}
}
+2 -2
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { BlockNumber, Header } from '@polkadot/types/interfaces';
import { BlockNumber } from '@polkadot/types/interfaces';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
@@ -25,6 +25,6 @@ import { memo } from '../util';
export function bestNumber (api: ApiInterfaceRx): () => Observable<BlockNumber> {
return memo((): Observable<BlockNumber> =>
api.derive.chain.subscribeNewHeads().pipe(
map((header: Header): BlockNumber => header.number.unwrap())
map((header) => header.number.unwrap())
));
}
@@ -26,6 +26,6 @@ import { memo } from '../util';
export function bestNumberFinalized (api: ApiInterfaceRx): () => Observable<BlockNumber> {
return memo((): Observable<BlockNumber> =>
api.rpc.chain.subscribeFinalizedHeads().pipe(
map((header): BlockNumber => header.number.unwrap())
map((header) => header.number.unwrap())
));
}
+1 -4
View File
@@ -2,14 +2,11 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { AccountId } from '@polkadot/types/interfaces';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Observable, combineLatest, of } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { Vec } from '@polkadot/types';
import { HeaderExtended } from '../type';
import { memo } from '../util';
@@ -32,7 +29,7 @@ export function getHeader (api: ApiInterfaceRx): (hash: Uint8Array | string) =>
combineLatest([
api.rpc.chain.getHeader(hash),
api.query.session
? api.query.session.validators.at(hash) as Observable<Vec<AccountId>>
? api.query.session.validators.at(hash)
: of([])
]).pipe(
map(([header, validators]): HeaderExtended =>
+15 -13
View File
@@ -12,6 +12,20 @@ import { Option } from '@polkadot/types';
type Result = [Hash[], Option<Proposal>[], Option<Votes>[]];
function parse ([hashes, proposals, votes]: Result): DerivedCollectiveProposal[] {
return proposals
.map((proposalOpt, index): DerivedCollectiveProposal | null =>
proposalOpt.isSome
? {
hash: hashes[index],
proposal: proposalOpt.unwrap(),
votes: votes[index].unwrapOr(null)
}
: null
)
.filter((proposal): proposal is DerivedCollectiveProposal => !!proposal);
}
export function proposals (api: ApiInterfaceRx, section: 'council' | 'technicalCommittee'): () => Observable<DerivedCollectiveProposal[]> {
return (): Observable<DerivedCollectiveProposal[]> =>
api.query[section]
@@ -25,19 +39,7 @@ export function proposals (api: ApiInterfaceRx, section: 'council' | 'technicalC
])
: of([[], [], []])
),
map(([hashes, proposals, votes]: Result): DerivedCollectiveProposal[] =>
proposals
.map((proposalOpt, index): DerivedCollectiveProposal | null =>
proposalOpt.isSome
? {
hash: hashes[index],
proposal: proposalOpt.unwrap(),
votes: votes[index].unwrapOr(null)
}
: null
)
.filter((proposal): boolean => !!proposal) as DerivedCollectiveProposal[]
)
map(parse)
)
: of([] as DerivedCollectiveProposal[]);
}
@@ -44,7 +44,7 @@ function constructInfo (api: ApiInterfaceRx, index: BN | number, _info: Option<R
export function retrieveInfo (api: ApiInterfaceRx, index: BN | number, info: Option<ReferendumInfo>): Observable<DerivedReferendum | null> {
return ((
info?.isSome
? api.query.democracy.preimages<PreImage>(info.unwrap().proposalHash)
? api.query.democracy.preimages(info.unwrap().proposalHash)
: of(undefined)
) as Observable<PreImage | undefined>).pipe(
map((preimage?: PreImage): DerivedReferendum | null =>
@@ -29,7 +29,7 @@ export function referendumInfos (api: ApiInterfaceRx): (ids?: (BN | number)[]) =
)
),
map((infos): DerivedReferendum[] =>
infos.filter((referendum): boolean => !!referendum) as DerivedReferendum[]
infos.filter((referendum): referendum is DerivedReferendum => !!referendum)
)
));
}
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { AccountId, SessionIndex } from '@polkadot/types/interfaces';
import { AccountId } from '@polkadot/types/interfaces';
import { DerivedHeartbeats } from '../types';
import { of, Observable, combineLatest } from 'rxjs';
@@ -23,9 +23,9 @@ export function receivedHeartbeats (api: ApiInterfaceRx): () => Observable<Deriv
combineLatest([
of(validators),
api.query.imOnline.receivedHeartbeats.multi<Option<Bytes>>(
validators.map((_address, index): [SessionIndex, number] => [currentIndex, index])),
validators.map((_address, index) => [currentIndex, index])),
api.query.imOnline.authoredBlocks.multi<u32>(
validators.map((address): [SessionIndex, AccountId] => [currentIndex, address]))
validators.map((address) => [currentIndex, address]))
])
),
map(([validators, heartbeats, numBlocks]): DerivedHeartbeats =>
+1 -2
View File
@@ -3,7 +3,6 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { BlockNumber } from '@polkadot/types/interfaces';
import { DerivedSessionInfo } from '../types';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
@@ -14,6 +13,6 @@ import { memo } from '../util';
export function eraLength (api: ApiInterfaceRx): () => Observable<BlockNumber> {
return memo((): Observable<BlockNumber> =>
api.derive.session.info().pipe(
map(({ eraLength }: DerivedSessionInfo): BlockNumber => eraLength)
map((info) => info.eraLength)
));
}
@@ -3,7 +3,6 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { BlockNumber } from '@polkadot/types/interfaces';
import { DerivedSessionInfo } from '../types';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
@@ -14,6 +13,6 @@ import { memo } from '../util';
export function eraProgress (api: ApiInterfaceRx): () => Observable<BlockNumber> {
return memo((): Observable<BlockNumber> =>
api.derive.session.info().pipe(
map(({ eraProgress }: DerivedSessionInfo): BlockNumber => eraProgress)
map((info) => info.eraProgress)
));
}
@@ -3,7 +3,6 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { BlockNumber } from '@polkadot/types/interfaces';
import { DerivedSessionInfo } from '../types';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
@@ -14,6 +13,6 @@ import { memo } from '../util';
export function sessionProgress (api: ApiInterfaceRx): () => Observable<BlockNumber> {
return memo((): Observable<BlockNumber> =>
api.derive.session.info().pipe(
map(({ sessionProgress }: DerivedSessionInfo): BlockNumber => sessionProgress)
map((info) => info.sessionProgress)
));
}