ghost-portal/packages/page-staking/src/Pools/usePoolIds.ts
Jaco 47913892cd
Expansion to pools logic (#7384)
* Expansion to pools logic

* Add filtering

* Pass through own pools

* Allow pool nominate tx

* Start with account actions

* Asho pool nominees

* Nomination operational

* Adjust table rows

* BondExtra for pools

* use availableBalance in calcs (w/ existential)

* Re-use isAmountError check

* Pass through actual pool info

* s/id/poolId/

* Display payout stashes

* Display points balance

* OCD cleanups

* claimPayout

* Calculate pool claimable

* Unbond

* Fix Unbond max

* Display remaining witldrawal eras

* Stashed/Pooled selection

* Withdraw unbonded

* StakingRedeemable display

* Renames as per metadata

* Additional

* s/pool/pools/

* PoolInfo display hints

* Adjust usePoolInfo hook

* Ordering adjustments

* Additional dedupes

* Combine queries

* Combinations & renames

* Flatten transform

* Non-existent reward

* bnMax sub

* lint fixes

* Bump API
2022-04-26 18:21:38 +03:00

43 lines
1.2 KiB
TypeScript

// Copyright 2017-2022 @polkadot/app-staking authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Changes } from '@polkadot/react-hooks/useEventChanges';
import type { StorageKey, u32 } from '@polkadot/types';
import type { EventRecord } from '@polkadot/types/interfaces';
import { createNamedHook, useApi, useEventChanges, useMapKeys } from '@polkadot/react-hooks';
const OPT_KEYS = {
transform: (keys: StorageKey<[u32]>[]): u32[] =>
keys
.map(({ args: [poolId] }) => poolId)
.sort((a, b) => a.cmp(b))
};
function filterEvents (records: EventRecord[]): Changes<u32> {
const added: u32[] = [];
const removed: u32[] = [];
records.forEach(({ event: { data, method } }): void => {
if (method === 'Created') {
added.push(data[1] as u32);
} else {
removed.push(data[0] as u32);
}
});
return { added, removed };
}
function usePoolIdsImpl (): u32[] | undefined {
const { api } = useApi();
const startValue = useMapKeys(api.query.nominationPools.bondedPools, OPT_KEYS);
return useEventChanges([
api.events.nominationPools.Created,
api.events.nominationPools.Destroyed
], filterEvents, startValue);
}
export default createNamedHook('usePoolIds', usePoolIdsImpl);