Optimize society multi-derive (#3342)
* Optimize society multi-derive * isVoting flag * isDefenderVoter
This commit is contained in:
@@ -2,9 +2,7 @@
|
|||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
import type { ApiInterfaceRx } from '@polkadot/api/types';
|
import type { ApiInterfaceRx } from '@polkadot/api/types';
|
||||||
import type { bool, Option, Vec } from '@polkadot/types';
|
import type { AccountId } from '@polkadot/types/interfaces';
|
||||||
import type { AccountId, Balance, BlockNumber, SocietyVote, StrikeCount, VouchingStatus } from '@polkadot/types/interfaces';
|
|
||||||
import type { ITuple } from '@polkadot/types/types';
|
|
||||||
import type { Observable } from '@polkadot/x-rxjs';
|
import type { Observable } from '@polkadot/x-rxjs';
|
||||||
import type { DeriveSocietyMember } from '../types';
|
import type { DeriveSocietyMember } from '../types';
|
||||||
|
|
||||||
@@ -12,28 +10,13 @@ import { map } from '@polkadot/x-rxjs/operators';
|
|||||||
|
|
||||||
import { memo } from '../util';
|
import { memo } from '../util';
|
||||||
|
|
||||||
type Result = [Vec<ITuple<[BlockNumber, Balance]>>, StrikeCount, Option<SocietyVote>, bool, Option<VouchingStatus>];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Get the member info for a society
|
* @description Get the member info for a society
|
||||||
*/
|
*/
|
||||||
export function member (instanceId: string, api: ApiInterfaceRx): (accountId: AccountId) => Observable<DeriveSocietyMember> {
|
export function member (instanceId: string, api: ApiInterfaceRx): (accountId: AccountId) => Observable<DeriveSocietyMember> {
|
||||||
return memo(instanceId, (accountId: AccountId): Observable<DeriveSocietyMember> =>
|
return memo(instanceId, (accountId: AccountId): Observable<DeriveSocietyMember> =>
|
||||||
api.queryMulti<Result>([
|
api.derive.society._members([accountId]).pipe(
|
||||||
[api.query.society.payouts, accountId],
|
map(([result]) => result)
|
||||||
[api.query.society.strikes, accountId],
|
|
||||||
[api.query.society.defenderVotes, accountId],
|
|
||||||
[api.query.society.suspendedMembers, accountId],
|
|
||||||
[api.query.society.vouching, accountId]
|
|
||||||
]).pipe(
|
|
||||||
map(([payouts, strikes, defenderVotes, suspended, vouching]: Result): DeriveSocietyMember => ({
|
|
||||||
accountId,
|
|
||||||
isSuspended: suspended.isTrue,
|
|
||||||
payouts,
|
|
||||||
strikes,
|
|
||||||
vote: defenderVotes.unwrapOr(undefined),
|
|
||||||
vouching: vouching.unwrapOr(undefined)
|
|
||||||
}))
|
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,28 +2,51 @@
|
|||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
import type { ApiInterfaceRx } from '@polkadot/api/types';
|
import type { ApiInterfaceRx } from '@polkadot/api/types';
|
||||||
import type { AccountId } from '@polkadot/types/interfaces';
|
import type { bool, Option, Vec } from '@polkadot/types';
|
||||||
|
import type { AccountId, Balance, BlockNumber, SocietyVote, StrikeCount, VouchingStatus } from '@polkadot/types/interfaces';
|
||||||
|
import type { ITuple } from '@polkadot/types/types';
|
||||||
import type { Observable } from '@polkadot/x-rxjs';
|
import type { Observable } from '@polkadot/x-rxjs';
|
||||||
import type { DeriveSocietyMember } from '../types';
|
import type { DeriveSocietyMember } from '../types';
|
||||||
|
|
||||||
import { combineLatest } from '@polkadot/x-rxjs';
|
import { combineLatest, of } from '@polkadot/x-rxjs';
|
||||||
import { switchMap } from '@polkadot/x-rxjs/operators';
|
import { map, switchMap } from '@polkadot/x-rxjs/operators';
|
||||||
|
|
||||||
import { memo } from '../util';
|
import { memo } from '../util';
|
||||||
|
|
||||||
|
type Result = [AccountId[], Vec<ITuple<[BlockNumber, Balance]>>[], StrikeCount[], Option<SocietyVote>[], bool[], Option<VouchingStatus>[]];
|
||||||
|
|
||||||
|
export function _members (instanceId: string, api: ApiInterfaceRx): (accountIds: AccountId[]) => Observable<DeriveSocietyMember[]> {
|
||||||
|
return memo(instanceId, (accountIds: AccountId[]): Observable<DeriveSocietyMember[]> =>
|
||||||
|
combineLatest<Result>([
|
||||||
|
of(accountIds),
|
||||||
|
api.query.society.payouts.multi(accountIds),
|
||||||
|
api.query.society.strikes.multi(accountIds),
|
||||||
|
api.query.society.defenderVotes.multi(accountIds),
|
||||||
|
api.query.society.suspendedMembers.multi(accountIds),
|
||||||
|
api.query.society.vouching.multi(accountIds)
|
||||||
|
]).pipe(
|
||||||
|
map(([accountIds, payouts, strikes, defenderVotes, suspended, vouching]) =>
|
||||||
|
accountIds.map((accountId, index) => ({
|
||||||
|
accountId,
|
||||||
|
isDefenderVoter: defenderVotes[index].isSome,
|
||||||
|
isSuspended: suspended[index].isTrue,
|
||||||
|
payouts: payouts[index],
|
||||||
|
strikes: strikes[index],
|
||||||
|
vote: defenderVotes[index].unwrapOr(undefined),
|
||||||
|
vouching: vouching[index].unwrapOr(undefined)
|
||||||
|
}))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Get the member info for a society
|
* @description Get the member info for a society
|
||||||
*/
|
*/
|
||||||
export function members (instanceId: string, api: ApiInterfaceRx): () => Observable<DeriveSocietyMember[]> {
|
export function members (instanceId: string, api: ApiInterfaceRx): () => Observable<DeriveSocietyMember[]> {
|
||||||
return memo(instanceId, (): Observable<DeriveSocietyMember[]> =>
|
return memo(instanceId, (): Observable<DeriveSocietyMember[]> =>
|
||||||
api.query.society.members<AccountId[]>().pipe(
|
api.query.society.members<AccountId[]>().pipe(
|
||||||
switchMap((members): Observable<DeriveSocietyMember[]> =>
|
switchMap((members) => api.derive.society._members(members))
|
||||||
combineLatest(
|
|
||||||
members.map((accountId): Observable<DeriveSocietyMember> =>
|
|
||||||
api.derive.society.member(accountId)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -108,6 +108,7 @@ export interface DeriveSocietyCandidate {
|
|||||||
|
|
||||||
export interface DeriveSocietyMember {
|
export interface DeriveSocietyMember {
|
||||||
accountId: AccountId;
|
accountId: AccountId;
|
||||||
|
isDefenderVoter: boolean;
|
||||||
isSuspended: boolean;
|
isSuspended: boolean;
|
||||||
payouts: [BlockNumber, Balance][];
|
payouts: [BlockNumber, Balance][];
|
||||||
strikes: StrikeCount;
|
strikes: StrikeCount;
|
||||||
|
|||||||
Reference in New Issue
Block a user