mirror of
https://github.com/pezkuwichain/pezkuwi-api.git
synced 2026-08-03 14:25:42 +00:00
Rebrand: polkadot → pezkuwi, substrate → bizinikiwi, kusama → dicle
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
// Copyright 2017-2025 @pezkuwi/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Observable } from 'rxjs';
|
||||
import type { Option, u64 } from '@pezkuwi/types';
|
||||
import type { PezpalletBagsListListBag } from '@pezkuwi/types/lookup';
|
||||
import type { BN } from '@pezkuwi/util';
|
||||
import type { DeriveApi } from '../types.js';
|
||||
import type { Bag } from './types.js';
|
||||
|
||||
import { map, of, switchMap } from 'rxjs';
|
||||
|
||||
import { BN_ZERO, bnToBn, objectSpread } from '@pezkuwi/util';
|
||||
|
||||
import { memo } from '../util/index.js';
|
||||
import { getQueryInterface } from './util.js';
|
||||
|
||||
function orderBags (ids: BN[], bags: Option<PezpalletBagsListListBag>[]): Bag[] {
|
||||
const sorted = ids
|
||||
.map((id, index) => ({
|
||||
bag: bags[index].unwrapOr(null),
|
||||
id,
|
||||
key: id.toString()
|
||||
}))
|
||||
.sort((a, b) => b.id.cmp(a.id));
|
||||
const max = sorted.length - 1;
|
||||
|
||||
return sorted.map((entry, index): Bag =>
|
||||
objectSpread(entry, {
|
||||
bagLower: index === max
|
||||
? BN_ZERO
|
||||
: sorted[index + 1].id,
|
||||
bagUpper: entry.id,
|
||||
index
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
export function _getIds (instanceId: string, api: DeriveApi): (ids: (BN | number)[]) => Observable<Bag[]> {
|
||||
const query = getQueryInterface(api);
|
||||
|
||||
return memo(instanceId, (_ids: (BN | number)[]): Observable<Bag[]> => {
|
||||
const ids = _ids.map((id) => bnToBn(id));
|
||||
|
||||
return ids.length
|
||||
? query.listBags.multi<Option<PezpalletBagsListListBag>>(ids).pipe(
|
||||
map((bags) => orderBags(ids, bags))
|
||||
)
|
||||
: of([]);
|
||||
});
|
||||
}
|
||||
|
||||
export function all (instanceId: string, api: DeriveApi): () => Observable<Bag[]> {
|
||||
const query = getQueryInterface(api);
|
||||
|
||||
return memo(instanceId, (): Observable<Bag[]> =>
|
||||
query.listBags.keys<[u64]>().pipe(
|
||||
switchMap((keys) =>
|
||||
api.derive.bagsList._getIds(keys.map(({ args: [id] }) => id))
|
||||
),
|
||||
map((list) =>
|
||||
list.filter(({ bag }) => bag)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name get
|
||||
* @param {(BN | number)} id The id of the bag to retrieve.
|
||||
* @description Retrieves a specific bag from the BagsList pallet by its id.
|
||||
*/
|
||||
export function get (instanceId: string, api: DeriveApi): (id: BN | number) => Observable<Bag> {
|
||||
return memo(instanceId, (id: BN | number): Observable<Bag> =>
|
||||
api.derive.bagsList._getIds([bnToBn(id)]).pipe(
|
||||
map((bags) => bags[0])
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// Copyright 2017-2025 @pezkuwi/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Observable } from 'rxjs';
|
||||
import type { BN } from '@pezkuwi/util';
|
||||
import type { DeriveApi } from '../types.js';
|
||||
import type { Bag, BagExpanded } from './types.js';
|
||||
|
||||
import { map, switchMap } from 'rxjs';
|
||||
|
||||
import { objectSpread } from '@pezkuwi/util';
|
||||
|
||||
import { memo } from '../util/index.js';
|
||||
|
||||
/**
|
||||
* @name expand
|
||||
* @description Expands a given bag by retrieving all its nodes (accounts contained within the bag).
|
||||
* @param {Bag} bag The bag to be expanded.
|
||||
*/
|
||||
export function expand (instanceId: string, api: DeriveApi): (bag: Bag) => Observable<BagExpanded> {
|
||||
return memo(instanceId, (bag: Bag): Observable<BagExpanded> =>
|
||||
api.derive.bagsList.listNodes(bag.bag).pipe(
|
||||
map((nodes) => objectSpread({ nodes }, bag))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name getExpanded
|
||||
* @description Retrieves and expands a specific bag from the BagsList pallet.
|
||||
* @param {BN | number} id The id of the bag to expand.
|
||||
*/
|
||||
export function getExpanded (instanceId: string, api: DeriveApi): (id: BN | number) => Observable<BagExpanded> {
|
||||
return memo(instanceId, (id: BN | number): Observable<BagExpanded> =>
|
||||
api.derive.bagsList.get(id).pipe(
|
||||
switchMap((bag) =>
|
||||
api.derive.bagsList.expand(bag)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// Copyright 2017-2025 @pezkuwi/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export * from './get.js';
|
||||
export * from './getExpanded.js';
|
||||
export * from './listNodes.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 } from '@pezkuwi/types';
|
||||
import type { AccountId32 } from '@pezkuwi/types/interfaces';
|
||||
import type { PezpalletBagsListListBag, PezpalletBagsListListNode } from '@pezkuwi/types/lookup';
|
||||
import type { DeriveApi } from '../types.js';
|
||||
|
||||
import { BehaviorSubject, map, of, switchMap, tap, toArray } from 'rxjs';
|
||||
|
||||
import { nextTick } from '@pezkuwi/util';
|
||||
|
||||
import { memo } from '../util/index.js';
|
||||
import { getQueryInterface } from './util.js';
|
||||
|
||||
function traverseLinks (api: DeriveApi, head: AccountId32 | string): Observable<PezpalletBagsListListNode[]> {
|
||||
const subject = new BehaviorSubject<AccountId32 | string>(head);
|
||||
const query = getQueryInterface(api);
|
||||
|
||||
return subject.pipe(
|
||||
switchMap((account) =>
|
||||
query.listNodes<Option<PezpalletBagsListListNode>>(account)
|
||||
),
|
||||
tap((node: Option<PezpalletBagsListListNode>): void => {
|
||||
nextTick((): void => {
|
||||
node.isSome && node.value.next.isSome
|
||||
? subject.next(node.unwrap().next.unwrap())
|
||||
: subject.complete();
|
||||
});
|
||||
}),
|
||||
toArray(), // toArray since we want to startSubject to be completed
|
||||
map((all: Option<PezpalletBagsListListNode>[]) =>
|
||||
all.map((o) => o.unwrap())
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name listNodes
|
||||
* @param {(PezpalletBagsListListBag | null)} bag A reference to a specific bag in the BagsList pallet.
|
||||
* @description Retrieves the list of nodes (accounts) contained in a specific bag within the BagsList pallet.
|
||||
*/
|
||||
export function listNodes (instanceId: string, api: DeriveApi): (bag: PezpalletBagsListListBag | null) => Observable<PezpalletBagsListListNode[]> {
|
||||
return memo(instanceId, (bag: PezpalletBagsListListBag | null): Observable<PezpalletBagsListListNode[]> =>
|
||||
bag && bag.head.isSome
|
||||
? traverseLinks(api, bag.head.unwrap())
|
||||
: of([])
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// Copyright 2017-2025 @pezkuwi/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { PezpalletBagsListListBag, PezpalletBagsListListNode } from '@pezkuwi/types/lookup';
|
||||
import type { BN } from '@pezkuwi/util';
|
||||
|
||||
export interface Bag {
|
||||
bag: PezpalletBagsListListBag | null;
|
||||
bagUpper: BN;
|
||||
bagLower: BN;
|
||||
id: BN;
|
||||
index: number;
|
||||
key: string;
|
||||
}
|
||||
|
||||
export interface BagExpanded extends Bag {
|
||||
nodes: PezpalletBagsListListNode[];
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright 2017-2025 @pezkuwi/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { DeriveApi } from '../types.js';
|
||||
|
||||
export function getQueryInterface (api: DeriveApi): DeriveApi['query']['voterList'] {
|
||||
return (
|
||||
// latest bizinikiwi & pezkuwi
|
||||
api.query.voterList ||
|
||||
// previous bizinikiwi
|
||||
api.query['voterBagsList'] ||
|
||||
api.query['bagsList']
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user