mirror of
https://github.com/pezkuwichain/pezkuwi-api.git
synced 2026-11-11 01:17:06 +00:00
Rebrand: polkadot → pezkuwi, substrate → bizinikiwi, kusama → dicle
This commit is contained in:
commit
d5f038faea
1383 files changed
+1088018
No files matched your search
@@ -0,0 +1,71 @@
|
||||
// Copyright 2017-2025 @pezkuwi/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Observable } from 'rxjs';
|
||||
import type { AccountId32, BalanceOf } from '@pezkuwi/types/interfaces';
|
||||
import type { PezpalletSocietyBid, PezpalletSocietyBidKind, PezpalletSocietyCandidacy } from '@pezkuwi/types/lookup';
|
||||
import type { ITuple } from '@pezkuwi/types/types';
|
||||
import type { Option, Vec } from '@pezkuwi/types-codec';
|
||||
import type { DeriveApi, DeriveSocietyCandidate } from '../types.js';
|
||||
|
||||
import { combineLatest, map, of, switchMap } from 'rxjs';
|
||||
|
||||
import { memo } from '../util/index.js';
|
||||
|
||||
type ResultSuspend = Option<ITuple<[BalanceOf, PezpalletSocietyBidKind]>>;
|
||||
type Result = [PezpalletSocietyBid[], ResultSuspend[]]
|
||||
|
||||
function getPrev (api: DeriveApi): Observable<DeriveSocietyCandidate[]> {
|
||||
return api.query.society.candidates<Vec<PezpalletSocietyBid>>().pipe(
|
||||
switchMap((candidates): Observable<Result> =>
|
||||
combineLatest([
|
||||
of(candidates),
|
||||
api.query.society['suspendedCandidates'].multi<Option<ITuple<[BalanceOf, PezpalletSocietyBidKind]>>>(
|
||||
candidates.map(({ who }) => who)
|
||||
)
|
||||
])
|
||||
),
|
||||
map(([candidates, suspended]: Result): DeriveSocietyCandidate[] =>
|
||||
candidates.map(({ kind, value, who }, index) => ({
|
||||
accountId: who,
|
||||
isSuspended: suspended[index].isSome,
|
||||
kind,
|
||||
value
|
||||
}))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function getCurr (api: DeriveApi) {
|
||||
return api.query.society.candidates.entries().pipe(
|
||||
map((entries) =>
|
||||
entries
|
||||
.filter(([, opt]) => opt.isSome)
|
||||
.map(([{ args: [accountId] }, opt]): [AccountId32, PezpalletSocietyCandidacy] => [accountId, opt.unwrap()])
|
||||
// FIXME We are missing the new fields from the candidate record
|
||||
.map(([accountId, { bid, kind }]) => ({
|
||||
accountId,
|
||||
isSuspended: false,
|
||||
kind,
|
||||
value: bid
|
||||
}))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name candidate
|
||||
* @description Retrieves the list of candidates for the society module.
|
||||
* @example
|
||||
* ```javascript
|
||||
* const societyCandidates = await api.derive.society.candidates();
|
||||
* console.log(societyCandidates);
|
||||
* ```
|
||||
*/
|
||||
export function candidates (instanceId: string, api: DeriveApi): () => Observable<DeriveSocietyCandidate[]> {
|
||||
return memo(instanceId, (): Observable<DeriveSocietyCandidate[]> =>
|
||||
api.query.society['suspendedCandidates'] && api.query.society.candidates.creator.meta.type.isPlain
|
||||
? getPrev(api)
|
||||
: getCurr(api)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// Copyright 2017-2025 @pezkuwi/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export * from './candidates.js';
|
||||
export * from './info.js';
|
||||
export * from './member.js';
|
||||
export * from './members.js';
|
||||
@@ -0,0 +1,50 @@
|
||||
// Copyright 2017-2025 @pezkuwi/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Observable } from 'rxjs';
|
||||
import type { Option, u32, Vec } from '@pezkuwi/types';
|
||||
import type { AccountId, BalanceOf } from '@pezkuwi/types/interfaces';
|
||||
import type { PezpalletSocietyBid } from '@pezkuwi/types/lookup';
|
||||
import type { DeriveApi, DeriveSociety } from '../types.js';
|
||||
|
||||
import { combineLatest, map, of } from 'rxjs';
|
||||
|
||||
import { memo } from '../util/index.js';
|
||||
|
||||
type Result = [Vec<PezpalletSocietyBid>, Option<AccountId> | undefined, Option<AccountId>, Option<AccountId>, u32 | undefined, BalanceOf]
|
||||
|
||||
/**
|
||||
* @name info
|
||||
* @description Get the overall info for a society.
|
||||
* @example
|
||||
* ```javascript
|
||||
* const societyInfo = await api.derive.society.candidates();
|
||||
* console.log(societyInfo);
|
||||
* ```
|
||||
*/
|
||||
export function info (instanceId: string, api: DeriveApi): () => Observable<DeriveSociety> {
|
||||
return memo(instanceId, (): Observable<DeriveSociety> =>
|
||||
combineLatest<Result>([
|
||||
api.query.society.bids(),
|
||||
api.query.society['defender']
|
||||
? api.query.society['defender']<Option<AccountId>>()
|
||||
: of(undefined),
|
||||
api.query.society.founder(),
|
||||
api.query.society.head(),
|
||||
api.query.society['maxMembers']
|
||||
? api.query.society['maxMembers']<u32>()
|
||||
: of(undefined),
|
||||
api.query.society.pot()
|
||||
]).pipe(
|
||||
map(([bids, defender, founder, head, maxMembers, pot]: Result): DeriveSociety => ({
|
||||
bids,
|
||||
defender: defender?.unwrapOr(undefined),
|
||||
founder: founder.unwrapOr(undefined),
|
||||
hasDefender: (defender?.isSome && head.isSome && !head.eq(defender)) || false,
|
||||
head: head.unwrapOr(undefined),
|
||||
maxMembers,
|
||||
pot
|
||||
}))
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// Copyright 2017-2025 @pezkuwi/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Observable } from 'rxjs';
|
||||
import type { AccountId } from '@pezkuwi/types/interfaces';
|
||||
import type { DeriveApi, DeriveSocietyMember } from '../types.js';
|
||||
|
||||
import { map } from 'rxjs';
|
||||
|
||||
import { memo } from '../util/index.js';
|
||||
|
||||
/**
|
||||
* @name member
|
||||
* @description Get the member info for a society.
|
||||
* @param { AccountId } accountId
|
||||
* @example
|
||||
* ```javascript
|
||||
* const member = await api.derive.society.member(ALICE);
|
||||
* console.log(member);
|
||||
* ```
|
||||
*/
|
||||
export function member (instanceId: string, api: DeriveApi): (accountId: AccountId) => Observable<DeriveSocietyMember> {
|
||||
return memo(instanceId, (accountId: AccountId): Observable<DeriveSocietyMember> =>
|
||||
api.derive.society._members([accountId]).pipe(
|
||||
map(([result]) => result)
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
// Copyright 2017-2025 @pezkuwi/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Observable } from 'rxjs';
|
||||
import type { AccountId } from '@pezkuwi/types/interfaces';
|
||||
import type { PezpalletSocietyVote, PezpalletSocietyVouchingStatus } from '@pezkuwi/types/lookup';
|
||||
import type { ITuple } from '@pezkuwi/types/types';
|
||||
import type { bool, Option, u32, u128, Vec } from '@pezkuwi/types-codec';
|
||||
import type { DeriveApi, DeriveSocietyMember } from '../types.js';
|
||||
|
||||
import { combineLatest, map, of, switchMap } from 'rxjs';
|
||||
|
||||
import { memo } from '../util/index.js';
|
||||
|
||||
function _membersPrev (api: DeriveApi, accountIds: AccountId[]): Observable<DeriveSocietyMember[]> {
|
||||
return combineLatest([
|
||||
of(accountIds),
|
||||
api.query.society.payouts.multi<Vec<ITuple<[u32, u128]>>>(accountIds),
|
||||
api.query.society['strikes'].multi<u32>(accountIds),
|
||||
api.query.society.defenderVotes.multi<Option<PezpalletSocietyVote>>(accountIds),
|
||||
api.query.society.suspendedMembers.multi<bool>(accountIds),
|
||||
api.query.society['vouching'].multi<Option<PezpalletSocietyVouchingStatus>>(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)
|
||||
}))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function _membersCurr (api: DeriveApi, accountIds: AccountId[]): Observable<DeriveSocietyMember[]> {
|
||||
return combineLatest([
|
||||
of(accountIds),
|
||||
api.query.society.members.multi(accountIds),
|
||||
api.query.society.payouts.multi(accountIds),
|
||||
api.query.society.challengeRoundCount().pipe(
|
||||
switchMap((round) =>
|
||||
api.query.society.defenderVotes.multi(accountIds.map((accountId) => [round, accountId]))
|
||||
)
|
||||
),
|
||||
api.query.society.suspendedMembers.multi(accountIds)
|
||||
]).pipe(
|
||||
map(([accountIds, members, payouts, defenderVotes, suspendedMembers]) =>
|
||||
accountIds
|
||||
.map((accountId, index) =>
|
||||
members[index].isSome
|
||||
? {
|
||||
accountId,
|
||||
isDefenderVoter: defenderVotes[index].isSome,
|
||||
isSuspended: suspendedMembers[index].isSome,
|
||||
member: members[index].unwrap(),
|
||||
payouts: payouts[index].payouts
|
||||
}
|
||||
: null
|
||||
)
|
||||
.filter((m): m is NonNullable<typeof m> => !!m)
|
||||
.map(({ accountId, isDefenderVoter, isSuspended, member, payouts }) => ({
|
||||
accountId,
|
||||
isDefenderVoter,
|
||||
isSuspended,
|
||||
payouts,
|
||||
strikes: member.strikes,
|
||||
vouching: member.vouching.unwrapOr(undefined)
|
||||
}))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
export function _members (instanceId: string, api: DeriveApi): (accountIds: AccountId[]) => Observable<DeriveSocietyMember[]> {
|
||||
return memo(instanceId, (accountIds: AccountId[]): Observable<DeriveSocietyMember[]> =>
|
||||
api.query.society.members.creator.meta.type.isMap
|
||||
? _membersCurr(api, accountIds)
|
||||
: _membersPrev(api, accountIds)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name members
|
||||
* @description Get the society members.
|
||||
* @example
|
||||
* ```javascript
|
||||
* const members = await api.derive.society.members();
|
||||
* console.log(members);
|
||||
* ```
|
||||
*/
|
||||
export function members (instanceId: string, api: DeriveApi): () => Observable<DeriveSocietyMember[]> {
|
||||
return memo(instanceId, (): Observable<DeriveSocietyMember[]> =>
|
||||
api.query.society.members.creator.meta.type.isMap
|
||||
? api.query.society.members.keys().pipe(
|
||||
switchMap((keys) =>
|
||||
api.derive.society._members(
|
||||
keys.map(({ args: [accountId] }) => accountId)
|
||||
)
|
||||
)
|
||||
)
|
||||
: api.query.society.members<Vec<AccountId>>().pipe(
|
||||
switchMap((members) => api.derive.society._members(members))
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Copyright 2017-2025 @pezkuwi/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { u32 } from '@pezkuwi/types';
|
||||
import type { AccountId, Balance, BalanceOf, BlockNumber, StrikeCount } from '@pezkuwi/types/interfaces';
|
||||
import type { PezpalletSocietyBid, PezpalletSocietyBidKind, PezpalletSocietyVote, PezpalletSocietyVouchingStatus } from '@pezkuwi/types/lookup';
|
||||
|
||||
export interface DeriveSociety {
|
||||
bids: PezpalletSocietyBid[];
|
||||
defender?: AccountId | undefined;
|
||||
hasDefender: boolean;
|
||||
head?: AccountId | undefined;
|
||||
founder?: AccountId | undefined;
|
||||
maxMembers?: u32 | undefined;
|
||||
pot: BalanceOf;
|
||||
}
|
||||
|
||||
export interface DeriveSocietyCandidate {
|
||||
accountId: AccountId;
|
||||
kind: PezpalletSocietyBidKind;
|
||||
value: Balance;
|
||||
isSuspended: boolean;
|
||||
}
|
||||
|
||||
export interface DeriveSocietyMember {
|
||||
accountId: AccountId;
|
||||
isDefenderVoter: boolean;
|
||||
isSuspended: boolean;
|
||||
payouts: [BlockNumber, Balance][];
|
||||
strikes: StrikeCount;
|
||||
vote?: PezpalletSocietyVote | undefined;
|
||||
vouching?: PezpalletSocietyVouchingStatus | undefined;
|
||||
}
|
||||
Reference in new issue
Block a user