-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgetPoolMembers.ts
More file actions
46 lines (42 loc) · 1.29 KB
/
getPoolMembers.ts
File metadata and controls
46 lines (42 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import type { Api, ChainIdRelay } from "@polkadot-agent-kit/common"
/**
* Interface for pool member information
*/
export interface PoolMember {
account: string
poolId: number
points: bigint
lastRecordedRewardCounter: bigint
unbondingEras: Array<{ era: number; value: bigint }>
}
/**
* Get members of a nomination pool
* @param api - The API instance to use for the query
* @param poolId - The ID of the pool to query members for
* @returns Promise that resolves to an array of pool members
*/
export const getPoolMembers = async (
api: Api<ChainIdRelay>,
poolId: number
): Promise<PoolMember[]> => {
try {
const poolMemberEntries = await api.query.NominationPools.PoolMembers.getEntries()
return poolMemberEntries
.filter(({ value }) => value && value.pool_id === poolId)
.map(({ keyArgs, value }) => {
const account = keyArgs[0] as string
return {
account,
poolId: value.pool_id,
points: value.points,
lastRecordedRewardCounter: value.last_recorded_reward_counter,
unbondingEras: value.unbonding_eras.map(([era, value]) => ({
era,
value
}))
}
})
} catch (error) {
throw new Error(`Error fetching pool members for pool ${poolId}: ${String(error)}`)
}
}