Derive with optional democracy (#1657)
* Derive with optional democracy * Cleanup optionals * Multi for preimage retrievals * Convert indexes to hex before lookup
This commit is contained in:
@@ -14,62 +14,62 @@ import { Bytes, Option, Vec, createType } from '@polkadot/types';
|
|||||||
import { memo } from '../util';
|
import { memo } from '../util';
|
||||||
|
|
||||||
export type PreImage = Option<ITuple<[Bytes, AccountId, Balance, BlockNumber]>>;
|
export type PreImage = Option<ITuple<[Bytes, AccountId, Balance, BlockNumber]>>;
|
||||||
|
|
||||||
type Depositors = Option<ITuple<[Balance, Vec<AccountId>]>>;
|
type Depositors = Option<ITuple<[Balance, Vec<AccountId>]>>;
|
||||||
|
type Proposals = Vec<ITuple<[PropIndex, Hash, AccountId]>>;
|
||||||
|
|
||||||
|
interface Result {
|
||||||
|
depositors: Depositors[];
|
||||||
|
preimages: PreImage[];
|
||||||
|
proposals: Proposals;
|
||||||
|
}
|
||||||
|
|
||||||
|
function parse (api: ApiInterfaceRx, { depositors, proposals, preimages }: Result): DeriveProposal[] {
|
||||||
|
return proposals
|
||||||
|
.filter(([, , proposer], index): boolean =>
|
||||||
|
!!(preimages[index]?.isSome) && !!(depositors[index]?.isSome) && !proposer.isEmpty
|
||||||
|
)
|
||||||
|
.map(([propIndex, hash, proposer], index): DeriveProposal => {
|
||||||
|
const preimage = preimages[index].unwrapOr(null);
|
||||||
|
const depositor = depositors[index].unwrap();
|
||||||
|
|
||||||
|
return {
|
||||||
|
balance: depositor[0],
|
||||||
|
hash,
|
||||||
|
index: propIndex,
|
||||||
|
preimage: preimage
|
||||||
|
? {
|
||||||
|
at: preimage[3],
|
||||||
|
balance: preimage[2],
|
||||||
|
proposer: preimage[1]
|
||||||
|
}
|
||||||
|
: undefined,
|
||||||
|
proposal: preimage
|
||||||
|
? createType(api.registry, 'Proposal', preimage[0].toU8a(true))
|
||||||
|
: undefined,
|
||||||
|
proposer,
|
||||||
|
seconds: depositor[1]
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export function proposals (api: ApiInterfaceRx): () => Observable<DeriveProposal[]> {
|
export function proposals (api: ApiInterfaceRx): () => Observable<DeriveProposal[]> {
|
||||||
return memo((): Observable<DeriveProposal[]> =>
|
return memo((): Observable<DeriveProposal[]> =>
|
||||||
api.query.democracy
|
api.query.democracy?.publicProps
|
||||||
.publicProps<Vec<ITuple<[PropIndex, Hash, AccountId]>>>()
|
? api.query.democracy
|
||||||
.pipe(
|
.publicProps<Proposals>()
|
||||||
switchMap((proposals) =>
|
.pipe(
|
||||||
combineLatest([
|
switchMap((proposals) =>
|
||||||
of(proposals),
|
combineLatest([
|
||||||
combineLatest(
|
of(proposals),
|
||||||
...proposals.map(([, hash]) =>
|
api.query.democracy.preimages.multi<PreImage>(proposals.map(([, hash]): Hash => hash)),
|
||||||
api.query.democracy.preimages<PreImage>(hash)
|
api.query.democracy.depositOf.multi<Depositors>(proposals.map(([index]): PropIndex => index))
|
||||||
)
|
])
|
||||||
)
|
),
|
||||||
])
|
map(([proposals, preimages, depositors]): DeriveProposal[] =>
|
||||||
),
|
parse(api, { depositors, proposals, preimages })
|
||||||
switchMap(([proposals, preimages]) =>
|
)
|
||||||
combineLatest([
|
|
||||||
of(proposals),
|
|
||||||
of(preimages),
|
|
||||||
combineLatest(
|
|
||||||
...proposals.map(([index]) =>
|
|
||||||
api.query.democracy.depositOf<Depositors>(index)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
])
|
|
||||||
),
|
|
||||||
map(([proposals, _preImages, _depositors]): DeriveProposal[] =>
|
|
||||||
proposals
|
|
||||||
.filter(([, , proposer], index): boolean =>
|
|
||||||
!!(_preImages[index]?.isSome) && !!(_depositors[index]?.isSome) && !proposer.isEmpty
|
|
||||||
)
|
|
||||||
.map(([propIndex, hash, proposer], index): DeriveProposal => {
|
|
||||||
const preImage = _preImages[index].unwrapOr(null);
|
|
||||||
const depositors = _depositors[index].unwrap();
|
|
||||||
|
|
||||||
return {
|
|
||||||
balance: depositors[0],
|
|
||||||
hash,
|
|
||||||
index: propIndex,
|
|
||||||
preimage: preImage
|
|
||||||
? {
|
|
||||||
at: preImage[3],
|
|
||||||
balance: preImage[2],
|
|
||||||
proposer: preImage[1]
|
|
||||||
}
|
|
||||||
: undefined,
|
|
||||||
proposal: preImage
|
|
||||||
? createType(api.registry, 'Proposal', preImage[0].toU8a(true))
|
|
||||||
: undefined,
|
|
||||||
proposer,
|
|
||||||
seconds: depositors[1]
|
|
||||||
};
|
|
||||||
})
|
|
||||||
)
|
)
|
||||||
)
|
: of([] as DeriveProposal[])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,18 +14,21 @@ import { memo } from '../util';
|
|||||||
|
|
||||||
export function referendums (api: ApiInterfaceRx): () => Observable<DerivedReferendum[]> {
|
export function referendums (api: ApiInterfaceRx): () => Observable<DerivedReferendum[]> {
|
||||||
return memo((): Observable<DerivedReferendum[]> =>
|
return memo((): Observable<DerivedReferendum[]> =>
|
||||||
api.queryMulti<[ReferendumIndex, ReferendumIndex]>([
|
api.query.democracy?.nextTally
|
||||||
api.query.democracy.nextTally,
|
? api.queryMulti<[ReferendumIndex, ReferendumIndex]>([
|
||||||
api.query.democracy.referendumCount
|
api.query.democracy.nextTally,
|
||||||
]).pipe(
|
api.query.democracy.referendumCount
|
||||||
switchMap(([nextTally, referendumCount]): Observable<DerivedReferendum[]> =>
|
]).pipe(
|
||||||
referendumCount && nextTally && referendumCount.gt(nextTally) && referendumCount.gtn(0)
|
switchMap(([nextTally, referendumCount]): Observable<DerivedReferendum[]> =>
|
||||||
? api.derive.democracy.referendumInfos(
|
referendumCount?.gt(nextTally) && referendumCount?.gtn(0)
|
||||||
[...Array(referendumCount.sub(nextTally).toNumber())].map((_, i): BN =>
|
? api.derive.democracy.referendumInfos(
|
||||||
nextTally.addn(i)
|
[...Array(referendumCount.sub(nextTally).toNumber())].map((_, i): BN =>
|
||||||
|
nextTally.addn(i)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
: of([])
|
||||||
: of([])
|
)
|
||||||
)
|
)
|
||||||
));
|
: of([] as DerivedReferendum[])
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ function decorateEvents (registry: Registry, metadata: RegistryMetadata, metadat
|
|||||||
console.error(error);
|
console.error(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
metadataEvents[eventIndex.toString()] = class extends EventData {
|
metadataEvents[u8aToHex(eventIndex)] = class extends EventData {
|
||||||
constructor (registry: Registry, value: Uint8Array) {
|
constructor (registry: Registry, value: Uint8Array) {
|
||||||
super(registry, Types, value, typeDef, meta, sectionName, methodName);
|
super(registry, Types, value, typeDef, meta, sectionName, methodName);
|
||||||
}
|
}
|
||||||
@@ -55,7 +55,7 @@ function decorateExtrinsics (registry: Registry, metadata: RegistryMetadata, met
|
|||||||
// decorate the extrinsics
|
// decorate the extrinsics
|
||||||
Object.values(extrinsics).forEach((methods): void =>
|
Object.values(extrinsics).forEach((methods): void =>
|
||||||
Object.values(methods).forEach((method): void => {
|
Object.values(methods).forEach((method): void => {
|
||||||
metadataCalls[method.callIndex.toString()] = method;
|
metadataCalls[u8aToHex(method.callIndex)] = method;
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -88,15 +88,18 @@ export class TypeRegistry implements Registry {
|
|||||||
public findMetaCall (callIndex: Uint8Array): CallFunction {
|
public findMetaCall (callIndex: Uint8Array): CallFunction {
|
||||||
assert(Object.keys(this._metadataCalls).length > 0, 'Calling registry.findMetaCall before metadata has been attached.');
|
assert(Object.keys(this._metadataCalls).length > 0, 'Calling registry.findMetaCall before metadata has been attached.');
|
||||||
|
|
||||||
return this._metadataCalls[callIndex.toString()] || FN_UNKNOWN;
|
const hexIndex = u8aToHex(callIndex);
|
||||||
|
|
||||||
|
return this._metadataCalls[hexIndex] || FN_UNKNOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
public findMetaEvent (eventIndex: Uint8Array): Constructor<EventData> {
|
public findMetaEvent (eventIndex: Uint8Array): Constructor<EventData> {
|
||||||
assert(Object.keys(this._metadataEvents).length > 0, 'Calling registry.findMetaEvent before metadata has been attached.');
|
assert(Object.keys(this._metadataEvents).length > 0, 'Calling registry.findMetaEvent before metadata has been attached.');
|
||||||
|
|
||||||
const Event = this._metadataEvents[eventIndex.toString()];
|
const hexIndex = u8aToHex(eventIndex);
|
||||||
|
const Event = this._metadataEvents[hexIndex];
|
||||||
|
|
||||||
assert(!isUndefined(Event), `Unable to find Event with index ${u8aToHex(eventIndex)}`);
|
assert(!isUndefined(Event), `Unable to find Event with index ${hexIndex}`);
|
||||||
|
|
||||||
return Event;
|
return Event;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user