// Copyright 2017-2020 @polkadot/api-derive authors & contributors // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. import { ApiInterfaceRx } from '@polkadot/api/types'; import { ReferendumIndex } from '@polkadot/types/interfaces'; import BN from 'bn.js'; import { Observable, of } from 'rxjs'; import { map } from 'rxjs/operators'; import { memo } from '../util'; export function referendumIds (api: ApiInterfaceRx): () => Observable { return memo((): Observable => api.query.democracy?.lowestUnbaked ? api.queryMulti<[ReferendumIndex, ReferendumIndex]>([ api.query.democracy.lowestUnbaked, api.query.democracy.referendumCount ]).pipe( map(([first, total]): BN[] => total.gt(first) // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment ? [...Array(total.sub(first).toNumber())].map((_, i): BN => first.addn(i)) : [] ) ) : of([]) ); }