Add get validators helper
This commit is contained in:
parent
9460b8ea6d
commit
fcf59e8aa4
25
lib/staking.ts
Normal file
25
lib/staking.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { QueryClient, StakingExtension, setupStakingExtension } from "@cosmjs/stargate";
|
||||
import { Tendermint34Client } from "@cosmjs/tendermint-rpc";
|
||||
import { Validator } from "cosmjs-types/cosmos/staking/v1beta1/staking";
|
||||
|
||||
const getValidatorsPage = (
|
||||
queryClient: QueryClient & StakingExtension,
|
||||
paginationKey: Uint8Array | undefined,
|
||||
) => queryClient.staking.validators("BOND_STATUS_BONDED", paginationKey);
|
||||
|
||||
export const getAllValidators = async (rpcUrl: string): Promise<readonly Validator[]> => {
|
||||
const validators: Validator[] = [];
|
||||
|
||||
const tmClient = await Tendermint34Client.connect(rpcUrl);
|
||||
const queryClient = QueryClient.withExtensions(tmClient, setupStakingExtension);
|
||||
|
||||
let paginationKey: Uint8Array | undefined = undefined;
|
||||
|
||||
do {
|
||||
const response = await getValidatorsPage(queryClient, paginationKey);
|
||||
validators.push(...response.validators);
|
||||
paginationKey = response.pagination?.nextKey;
|
||||
} while (paginationKey?.length);
|
||||
|
||||
return validators;
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user