Update Substrate metadata & scheduler usage (#4371)

This commit is contained in:
Jaco
2021-12-21 08:33:23 +02:00
committed by GitHub
parent 34f6b7b560
commit c0e841031a
18 changed files with 4171 additions and 3234 deletions
+5 -5
View File
@@ -3,6 +3,7 @@
import type { Observable } from 'rxjs';
import type { AccountId, Address, Balance } from '@polkadot/types/interfaces';
import type { Option } from '@polkadot/types-codec';
import type { DeriveAccountFlags, DeriveApi } from '../types';
import { combineLatest, map, of } from 'rxjs';
@@ -14,19 +15,18 @@ type FlagsIntermediate = [
AccountId[],
AccountId[],
AccountId[],
AccountId | undefined
Option<AccountId> | AccountId | undefined
];
function parseFlags (address: AccountId | Address | string | null | undefined, [electionsMembers, councilMembers, technicalCommitteeMembers, societyMembers, sudoKey]: FlagsIntermediate): DeriveAccountFlags {
const addrStr = address && address.toString();
const isIncluded = (id: AccountId | Address | string) =>
address
? id.toString() === address.toString()
: false;
id.toString() === addrStr;
return {
isCouncil: (electionsMembers?.map(([id]) => id) || councilMembers || []).some(isIncluded),
isSociety: (societyMembers || []).some(isIncluded),
isSudo: sudoKey?.toString() === address?.toString(),
isSudo: sudoKey?.toString() === addrStr,
isTechCommittee: (technicalCommitteeMembers || []).some(isIncluded)
};
}
@@ -3,8 +3,8 @@
import type { Observable } from 'rxjs';
import type { Option, Vec } from '@polkadot/types';
import type { BlockNumber, Hash, ReferendumIndex } from '@polkadot/types/interfaces';
import type { PalletSchedulerScheduledV2 } from '@polkadot/types/lookup';
import type { BlockNumber, Call, Hash, ReferendumIndex, Scheduled } from '@polkadot/types/interfaces';
import type { FrameSupportScheduleMaybeHashed, PalletSchedulerScheduledV3 } from '@polkadot/types/lookup';
import type { ITuple } from '@polkadot/types/types';
import type { DeriveApi, DeriveDispatch, DeriveProposalImage } from '../types';
@@ -22,6 +22,11 @@ interface SchedulerInfo {
index: ReferendumIndex;
}
function isMaybeHashed (call: FrameSupportScheduleMaybeHashed | Call): call is FrameSupportScheduleMaybeHashed {
// check for enum
return (call as FrameSupportScheduleMaybeHashed).isBasic === false;
}
function queryQueue (api: DeriveApi): Observable<DeriveDispatch[]> {
return api.query.democracy.dispatchQueue<Vec<ITuple<[BlockNumber, Hash, ReferendumIndex]>>>().pipe(
switchMap((dispatches) =>
@@ -42,7 +47,7 @@ function queryQueue (api: DeriveApi): Observable<DeriveDispatch[]> {
);
}
function schedulerEntries (api: DeriveApi): Observable<[BlockNumber[], (Option<PalletSchedulerScheduledV2>[] | null)[]]> {
function schedulerEntries (api: DeriveApi): Observable<[BlockNumber[], (Option<PalletSchedulerScheduledV3 | Scheduled>[] | null)[]]> {
// We don't get entries, but rather we get the keys (triggered via finished referendums) and
// the subscribe to those keys - this means we pickup when the schedulers actually executes
// at a block, the entry for that block will become empty
@@ -81,7 +86,15 @@ function queryScheduler (api: DeriveApi): Observable<DeriveDispatch[]> {
if (scheduled.maybeId.isSome) {
const id = scheduled.maybeId.unwrap().toHex();
id.startsWith(DEMOCRACY_ID) && result.push({ at, imageHash: scheduled.call.args[0] as Hash, index: api.registry.createType('(u64, ReferendumIndex)', id)[1] });
if (id.startsWith(DEMOCRACY_ID)) {
const imageHash = isMaybeHashed(scheduled.call)
? scheduled.call.isHash
? scheduled.call.asHash
: scheduled.call.asValue.args[0] as Hash
: scheduled.call.args[0] as Hash;
result.push({ at, imageHash, index: api.registry.createType('(u64, ReferendumIndex)', id)[1] });
}
}
});
});
@@ -4,7 +4,6 @@
export * from './dispatchQueue';
export * from './locks';
export * from './nextExternal';
export * from './preimage';
export * from './preimages';
export * from './proposals';
export * from './referendumIds';
@@ -1,19 +0,0 @@
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Observable } from 'rxjs';
import type { Hash } from '@polkadot/types/interfaces';
import type { DeriveApi, DeriveProposalImage } from '../types';
import { map } from 'rxjs';
import { memo } from '../util';
import { parseImage } from './util';
export function preimage (instanceId: string, api: DeriveApi): (hash: Hash) => Observable<DeriveProposalImage | undefined> {
return memo(instanceId, (hash: Hash): Observable<DeriveProposalImage | undefined> =>
api.query.democracy.preimages(hash).pipe(
map((imageOpt) => parseImage(api, imageOpt))
)
);
}
+21 -8
View File
@@ -7,17 +7,30 @@ import type { DeriveApi, DeriveProposalImage } from '../types';
import { map, of } from 'rxjs';
import { memo } from '../util';
import { isFunction } from '@polkadot/util';
import { firstMemo, memo } from '../util';
import { parseImage } from './util';
export function preimages (instanceId: string, api: DeriveApi): (hashes: Hash[]) => Observable<(DeriveProposalImage | undefined)[]> {
return memo(instanceId, (hashes: Hash[]): Observable<(DeriveProposalImage | undefined)[]> =>
function getDemocracyImages (api: DeriveApi, hashes: (Hash | Uint8Array | string)[]): Observable<(DeriveProposalImage | undefined)[]> {
return api.query.democracy.preimages.multi(hashes).pipe(
map((images): (DeriveProposalImage | undefined)[] =>
images.map((imageOpt) => parseImage(api, imageOpt))
)
);
}
export function preimages (instanceId: string, api: DeriveApi): (hashes: (Hash | Uint8Array | string)[]) => Observable<(DeriveProposalImage | undefined)[]> {
return memo(instanceId, (hashes: (Hash | Uint8Array | string)[]): Observable<(DeriveProposalImage | undefined)[]> =>
hashes.length
? api.query.democracy.preimages.multi(hashes).pipe(
map((images): (DeriveProposalImage | undefined)[] =>
images.map((imageOpt) => parseImage(api, imageOpt))
)
)
? isFunction(api.query.democracy.preimages)
? getDemocracyImages(api, hashes)
: of([])
: of([])
);
}
export const preimage = firstMemo(
(api: DeriveApi, hash: Hash | Uint8Array | string) =>
api.derive.democracy.preimages([hash])
);