Files
api/packages/api-derive/src/council/votesOf.ts
T
Valentin Fernandez ed62d164ea Document available Api Derives (#6106)
* document derives from accounts to democracy

* Derive Documentation progress

* Derive Documentation progress

* tx and staking docs

* Staking docs progress

* more staking docs

* Staking module complete

* imonline, elections and progress in democracy

* Document missing derives

* lint
2025-02-27 10:56:34 -03:00

36 lines
1.3 KiB
TypeScript

// Copyright 2017-2025 @polkadot/api-derive authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Observable } from 'rxjs';
import type { AccountId } from '@polkadot/types/interfaces';
import type { DeriveApi, DeriveCouncilVote } from '../types.js';
import { map } from 'rxjs';
import { memo } from '../util/index.js';
/**
* @name votesOf
* @description Retrieves the council votes associated with a given account.
* @returns The stake and the list of candidates the account has voted for.
* @param {string | Uint8Array | AccountId} accountId The accountId to retrieve votes for.
* @example
* ```javascript
* const accountId = "5Gw3s7qQ9Z..."; // Replace with a valid account ID
* const votes = await api.derive.council.votesOf(accountId);
* console.log("Account votes:", votes);
* ```
*/
export function votesOf (instanceId: string, api: DeriveApi): (accountId: string | Uint8Array | AccountId) => Observable<DeriveCouncilVote> {
return memo(instanceId, (accountId: string | Uint8Array | AccountId): Observable<DeriveCouncilVote> =>
api.derive.council.votes().pipe(
map((votes): DeriveCouncilVote =>
(
votes.find(([from]) => from.eq(accountId)) ||
[null, { stake: api.registry.createType('Balance'), votes: [] as AccountId[] }]
)[1]
)
)
);
}